public async Task Missing_primary_key() { using (var testStore = SqliteTestStore.CreateScratch()) { testStore.ExecuteNonQuery("CREATE TABLE Alicia ( Keys TEXT );"); var results = await Generator.GenerateAsync(new ReverseEngineeringConfiguration { ConnectionString = testStore.ConnectionString, ProjectPath = TestProjectPath, ProjectRootNamespace = "E2E.Sqlite", UseFluentApiOnly = UseFluentApiOnly, TableSelectionSet = TableSelectionSet.All }); var errorMessage = RelationalDesignStrings.UnableToGenerateEntityType("Alicia"); var expectedLog = new LoggerMessages { Warn = { RelationalDesignStrings.MissingPrimaryKey("Alicia"), errorMessage } }; AssertLog(expectedLog); Assert.Contains(errorMessage, InMemoryFiles.RetrieveFileContents(TestProjectFullPath, Path.GetFileName(results.ContextFile))); } }
public async void Missing_primary_key() { using (var testStore = SqliteTestStore.CreateScratch()) { testStore.ExecuteNonQuery("CREATE TABLE Alicia ( Keys TEXT );"); var results = await Generator.GenerateAsync(new ReverseEngineeringConfiguration { ConnectionString = testStore.Connection.ConnectionString, ProjectPath = "testout", ProjectRootNamespace = "E2E.Sqlite", UseFluentApiOnly = UseFluentApiOnly }); var errorMessage = SqliteDesignStrings.MissingPrimaryKey("Alicia"); var expectedLog = new LoggerMessages { Warn = { errorMessage } }; AssertLog(expectedLog); Assert.Contains(errorMessage, InMemoryFiles.RetrieveFileContents("testout", "Alicia.cs")); } }
public async void It_uses_templates() { var dbContextFileName = "EntityFramework.Sqlite.Design." + ReverseEngineeringGenerator.DbContextTemplateFileName; var entityTypeFileName = "EntityFramework.Sqlite.Design." + ReverseEngineeringGenerator.EntityTypeTemplateFileName; var entityTemplate = "This is an entity type template! (For real)"; var contextTemplate = "Also a 100% legit template"; var outputDir = "gen"; var templatesDir = "templates"; using (var testStore = SqliteTestStore.CreateScratch()) { testStore.ExecuteNonQuery("CREATE TABLE RealMccoy ( Col1 text PRIMARY KEY); "); InMemoryFiles.OutputFile(templatesDir, dbContextFileName, contextTemplate); InMemoryFiles.OutputFile(templatesDir, entityTypeFileName, entityTemplate); var config = new ReverseEngineeringConfiguration { ConnectionString = testStore.Connection.ConnectionString, Provider = MetadataModelProvider, Namespace = "Test", OutputPath = outputDir, CustomTemplatePath = templatesDir }; var filePaths = await Generator.GenerateAsync(config); var expectedLog = new LoggerMessages { Info = { "Using custom template " + Path.Combine(templatesDir, dbContextFileName), "Using custom template " + Path.Combine(templatesDir, entityTypeFileName) } }; AssertLog(expectedLog); Assert.Equal(2, filePaths.Count); foreach (var fileName in filePaths.Select(Path.GetFileName)) { var fileContents = InMemoryFiles.RetrieveFileContents(outputDir, fileName); var contents = fileName.EndsWith("Context.cs") ? contextTemplate : entityTemplate; Assert.Equal(contents, fileContents); } } }
public void Code_generation_will_use_customized_templates_if_present() { var configuration = new ReverseEngineeringConfiguration { Provider = MetadataModelProvider, ConnectionString = _connectionString, Namespace = TestNamespace, CustomTemplatePath = CustomizedTemplateDir, OutputPath = TestOutputDir }; InMemoryFiles.OutputFile(CustomizedTemplateDir, ProviderDbContextTemplateName, "DbContext template"); InMemoryFiles.OutputFile(CustomizedTemplateDir, ProviderEntityTypeTemplateName, "EntityType template"); var filePaths = Generator.GenerateAsync(configuration).GetAwaiter().GetResult(); AssertLog(new LoggerMessages { Warn = { @"For column [dbo][AllDataTypes][hierarchyidColumn]. Could not find type mapping for SQL Server type hierarchyid. Skipping column.", @"For column [dbo][AllDataTypes][sql_variantColumn]. Could not find type mapping for SQL Server type sql_variant. Skipping column.", @"For column [dbo][AllDataTypes][xmlColumn]. Could not find type mapping for SQL Server type xml. Skipping column.", @"For column [dbo][AllDataTypes][geographyColumn]. Could not find type mapping for SQL Server type geography. Skipping column.", @"For column [dbo][AllDataTypes][geometryColumn]. Could not find type mapping for SQL Server type geometry. Skipping column.", @"For column [dbo][PropertyConfiguration][PropertyConfigurationID]. This column is set up as an Identity column, but the SQL Server data type is tinyint. This will be mapped to CLR type byte which does not allow the SqlServerIdentityStrategy.IdentityColumn setting. Generating a matching Property but ignoring the Identity setting.", @"For column [dbo][TableWithUnmappablePrimaryKeyColumn][TableWithUnmappablePrimaryKeyColumnID]. Could not find type mapping for SQL Server type hierarchyid. Skipping column.", @"Unable to identify any primary key columns in the underlying SQL Server table [dbo].[TableWithUnmappablePrimaryKeyColumn]." }, Info = { "Using custom template " + Path.Combine(CustomizedTemplateDir, ProviderDbContextTemplateName), "Using custom template " + Path.Combine(CustomizedTemplateDir, ProviderEntityTypeTemplateName) } }); foreach (var fileName in filePaths.Select(Path.GetFileName)) { var fileContents = InMemoryFiles.RetrieveFileContents(TestOutputDir, fileName); var contents = "SqlServerReverseEngineerTestE2EContext.cs" == fileName ? "DbContext template" : "EntityType template"; Assert.Contains(fileName.Replace(".cs", ".expected"), _expectedFiles); Assert.Equal(contents, fileContents); } }
public virtual void Can_output_templates_to_be_customized() { var filePaths = Generator.Customize(MetadataModelProvider, TestOutputDir); AssertLog(new LoggerMessages()); Assert.Collection(filePaths, file1 => Assert.Equal(file1, Path.Combine(TestOutputDir, ProviderDbContextTemplateName)), file2 => Assert.Equal(file2, Path.Combine(TestOutputDir, ProviderEntityTypeTemplateName))); var dbContextTemplateContents = InMemoryFiles.RetrieveFileContents( TestOutputDir, ProviderDbContextTemplateName); Assert.Equal(MetadataModelProvider.DbContextTemplate, dbContextTemplateContents); var entityTypeTemplateContents = InMemoryFiles.RetrieveFileContents( TestOutputDir, ProviderEntityTypeTemplateName); Assert.Equal(MetadataModelProvider.EntityTypeTemplate, entityTypeTemplateContents); }
public void Missing_primary_key() { using (var testStore = SqliteTestStore.CreateScratch()) { testStore.ExecuteNonQuery("CREATE TABLE Alicia ( Keys TEXT );"); var results = Generator.Generate( testStore.ConnectionString, Enumerable.Empty <string>(), Enumerable.Empty <string>(), TestProjectPath, outputPath: null, rootNamespace: "E2E.Sqlite", contextName: null, useDataAnnotations: UseDataAnnotations, overwriteFiles: false, useDatabaseNames: false); var errorMessage = DesignStrings.UnableToGenerateEntityType("Alicia"); Assert.Contains("warn: " + DesignStrings.MissingPrimaryKey("Alicia"), _reporter.Messages); Assert.Contains("warn: " + errorMessage, _reporter.Messages); Assert.Contains(errorMessage, InMemoryFiles.RetrieveFileContents(TestProjectFullPath, Path.GetFileName(results.ContextFile))); } }
public void It_outputs_templates() { var dbContextFileName = "EntityFramework.Sqlite.Design." + ReverseEngineeringGenerator.DbContextTemplateFileName; var entityTypeFileName = "EntityFramework.Sqlite.Design." + ReverseEngineeringGenerator.EntityTypeTemplateFileName; var outputDir = "templates/"; var filePaths = Generator.Customize(MetadataModelProvider, outputDir); AssertLog(new LoggerMessages()); Assert.Collection(filePaths, file1 => Assert.Equal(file1, Path.Combine(outputDir, dbContextFileName)), file2 => Assert.Equal(file2, Path.Combine(outputDir, entityTypeFileName))); var dbContextTemplateContents = InMemoryFiles.RetrieveFileContents( outputDir, dbContextFileName); Assert.Equal(MetadataModelProvider.DbContextTemplate, dbContextTemplateContents); var entityTypeTemplateContents = InMemoryFiles.RetrieveFileContents( outputDir, entityTypeFileName); Assert.Equal(MetadataModelProvider.EntityTypeTemplate, entityTypeTemplateContents); }