Esempio n. 1
0
        public void SqlServerUpdateQuery_ShouldGenQuery()
        {
            // Arrange
            var command = new System.Data.SqlClient.SqlCommand();
            var db = MockRepository.GenerateStub<IDataMapper>();
            db.Expect(d => d.Command).Return(command);
            ColumnMapCollection columns = MapRepository.Instance.GetColumns(typeof(Person));
            MappingHelper mappingHelper = new MappingHelper(db);

            Person person = new Person();
            person.ID = 1;
            person.Name = "Jordan";
            person.Age = 33;
            person.IsHappy = true;
            person.BirthDate = new DateTime(1977, 1, 22);

            mappingHelper.CreateParameters<Person>(person, columns, true);

            int idValue = 7;
            TableCollection tables = new TableCollection { new Table(typeof(Person)) };
            Expression<Func<Person, bool>> filter =  p => p.ID == person.ID || p.ID == idValue || p.Name == person.Name && p.Name == "Bob";
            var where = new WhereBuilder<Person>(command, new SqlServerDialect(), filter, tables, false, true);

            IQuery query = new UpdateQuery(new SqlServerDialect(), columns, command, "dbo.People", where.ToString());

            // Act
            string queryText = query.Generate();

            // Assert
            Assert.IsNotNull(queryText);
            Assert.IsTrue(queryText.Contains("UPDATE [dbo].[People]"));
            Assert.IsTrue(queryText.Contains("[Name]"));
            Assert.IsTrue(queryText.Contains("[Age]"));
            Assert.IsTrue(queryText.Contains("[IsHappy]"));
            Assert.IsTrue(queryText.Contains("[BirthDate]"));
            Assert.IsTrue(queryText.Contains("[ID] = @P4"));
            Assert.IsTrue(queryText.Contains("[ID] = @P5"));
            Assert.IsTrue(queryText.Contains("[Name] = @P6"));
            Assert.IsTrue(queryText.Contains("[Name] = @P7"));
            Assert.AreEqual(command.Parameters["@P4"].Value, 1);
            Assert.AreEqual(command.Parameters["@P5"].Value, 7);
            Assert.AreEqual(command.Parameters["@P6"].Value, "Jordan");
            Assert.AreEqual(command.Parameters["@P7"].Value, "Bob");
        }
Esempio n. 2
0
        public void SqlServerUpdateQuery_ShouldGenQuery()
        {
            // Arrange
            var command = new System.Data.SqlClient.SqlCommand();
            ColumnMapCollection columns = MapRepository.Instance.GetColumns(typeof(Person));
            MappingHelper mappingHelper = new MappingHelper(command);

            Person person = new Person();
            person.ID = 1;
            person.Name = "Jordan";
            person.Age = 33;
            person.IsHappy = true;
            person.BirthDate = new DateTime(1977, 1, 22);

            mappingHelper.CreateParameters<Person>(person, columns, false, true);

            int idValue = 7;
            var where = new WhereBuilder<Person>(command, p => p.ID == person.ID || p.ID == idValue || p.Name == person.Name && p.Name == "Bob", false);

            IQuery query = new UpdateQuery(columns, command, "dbo.People", where.ToString());

            // Act
            string queryText = query.Generate();

            // Assert
            Assert.IsNotNull(queryText);
            Assert.IsTrue(queryText.Contains("UPDATE dbo.People"));
            Assert.IsTrue(queryText.Contains("[Name]"));
            Assert.IsTrue(queryText.Contains("[Age]"));
            Assert.IsTrue(queryText.Contains("[IsHappy]"));
            Assert.IsTrue(queryText.Contains("[BirthDate]"));
            Assert.IsTrue(queryText.Contains("[ID] = @P5"));
            Assert.IsTrue(queryText.Contains("[ID] = @P6"));
            Assert.IsTrue(queryText.Contains("[Name] = @P7"));
            Assert.IsTrue(queryText.Contains("[Name] = @P8"));
            Assert.AreEqual(command.Parameters["@P5"].Value, 1);
            Assert.AreEqual(command.Parameters["@P6"].Value, 7);
            Assert.AreEqual(command.Parameters["@P7"].Value, "Jordan");
            Assert.AreEqual(command.Parameters["@P8"].Value, "Bob");
        }