public void LazyLoaded_Implicit_Conversion_When_Not_IsLoad_Should_Call_DB_Once() { // Arrange StubResultSet rsOffices = new StubResultSet("Number"); rsOffices.AddRow(1); rsOffices.AddRow(2); var db = CreateDB_ForQuery(rsOffices); Building building = new Building(); int calls = 0; var lazyProxy = new LazyLoaded<Building, List<Office>>((d, b) => { calls++; return d.Query<Office>().ToList(); }, RelationshipTypes.Many); lazyProxy.Prepare(() => db, building, "Offices"); building._offices = lazyProxy; // Act int count = building.Offices.Count; // Assert Assert.AreEqual(2, count); Assert.AreEqual(1, calls); // Act again (should not hit db) count = building.Offices.Count; // Assert Assert.AreEqual(2, count); Assert.AreEqual(1, calls); }
public void DupesAtSameLevel_WithDifferentIDs() { // Arrange var rs1 = new StubResultSet("ID", "Dupe1ID", "Dupe2ID"); rs1.AddRow(1, 100, 101); var rs2 = new StubResultSet("ID", "Name", "ChildID"); rs2.AddRow(100, "Dupe_100", 1000); var rs3 = new StubResultSet("ID", "Name", "ChildID"); rs3.AddRow(101, "Dupe_101", 1001); var db = CreateDB_ForQuery(rs1, rs2, rs3); // Act var rootEnt = db.Query<RootClass>() .Graph(r => r.Dupe1, r => r.Dupe2) .FirstOrDefault(); // Assert Assert.AreEqual(100, rootEnt.Dupe1ID); Assert.AreEqual(101, rootEnt.Dupe2ID); Assert.IsNotNull(rootEnt.Dupe1); Assert.IsNotNull(rootEnt.Dupe2); Assert.AreEqual(100, rootEnt.Dupe1.ID); Assert.AreEqual(101, rootEnt.Dupe2.ID); }
public void Query_ShouldMapToList() { // Arrange StubResultSet rs = new StubResultSet("ID", "Name", "Age", "IsHappy", "BirthDate"); rs.AddRow(1, "Jordan", 33, true, new DateTime(1977, 1, 22)); rs.AddRow(2, "Amyme", 31, false, new DateTime(1979, 10, 19)); // Act var db = CreateDB_ForQuery(rs); var people = db.Query<Person>("sql..."); // Assert Assert.IsTrue(people.Count == 2); Person jordan = people[0]; Assert.AreEqual(1, jordan.ID); Assert.AreEqual("Jordan", jordan.Name); Assert.AreEqual(33, jordan.Age); Assert.AreEqual(true, jordan.IsHappy); Assert.AreEqual(new DateTime(1977, 1, 22), jordan.BirthDate); Person amyme = people[1]; Assert.AreEqual(2, amyme.ID); Assert.AreEqual("Amyme", amyme.Name); Assert.AreEqual(31, amyme.Age); Assert.AreEqual(false, amyme.IsHappy); Assert.AreEqual(new DateTime(1979, 10, 19), amyme.BirthDate); }
public void WhenFindingOneString_ShouldReturnAString() { StubResultSet rs = new StubResultSet("Name"); rs.AddRow("Person1"); var db = CreateDB_ForQuery(rs); string name = db.Find<string>("SELECT [Name] FROM Person"); Assert.AreEqual("Person1", name); }
public void ShouldLoadEntityWithDelegateSyntax() { StubResultSet rs = new StubResultSet("ID", "Name", "Age"); rs.AddRow(2, "Person2", 35); var db = CreateDB_ForQuery(rs); Person p = db.ExecuteReader<Person>("SELECT PersonName FROM tbl WHERE ID=2", LoadPerson).FirstOrDefault(); Assert.AreEqual("Person2", p.Name); }
public void Find_WithNoRows_ShouldReturnNull() { // Arrange StubResultSet rs = new StubResultSet("ID", "Name", "Age", "IsHappy", "BirthDate"); // Act var db = CreateDB_ForQuery(rs); Person person = db.Find<Person>("sql..."); // Assert Assert.IsNull(person); }
public void ShouldLoadEntityWithLambdaSyntax() { StubResultSet rs = new StubResultSet("ID", "Name", "Age"); rs.AddRow(2, "Person2", 35); var db = CreateDB_ForQuery(rs); Person p = db.ExecuteReader("SELECT PersonName FROM tbl WHERE ID=2", r => new Person { ID = r.GetValue<int>("ID"), Name = r.GetValue<string>("Name"), Age = r.GetValue<int>("Age") } ).FirstOrDefault(); Assert.AreEqual("Person2", p.Name); }
public void Find_WithNoRows_PassingInObject_ShouldReturnObject() { // Arrange StubResultSet rs = new StubResultSet("ID", "Name", "Age", "IsHappy", "BirthDate"); // Act var db = CreateDB_ForQuery(rs); Person person = new Person { ID = 5 }; db.Find<Person>("sql...", person); // Assert Assert.IsNotNull(person); Assert.AreEqual(5, person.ID); }
public void WhenQueryingManyIntegers_ShouldReturnListOfIntegers() { StubResultSet rs = new StubResultSet("ID"); rs.AddRow(1); rs.AddRow(2); rs.AddRow(3); var db = CreateDB_ForQuery(rs); List<int> ids = db.Query<int>("SELECT [ID] FROM Person"); Assert.AreEqual(1, ids[0]); Assert.AreEqual(2, ids[1]); Assert.AreEqual(3, ids[2]); }
public void WhenQueryingManyStrings_ShouldReturnListOfStrings() { StubResultSet rs = new StubResultSet("Name"); rs.AddRow("Person1"); rs.AddRow("Person2"); rs.AddRow("Person3"); var db = CreateDB_ForQuery(rs); List<string> names = db.Query<string>("SELECT [Name] FROM Person"); Assert.AreEqual("Person1", names[0]); Assert.AreEqual("Person2", names[1]); Assert.AreEqual("Person3", names[2]); }
public void ShouldLoadEntityListWithDelegateSyntax() { StubResultSet rs = new StubResultSet("ID", "Name", "Age"); rs.AddRow(1, "Person1", 31); rs.AddRow(2, "Person2", 32); rs.AddRow(3, "Person3", 33); var db = CreateDB_ForQuery(rs); List<Person> people = db.ExecuteReader("SELECT PersonName FROM tbl WHERE ID=2", r => new Person { ID = r.GetInt32(0), Name = r.GetString(1), Age = r.GetInt32(2) } ).ToList(); Assert.AreEqual(3, people.Count); Assert.AreEqual(1, people[0].ID); Assert.AreEqual(2, people[1].ID); Assert.AreEqual(3, people[2].ID); }
public void Init() { // Register type converters first MapRepository.Instance.RegisterTypeConverter(typeof(decimal), new Converters.CastConverter<decimal, IConvertible>()); MapRepository.Instance.RegisterTypeConverter(typeof(double), new Converters.CastConverter<double, IConvertible>()); MapRepository.Instance.RegisterTypeConverter(typeof(Single), new Converters.CastConverter<Single, IConvertible>()); MapRepository.Instance.RegisterTypeConverter(typeof(long), new Converters.CastConverter<long, IConvertible>()); MapRepository.Instance.RegisterTypeConverter(typeof(int), new Converters.CastConverter<int, IConvertible>()); MapRepository.Instance.RegisterTypeConverter(typeof(short), new Converters.CastConverter<short, IConvertible>()); MapRepository.Instance.RegisterTypeConverter(typeof(Byte), new Converters.CastConverter<Byte, IConvertible>()); new FluentMappings() .Entity<ImplicitProperties>() .Columns.AutoMapAllProperties(); _rs = new StubResultSet("DecimalValue", "DoubleValue", "SingleValue", "LongValue", "IntValue", "ShortValue", "ByteValue"); }
public void ExecuteReaderAction_ShouldLoadEntityDictionary() { StubResultSet rs = new StubResultSet("ID", "Hash"); rs.AddRow(1, "Hash1"); rs.AddRow(2, "Hash2"); rs.AddRow(3, "Hash3"); var db = CreateDB_ForQuery(rs); Dictionary<string, int> people = new Dictionary<string, int>(); db.ExecuteReader("SELECT PersonName FROM tbl", r => { people.Add(r.GetString(1), r.GetInt32(0)); }); Assert.AreEqual(3, people.Count); Assert.AreEqual(1, people["Hash1"]); Assert.AreEqual(2, people["Hash2"]); Assert.AreEqual(3, people["Hash3"]); }
public void Find_ShouldMapToEntity() { // Arrange StubResultSet rs = new StubResultSet("ID", "Name", "Age", "IsHappy", "BirthDate"); rs.AddRow(1, "Jordan", 33, true, new DateTime(1977, 1, 22)); // Act var db = CreateDB_ForQuery(rs); Person person = db.Find<Person>("sql..."); // Assert Assert.IsNotNull(person); Assert.AreEqual(1, person.ID); Assert.AreEqual("Jordan", person.Name); Assert.AreEqual(33, person.Age); Assert.AreEqual(true, person.IsHappy); Assert.AreEqual(new DateTime(1977, 1, 22), person.BirthDate); }
public void LazyLoadedException_ShouldThrowDataMappingException() { // Arrange StubResultSet rsOffices = new StubResultSet("Number"); rsOffices.AddRow(1); rsOffices.AddRow(2); var db = CreateDB_ForQuery(rsOffices); Building building = new Building(); var lazyProxy = new LazyLoaded<Building, List<Office>>((d, b) => { throw new Exception("Oops!"); //return d.Query<Office>().ToList(); }, RelationshipTypes.Many); lazyProxy.Prepare(() => db, building, "Offices"); building._offices = lazyProxy; // Act int count = building.Offices.Count; }
public void WhenQuerying_CastingError_ShouldGiveCustomErrorMessage() { // Create a result set with a long value and then // purposefully cast it to a string to create a cast exception StubResultSet rs = new StubResultSet("LongValue"); rs.AddRow(12345678L); var db = CreateDB_ForQuery(rs); bool exceptionWasThrown = false; try { string name = db.Query<string>("SELECT [LongValue] FROM Tbl").FirstOrDefault(); } catch (Exception ex) { exceptionWasThrown = true; Assert.IsInstanceOfType(ex, typeof(DataMappingException)); } Assert.IsTrue(exceptionWasThrown); }
/// <summary> /// Creates a DataMapper that can be used to test queries. /// </summary> /// <param name="rs">The stubbed record set.</param> /// <returns>Returns a StubDataMapper.</returns> public static IDataMapper CreateForQuery(StubResultSet rs) { StubDataReader reader = new StubDataReader(rs); var parameters = MockRepository.GenerateMock<DbParameterCollection>(); parameters.Expect(p => p.Add(null)).Return(1).IgnoreArguments(); var command = MockRepository.GenerateMock<DbCommand>(); command.Expect(c => c.ExecuteReader()).Return(reader); command.Expect(c => c.Parameters).Return(parameters); command.Expect(c => c.CreateParameter()).Return(new System.Data.SqlClient.SqlParameter()).Repeat.Any(); command.Stub(c => c.CommandText); var connection = MockRepository.GenerateMock<DbConnection>(); connection.Expect(c => c.CreateCommand()).Return(command); command.Expect(c => c.Connection).Return(connection); DbProviderFactory dbFactory = MockRepository.GenerateMock<DbProviderFactory>(); dbFactory.Expect(f => f.CreateConnection()).Return(connection); return new StubDataMapper(dbFactory, command, connection, parameters); }
public void QueryToGraph_WithNestedRelationships_ShouldMapToGraph() { // Arrange StubResultSet rs = new StubResultSet("ID", "OrderName", "OrderItemID", "OrderID", "ItemDescription", "Price", "AmountPaid"); rs.AddRow(1, "Order1", 50, 1, "Red car", 100.35m, DBNull.Value); rs.AddRow(1, "Order1", 51, 1, "Blue wagon", 44.87m, DBNull.Value); rs.AddRow(2, "Order2", 60, 2, "Guitar", 1500.50m, 1500.50m); rs.AddRow(2, "Order2", 61, 3, "Bass", 2380.00m, 50.00m); rs.AddRow(3, "Order3", DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value); // Act var db = CreateDB_ForQuery(rs); List<Order> orders = db.Query<Order>().Graph().QueryText("sql..."); // Assert Assert.IsTrue(orders.Count == 3); Order order1 = orders[0]; Order order2 = orders[1]; Order order3 = orders[2]; Assert.IsTrue(order1.OrderItems.Count == 2); Assert.IsTrue(order2.OrderItems.Count == 2); Assert.IsTrue(order3.OrderItems.Count == 0); // Order 1 Assert.AreEqual(1, order1.ID); Assert.AreEqual("Order1", order1.OrderName); // Order 1 -> Item 1 Assert.AreEqual(50, order1.OrderItems[0].ID); Assert.AreEqual("Red car", order1.OrderItems[0].ItemDescription); Assert.AreEqual(100.35m, order1.OrderItems[0].Price); Assert.IsNull(order1.OrderItems[0].ItemReceipt.AmountPaid); // Order 1 -> Item 2 Assert.AreEqual(51, order1.OrderItems[1].ID); Assert.AreEqual("Blue wagon", order1.OrderItems[1].ItemDescription); Assert.AreEqual(44.87m, order1.OrderItems[1].Price); Assert.IsNull(order1.OrderItems[1].ItemReceipt.AmountPaid); // Order 2 -> Item 1 Assert.AreEqual(60, order2.OrderItems[0].ID); Assert.AreEqual("Guitar", order2.OrderItems[0].ItemDescription); Assert.AreEqual(1500.50m, order2.OrderItems[0].Price); Assert.AreEqual(1500.50m, order2.OrderItems[0].ItemReceipt.AmountPaid); // Order 2 -> Item 2 Assert.AreEqual(61, order2.OrderItems[1].ID); Assert.AreEqual("Bass", order2.OrderItems[1].ItemDescription); Assert.AreEqual(2380.00m, order2.OrderItems[1].Price); Assert.AreEqual(50.00m, order2.OrderItems[1].ItemReceipt.AmountPaid); }
public void DupesAtSameLevel_WithSameIDs_OneWithJoin() { // Arrange var rsRoot = new StubResultSet("ID", "Dupe1ID", "Dupe2ID"); rsRoot.AddRow(1, 100, 100); var rsDupe1 = new StubResultSet("ID", "Name", "ChildID", "dccID", "dccName"); rsDupe1.AddRow(100, "Dupe_100", 1000, 1000, "Child_1000"); var rsDupe2 = new StubResultSet("ID", "Name", "ChildID"); rsDupe2.AddRow(100, "Dupe_100", 1000); var db = CreateDB_ForQuery(rsRoot, rsDupe1, rsDupe2); // Act var rootEnt = db.Query<RootClass>() .Graph( r => r.Dupe1, r => r.Dupe2, r => r.Dupe1.Child) .FirstOrDefault(); // Assert Assert.AreEqual(100, rootEnt.Dupe1ID); Assert.AreEqual(100, rootEnt.Dupe2ID); Assert.IsNotNull(rootEnt.Dupe1); Assert.IsNotNull(rootEnt.Dupe2); Assert.AreEqual(100, rootEnt.Dupe1.ID); Assert.AreEqual(100, rootEnt.Dupe2.ID); Assert.IsNotNull(rootEnt.Dupe1.Child); Assert.IsNull(rootEnt.Dupe2.Child); Assert.AreEqual(1000, rootEnt.Dupe1.Child.ID); Assert.AreEqual("Child_1000", rootEnt.Dupe1.Child.Name); }
//[TestMethod] public void MultiEntityTest() { // Arrange StubResultSet rs = new StubResultSet("ID", "OrderName", "OrderItemID", "ItemDescription", "Price", "AmountPaid"); rs.AddRow(1, "Order1", 50, "Red car", 100.35m, DBNull.Value); rs.AddRow(1, "Order1", 51, "Blue wagon", 44.87m, DBNull.Value); rs.AddRow(2, "Order2", 60, "Guitar", 1500.50m, 1500.50m); rs.AddRow(2, "Order2", 61, "Bass", 2380.00m, 50.00m); rs.AddRow(3, "Order3", DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value); // Act var db = CreateDB_ForQuery(rs); List<Order> people = db .Query<Order>() .Graph(o => o.OrderItems); }
public void DataReader_Test() { // Arrange StubResultSet rs = new StubResultSet("ID", "Name", "Age", "IsHappy", "BirthDate"); rs.AddRow(1, "Jordan", 33, true, new DateTime(1977, 1, 22)); rs.AddRow(2, "Amyme", 31, false, new DateTime(1979, 10, 19)); rs.AddRow(3, "Guy", 29, false, new DateTime(1981, 10, 3)); // Act var db = CreateDB_ForQuery(rs); List<Person> people = new List<Person>(); db.Query<Person>() .QueryText("sql...") .DataReader(r => { while (r.Read()) { Person p = new Person(); p.ID = r.GetInt32(r.GetOrdinal("ID")); p.Name = r.GetString(r.GetOrdinal("Name")); p.Age = r.GetInt32(r.GetOrdinal("Age")); p.IsHappy = r.GetBoolean(r.GetOrdinal("IsHappy")); p.BirthDate = r.GetDateTime(r.GetOrdinal("BirthDate")); people.Add(p); } }); // Assert Assert.IsTrue(people.Count == 3); Person jordan = people[0]; Assert.AreEqual(1, jordan.ID); Assert.AreEqual("Jordan", jordan.Name); Assert.AreEqual(33, jordan.Age); Assert.AreEqual(true, jordan.IsHappy); Assert.AreEqual(new DateTime(1977, 1, 22), jordan.BirthDate); Person amyme = people[1]; Assert.AreEqual(2, amyme.ID); Assert.AreEqual("Amyme", amyme.Name); Assert.AreEqual(31, amyme.Age); Assert.AreEqual(false, amyme.IsHappy); Assert.AreEqual(new DateTime(1979, 10, 19), amyme.BirthDate); }
public void QueryToGraph_ShouldMapToList() { // Arrange StubResultSet rs = new StubResultSet("ID", "Name", "Age", "IsHappy", "BirthDate", "Pet_ID", "Pet_Name"); rs.AddRow(1, "Jordan", 33, true, new DateTime(1977, 1, 22), 1, "Chuy"); rs.AddRow(1, "Jordan", 33, true, new DateTime(1977, 1, 22), 2, "Bela"); rs.AddRow(2, "Amyme", 31, false, new DateTime(1979, 10, 19), 3, "Bird"); rs.AddRow(2, "Amyme", 31, false, new DateTime(1979, 10, 19), 4, "Alligator"); // Act var db = CreateDB_ForQuery(rs); var people = db.Query<Person>().Graph().QueryText("sql...").ToList(); // Assert Assert.IsTrue(people.Count == 2); Person jordan = people[0]; Assert.AreEqual(1, jordan.ID); Assert.AreEqual("Jordan", jordan.Name); Assert.AreEqual(33, jordan.Age); Assert.AreEqual(true, jordan.IsHappy); Assert.AreEqual(new DateTime(1977, 1, 22), jordan.BirthDate); Assert.IsTrue(jordan.Pets.Count == 2); Assert.AreEqual("Chuy", jordan.Pets[0].Name); Assert.AreEqual("Bela", jordan.Pets[1].Name); Person amyme = people[1]; Assert.AreEqual(2, amyme.ID); Assert.AreEqual("Amyme", amyme.Name); Assert.AreEqual(31, amyme.Age); Assert.AreEqual(false, amyme.IsHappy); Assert.AreEqual(new DateTime(1979, 10, 19), amyme.BirthDate); Assert.IsTrue(amyme.Pets.Count == 2); Assert.AreEqual("Bird", amyme.Pets[0].Name); Assert.AreEqual("Alligator", amyme.Pets[1].Name); }
public void QueryToGraph_WithNestedRelationships_UnsortedResults_ShouldMapToGraph() { // Arrange StubResultSet rs = new StubResultSet("ID", "OrderName", "OrderItemID", "OrderID", "ItemDescription", "Price", "AmountPaid"); // For this test, results are purposefully out of order rs.AddRow(3, "Order3", DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value); rs.AddRow(1, "Order1", 50, 1, "Red car", 100.35m, DBNull.Value); rs.AddRow(2, "Order2", 60, 2, "Guitar", 1500.50m, 1500.50m); rs.AddRow(1, "Order1", 51, 1, "Blue wagon", 44.87m, DBNull.Value); rs.AddRow(2, "Order2", 61, 2, "Bass", 2380.00m, 50.00m); // Act var db = CreateDB_ForQuery(rs); List<Order> orders = db.Query<Order>().Graph().QueryText("sql..."); // Assert Assert.IsTrue(orders.Count == 3); // NOTE: Cannot assume that orders are sorted, so get by OrderName to verify children Order order1 = orders.Where(o => o.OrderName == "Order1").FirstOrDefault(); Order order2 = orders.Where(o => o.OrderName == "Order2").FirstOrDefault(); Order order3 = orders.Where(o => o.OrderName == "Order3").FirstOrDefault(); Assert.IsTrue(order1.OrderItems.Count == 2); Assert.IsTrue(order2.OrderItems.Count == 2); Assert.IsTrue(order3.OrderItems.Count == 0); // Order 1 Assert.AreEqual(1, order1.ID); Assert.AreEqual("Order1", order1.OrderName); // Order 1 -> Item 1 Assert.AreEqual(50, order1.OrderItems[0].ID); Assert.AreEqual("Red car", order1.OrderItems[0].ItemDescription); Assert.AreEqual(100.35m, order1.OrderItems[0].Price); Assert.IsNull(order1.OrderItems[0].ItemReceipt.AmountPaid); // Order 1 -> Item 2 Assert.AreEqual(51, order1.OrderItems[1].ID); Assert.AreEqual("Blue wagon", order1.OrderItems[1].ItemDescription); Assert.AreEqual(44.87m, order1.OrderItems[1].Price); Assert.IsNull(order1.OrderItems[1].ItemReceipt.AmountPaid); // Order 2 -> Item 1 Assert.AreEqual(60, order2.OrderItems[0].ID); Assert.AreEqual("Guitar", order2.OrderItems[0].ItemDescription); Assert.AreEqual(1500.50m, order2.OrderItems[0].Price); Assert.AreEqual(1500.50m, order2.OrderItems[0].ItemReceipt.AmountPaid); // Order 2 -> Item 2 Assert.AreEqual(61, order2.OrderItems[1].ID); Assert.AreEqual("Bass", order2.OrderItems[1].ItemDescription); Assert.AreEqual(2380.00m, order2.OrderItems[1].Price); Assert.AreEqual(50.00m, order2.OrderItems[1].ItemReceipt.AmountPaid); }
protected IDataMapper CreateDB_ForQuery(StubResultSet rs) { return StubDataMapperFactory.CreateForQuery(rs); }