protected virtual void AssertEqualFileContents(FileSet expected, FileSet actual) { Assert.Equal(expected.Files.Count, actual.Files.Count); for (var i = 0; i < expected.Files.Count; i++) { Assert.True(actual.Exists(i), $"Could not find file '{actual.Files[i]}' in directory '{actual.Directory}'"); var expectedContents = expected.Contents(i); var actualContents = actual.Contents(i); try { Assert.Equal(expectedContents, actualContents); } catch (EqualException e) { var sep = new string('=', 60); _output.WriteLine($"Contents of actual: '{actual.Files[i]}'"); _output.WriteLine(sep); _output.WriteLine(actualContents); _output.WriteLine(sep); throw new XunitException($"Files did not match: '{expected.Files[i]}' and '{actual.Files[i]}'" + Environment.NewLine + $"{e.Message}"); } } }
public async void One_to_one() { using (var testStore = SqliteTestStore.GetOrCreateShared("OneToOne" + DbSuffix).AsTransient()) { testStore.ExecuteNonQuery(@" CREATE TABLE IF NOT EXISTS Principal ( Id INTEGER PRIMARY KEY AUTOINCREMENT ); CREATE TABLE IF NOT EXISTS Dependent ( Id INT, PrincipalId INT NOT NULL UNIQUE, PRIMARY KEY (Id), FOREIGN KEY (PrincipalId) REFERENCES Principal (Id) ); "); testStore.Transaction.Commit(); var results = await Generator.GenerateAsync(new ReverseEngineeringConfiguration { ConnectionString = testStore.Connection.ConnectionString, ProjectPath = "testout", ProjectRootNamespace = "E2E.Sqlite", UseFluentApiOnly = UseFluentApiOnly, TableSelectionSet = TableSelectionSet.All }); AssertLog(new LoggerMessages()); var expectedFileSet = new FileSet(new FileSystemFileService(), Path.Combine(ExpectedResultsParentDir, "OneToOne")) { Files = { "OneToOne" + DbSuffix + "Context.expected", "Dependent.expected", "Principal.expected" } }; var actualFileSet = new FileSet(InMemoryFiles, "testout") { Files = Enumerable.Repeat(results.ContextFile, 1).Concat(results.EntityTypeFiles).Select(Path.GetFileName).ToList() }; AssertEqualFileContents(expectedFileSet, actualFileSet); AssertCompile(actualFileSet); } }
protected virtual void AssertCompile(FileSet fileSet) { var fileContents = fileSet.Files.Select(fileSet.Contents).ToList(); var compilationResult = GetCompiler().Compile(fileContents); if (compilationResult.Messages.Any()) { _output.WriteLine("Compilation Errors from compiling generated code"); _output.WriteLine("================================================"); foreach (var message in compilationResult.Messages) { _output.WriteLine(message); } _output.WriteLine("================================================"); Assert.True(false, "Failed to compile: see Compilation Errors in Output."); } }
public async void One_to_one() { using (var testStore = SqliteTestStore.GetOrCreateShared("OneToOne").AsTransient()) { testStore.ExecuteNonQuery(@" CREATE TABLE IF NOT EXISTS Principal ( Id INTEGER PRIMARY KEY AUTOINCREMENT ); CREATE TABLE IF NOT EXISTS Dependent ( Id INT, PrincipalId INT NOT NULL UNIQUE, PRIMARY KEY (Id), FOREIGN KEY (PrincipalId) REFERENCES Principal (Id) ); "); testStore.Transaction.Commit(); var results = await Generator.GenerateAsync(new ReverseEngineeringConfiguration { Provider = MetadataModelProvider, ConnectionString = testStore.Connection.ConnectionString, Namespace = "E2E.Sqlite", OutputPath = "testout" }); AssertLog(new LoggerMessages()); var expectedFileSet = new FileSet(new FileSystemFileService(), "ReverseEngineering/Expected/OneToOne") { Files = { "ModelContext.expected", "Dependent.expected", "Principal.expected" } }; var actualFileSet = new FileSet(InMemoryFiles, "testout") { Files = results.Select(Path.GetFileName).ToList() }; AssertEqualFileContents(expectedFileSet, actualFileSet); AssertCompile(actualFileSet); } }
public async void One_to_many() { using (var testStore = SqliteTestStore.GetOrCreateShared("OneToMany").AsTransient()) { testStore.ExecuteNonQuery(@" CREATE TABLE IF NOT EXISTS OneToManyPrincipal ( OneToManyPrincipalID1 INT, OneToManyPrincipalID2 INT, Other TEXT NOT NULL, PRIMARY KEY (OneToManyPrincipalID1, OneToManyPrincipalID2) ); CREATE TABLE IF NOT EXISTS OneToManyDependent ( OneToManyDependentID1 INT NOT NULL, OneToManyDependentID2 INT NOT NULL, SomeDependentEndColumn VARCHAR NOT NULL, OneToManyDependentFK1 INT, OneToManyDependentFK2 INT, PRIMARY KEY (OneToManyDependentID1, OneToManyDependentID2), FOREIGN KEY ( OneToManyDependentFK1, OneToManyDependentFK2) REFERENCES OneToManyPrincipal ( OneToManyPrincipalID1, OneToManyPrincipalID2 ) ); "); testStore.Transaction.Commit(); var results = await Generator.GenerateAsync(new ReverseEngineeringConfiguration { Provider = MetadataModelProvider, ConnectionString = testStore.Connection.ConnectionString, Namespace = "E2E.Sqlite", OutputPath = "testout" }); AssertLog(new LoggerMessages()); var expectedFileSet = new FileSet(new FileSystemFileService(), "ReverseEngineering/Expected/OneToMany") { Files = { "ModelContext.expected", "OneToManyDependent.expected", "OneToManyPrincipal.expected" } }; var actualFileSet = new FileSet(InMemoryFiles, "testout") { Files = results.Select(Path.GetFileName).ToList() }; AssertEqualFileContents(expectedFileSet, actualFileSet); AssertCompile(actualFileSet); } }
public async void Principal_missing_primary_key() { using (var testStore = SqliteTestStore.GetOrCreateShared("NoPrincipal").AsTransient()) { testStore.ExecuteNonQuery(@"CREATE TABLE Dependent ( Id PRIMARY KEY, PrincipalId INT, FOREIGN KEY (PrincipalId) REFERENCES Principal(Id) ); CREATE TABLE Principal ( Id INT);"); testStore.Transaction.Commit(); var results = await Generator.GenerateAsync(new ReverseEngineeringConfiguration { Provider = MetadataModelProvider, ConnectionString = testStore.Connection.ConnectionString, Namespace = "E2E.Sqlite", OutputPath = "testout" }); var expectedLog = new LoggerMessages { Warn = { Strings.MissingPrimaryKey("Principal"), Strings.ForeignKeyScaffoldError("Dependent","PrincipalId"), } }; AssertLog(expectedLog); var expectedFileSet = new FileSet(new FileSystemFileService(), "ReverseEngineering/Expected/NoPrincipalPk") { Files = { "ModelContext.expected", "Dependent.expected", "Principal.expected", } }; var actualFileSet = new FileSet(InMemoryFiles, "testout") { Files = results.Select(Path.GetFileName).ToList() }; AssertEqualFileContents(expectedFileSet, actualFileSet); AssertCompile(actualFileSet); } }
public async void It_handles_unsafe_names() { using (var testStore = SqliteTestStore.CreateScratch()) { testStore.ExecuteNonQuery(@" CREATE TABLE 'Named with space' ( Id PRIMARY KEY ); CREATE TABLE '123 Invalid Class Name' ( Id PRIMARY KEY); CREATE TABLE 'Bad characters `~!@#$%^&*()+=-[];''"",.<>/?|\ ' ( Id PRIMARY KEY); CREATE TABLE ' Bad columns ' ( 'Space jam' PRIMARY KEY, '123 Go`', 'Bad to the bone. `~!@#$%^&*()+=-[];''"",.<>/?|\ ', 'Next one is all bad', '@#$%^&*()' ); CREATE TABLE Keywords ( namespace PRIMARY KEY, virtual, public, class, string, FOREIGN KEY (class) REFERENCES string (string) ); CREATE TABLE String ( string PRIMARY KEY, FOREIGN KEY (string) REFERENCES String (string) ); "); var results = await Generator.GenerateAsync(new ReverseEngineeringConfiguration { Provider = MetadataModelProvider, ConnectionString = testStore.Connection.ConnectionString, Namespace = "E2E.Sqlite", OutputPath = "testout" }); AssertLog(new LoggerMessages()); var files = new FileSet(InMemoryFiles, "testout") { Files = results.Select(Path.GetFileName).ToList() }; AssertCompile(files); } }
public async void Self_referencing() { using (var testStore = SqliteTestStore.GetOrCreateShared("SelfRef").AsTransient()) { testStore.ExecuteNonQuery(@"CREATE TABLE SelfRef ( Id INTEGER PRIMARY KEY, SelfForeignKey INTEGER, FOREIGN KEY (SelfForeignKey) REFERENCES SelfRef (Id) );"); testStore.Transaction.Commit(); var results = await Generator.GenerateAsync(new ReverseEngineeringConfiguration { Provider = MetadataModelProvider, ConnectionString = testStore.Connection.ConnectionString, Namespace = "E2E.Sqlite", OutputPath = "testout" }); AssertLog(new LoggerMessages()); var expectedFileSet = new FileSet(new FileSystemFileService(), "ReverseEngineering/Expected/SelfRef") { Files = { "ModelContext.expected", "SelfRef.expected" } }; var actualFileSet = new FileSet(InMemoryFiles, "testout") { Files = results.Select(Path.GetFileName).ToList() }; AssertEqualFileContents(expectedFileSet, actualFileSet); AssertCompile(actualFileSet); } }
public async void Many_to_many() { using (var testStore = SqliteTestStore.GetOrCreateShared("ManyToMany").AsTransient()) { testStore.ExecuteNonQuery(@" CREATE TABLE Users ( Id PRIMARY KEY); CREATE TABLE Groups (Id PRIMARY KEY); CREATE TABLE Users_Groups ( Id PRIMARY KEY, UserId, GroupId, UNIQUE (UserId, GroupId), FOREIGN KEY (UserId) REFERENCES Users (Id), FOREIGN KEY (GroupId) REFERENCES Groups (Id) ); "); testStore.Transaction.Commit(); var results = await Generator.GenerateAsync(new ReverseEngineeringConfiguration { Provider = MetadataModelProvider, ConnectionString = testStore.Connection.ConnectionString, Namespace = "E2E.Sqlite", OutputPath = "testout" }); AssertLog(new LoggerMessages()); var expectedFileSet = new FileSet(new FileSystemFileService(), "ReverseEngineering/Expected/ManyToMany") { Files = { "ModelContext.expected", "Groups.expected", "Users.expected", "Users_Groups.expected" } }; var actualFileSet = new FileSet(InMemoryFiles, "testout") { Files = results.Select(Path.GetFileName).ToList() }; AssertEqualFileContents(expectedFileSet, actualFileSet); AssertCompile(actualFileSet); } }
public virtual async void Foreign_key_to_unique_index() { using (var testStore = SqliteTestStore.GetOrCreateShared("FkToAltKey" + DbSuffix).AsTransient()) { testStore.ExecuteNonQuery(@" CREATE TABLE User ( Id INTEGER PRIMARY KEY, AltId INTEGER NOT NULL UNIQUE ); CREATE TABLE Comment ( Id INTEGER PRIMARY KEY, UserAltId INTEGER NOT NULL, Contents TEXT, FOREIGN KEY (UserAltId) REFERENCES User (AltId) );"); testStore.Transaction.Commit(); var results = await Generator.GenerateAsync(new ReverseEngineeringConfiguration { ConnectionString = testStore.Connection.ConnectionString, ContextClassName = "FkToAltKeyContext", ProjectPath = "testout", ProjectRootNamespace = "E2E.Sqlite", UseFluentApiOnly = UseFluentApiOnly, TableSelectionSet = TableSelectionSet.All }); AssertLog(new LoggerMessages()); var expectedFileSet = new FileSet(new FileSystemFileService(), Path.Combine(ExpectedResultsParentDir, "FkToAltKey")) { Files = { "FkToAltKeyContext.expected", "Comment.expected", "User.expected" } }; var actualFileSet = new FileSet(InMemoryFiles, "testout") { Files = Enumerable.Repeat(results.ContextFile, 1).Concat(results.EntityTypeFiles).Select(Path.GetFileName).ToList() }; AssertEqualFileContents(expectedFileSet, actualFileSet); AssertCompile(actualFileSet); } }
public async void Principal_missing_primary_key() { using (var testStore = SqliteTestStore.GetOrCreateShared("NoPrincipalPk" + DbSuffix).AsTransient()) { testStore.ExecuteNonQuery(@"CREATE TABLE Dependent ( Id PRIMARY KEY, PrincipalId INT, FOREIGN KEY (PrincipalId) REFERENCES Principal(Id) ); CREATE TABLE Principal ( Id INT);"); testStore.Transaction.Commit(); var results = await Generator.GenerateAsync(new ReverseEngineeringConfiguration { ConnectionString = testStore.Connection.ConnectionString, ProjectPath = "testout", ProjectRootNamespace = "E2E.Sqlite", UseFluentApiOnly = UseFluentApiOnly, TableSelectionSet = TableSelectionSet.All }); var expectedLog = new LoggerMessages { Warn = { RelationalDesignStrings.MissingPrimaryKey("Principal"), RelationalDesignStrings.UnableToGenerateEntityType("Principal"), RelationalDesignStrings.ForeignKeyScaffoldErrorPrincipalTableScaffoldingError("Dependent(PrincipalId)", "Principal") } }; AssertLog(expectedLog); var expectedFileSet = new FileSet(new FileSystemFileService(), Path.Combine(ExpectedResultsParentDir, "NoPrincipalPk")) { Files = { "NoPrincipalPk" + DbSuffix + "Context.expected", "Dependent.expected", } }; var actualFileSet = new FileSet(InMemoryFiles, "testout") { Files = Enumerable.Repeat(results.ContextFile, 1).Concat(results.EntityTypeFiles).Select(Path.GetFileName).ToList() }; AssertEqualFileContents(expectedFileSet, actualFileSet); AssertCompile(actualFileSet); } }
public async void Self_referencing() { using (var testStore = SqliteTestStore.GetOrCreateShared("SelfRef" + DbSuffix).AsTransient()) { testStore.ExecuteNonQuery(@"CREATE TABLE SelfRef ( Id INTEGER PRIMARY KEY, SelfForeignKey INTEGER, FOREIGN KEY (SelfForeignKey) REFERENCES SelfRef (Id) );"); testStore.Transaction.Commit(); var results = await Generator.GenerateAsync(new ReverseEngineeringConfiguration { ConnectionString = testStore.Connection.ConnectionString, ProjectPath = "testout", ProjectRootNamespace = "E2E.Sqlite", UseFluentApiOnly = UseFluentApiOnly, TableSelectionSet = TableSelectionSet.All }); AssertLog(new LoggerMessages()); var expectedFileSet = new FileSet(new FileSystemFileService(), Path.Combine(ExpectedResultsParentDir, "SelfRef")) { Files = { "SelfRef" + DbSuffix + "Context.expected", "SelfRef.expected" } }; var actualFileSet = new FileSet(InMemoryFiles, "testout") { Files = Enumerable.Repeat(results.ContextFile, 1).Concat(results.EntityTypeFiles).Select(Path.GetFileName).ToList() }; AssertEqualFileContents(expectedFileSet, actualFileSet); AssertCompile(actualFileSet); } }
public async void Many_to_many() { using (var testStore = SqliteTestStore.GetOrCreateShared("ManyToMany" + DbSuffix).AsTransient()) { testStore.ExecuteNonQuery(@" CREATE TABLE Users ( Id PRIMARY KEY); CREATE TABLE Groups (Id PRIMARY KEY); CREATE TABLE Users_Groups ( Id PRIMARY KEY, UserId, GroupId, UNIQUE (UserId, GroupId), FOREIGN KEY (UserId) REFERENCES Users (Id), FOREIGN KEY (GroupId) REFERENCES Groups (Id) ); "); testStore.Transaction.Commit(); var results = await Generator.GenerateAsync(new ReverseEngineeringConfiguration { ConnectionString = testStore.Connection.ConnectionString, ProjectPath = "testout", ProjectRootNamespace = "E2E.Sqlite", UseFluentApiOnly = UseFluentApiOnly, TableSelectionSet = TableSelectionSet.All }); AssertLog(new LoggerMessages()); var expectedFileSet = new FileSet(new FileSystemFileService(), Path.Combine(ExpectedResultsParentDir, "ManyToMany")) { Files = { "ManyToMany" + DbSuffix + "Context.expected", "Groups.expected", "Users.expected", "Users_Groups.expected" } }; var actualFileSet = new FileSet(InMemoryFiles, "testout") { Files = Enumerable.Repeat(results.ContextFile, 1).Concat(results.EntityTypeFiles).Select(Path.GetFileName).ToList() }; AssertEqualFileContents(expectedFileSet, actualFileSet); AssertCompile(actualFileSet); } }
public async void One_to_many() { using (var testStore = SqliteTestStore.GetOrCreateShared("OneToMany" + DbSuffix).AsTransient()) { testStore.ExecuteNonQuery(@" CREATE TABLE IF NOT EXISTS OneToManyPrincipal ( OneToManyPrincipalID1 INT, OneToManyPrincipalID2 INT, Other TEXT NOT NULL, PRIMARY KEY (OneToManyPrincipalID1, OneToManyPrincipalID2) ); CREATE TABLE IF NOT EXISTS OneToManyDependent ( OneToManyDependentID1 INT NOT NULL, OneToManyDependentID2 INT NOT NULL, SomeDependentEndColumn VARCHAR NOT NULL, OneToManyDependentFK1 INT, OneToManyDependentFK2 INT, PRIMARY KEY (OneToManyDependentID1, OneToManyDependentID2), FOREIGN KEY ( OneToManyDependentFK1, OneToManyDependentFK2) REFERENCES OneToManyPrincipal ( OneToManyPrincipalID1, OneToManyPrincipalID2 ) ); "); testStore.Transaction.Commit(); var results = await Generator.GenerateAsync(new ReverseEngineeringConfiguration { ConnectionString = testStore.Connection.ConnectionString, ProjectPath = "testout", ProjectRootNamespace = "E2E.Sqlite", UseFluentApiOnly = UseFluentApiOnly }); AssertLog(new LoggerMessages()); var expectedFileSet = new FileSet(new FileSystemFileService(), Path.Combine(ExpectedResultsParentDir, "OneToMany")) { Files = { "OneToMany" + DbSuffix + "Context.expected", "OneToManyDependent.expected", "OneToManyPrincipal.expected" } }; var actualFileSet = new FileSet(InMemoryFiles, "testout") { Files = Enumerable.Repeat(results.ContextFile, 1).Concat(results.EntityTypeFiles).Select(Path.GetFileName).ToList() }; AssertEqualFileContents(expectedFileSet, actualFileSet); AssertCompile(actualFileSet); } }