コード例 #1
0
        protected IDataMapper CreateDB_ForQuery(StubResultSet rs)
        {
            StubDataReader reader = new StubDataReader(rs);

            _parameters = MockRepository.GenerateMock <DbParameterCollection>();
            _parameters.Expect(p => p.Add(null)).Return(1).IgnoreArguments();

            _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);

            _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 DataMapper(dbFactory, "connString..."));
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        public void QueryToGraph_WithNestedRelationships_UnsortedResults_ShouldMapToGraph()
        {
            // Arrange
            StubResultSet rs = new StubResultSet("ID", "OrderName", "OrderItemID", "ItemDescription", "Price", "AmountPaid");

            // For this test, results are purposefully out of order
            rs.AddRow(3, "Order3", DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value);
            rs.AddRow(1, "Order1", 50, "Red car", 100.35m, DBNull.Value);
            rs.AddRow(2, "Order2", 60, "Guitar", 1500.50m, 1500.50m);
            rs.AddRow(1, "Order1", 51, "Blue wagon", 44.87m, DBNull.Value);
            rs.AddRow(2, "Order2", 61, "Bass", 2380.00m, 50.00m);

            // Act
            var          db     = CreateDB_ForQuery(rs);
            List <Order> orders = db.QueryToGraph <Order>("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);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        public void QueryToGraph_WithNestedRelationships_ShouldMapToGraph()
        {
            // 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> orders = db.QueryToGraph <Order>("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);
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        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.AutoQueryToGraph <Order>()
                                  .Load(o => o.OrderItems)
                                  .Load(o => o.OrderItems, o => o.OrderItems.First().ItemReceipt)
                                  .Where(o => o.ID > 1);
        }
コード例 #11
0
        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);
        }
コード例 #12
0
        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);
        }
コード例 #13
0
ファイル: TestBase.cs プロジェクト: vawen/Marr.DataMapper
        protected IDataMapper CreateDB_ForQuery(StubResultSet rs)
        {
            StubDataReader reader = new StubDataReader(rs);

            _parameters = MockRepository.GenerateMock<DbParameterCollection>();
            _parameters.Expect(p => p.Add(null)).Return(1).IgnoreArguments();

            _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);

            _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 DataMapper(dbFactory, "connString...");
        }
コード例 #14
0
        public void QueryToGraph_WithNestedRelationships_ShouldMapToGraph()
        {
            // 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> orders = db.QueryToGraph<Order>("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);
        }
コード例 #15
0
        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.AutoQueryToGraph<Order>()
                .Load(o => o.OrderItems)
                .Load(o => o.OrderItems, o => o.OrderItems.First().ItemReceipt)
                .Where(o => o.ID > 1);
                
                
        }
コード例 #16
0
        public void QueryToGraph_WithNestedRelationships_UnsortedResults_ShouldMapToGraph()
        {
            // Arrange
            StubResultSet rs = new StubResultSet("ID", "OrderName", "OrderItemID", "ItemDescription", "Price", "AmountPaid");

            // For this test, results are purposefully out of order
            rs.AddRow(3, "Order3", DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value);
            rs.AddRow(1, "Order1", 50, "Red car", 100.35m, DBNull.Value);
            rs.AddRow(2, "Order2", 60, "Guitar", 1500.50m, 1500.50m);
            rs.AddRow(1, "Order1", 51, "Blue wagon", 44.87m, DBNull.Value);
            rs.AddRow(2, "Order2", 61, "Bass", 2380.00m, 50.00m);

            // Act
            var db = CreateDB_ForQuery(rs);
            List<Order> orders = db.QueryToGraph<Order>("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);
        }