public void One_to_many() { using (var testStore = SqliteTestStore.GetOrCreateShared("OneToMany" + DbSuffix)) { 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 ) ); "); 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); Assert.Empty(_reporter.Messages.Where(m => m.StartsWith("warn: "))); var expectedFileSet = new FileSet(new FileSystemFileService(), Path.Combine(ExpectedResultsParentDir, "OneToMany")) { Files = { "OneToMany" + DbSuffix + "Context.cs", "OneToManyDependent.cs", "OneToManyPrincipal.cs" } }; var actualFileSet = new FileSet(InMemoryFiles, TestProjectFullPath) { 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, TableSelectionSet = TableSelectionSet.All }); 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); } }
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 void Many_to_many() { using (var testStore = SqliteTestStore.GetOrCreateShared("ManyToMany" + DbSuffix)) { testStore.ExecuteNonQuery(@" CREATE TABLE IF NOT EXISTS Users ( Id PRIMARY KEY); CREATE TABLE IF NOT EXISTS Groups (Id PRIMARY KEY); CREATE TABLE IF NOT EXISTS Users_Groups ( Id PRIMARY KEY, UserId, GroupId, UNIQUE (UserId, GroupId), FOREIGN KEY (UserId) REFERENCES Users (Id), FOREIGN KEY (GroupId) REFERENCES Groups (Id) ); "); 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); Assert.Empty(_reporter.Messages.Where(m => m.StartsWith("warn: "))); var expectedFileSet = new FileSet(new FileSystemFileService(), Path.Combine(ExpectedResultsParentDir, "ManyToMany")) { Files = { "ManyToMany" + DbSuffix + "Context.cs", "Groups.cs", "Users.cs", "UsersGroups.cs" } }; var actualFileSet = new FileSet(InMemoryFiles, TestProjectFullPath) { 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 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 virtual void Foreign_key_to_unique_index() { using (var testStore = SqliteTestStore.GetOrCreateShared("FkToAltKey" + DbSuffix)) { testStore.ExecuteNonQuery(@" CREATE TABLE IF NOT EXISTS User ( Id INTEGER PRIMARY KEY, AltId INTEGER NOT NULL UNIQUE ); CREATE TABLE IF NOT EXISTS Comment ( Id INTEGER PRIMARY KEY, UserAltId INTEGER NOT NULL, Contents TEXT, FOREIGN KEY (UserAltId) REFERENCES User (AltId) );"); var results = Generator.Generate( testStore.ConnectionString, Enumerable.Empty <string>(), Enumerable.Empty <string>(), TestProjectPath, outputPath: null, rootNamespace: "E2E.Sqlite", contextName: "FkToAltKeyContext", useDataAnnotations: UseDataAnnotations, overwriteFiles: false, useDatabaseNames: false); Assert.Empty(_reporter.Messages.Where(m => m.StartsWith("warn: "))); var expectedFileSet = new FileSet(new FileSystemFileService(), Path.Combine(ExpectedResultsParentDir, "FkToAltKey")) { Files = { "FkToAltKeyContext.cs", "Comment.cs", "User.cs" } }; var actualFileSet = new FileSet(InMemoryFiles, TestProjectFullPath) { 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("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 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 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 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.ConnectionString, ProjectPath = TestProjectPath, 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, TestProjectFullPath) { Files = Enumerable.Repeat(results.ContextFile, 1).Concat(results.EntityTypeFiles).Select(Path.GetFileName).ToList() }; AssertEqualFileContents(expectedFileSet, actualFileSet); AssertCompile(actualFileSet); } }
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 void Principal_missing_primary_key() { using (var testStore = SqliteTestStore.GetOrCreateShared("NoPrincipalPk" + DbSuffix)) { testStore.ExecuteNonQuery(@"CREATE TABLE IF NOT EXISTS Dependent ( Id PRIMARY KEY, PrincipalId INT, FOREIGN KEY (PrincipalId) REFERENCES Principal(Id) ); CREATE TABLE IF NOT EXISTS Principal ( Id INT);"); 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); Assert.Contains("warn: " + DesignStrings.MissingPrimaryKey("Principal"), _reporter.Messages); Assert.Contains("warn: " + DesignStrings.UnableToGenerateEntityType("Principal"), _reporter.Messages); Assert.Contains("warn: " + DesignStrings.ForeignKeyScaffoldErrorPrincipalTableScaffoldingError("Dependent(PrincipalId)", "Principal"), _reporter.Messages); var expectedFileSet = new FileSet(new FileSystemFileService(), Path.Combine(ExpectedResultsParentDir, "NoPrincipalPk")) { Files = { "NoPrincipalPk" + DbSuffix + "Context.cs", "Dependent.cs" } }; var actualFileSet = new FileSet(InMemoryFiles, TestProjectFullPath) { 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 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 static SqliteTestStore GetSharedStore() => SqliteTestStore.GetOrCreateShared("northwind", () => { });