-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Rewrote a bit to make sure files are sorted on folder name if subfolders are used in up, sprocs, etc folders
- Loading branch information
Showing
16 changed files
with
318 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.IO; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using grate.Configuration; | ||
using grate.Migration; | ||
using grate.unittests.TestInfrastructure; | ||
using NUnit.Framework; | ||
|
||
namespace grate.unittests.Basic.Infrastructure; | ||
|
||
[TestFixture] | ||
[Category("Basic")] | ||
public class FileSystem_ | ||
{ | ||
[Test] | ||
public void Sorts_enumerated_files_on_filename_when_no_subfolders() | ||
{ | ||
var knownFolders = KnownFolders.In(TestConfig.CreateRandomTempDirectory()); | ||
|
||
var path = knownFolders.Up!.Path; | ||
|
||
var folder1 = new DirectoryInfo(path.ToString()); | ||
|
||
string filename1 = "01_any_filename.sql"; | ||
string filename2 = "02_any_filename.sql"; | ||
|
||
TestConfig.WriteContent(folder1, filename1, "Whatever"); | ||
TestConfig.WriteContent(folder1, filename2, "Whatever"); | ||
|
||
var files = FileSystem.GetFiles(path, "*.sql").ToList(); | ||
|
||
files.First().FullName.Should().Be(Path.Combine(folder1.ToString(), filename1)); | ||
files.Last().FullName.Should().Be(Path.Combine(folder1.ToString(), filename2)); | ||
} | ||
|
||
[Test] | ||
public void Sorts_enumerated_files_on_sub_path_when_subfolders_are_used() | ||
{ | ||
var knownFolders = KnownFolders.In(TestConfig.CreateRandomTempDirectory()); | ||
|
||
var path = knownFolders.Up!.Path; | ||
|
||
var folder1 = new DirectoryInfo(Path.Combine(path.ToString(), "01_sub", "folder", "long", "way")); | ||
var folder2 = new DirectoryInfo(Path.Combine(path.ToString(), "02_sub", "folder", "long", "way")); | ||
|
||
string filename1 = "01_any_filename.sql"; | ||
string filename2 = "02_any_filename.sql"; | ||
|
||
TestConfig.WriteContent(folder1, filename2, "Whatever"); | ||
TestConfig.WriteContent(folder2, filename1, "Whatever"); | ||
|
||
var files = FileSystem.GetFiles(path, "*.sql").ToList(); | ||
|
||
files.First().FullName.Should().Be(Path.Combine(folder1.ToString(), filename2)); | ||
files.Last().FullName.Should().Be(Path.Combine(folder2.ToString(), filename1)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 13 additions & 37 deletions
50
grate.unittests/Generic/Running_MigrationScripts/MigrationsScriptsBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,35 @@ | ||
using System; | ||
using System.IO; | ||
using System.IO; | ||
using grate.Configuration; | ||
using grate.unittests.TestInfrastructure; | ||
|
||
namespace grate.unittests.Generic.Running_MigrationScripts; | ||
|
||
public abstract class MigrationsScriptsBase | ||
{ | ||
protected static DirectoryInfo CreateRandomTempDirectory() | ||
{ | ||
|
||
// I keep seeing the same temp di name in repeat test runs, and the dir has leftover scripts sitting in it. | ||
// Trying to get a clean folder each time we ask for it | ||
|
||
var dummyFile = Path.GetRandomFileName(); | ||
File.Delete(dummyFile); | ||
|
||
var scriptsDir = Directory.CreateDirectory(dummyFile); | ||
return scriptsDir; | ||
} | ||
protected static DirectoryInfo CreateRandomTempDirectory() => TestConfig.CreateRandomTempDirectory(); | ||
|
||
protected void CreateDummySql(MigrationsFolder? folder, string filename = "1_jalla.sql") | ||
=> CreateDummySql(folder?.Path, filename); | ||
|
||
protected void WriteSomeOtherSql(MigrationsFolder? folder, string filename = "1_jalla.sql") | ||
=> WriteSomeOtherSql(folder?.Path, filename); | ||
|
||
protected void CreateDummySql(DirectoryInfo? path, string filename = "1_jalla.sql") | ||
{ | ||
var dummySql = Context.Sql.SelectVersion; | ||
var path = MakeSurePathExists(folder); | ||
WriteSql(path, filename, dummySql); | ||
} | ||
|
||
protected void WriteSomeOtherSql(MigrationsFolder? folder, string filename = "1_jalla.sql") | ||
protected void WriteSomeOtherSql(DirectoryInfo? path, string filename = "1_jalla.sql") | ||
{ | ||
var dummySql = Context.Syntax.CurrentDatabase; | ||
var path = MakeSurePathExists(folder); | ||
WriteSql(path, filename, dummySql); | ||
} | ||
|
||
protected static void WriteSql(DirectoryInfo path, string filename, string? sql) | ||
{ | ||
if (!path.Exists) | ||
{ | ||
path.Create(); | ||
} | ||
File.WriteAllText(Path.Combine(path.ToString(), filename), sql); | ||
} | ||
protected static void WriteSql(DirectoryInfo? path, string filename, string? sql) => | ||
TestConfig.WriteContent(path, filename, sql); | ||
|
||
protected static DirectoryInfo MakeSurePathExists(MigrationsFolder? folder) | ||
{ | ||
var path = folder?.Path ?? throw new ArgumentException(nameof(folder.Path)); | ||
|
||
if (!path.Exists) | ||
{ | ||
path.Create(); | ||
} | ||
|
||
return path; | ||
} | ||
protected static DirectoryInfo MakeSurePathExists(MigrationsFolder? folder) => TestConfig.MakeSurePathExists(folder?.Path); | ||
|
||
protected abstract IGrateTestContext Context { get; } | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
grate.unittests/Generic/Running_MigrationScripts/One_time_scripts.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Dapper; | ||
|
126 changes: 126 additions & 0 deletions
126
grate.unittests/Generic/Running_MigrationScripts/ScriptsRun_Table.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Dapper; | ||
using FluentAssertions; | ||
using grate.Configuration; | ||
using grate.Migration; | ||
using grate.unittests.TestInfrastructure; | ||
using NUnit.Framework; | ||
|
||
namespace grate.unittests.Generic.Running_MigrationScripts; | ||
|
||
[TestFixture] | ||
public abstract class ScriptsRun_Table : MigrationsScriptsBase | ||
{ | ||
[Test()] | ||
public async Task Includes_the_folder_name_in_the_script_name_if_subfolders() | ||
{ | ||
var db = TestConfig.RandomDatabase(); | ||
|
||
var knownFolders = KnownFolders.In(TestConfig.CreateRandomTempDirectory()); | ||
GrateMigrator? migrator; | ||
|
||
var folder = new DirectoryInfo(Path.Combine(knownFolders.Up!.Path.ToString(), "sub", "folder", "long", "way")); | ||
|
||
string filename = "any_filename.sql"; | ||
|
||
CreateDummySql(folder, filename); | ||
await using (migrator = Context.GetMigrator(db, knownFolders)) | ||
{ | ||
await migrator.Migrate(); | ||
} | ||
|
||
string[] scripts; | ||
string sql = $"SELECT script_name FROM {Context.Syntax.TableWithSchema("grate", "ScriptsRun")}"; | ||
|
||
await using (var conn = Context.CreateDbConnection(db)) | ||
{ | ||
scripts = (await conn.QueryAsync<string>(sql)).ToArray(); | ||
} | ||
|
||
var expectedName = $"sub/folder/long/way/{filename}"; | ||
scripts.First().Should().Be(expectedName); | ||
} | ||
|
||
[Test()] | ||
public async Task Does_not_include_the_folder_name_in_the_script_name_if_no_subfolders() | ||
{ | ||
var db = TestConfig.RandomDatabase(); | ||
|
||
var knownFolders = KnownFolders.In(TestConfig.CreateRandomTempDirectory()); | ||
GrateMigrator? migrator; | ||
|
||
string filename = "any_filename.sql"; | ||
|
||
CreateDummySql(knownFolders.Up, filename); | ||
|
||
await using (migrator = Context.GetMigrator(db, knownFolders)) | ||
{ | ||
await migrator.Migrate(); | ||
} | ||
|
||
string[] scripts; | ||
string sql = $"SELECT script_name FROM {Context.Syntax.TableWithSchema("grate", "ScriptsRun")}"; | ||
|
||
await using (var conn = Context.CreateDbConnection(db)) | ||
{ | ||
scripts = (await conn.QueryAsync<string>(sql)).ToArray(); | ||
} | ||
|
||
scripts.First().Should().Be(filename); | ||
} | ||
|
||
// ReSharper disable InconsistentNaming | ||
// ReSharper disable once ClassNeverInstantiated.Local | ||
record Result(string script_name, string text_of_script); | ||
// ReSharper restore InconsistentNaming | ||
|
||
[Test()] | ||
public async Task Does_not_overwrite_scripts_from_different_folders_with_last_content() | ||
{ | ||
var db = TestConfig.RandomDatabase(); | ||
|
||
var knownFolders = KnownFolders.In(TestConfig.CreateRandomTempDirectory()); | ||
GrateMigrator? migrator; | ||
|
||
string filename = "any_filename.sql"; | ||
var folder1 = new DirectoryInfo(Path.Combine(knownFolders.Up!.Path.ToString(), "dub", "folder", "long", "way")); | ||
var folder2 = new DirectoryInfo(Path.Combine(knownFolders.Up!.Path.ToString(), "sub", "dolder", "gong", "way")); | ||
|
||
CreateDummySql(folder1, filename); | ||
WriteSomeOtherSql(folder2, filename); | ||
|
||
await using (migrator = Context.GetMigrator(db, knownFolders)) | ||
{ | ||
await migrator.Migrate(); | ||
} | ||
|
||
|
||
Result[] scripts; | ||
string sql = $"SELECT script_name, text_of_script FROM {Context.Syntax.TableWithSchema("grate", "ScriptsRun")}"; | ||
|
||
await using (var conn = Context.CreateDbConnection(db)) | ||
{ | ||
scripts = (await conn.QueryAsync<Result>(sql)).ToArray(); | ||
} | ||
|
||
Assert.Multiple(() => | ||
{ | ||
scripts.Should().HaveCount(2); | ||
var first = scripts.First(); | ||
var second = scripts.Last(); | ||
first.script_name.Should().Be($"dub/folder/long/way/{filename}"); | ||
first.text_of_script.Should().Be(Context.Sql.SelectVersion); | ||
second.script_name.Should().Be($"sub/dolder/gong/way/{filename}"); | ||
second.text_of_script.Should().Be(Context.Syntax.CurrentDatabase); | ||
}); | ||
|
||
|
||
|
||
} | ||
|
||
|
||
} |
8 changes: 8 additions & 0 deletions
8
grate.unittests/MariaDB/Running_MigrationScripts/ScriptsRun_Table.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using grate.unittests.TestInfrastructure; | ||
|
||
namespace grate.unittests.MariaDB.Running_MigrationScripts; | ||
|
||
public class ScriptsRun_Table: Generic.Running_MigrationScripts.ScriptsRun_Table | ||
{ | ||
protected override IGrateTestContext Context => GrateTestContext.MariaDB; | ||
} |
8 changes: 8 additions & 0 deletions
8
grate.unittests/Oracle/Running_MigrationScripts/ScriptsRun_Table.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using grate.unittests.TestInfrastructure; | ||
|
||
namespace grate.unittests.Oracle.Running_MigrationScripts; | ||
|
||
public class ScriptsRun_Table: Generic.Running_MigrationScripts.ScriptsRun_Table | ||
{ | ||
protected override IGrateTestContext Context => GrateTestContext.Oracle; | ||
} |
8 changes: 8 additions & 0 deletions
8
grate.unittests/PostgreSQL/Running_MigrationScripts/ScriptsRun_Table.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using grate.unittests.TestInfrastructure; | ||
|
||
namespace grate.unittests.PostgreSQL.Running_MigrationScripts; | ||
|
||
public class ScriptsRun_Table: Generic.Running_MigrationScripts.ScriptsRun_Table | ||
{ | ||
protected override IGrateTestContext Context => GrateTestContext.PostgreSql; | ||
} |
8 changes: 8 additions & 0 deletions
8
grate.unittests/SqLite/Running_MigrationScripts/ScriptsRun_Table.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using grate.unittests.TestInfrastructure; | ||
|
||
namespace grate.unittests.SqLite.Running_MigrationScripts; | ||
|
||
public class ScriptsRun_Table: Generic.Running_MigrationScripts.ScriptsRun_Table | ||
{ | ||
protected override IGrateTestContext Context => GrateTestContext.Sqlite; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
grate.unittests/SqlServer/Running_MigrationScripts/ScriptsRun_Table.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using grate.unittests.TestInfrastructure; | ||
|
||
namespace grate.unittests.SqlServer.Running_MigrationScripts; | ||
|
||
public class ScriptsRun_Table: Generic.Running_MigrationScripts.ScriptsRun_Table | ||
{ | ||
protected override IGrateTestContext Context => GrateTestContext.SqlServer; | ||
} |
Oops, something went wrong.