Ejemplo n.º 1
0
        public void SqlServerInsertQuery_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);

            IQuery query = new InsertQuery(new SqlServerDialect(), columns, command, "dbo.People");

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

            // Assert
            Assert.IsNotNull(queryText);
            Assert.IsTrue(queryText.Contains("INSERT INTO [dbo].[People]"));
            Assert.IsFalse(queryText.Contains("@ID"), "Should not contain ID column since it is marked as AutoIncrement");
            Assert.IsTrue(queryText.Contains("[Name]"), "Should contain the name column");
        }
Ejemplo n.º 2
0
        public void WhenColumnNameDiffersFromProperty_InsertQueryShouldUseColumnName()
        {
            // Arrange
            Person2 person = new Person2 { Name = "Bob", Age = 40, BirthDate = DateTime.Now };
            Dialect dialect = new SqlServerDialect();
            ColumnMapCollection mappings = MapRepository.Instance.GetColumns(typeof(Person2));
            var command = new System.Data.SqlClient.SqlCommand();
            var db = MockRepository.GenerateStub<IDataMapper>();
            db.Expect(d => d.Command).Return(command);
            var mappingHelper = new MappingHelper(db);
            mappingHelper.CreateParameters<Person2>(person, mappings, true);
            string targetTable = "PersonTable";
            InsertQuery query = new InsertQuery(dialect, mappings, command, targetTable);

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

            // Assert
            Assert.IsTrue(queryText.Contains("[PersonName]"), "Query should contain column name");
            Assert.IsTrue(queryText.Contains("[PersonAge]"), "Query should contain column name");
            Assert.IsTrue(queryText.Contains("[BirthDate]"), "Query should contain property name");
            Assert.IsTrue(queryText.Contains("[IsHappy]"), "Query should contain property name");
        }