public void TestIEnumerableOfAnonymousType() { // create generic list Func<Type, IList> toGenericList = type => (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(new[] { type })); // create generic list of anonymous type IList listOfAnonymousType = toGenericList(new { Name = "1", Age = 16 }.GetType()); listOfAnonymousType.Add(new { Name = "1", Age = 16 }); listOfAnonymousType.Add(new { Name = "2", Age = 26 }); listOfAnonymousType.Add(new { Name = "3", Age = 36 }); listOfAnonymousType.Add(new { Name = "4", Age = 46 }); var r = new EnumerableDataReader(listOfAnonymousType); while (r.Read()) { var values = new object[2]; int count = r.GetValues(values); Assert.AreEqual(2, count); values = new object[1]; count = r.GetValues(values); Assert.AreEqual(1, count); values = new object[3]; count = r.GetValues(values); Assert.AreEqual(2, count); Assert.IsInstanceOf(typeof(string), r.GetValue(0)); Assert.IsInstanceOf(typeof(int), r.GetValue(1)); Console.WriteLine("Name: {0}, Age: {1}", r.GetValue(0), r.GetValue(1)); } }
public void TestEnumerableDataReaderWithIQueryableOfAnonymousType() { var ctx = new NorthwindEntities(); var q = ctx.Orders.Where(o => o.Customers.CustomerID == "VINET").Select( o => new { o.OrderID, o.OrderDate, o.Customers.CustomerID, Total = o.Order_Details.Sum( od => od.Quantity * ((float)od.UnitPrice - ((float)od.UnitPrice * od.Discount))) }); var r = new EnumerableDataReader(q); while (r.Read()) { var values = new object[4]; r.GetValues(values); Console.WriteLine("{0} {1} {2} {3}", values); } }
public void DataReaderReadStruct() { var d = new TestStruct[] { new TestStruct (1, new DateTime (1997, 7, 1), 7.1f), new TestStruct (3, new DateTime (2046, 10, 1), null) }; using (var r = new EnumerableDataReader<TestStruct> (d)) { var xi = r.GetOrdinal ("X"); var yi = r.GetOrdinal ("Y"); var zi = r.GetOrdinal ("Z"); Assert.AreEqual (true, r.Read ()); Assert.AreEqual (1, r.GetInt32 (xi)); Assert.AreEqual (new DateTime (1997, 7, 1), r.GetDateTime (yi)); Assert.AreEqual (7.1f, r.GetFloat (zi)); Assert.AreEqual (true, r.Read ()); Assert.AreEqual (3, r.GetInt32 (xi)); Assert.AreEqual (new DateTime (2046, 10, 1), r.GetDateTime (yi)); Assert.IsNull (r.GetValue (zi)); } }
public void DataReaderReadStruct() { var d = new TestStruct[] { new TestStruct(1, new DateTime(1997, 7, 1), 7.1f), new TestStruct(3, new DateTime(2046, 10, 1), null) }; using (var r = new EnumerableDataReader <TestStruct> (d)) { var xi = r.GetOrdinal("X"); var yi = r.GetOrdinal("Y"); var zi = r.GetOrdinal("Z"); Assert.AreEqual(true, r.Read()); Assert.AreEqual(1, r.GetInt32(xi)); Assert.AreEqual(new DateTime(1997, 7, 1), r.GetDateTime(yi)); Assert.AreEqual(7.1f, r.GetFloat(zi)); Assert.AreEqual(true, r.Read()); Assert.AreEqual(3, r.GetInt32(xi)); Assert.AreEqual(new DateTime(2046, 10, 1), r.GetDateTime(yi)); Assert.IsNull(r.GetValue(zi)); } }
public EnumerableDataReaderTests() { // // Setup for testing default mapping using the source entity's property positions as the ordinals. // _enumerable = new[] { new MyTestClass() }; TestHelpers.ExecuteNonQuery(_connectionString, $"DROP TABLE IF EXISTS [dbo].[{_tableName}]"); TestHelpers.ExecuteNonQuery(_connectionString, "CREATE TABLE [dbo].[" + _tableName + "](" + "[Id] [int] IDENTITY(1,1) NOT NULL," + "[Name] [nvarchar](50) NULL," + "[Data] [varbinary](max) NULL," + "CONSTRAINT [PK_" + _tableName + "] PRIMARY KEY CLUSTERED ([Id] ASC)" + ")"); var propertyMappings = typeof(MyTestClass).BuildMappings(); _dataReader = new EnumerableDataReader <MyTestClass>(_enumerable, propertyMappings); _dataReader.Read(); // // Setup for testing custom mapping using [Column(Order = ...)] to specify ordinals on the source // entity. This is useful when the layout of the properties on the source entity doesn't match // the column ordinals in the database table (e.g., tables generated by EF Core <= 2.0, which // seems to create the columns by ordering the property names alphabetically). // _customOrderEnumerable = new[] { new MyCustomOrderTestClass() }; TestHelpers.ExecuteNonQuery(_connectionString, $"DROP TABLE IF EXISTS [dbo].[{_customOrderTableName}]"); TestHelpers.ExecuteNonQuery(_connectionString, "CREATE TABLE [dbo].[" + _customOrderTableName + "](" + "[Id] [int] IDENTITY(1,1) NOT NULL," + "[FirstName] [nvarchar](50) NULL," + "[MiddleName] [nvarchar](50) NULL," + "[LastName] [nvarchar](50) NULL," + "CONSTRAINT [PK_" + _customOrderTableName + "] PRIMARY KEY CLUSTERED ([Id] ASC)" + ")"); var customOrderPropertyMappings = typeof(MyCustomOrderTestClass).BuildMappings(); _customOrderDataReader = new EnumerableDataReader <MyCustomOrderTestClass>(_customOrderEnumerable, customOrderPropertyMappings); _customOrderDataReader.Read(); }
public void ThrowsWhenReadAfterDisposed() { var localReader = new EnumerableDataReader <FakeEntity>(this.data); localReader.IsClosed.Should().BeFalse(); localReader.Close(); localReader.IsClosed.Should().BeTrue(); Action act = () => localReader.Read(); act.ShouldThrow <ObjectDisposedException>() .Where(ex => ex.ObjectName == "EnumerableDataReader"); }
public EnumerableDataReaderTests() { _enumerable = new[] { new MyTestClass() }; TestHelpers.ExecuteNonQuery(_connectionString, $"DROP TABLE IF EXISTS [dbo].[{_tableName}]"); TestHelpers.ExecuteNonQuery(_connectionString, "CREATE TABLE [dbo].[" + _tableName + "](" + "[Id] [int] IDENTITY(1,1) NOT NULL," + "[Name] [nvarchar](50) NULL," + "CONSTRAINT [PK_" + _tableName + "] PRIMARY KEY CLUSTERED ([Id] ASC)" + ")"); var propertyMappings = typeof(MyTestClass).BuildMappings(); _dataReader = new EnumerableDataReader <MyTestClass>(_enumerable, propertyMappings); _dataReader.Read(); }
public void Initialize_Test() { this.enumerable = new[] { new MyTestClass() }; TestHelpers.ExecuteNonQuery(connectionString, "CREATE TABLE [dbo].[" + tableName + "](" + "[Id] [int] IDENTITY(1,1) NOT NULL," + "[Name] [nvarchar](50) NULL," + "CONSTRAINT [PK_" + tableName + "] PRIMARY KEY CLUSTERED ([Id] ASC)" + ")"); var mapping = MapBuilder.MapAllProperties<MyTestClass>(); var propertyMappings = ((MapBuilderContext<MyTestClass>)mapping).GetPropertyMappings(); AutoDiscover.Mappings(connectionString, tableName, propertyMappings); this.dataReader = new EnumerableDataReader<MyTestClass>(enumerable, propertyMappings); dataReader.Read(); }
public void Initialize_Test() { this.enumerable = new[] { new MyTestClass() }; TestHelpers.ExecuteNonQuery(connectionString, "CREATE TABLE [dbo].[" + tableName + "](" + "[Id] [int] IDENTITY(1,1) NOT NULL," + "[Name] [nvarchar](50) NULL," + "CONSTRAINT [PK_" + tableName + "] PRIMARY KEY CLUSTERED ([Id] ASC)" + ")"); var mapping = MapBuilder.MapAllProperties <MyTestClass>(); var propertyMappings = ((MapBuilderContext <MyTestClass>)mapping).GetPropertyMappings(); AutoDiscover.Mappings(connectionString, tableName, propertyMappings); this.dataReader = new EnumerableDataReader <MyTestClass>(enumerable, propertyMappings); dataReader.Read(); }
public void TestIEnumerableOfTCtor() { var r = new EnumerableDataReader(DataSource); while (r.Read()) { var values = new object[2]; int count = r.GetValues(values); Assert.AreEqual(2, count); values = new object[1]; count = r.GetValues(values); Assert.AreEqual(1, count); values = new object[3]; count = r.GetValues(values); Assert.AreEqual(2, count); Assert.IsInstanceOf(typeof(string), r.GetValue(0)); Assert.IsInstanceOf(typeof(int), r.GetValue(1)); Console.WriteLine("Name: {0}, Age: {1}", r.GetValue(0), r.GetValue(1)); } }
public void ReturnCorrectData() { var item1 = new DataItem() { Id = 1, Name = "Item 1" }; var item2 = new DataItem() { Id = 2, Name = "Item 2" }; var data = new List <DataItem>(new DataItem[] { item1, item2 }); using (var reader = new EnumerableDataReader <DataItem>(new List <DataItem>(), new string[] { "Id", "Name" })) { int itemIndex = 0; while (reader.Read()) { reader.GetValue(0).Should().Be(data[itemIndex].Id); reader.GetValue(1).Should().Be(data[itemIndex].Name); itemIndex++; } } }
public void ThrowsWhenReadAfterDisposed() { var localReader = new EnumerableDataReader<FakeEntity>(this.data); localReader.IsClosed.Should().BeFalse(); localReader.Close(); localReader.IsClosed.Should().BeTrue(); Action act = () => localReader.Read(); act.ShouldThrow<ObjectDisposedException>() .Where(ex => ex.ObjectName == "EnumerableDataReader"); }