Skip to content

Commit 7b645b4

Browse files
Restructure code to better grok it, making intent more clear
1 parent 89ba959 commit 7b645b4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+972
-886
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Optional;
2+
3+
namespace DbUp.Cli.Tests.TestInfrastructure;
4+
5+
public static class OptionExtensions
6+
{
7+
public static string GetErrorOrNull<T>(this Option<T, Error> option)
8+
{
9+
string error = null;
10+
option.MatchNone(err => error = err.Message);
11+
return error;
12+
}
13+
14+
public static string GetErrorOrThrow<T>(this Option<T, Error> option) =>
15+
option.GetErrorOrNull() ?? throw new Exception("option is missing error");
16+
17+
public static T GetValueOrNull<T>(this Option<T, Error> option)
18+
{
19+
T value = default;
20+
option.MatchSome(v => value = v);
21+
return value;
22+
}
23+
24+
public static T GetValueOrThrow<T>(this Option<T, Error> option) =>
25+
option.GetValueOrNull() ?? throw new Exception("option is missing value. error: " + option.GetErrorOrNull());
26+
}

src/dbup-cli.tests.core/TestInfrastructure/TestDatabaseExtension.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,13 @@ namespace DbUp.Cli.Tests.TestInfrastructure;
99
/// </summary>
1010
public static class TestDatabaseExtension
1111
{
12-
public static UpgradeEngineBuilder OverrideConnectionFactory(this UpgradeEngineBuilder engineBuilder, IDbConnection connection)
13-
{
14-
return engineBuilder.OverrideConnectionFactory(new DelegateConnectionFactory(l => connection));
15-
}
12+
public static UpgradeEngineBuilder OverrideConnectionFactory(this UpgradeEngineBuilder engineBuilder, IDbConnection connection) =>
13+
engineBuilder.OverrideConnectionFactory(new DelegateConnectionFactory(l => connection));
1614

1715
public static UpgradeEngineBuilder OverrideConnectionFactory(this UpgradeEngineBuilder engineBuilder, IConnectionFactory connectionFactory)
1816
{
19-
engineBuilder.Configure(c => ((DatabaseConnectionManager)c.ConnectionManager).OverrideFactoryForTest(connectionFactory));
17+
engineBuilder.Configure(c =>
18+
((DatabaseConnectionManager)c.ConnectionManager).OverrideFactoryForTest(connectionFactory));
2019
return engineBuilder;
2120
}
22-
23-
public static UpgradeEngineBuilder TestDatabase(this SupportedDatabases supportedDatabases, IDbConnection connection)
24-
{
25-
var builder = supportedDatabases.SqlDatabase("");
26-
builder.OverrideConnectionFactory(connection);
27-
return builder;
28-
}
2921
}

src/dbup-cli.tests.core/TestInfrastructure/TestEnvironment.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,35 @@ namespace DbUp.Cli.Tests.TestInfrastructure;
22

33
public class TestEnvironment(string currentDirectory = null) : CliEnvironment
44
{
5+
private const string inMemFilePrefix = "/in-mem/";
6+
private Dictionary<string, string> fileByPath = new();
7+
58
public override string GetCurrentDirectory() => currentDirectory ?? ProjectPaths.TempDir;
9+
10+
/// <summary>Save the file in memory and return a path prefixed with inMem root</summary>
11+
public string WriteFileInMem(string content, string path = null)
12+
{
13+
if(path is null)
14+
path = $"{inMemFilePrefix}{fileByPath.Count + 1}";
15+
else if (!path.StartsWith("inMem"))
16+
path = $"{inMemFilePrefix}{path}";
17+
18+
fileByPath[path] = content;
19+
20+
return path;
21+
}
22+
23+
public override bool FileExists(string path)
24+
{
25+
return path.StartsWith(inMemFilePrefix)
26+
? fileByPath.ContainsKey(path)
27+
: base.FileExists(path);
28+
}
29+
30+
public override string ReadFile(string path)
31+
{
32+
return path.StartsWith(inMemFilePrefix)
33+
? fileByPath[path]
34+
: base.ReadFile(path);
35+
}
636
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using DbUp.Cli.Tests.TestInfrastructure;
2+
using FluentAssertions;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
namespace DbUp.Cli.Tests.CommandTests;
6+
7+
[TestClass]
8+
public class InitTests
9+
{
10+
private readonly TestHost host = new();
11+
private readonly string tempDbUpYmlPath = ProjectPaths.GetTempPath("dbup.yml");
12+
13+
[TestMethod]
14+
public void ShouldCreateDefaultConfig_IfItIsNotPresent()
15+
{
16+
host.EnsureDirectoryExists(ProjectPaths.TempDir);
17+
host.EnsureFileDoesNotExist(tempDbUpYmlPath);
18+
host.ToolEngine.Run("init").ShouldSucceed();
19+
host.Environment.FileExists(tempDbUpYmlPath).Should().BeTrue();
20+
}
21+
22+
[TestMethod]
23+
public void ShouldReturn1AndNotCreateConfig_IfItIsPresent()
24+
{
25+
// ensure exists
26+
host.ToolEngine.Run("init");
27+
var lastWriteTime = new FileInfo(tempDbUpYmlPath).LastWriteTime;
28+
29+
host.ToolEngine.Run("init").ShouldFail(assert: error => error.Should().StartWith("File already exists"));
30+
host.Environment.FileExists(tempDbUpYmlPath).Should().BeTrue();
31+
var newLastWriteTime = new FileInfo(tempDbUpYmlPath).LastWriteTime;
32+
newLastWriteTime.Should().Be(lastWriteTime);
33+
}
34+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using DbUp.Cli.Tests.TestInfrastructure;
2+
using FluentAssertions;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
namespace DbUp.Cli.Tests.CommandTests;
6+
7+
[TestClass]
8+
public class MarkAsExecutedTests
9+
{
10+
private readonly TestHost host = new();
11+
12+
[TestMethod]
13+
public void WhenCalled_ShouldNotMakeAnyChangesInDb()
14+
{
15+
host.ToolEngine
16+
.Run("mark-as-executed", ProjectPaths.GetConfigPath("mark-as-executed.yml"))
17+
.ShouldSucceed();
18+
19+
host.Logger.Log.Should().NotContain("print 'You should not see this message'");
20+
}
21+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using DbUp.Cli.Tests.TestInfrastructure;
2+
using FluentAssertions;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
namespace DbUp.Cli.Tests.CommandTests;
6+
7+
[TestClass]
8+
public class StatusTests
9+
{
10+
private readonly TestHost host = new();
11+
12+
[TestMethod]
13+
public void ShouldPrintGeneralInformation_IfNoScriptsToExecute()
14+
{
15+
host.ToolEngine
16+
.Run("status", ProjectPaths.GetConfigPath("noscripts.yml"))
17+
.ShouldSucceed();
18+
19+
host.Logger.InfoMessages.Last().Should().StartWith("Database is up-to-date");
20+
}
21+
22+
[TestMethod]
23+
public void ShouldPrintGeneralInformation_IfThereAreTheScriptsToExecute()
24+
{
25+
host.ToolEngine
26+
.Run("status", ProjectPaths.GetConfigPath("onescript.yml"))
27+
.ShouldExitWithCode(-1);
28+
29+
host.Logger.InfoMessages.Last().Should().StartWith("You have 1 more scripts");
30+
}
31+
32+
[TestMethod]
33+
public void ShouldPrintScriptName_IfThereAreTheScriptsToExecute()
34+
{
35+
host.ToolEngine
36+
.Run("status", ProjectPaths.GetConfigPath("onescript.yml"), "-n")
37+
.ShouldExitWithCode(-1);
38+
39+
host.Logger.InfoMessages.Last().Should().EndWith("c001.sql");
40+
}
41+
42+
[TestMethod]
43+
public void ShouldReturnMinusOne_IfThereAreTheScriptsToExecute()
44+
{
45+
host.ToolEngine
46+
.Run("status", ProjectPaths.GetConfigPath("onescript.yml"), "-n")
47+
.ShouldExitWithCode(-1);
48+
49+
host.Logger.InfoMessages.Last().Should().EndWith("c001.sql");
50+
}
51+
52+
[TestMethod]
53+
public void ShouldUseSpecifiedEnvFiles()
54+
{
55+
host.ToolEngine.Run("status",
56+
ProjectPaths.GetConfigPath("Status", "status.yml"),
57+
"-n",
58+
"--env",
59+
ProjectPaths.GetConfigPath("Status", "file1.env"),
60+
ProjectPaths.GetConfigPath("Status", "file2.env"))
61+
.ShouldExitWithCode(-1);
62+
63+
host.Logger.InfoMessages.Last().Should().EndWith("c001.sql");
64+
}
65+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using DbUp.Cli.Tests.TestInfrastructure;
2+
using FluentAssertions;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
namespace DbUp.Cli.Tests.CommandTests;
6+
7+
[TestClass]
8+
public class UpgradeTests
9+
{
10+
private readonly TestHost host = new();
11+
12+
[TestMethod]
13+
public void ShouldRespectScriptEncoding()
14+
{
15+
host.ToolEngine
16+
.Run("upgrade", ProjectPaths.GetConfigPath("encoding.yml"))
17+
.ShouldSucceed();
18+
19+
host.Logger.Log.Should().Contain("print 'Превед, медвед'");
20+
}
21+
22+
[DataRow("d0a1.sql")]
23+
[DataRow("d0aa1.sql")]
24+
[DataRow("c001.sql")]
25+
[DataRow("c0a1.sql")]
26+
[DataRow("c0b1.sql")]
27+
[DataRow("e001.sql")]
28+
[DataRow("e0a1.sql")]
29+
[DataRow("e0b1.sql")]
30+
[DataTestMethod]
31+
public void ToolEngine_ShouldRespectScriptFiltersAndMatchFiles(string filename)
32+
{
33+
host.ToolEngine
34+
.Run("upgrade", ProjectPaths.GetConfigPath("filter.yml"))
35+
.ShouldSucceed();
36+
37+
host.Logger.Log.Should().Contain(filename);
38+
}
39+
40+
[DataRow("d001.sql")]
41+
[DataRow("d01.sql")]
42+
[DataRow("d0b1.sql")]
43+
[DataRow("c01.sql")]
44+
[DataRow("c0aa1.sql")]
45+
[DataRow("e01.sql")]
46+
[DataRow("e0aa1.sql")]
47+
[DataTestMethod]
48+
public void ToolEngine_ShouldRespectScriptFiltersAndNotMatchFiles(string filename)
49+
{
50+
host.ToolEngine
51+
.Run("upgrade", ProjectPaths.GetConfigPath("filter.yml"))
52+
.ShouldSucceed();
53+
54+
host.Logger.Log.Should().NotContain(filename);
55+
}
56+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using DbUp.Cli.Tests.TestInfrastructure;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
4+
namespace DbUp.Cli.Tests.CommandTests;
5+
6+
[TestClass]
7+
public class VersionTests
8+
{
9+
private readonly TestHost host = new();
10+
11+
[TestMethod]
12+
public void ShouldReturnZero() => host.ToolEngine.Run("version").ShouldSucceed();
13+
}

0 commit comments

Comments
 (0)