Files
sqltoolsservice/test/Microsoft.SqlTools.ServiceLayer.Test/Utility/TestDbDataReader.cs
Benjamin Russell ec94d986a8 Unit Test Cleanup (#141)
This is a fairly large set of changes to the unit tests that help isolate the effectiveness of the unit tests.

* Unit tests for query execution have been split into separate files for different classes.
* Unit tests have been added for the ResultSet class which previously did not have tests
* The InMemoryStreamWrapper has been improved to share memory, creating a simulated filesystem
* Creating a mock ConnectionService to decrease noisy exceptions and prevent "row stealing". Unfortunately this lowers code coverage. However, since the tests that touched the connection service were not really testing it, this helps keep us honest. But it will require adding more unit tests for connection service.
* Standardizing the await mechanism for query execution
* Cleaning up the mechanism for getting WorkspaceService mocks and mock FileStreamFactories

* Refactor the query execution tests into their own files

* Removing tests from ExecuteTests.cs that were moved to separate files

* Adding tests for ResultSet class

* Adding test for the FOR XML/JSON component of the resultset class

* Setting up shared storage between file stream readers/writers

* Standardizing on Workspace mocking, awaiting execution completion

* Adding comment for ResultSet class
2016-11-10 11:42:31 -08:00

216 lines
5.8 KiB
C#

//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Common;
using System.Linq;
namespace Microsoft.SqlTools.ServiceLayer.Test.Utility
{
public class TestDbDataReader : DbDataReader, IDbColumnSchemaGenerator
{
#region Test Specific Implementations
private Dictionary<string, string>[][] Data { get; set; }
public IEnumerator<Dictionary<string, string>[]> ResultSet { get; private set; }
private IEnumerator<Dictionary<string, string>> Rows { get; set; }
public TestDbDataReader(Dictionary<string, string>[][] data)
{
Data = data;
if (Data != null)
{
ResultSet = ((IEnumerable<Dictionary<string, string>[]>) Data).GetEnumerator();
ResultSet.MoveNext();
}
}
#endregion
public override bool HasRows
{
get { return ResultSet != null && ResultSet.Current.Length > 0; }
}
public override bool Read()
{
if (Rows == null)
{
Rows = ((IEnumerable<Dictionary<string, string>>) ResultSet.Current).GetEnumerator();
}
return Rows.MoveNext();
}
public override bool NextResult()
{
if (Data == null || !ResultSet.MoveNext())
{
return false;
}
Rows = ((IEnumerable<Dictionary<string, string>>)ResultSet.Current).GetEnumerator();
return true;
}
public override object GetValue(int ordinal)
{
return this[ordinal];
}
public override int GetValues(object[] values)
{
for(int i = 0; i < Rows.Current.Count; i++)
{
values[i] = this[i];
}
return Rows.Current.Count;
}
public override object this[string name]
{
get { return Rows.Current[name]; }
}
public override object this[int ordinal]
{
get { return Rows.Current[Rows.Current.Keys.AsEnumerable().ToArray()[ordinal]]; }
}
public ReadOnlyCollection<DbColumn> GetColumnSchema()
{
if (ResultSet?.Current == null || ResultSet.Current.Length <= 0)
{
return new ReadOnlyCollection<DbColumn>(new List<DbColumn>());
}
List<DbColumn> columns = new List<DbColumn>();
for (int i = 0; i < ResultSet.Current[0].Count; i++)
{
columns.Add(new TestDbColumn(ResultSet.Current[0].Keys.ToArray()[i]));
}
return new ReadOnlyCollection<DbColumn>(columns);
}
public override bool IsDBNull(int ordinal)
{
return this[ordinal] == null;
}
public override int FieldCount { get { return Rows?.Current.Count ?? 0; } }
public override int RecordsAffected
{
// Mimics the behavior of SqlDataReader
get { return Rows != null ? -1 : 1; }
}
#region Not Implemented
public override bool GetBoolean(int ordinal)
{
throw new NotImplementedException();
}
public override byte GetByte(int ordinal)
{
throw new NotImplementedException();
}
public override long GetBytes(int ordinal, long dataOffset, byte[] buffer, int bufferOffset, int length)
{
throw new NotImplementedException();
}
public override char GetChar(int ordinal)
{
throw new NotImplementedException();
}
public override long GetChars(int ordinal, long dataOffset, char[] buffer, int bufferOffset, int length)
{
throw new NotImplementedException();
}
public override string GetDataTypeName(int ordinal)
{
throw new NotImplementedException();
}
public override DateTime GetDateTime(int ordinal)
{
throw new NotImplementedException();
}
public override decimal GetDecimal(int ordinal)
{
throw new NotImplementedException();
}
public override double GetDouble(int ordinal)
{
throw new NotImplementedException();
}
public override int GetOrdinal(string name)
{
throw new NotImplementedException();
}
public override string GetName(int ordinal)
{
throw new NotImplementedException();
}
public override long GetInt64(int ordinal)
{
throw new NotImplementedException();
}
public override int GetInt32(int ordinal)
{
throw new NotImplementedException();
}
public override short GetInt16(int ordinal)
{
throw new NotImplementedException();
}
public override Guid GetGuid(int ordinal)
{
throw new NotImplementedException();
}
public override float GetFloat(int ordinal)
{
throw new NotImplementedException();
}
public override Type GetFieldType(int ordinal)
{
throw new NotImplementedException();
}
public override string GetString(int ordinal)
{
throw new NotImplementedException();
}
public override IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
public override int Depth { get; }
public override bool IsClosed { get; }
#endregion
}
}