public void CanAlterDefaultConstraintWithNewGuidAsDefault()
        {
            var expression = GeneratorTestHelper.GetAlterDefaultConstraintExpression();

            expression.DefaultValue = SystemMethods.NewGuid;

            var expected = "ALTER TABLE [dbo].[TestTable1] ALTER [TestColumn1] DROP DEFAULT;" + Environment.NewLine +
                           "-- create alter table command to create new default constraint as string and run it" + Environment.NewLine +
                           "ALTER TABLE [dbo].[TestTable1] ALTER [TestColumn1] DEFAULT NEWID();";

            var result = Generator.Generate(expression);

            result.ShouldBe(expected);
        }
Exemplo n.º 2
0
        public override void CanCreateNamedMultiColumnForeignKeyWithDifferentSchemas()
        {
            Assert.Ignore("HANA does not support schema like us know schema in hana is a database name");

            var expression = GeneratorTestHelper.GetCreateNamedMultiColumnForeignKeyExpression();

            expression.ForeignKey.ForeignTableSchema = "TestSchema";

            var result = Generator.Generate(expression);

            result.ShouldBe("ALTER TABLE \"TestTable1\" " +
                            "ADD CONSTRAINT \"FK_Test\" FOREIGN KEY (\"TestColumn1\", \"TestColumn3\") " +
                            "REFERENCES \"TestTable2\" (\"TestColumn2\", \"TestColumn4\")");
        }
Exemplo n.º 3
0
        public override void CanInsertDataWithCustomSchema()
        {
            var expression = GeneratorTestHelper.GetInsertDataExpression();

            expression.SchemaName = "TestSchema";

            var expected = "INSERT INTO \"TestSchema\".\"TestTable1\" (\"Id\",\"Name\",\"Website\") VALUES (1,'Just''in','codethinked.com');";

            expected += "INSERT INTO \"TestSchema\".\"TestTable1\" (\"Id\",\"Name\",\"Website\") VALUES (2,'Na\\te','kohari.org');";

            var result = Generator.Generate(expression);

            result.ShouldBe(expected);
        }
Exemplo n.º 4
0
        public void CanCreateTableWithCustomColumnTypeWithCustomSchema()
        {
            var expression = GeneratorTestHelper.GetCreateTableExpression();

            expression.SchemaName = "TestSchema";
            expression.Columns[0].IsPrimaryKey = true;
            expression.Columns[1].Type         = null;
            expression.Columns[1].CustomType   = "[timestamp]";

            var sql = generator.Generate(expression);

            sql.ShouldBe(
                "CREATE TABLE [TestSchema].[TestTable1] ([TestColumn1] NVARCHAR(255) NOT NULL, [TestColumn2] [timestamp] NOT NULL, PRIMARY KEY ([TestColumn1]))");
        }
Exemplo n.º 5
0
        public override void CanInsertDataWithCustomSchema()
        {
            var expression = GeneratorTestHelper.GetInsertDataExpression();

            expression.SchemaName = "TestSchema";

            var expected = "INSERT INTO [TestSchema].[TestTable1] ([Id], [Name], [Website]) VALUES (1, N'Just''in', N'codethinked.com');";

            expected += @" INSERT INTO [TestSchema].[TestTable1] ([Id], [Name], [Website]) VALUES (2, N'Na\te', N'kohari.org')";

            var result = Generator.Generate(expression);

            result.ShouldBe(expected);
        }
        public override void CanInsertDataWithCustomSchema()
        {
            var expression = GeneratorTestHelper.GetInsertDataExpression();

            expression.SchemaName = "TestSchema";

            var expected = "INSERT INTO TestTable1 (Id, Name, Website) VALUES (1, 'Just''in', 'codethinked.com');";

            expected += " INSERT INTO TestTable1 (Id, Name, Website) VALUES (2, 'Na\\te', 'kohari.org')";

            var result = CreateFixture().Generate(expression);

            result.ShouldBe(expected);
        }
Exemplo n.º 7
0
        public void CanCreateForeignKeyWithOnDeleteOptions(Rule rule, string output)
        {
            var expression = GeneratorTestHelper.GetCreateForeignKeyExpression();

            expression.ForeignKey.OnDelete = rule;
            var sql = generator.Generate(expression);

            sql.ShouldBe(
                string.Format("ALTER TABLE TestTable1 ADD CONSTRAINT FK_Test FOREIGN KEY (TestColumn1) REFERENCES TestTable2 (TestColumn2) ON DELETE {0}", output));

            sql = quotedIdentiferGenerator.Generate(expression);
            sql.ShouldBe(
                string.Format("ALTER TABLE \"TestTable1\" ADD CONSTRAINT \"FK_Test\" FOREIGN KEY (\"TestColumn1\") REFERENCES \"TestTable2\" (\"TestColumn2\") ON DELETE {0}", output));
        }
Exemplo n.º 8
0
        public void CanInsertDataWithSqlServerIdentityInsertInLooseMode()
        {
            var expression = GeneratorTestHelper.GetInsertDataExpression();

            expression.AdditionalFeatures.Add(SqlServerExtensions.IdentityInsert, true);
            generator.compatabilityMode = Runner.CompatabilityMode.LOOSE;
            string sql = generator.Generate(expression);

            string expected = "INSERT INTO 'TestTable1' ('Id', 'Name', 'Website') VALUES (1, 'Just''in', 'codethinked.com');";

            expected += @" INSERT INTO 'TestTable1' ('Id', 'Name', 'Website') VALUES (2, 'Na\te', 'kohari.org')";

            sql.ShouldBe(expected);
        }
        public override void CanInsertWithOverridingUserValue()
        {
            var expression = GeneratorTestHelper.GetInsertDataExpression();

            expression.AdditionalFeatures[PostgresExtensions.OverridingIdentityValues] = PostgresOverridingIdentityValuesType.User;

            var expected = "INSERT INTO \"public\".\"TestTable1\" (\"Id\",\"Name\",\"Website\") OVERRIDING USER VALUE VALUES (1,'Just''in','codethinked.com');";

            expected += "INSERT INTO \"public\".\"TestTable1\" (\"Id\",\"Name\",\"Website\") OVERRIDING USER VALUE VALUES (2,'Na\\te','kohari.org');";

            var result = Generator.Generate(expression);

            result.ShouldBe(expected);
        }
Exemplo n.º 10
0
        public void CanCreateForeignKeyWithOnDeleteAndOnUpdateOptions()
        {
            var expression = GeneratorTestHelper.GetCreateForeignKeyExpression();

            expression.ForeignKey.OnDelete = Rule.Cascade;
            expression.ForeignKey.OnUpdate = Rule.SetDefault;
            var sql = generator.Generate(expression);

            sql.ShouldBe(
                "ALTER TABLE TestTable1 ADD CONSTRAINT FK_Test FOREIGN KEY (TestColumn1) REFERENCES TestTable2 (TestColumn2) ON DELETE CASCADE ON UPDATE SET DEFAULT");

            sql = quotedIdentiferGenerator.Generate(expression);
            sql.ShouldBe(
                "ALTER TABLE \"TestTable1\" ADD CONSTRAINT \"FK_Test\" FOREIGN KEY (\"TestColumn1\") REFERENCES \"TestTable2\" (\"TestColumn2\") ON DELETE CASCADE ON UPDATE SET DEFAULT");
        }
Exemplo n.º 11
0
        public override void CanAlterSchema()
        {
            var expression = GeneratorTestHelper.GetAlterSchemaExpression();
            var currentCompatabilityMode = Generator.CompatibilityMode;

            try
            {
                Generator.CompatibilityMode = Runner.CompatibilityMode.STRICT;
                Should.Throw <DatabaseOperationNotSupportedException>(() => Generator.Generate(expression));
            }
            finally
            {
                Generator.CompatibilityMode = currentCompatabilityMode;
            }
        }
        public void CanInsertDataWithSqlServerIdentityInsertInLooseMode()
        {
            var expression = GeneratorTestHelper.GetInsertDataExpression();

            expression.AdditionalFeatures.Add(SqlServerExtensions.IdentityInsert, true);
            Generator.CompatibilityMode = Runner.CompatibilityMode.LOOSE;

            var expected = "INSERT INTO \"TestTable1\" (\"Id\", \"Name\", \"Website\") VALUES (1, 'Just''in', 'codethinked.com');";

            expected += " INSERT INTO \"TestTable1\" (\"Id\", \"Name\", \"Website\") VALUES (2, 'Na\\te', 'kohari.org')";

            var result = Generator.Generate(expression);

            result.ShouldBe(expected);
        }
Exemplo n.º 13
0
        public void CanInsertDataWithIdentityInsert()
        {
            var expression = GeneratorTestHelper.GetInsertDataExpression();

            expression.AdditionalFeatures.Add(SqlServerExtensions.IdentityInsert, true);
            var sql = generator.Generate(expression);

            var expected = "SET IDENTITY_INSERT [dbo].[TestTable1] ON;";

            expected += " INSERT INTO [dbo].[TestTable1] ([Id], [Name], [Website]) VALUES (1, 'Just''in', 'codethinked.com');";
            expected += @" INSERT INTO [dbo].[TestTable1] ([Id], [Name], [Website]) VALUES (2, 'Na\te', 'kohari.org');";
            expected += " SET IDENTITY_INSERT [dbo].[TestTable1] OFF";

            sql.ShouldBe(expected);
        }
Exemplo n.º 14
0
        public void CanInsertDataWithIdentityInsert()
        {
            var expression = GeneratorTestHelper.GetInsertDataExpression();

            expression.AdditionalFeatures.Add(SqlServerExtensions.IdentityInsert, true);

            var expected = "SET IDENTITY_INSERT [TestTable1] ON;";

            expected += " INSERT INTO [TestTable1] ([Id], [Name], [Website]) SELECT 1, N'Just''in', N'codethinked.com'";
            expected += @" UNION ALL SELECT 2, N'Na\te', N'kohari.org';";
            expected += " SET IDENTITY_INSERT [TestTable1] OFF";

            var result = Generator.Generate(expression);

            result.ShouldBe(expected);
        }
        public override void CanDropMultipleColumnsWithCustomSchema()
        {
            //This does not work if it is a primary key
            var expression = GeneratorTestHelper.GetDeleteColumnExpression(new[] { "TestColumn1", "TestColumn2" });

            expression.SchemaName = "TestSchema";

            var expected = "DECLARE @default sysname, @sql nvarchar(4000);" + Environment.NewLine + Environment.NewLine +
                           "-- get name of default constraint" + Environment.NewLine +
                           "SELECT @default = name" + Environment.NewLine +
                           "FROM sys.default_constraints" + Environment.NewLine +
                           "WHERE parent_object_id = object_id('[TestTable1]')" + Environment.NewLine + "" +
                           "AND type = 'D'" + Environment.NewLine +
                           "AND parent_column_id = (" + Environment.NewLine +
                           "SELECT column_id" + Environment.NewLine +
                           "FROM sys.columns" + Environment.NewLine +
                           "WHERE object_id = object_id('[TestTable1]')" + Environment.NewLine +
                           "AND name = 'TestColumn1'" + Environment.NewLine +
                           ");" + Environment.NewLine + Environment.NewLine +
                           "-- create alter table command to drop constraint as string and run it" + Environment.NewLine +
                           "SET @sql = N'ALTER TABLE [TestTable1] DROP CONSTRAINT ' + QUOTENAME(@default);" + Environment.NewLine +
                           "EXEC sp_executesql @sql;" + Environment.NewLine + Environment.NewLine +
                           "-- now we can finally drop column" + Environment.NewLine +
                           "ALTER TABLE [TestTable1] DROP COLUMN [TestColumn1];" + Environment.NewLine +
                           "GO" + Environment.NewLine +
                           "DECLARE @default sysname, @sql nvarchar(4000);" + Environment.NewLine + Environment.NewLine +
                           "-- get name of default constraint" + Environment.NewLine +
                           "SELECT @default = name" + Environment.NewLine +
                           "FROM sys.default_constraints" + Environment.NewLine +
                           "WHERE parent_object_id = object_id('[TestTable1]')" + Environment.NewLine +
                           "AND type = 'D'" + Environment.NewLine +
                           "AND parent_column_id = (" + Environment.NewLine +
                           "SELECT column_id" + Environment.NewLine +
                           "FROM sys.columns" + Environment.NewLine +
                           "WHERE object_id = object_id('[TestTable1]')" + Environment.NewLine +
                           "AND name = 'TestColumn2'" + Environment.NewLine +
                           ");" + Environment.NewLine + Environment.NewLine +
                           "-- create alter table command to drop constraint as string and run it" + Environment.NewLine +
                           "SET @sql = N'ALTER TABLE [TestTable1] DROP CONSTRAINT ' + QUOTENAME(@default);" + Environment.NewLine +
                           "EXEC sp_executesql @sql;" + Environment.NewLine + Environment.NewLine +
                           "-- now we can finally drop column" + Environment.NewLine +
                           "ALTER TABLE [TestTable1] DROP COLUMN [TestColumn2];" + Environment.NewLine;

            var result = Generator.Generate(expression);

            result.ShouldBe(expected);
        }
Exemplo n.º 16
0
        public void CanInsertDataWithIdentityInsertInStrictMode()
        {
            var expression = GeneratorTestHelper.GetInsertDataExpression();

            expression.AdditionalFeatures.Add(SqlServerExtensions.IdentityInsert, true);
            Generator.CompatibilityMode = Runner.CompatibilityMode.STRICT;

            var expected = "SET IDENTITY_INSERT [TestTable1] ON;";

            expected += " INSERT INTO [TestTable1] ([Id], [Name], [Website]) VALUES (1, N'Just''in', N'codethinked.com');";
            expected += @" INSERT INTO [TestTable1] ([Id], [Name], [Website]) VALUES (2, N'Na\te', N'kohari.org');";
            expected += " SET IDENTITY_INSERT [TestTable1] OFF";

            var result = Generator.Generate(expression);

            result.ShouldBe(expected);
        }
Exemplo n.º 17
0
        public void CanDropMultipleColumnsWithCustomSchema()
        {
            //This does not work if it is a primary key
            var expression = GeneratorTestHelper.GetDeleteColumnExpression(new string[] { "TestColumn1", "TestColumn2" });

            expression.SchemaName = "TestSchema";
            var sql = generator.Generate(expression);

            const string expected = "DECLARE @default sysname, @sql nvarchar(max);\r\n\r\n" +
                                    "-- get name of default constraint\r\n" +
                                    "SELECT @default = name\r\n" +
                                    "FROM sys.default_constraints\r\n" +
                                    "WHERE parent_object_id = object_id('[TestSchema].[TestTable1]')\r\n" + "" +
                                    "AND type = 'D'\r\n" + "" +
                                    "AND parent_column_id = (\r\n" + "" +
                                    "SELECT column_id\r\n" +
                                    "FROM sys.columns\r\n" +
                                    "WHERE object_id = object_id('[TestSchema].[TestTable1]')\r\n" +
                                    "AND name = 'TestColumn1'\r\n" +
                                    ");\r\n\r\n" +
                                    "-- create alter table command to drop contraint as string and run it\r\n" +
                                    "SET @sql = N'ALTER TABLE [TestSchema].[TestTable1] DROP CONSTRAINT ' + @default;\r\n" +
                                    "EXEC sp_executesql @sql;\r\n\r\n" +
                                    "-- now we can finally drop column\r\n" +
                                    "ALTER TABLE [TestSchema].[TestTable1] DROP COLUMN [TestColumn1];\r\n" +
                                    "GO\r\n" +
                                    "DECLARE @default sysname, @sql nvarchar(max);\r\n\r\n" +
                                    "-- get name of default constraint\r\n" +
                                    "SELECT @default = name\r\n" +
                                    "FROM sys.default_constraints\r\n" +
                                    "WHERE parent_object_id = object_id('[TestSchema].[TestTable1]')\r\n" + "" +
                                    "AND type = 'D'\r\n" + "" +
                                    "AND parent_column_id = (\r\n" + "" +
                                    "SELECT column_id\r\n" +
                                    "FROM sys.columns\r\n" +
                                    "WHERE object_id = object_id('[TestSchema].[TestTable1]')\r\n" +
                                    "AND name = 'TestColumn2'\r\n" +
                                    ");\r\n\r\n" +
                                    "-- create alter table command to drop contraint as string and run it\r\n" +
                                    "SET @sql = N'ALTER TABLE [TestSchema].[TestTable1] DROP CONSTRAINT ' + @default;\r\n" +
                                    "EXEC sp_executesql @sql;\r\n\r\n" +
                                    "-- now we can finally drop column\r\n" +
                                    "ALTER TABLE [TestSchema].[TestTable1] DROP COLUMN [TestColumn2];\r\n";

            sql.ShouldBe(expected);
        }
Exemplo n.º 18
0
 public void WhenOneToOneImplicitConfigurationIsRead_ThenOneToOneEntitiesAreGenerated()
 {
     GeneratorTestHelper.TestGeneratedCode(
         new[]
     {
         // corresponding property in A is set, making it the Depending side of the one-to-one relation between A & B
         "./modelmaker.one-to-one-one-as.json",
         "./modelmaker.one-to-one-one-bs-explicit.json"
     },
         OneToOneOneACode.Entity,
         OneToOneOneACode.Collection,
         OneToOneOneBCode.Entity,
         OneToOneOneBCode.Collection,
         ContextCode.OneToOneContext,
         OneToOneOneACode.EntityTypeConfiguration,
         OneToOneOneACode.Repository,
         OneToOneOneBCode.EntityTypeConfiguration,
         OneToOneOneBCode.Repository);
 }
Exemplo n.º 19
0
 public void WhenBasicBlogConfigurationIsRead_ThenBlogEntitiesAreGenerated()
 {
     GeneratorTestHelper.TestGeneratedCode(
         new[]
     {
         "./modelmaker.category.json",
         "./modelmaker.blog.json"
     },
         CategoryCode.EntityWithBlog,
         CategoryCode.EntityValidator,
         CategoryCode.Collection,
         BlogCode.Entity,
         BlogCode.EntityValidator,
         BlogCode.Collection,
         ContextCode.BlogCategoryContext,
         CategoryCode.EntityConfiguration,
         CategoryCode.Repository,
         BlogCode.EntityConfiguration,
         BlogCode.Repository);
 }
Exemplo n.º 20
0
 public void WhenoneTooneExplicitConfigurationIsRead_ThenoneTooneEntitiesAreGenerated()
 {
     GeneratorTestHelper.TestGeneratedCode(
         new[]
     {
         // corresponding property in A and B are set, making the Depending side not clear for the one-to-one relation.
         // the lowest ranking entity name will be made Depending side
         "./modelmaker.one-to-one-one-as.json",
         "./modelmaker.one-to-one-one-bs-implicit.json"
     },
         OneToOneOneACode.Entity,
         OneToOneOneACode.Collection,
         OneToOneOneBCode.Entity,
         OneToOneOneBCode.Collection,
         ContextCode.OneToOneContext,
         OneToOneOneACode.EntityTypeConfiguration,
         OneToOneOneACode.Repository,
         OneToOneOneBCode.EntityTypeConfiguration,
         OneToOneOneBCode.Repository);
 }
Exemplo n.º 21
0
        public void Property_Mismatch_Throw_Diagnostic_Error()
        {
            #region Produce two mapping class text

            const string sourceText = @"
namespace Sample.Entities
{
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}
";

            const string targetText = @"
using MapperGenerator;
using Sample.Entities;
namespace Sample.Models
{
    [Mapping(typeof(Person))]
    public class PersonViewModel
    {
        public int Sn { get; set; }
        public string Name { get; set; }
    }
}
";

            #endregion

            var originCompilation = GeneratorTestHelper.CreateCompilation(sourceText, targetText);
            var(_, generatorDiagnostics) =
                GeneratorTestHelper.RunGenerators(originCompilation,
                                                  new Generators.MapperGenerator());
            var diagnostic = generatorDiagnostics.FirstOrDefault();


            Assert.AreEqual("MPERR001", diagnostic.Id);
        }
Exemplo n.º 22
0
        public override void CanCreateAutoIncrementColumnWithDefaultSchema()
        {
            var expression = GeneratorTestHelper.GetAlterColumnAddAutoIncrementExpression();

            Assert.Throws <DatabaseOperationNotSupportedException>(() => Generator.Generate(expression));
        }
Exemplo n.º 23
0
 public override void CanCreateTableWithIdentityWithDefaultSchema()
 {
     Assert.Throws <DatabaseOperationNotSupportedException>(() => Generator.Generate(GeneratorTestHelper.GetCreateTableWithAutoIncrementExpression()));
 }
Exemplo n.º 24
0
        public void AlterDefaultConstraintThrowsNotSupportedException()
        {
            var expression = GeneratorTestHelper.GetAlterDefaultConstraintExpression();

            Assert.Throws <DatabaseOperationNotSupportedException>(() => Generator.Generate(expression));
        }
        public void AlterDefaultConstraintThrowsNotSupportedException()
        {
            var expression = GeneratorTestHelper.GetAlterDefaultConstraintExpression();

            generator.Generate(expression);
        }
        public void RenameColumnThrowsException()
        {
            var expression = GeneratorTestHelper.GetRenameColumnExpression();

            generator.Generate(expression);
        }
Exemplo n.º 27
0
        public void General_Property_Mapping()
        {
            #region Produce two mapping class text

            const string sourceText = @"
namespace Sample.Entities
{
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}
";

            const string targetText = @"
using MapperGenerator;
using Sample.Entities;
namespace Sample.Models
{
    [Mapping(typeof(Person))]
    public class PersonViewModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}
";

            #endregion

            var originCompilation = GeneratorTestHelper.CreateCompilation(sourceText, targetText);
            var(resultCompilation, generatorDiagnostics) =
                GeneratorTestHelper.RunGenerators(originCompilation, new Generators.MapperGenerator());

            // verify no errors or warnings are returned
            Assert.IsEmpty(generatorDiagnostics);
            Assert.IsEmpty(resultCompilation.GetDiagnostics());

            // compile and get an assembly along with our methods.
            var assembly             = GeneratorTestHelper.GetAssemblyFromCompilation(resultCompilation);
            var mapperType           = assembly.GetType("MapperGenerator.Mapper");
            var generalMappingMethod =
                mapperType?.GetMethod("MapToPersonViewModel");                       // this one is added via the generator
            var extensionMappingMethod = mapperType?.GetMethod("ToPersonViewModel"); // this is in our source

            Assert.NotNull(generalMappingMethod);
            Assert.NotNull(extensionMappingMethod);

            //var mapper = Activator.CreateInstance(mapperType);
            var personType = assembly.GetType("Sample.Entities.Person");
            var person     = Activator.CreateInstance(personType);
            personType.GetProperty("Id")?.SetValue(person, 1);
            personType.GetProperty("Name")?.SetValue(person, "Roberson");


            var result = generalMappingMethod.Invoke(null, new[] { person });

            var expectedType = assembly.GetType("Sample.Models.PersonViewModel");
            Assert.AreEqual(expectedType, result.GetType());

            var actualId   = expectedType.GetProperty("Id").GetValue(result);
            var actualName = expectedType.GetProperty("Name").GetValue(result);

            Assert.AreEqual(1, actualId);
            Assert.AreEqual("Roberson", actualName);
        }
Exemplo n.º 28
0
        public void CanRenameColumnInStrictMode()
        {
            Generator.compatabilityMode = CompatabilityMode.STRICT;

            Assert.Throws <DatabaseOperationNotSupportedException>(() => Generator.Generate(GeneratorTestHelper.GetRenameColumnExpression()));
        }
Exemplo n.º 29
0
        public void CantCreateTableWithDescription()
        {
            var expression = GeneratorTestHelper.GetCreateTableWithTableDescription();

            Assert.Throws <ArgumentException>(() => Generator.Generate(expression));
        }
Exemplo n.º 30
0
        public void CanCreateMulitColumnForeignKeyInStrictMode()
        {
            Generator.compatabilityMode = CompatabilityMode.STRICT;

            Assert.Throws <DatabaseOperationNotSupportedException>(() => Generator.Generate(GeneratorTestHelper.GetCreateNamedMultiColumnForeignKeyExpression()));
        }