Skip to content

Commit

Permalink
Used Assent instead of ApprovalTests so that they run on dotnet core
Browse files Browse the repository at this point in the history
  • Loading branch information
droyad committed Aug 28, 2017
1 parent 13d010c commit b61bfc0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 58 deletions.
53 changes: 39 additions & 14 deletions src/dbup-tests/DatabaseSupportTests.cs
@@ -1,10 +1,14 @@
using System;
using System.Collections.Generic;
using System.IO;
using Assent;
using Assent.Namers;
using DbUp.Builder;
using DbUp.Engine;
using DbUp.Engine.Transactions;
using DbUp.SqlServer;
using DbUp.SQLite;
using DbUp.Tests.Helpers;
using DbUp.Tests.TestInfrastructure;
using Shouldly;
using TestStack.BDDfy;
Expand All @@ -27,6 +31,7 @@ public class DatabaseSupportTests
Func<UpgradeEngineBuilder, string, string, UpgradeEngineBuilder> addCustomNamedJournalToBuilder;
CaptureLogsLogger logger;


[Fact]
public void VerifyBasicSupport()
{
Expand All @@ -37,7 +42,7 @@ public void VerifyBasicSupport()
.And(_ => SingleScriptExists())
.When(_ => UpgradeIsPerformed())
.Then(_ => UpgradeIsSuccessful())
.And(_ => CommandLogReflectsScript(deployTo), "Command log matches expected steps")
.And(_ => CommandLogReflectsScript(deployTo, nameof(VerifyBasicSupport)), "Command log matches expected steps")
.WithExamples(DatabaseExampleTable)
.BDDfy();
}
Expand All @@ -53,14 +58,15 @@ public void VerifyVariableSubstitutions()
.And(_ => VariableSubstitutionIsSetup())
.When(_ => UpgradeIsPerformed())
.Then(_ => UpgradeIsSuccessful())
.And(_ => CommandLogReflectsScript(deployTo), "Variables substituted correctly in command log")
.And(_ => CommandLogReflectsScript(deployTo, nameof(VerifyVariableSubstitutions)), "Variables substituted correctly in command log")
.WithExamples(DatabaseExampleTable)
.BDDfy();
}

[Fact]
public void VerifyJournalCreationIfNameChanged()
{

ExampleAction deployTo = null;
this
.Given(() => deployTo)
Expand All @@ -69,7 +75,7 @@ public void VerifyJournalCreationIfNameChanged()
.And(_ => SingleScriptExists())
.When(_ => UpgradeIsPerformed())
.Then(_ => UpgradeIsSuccessful())
.And(_ => CommandLogReflectsScript(deployTo), "Command log matches expected steps")
.And(_ => CommandLogReflectsScript(deployTo, nameof(VerifyJournalCreationIfNameChanged)), "Command log matches expected steps")
.WithExamples(DatabaseExampleTable)
.BDDfy();
}
Expand Down Expand Up @@ -102,20 +108,18 @@ void JournalTableNameIsCustomised()
upgradeEngineBuilder = addCustomNamedJournalToBuilder(upgradeEngineBuilder, "test", "TestSchemaVersions");
}

void CommandLogReflectsScript(ExampleAction target)
void CommandLogReflectsScript(ExampleAction target, string testName)
{
#if !NETCORE
logger.Log
.ShouldMatchApproved(b =>
{
b.LocateTestMethodUsingAttribute<FactAttribute>();
b.WithScrubber(Scrubbers.ScrubDates);
b.WithDescriminator(target.ToString().Replace(" ", string.Empty));
b.SubFolder("ApprovalFiles");
});
#endif
this.Assent(
logger.Log,
new Configuration()
.UsingSanitiser(Scrubbers.ScrubDates)
.UsingNamer(new Namer(target, testName))
);
}



void UpgradeIsSuccessful()
{
result.Successful.ShouldBe(true);
Expand Down Expand Up @@ -156,5 +160,26 @@ Action Deploy(Func<SupportedDatabases, UpgradeEngineBuilder> deployTo, Func<Upgr
addCustomNamedJournalToBuilder = addCustomNamedJournal;
};
}

private class Namer : INamer
{
private readonly ExampleAction target;
private readonly string testName;

public Namer(ExampleAction target, string testName)
{
this.target = target;
this.testName = testName;
}

public string GetName(TestMetadata metadata)
{
var targetName = target.ToString().Replace(" ", "");
var dir = Path.GetDirectoryName(metadata.FilePath);
var filename = $"{metadata.TestFixture.GetType().Name}.{testName}.{targetName}";

return Path.Combine(dir, "ApprovalFiles", filename);
}
}
}
}
66 changes: 22 additions & 44 deletions src/dbup-tests/TransactionScenarios.cs
@@ -1,4 +1,5 @@
#if !NETCORE
using Assent;
using Assent.Namers;
using DbUp.Builder;
using DbUp.Engine;
using DbUp.Tests.TestInfrastructure;
Expand All @@ -14,6 +15,9 @@ public class TransactionScenarios
RecordingDbConnection testConnection;
SqlScript[] scripts;
CaptureLogsLogger logger;
Configuration assentConfig = new Configuration()
.UsingNamer(new SubdirectoryNamer("ApprovalFiles"))
.UsingSanitiser(Scrubbers.ScrubDates);

public TransactionScenarios()
{
Expand All @@ -26,7 +30,7 @@ public void UsingNoTransactionsScenario()
this
.Given(_ => DbUpSetupToNotUseTransactions())
.When(_ => UpgradeIsPerformedExecutingTwoScripts())
.Then(_ => ShouldExecuteScriptsWithoutUsingATransaction())
.Then(_ => ShouldExecuteScriptsWithoutUsingATransaction(nameof(UsingNoTransactionsScenario)))
.BDDfy();
}

Expand All @@ -36,7 +40,7 @@ public void UsingNoTransactionsScenarioScriptFails()
this
.Given(_ => DbUpSetupToNotUseTransactions())
.When(_ => UpgradeIsPerformedWithFirstOfTwoScriptsFails())
.Then(_ => ShouldStopExecution())
.Then(_ => ShouldStopExecution(nameof(UsingNoTransactionsScenarioScriptFails)))
.BDDfy();
}

Expand All @@ -46,7 +50,7 @@ public void UsingTransactionPerScriptScenarioSuccess()
this
.Given(_ => DbUpSetupToUseTransactionPerScript())
.When(_ => UpgradeIsPerformedExecutingTwoScripts())
.Then(_ => ShouldHaveExecutedEachScriptInATransaction())
.Then(_ => ShouldHaveExecutedEachScriptInATransaction(nameof(UsingTransactionPerScriptScenarioSuccess)))
.BDDfy();
}

Expand All @@ -56,7 +60,7 @@ public void UsingTransactionPerScriptScenarioScriptFails()
this
.Given(_ => DbUpSetupToUseTransactionPerScript())
.When(_ => UpgradeIsPerformedWithFirstOfTwoScriptsFails())
.Then(_ => ShouldRollbackFailedScriptAndStopExecution())
.Then(_ => ShouldRollbackFailedScriptAndStopExecution(nameof(UsingTransactionPerScriptScenarioScriptFails)))
.BDDfy();
}

Expand All @@ -66,7 +70,7 @@ public void UsingSingleTransactionScenarioSuccess()
this
.Given(_ => DbUpSetupToUseSingleTransaction())
.When(_ => UpgradeIsPerformedExecutingTwoScripts())
.Then(_ => ShouldExecuteAllScriptsInASingleTransaction())
.Then(_ => ShouldExecuteAllScriptsInASingleTransaction(nameof(UsingSingleTransactionScenarioSuccess)))
.BDDfy();
}

Expand All @@ -76,7 +80,7 @@ public void UsingSingleTransactionScenarioSuccessScriptFails()
this
.Given(_ => DbUpSetupToUseSingleTransaction())
.When(_ => UpgradeIsPerformedWithFirstOfTwoScriptsFails())
.Then(_ => ShouldRollbackFailedScriptAndStopExecution())
.Then(_ => ShouldRollbackFailedScriptAndStopExecution(nameof(UsingSingleTransactionScenarioSuccessScriptFails)))
.BDDfy();
}

Expand All @@ -93,54 +97,29 @@ void UpgradeIsPerformedWithFirstOfTwoScriptsFails()
.PerformUpgrade();
}

void ShouldStopExecution()
void ShouldStopExecution(string testName)
{
logger.Log.ShouldMatchApproved(b =>
{
b.WithScrubber(Scrubbers.ScrubDates);
b.LocateTestMethodUsingAttribute<FactAttribute>();
b.SubFolder("ApprovalFiles");
});
this.Assent(logger.Log, assentConfig, testName);
}

void ShouldRollbackFailedScriptAndStopExecution()
void ShouldRollbackFailedScriptAndStopExecution(string testName)
{
logger.Log.ShouldMatchApproved(b =>
{
b.WithScrubber(Scrubbers.ScrubDates);
b.LocateTestMethodUsingAttribute<FactAttribute>();
b.SubFolder("ApprovalFiles");
});
this.Assent(logger.Log, assentConfig, testName);
}

void ShouldExecuteAllScriptsInASingleTransaction()
void ShouldExecuteAllScriptsInASingleTransaction(string testName)
{
logger.Log.ShouldMatchApproved(b =>
{
b.WithScrubber(Scrubbers.ScrubDates);
b.LocateTestMethodUsingAttribute<FactAttribute>();
b.SubFolder("ApprovalFiles");
});
this.Assent(logger.Log, assentConfig, testName);
}

void ShouldHaveExecutedEachScriptInATransaction()
void ShouldHaveExecutedEachScriptInATransaction(string testName)
{
logger.Log.ShouldMatchApproved(b =>
{
b.WithScrubber(Scrubbers.ScrubDates);
b.LocateTestMethodUsingAttribute<FactAttribute>();
b.SubFolder("ApprovalFiles");
});
this.Assent(logger.Log, assentConfig, testName);
}

void ShouldExecuteScriptsWithoutUsingATransaction()
void ShouldExecuteScriptsWithoutUsingATransaction(string testName)
{
logger.Log.ShouldMatchApproved(b =>
{
b.WithScrubber(Scrubbers.ScrubDates);
b.LocateTestMethodUsingAttribute<FactAttribute>();
b.SubFolder("ApprovalFiles");
});
this.Assent(logger.Log, assentConfig, testName);
}

void DbUpSetupToUseSingleTransaction()
Expand Down Expand Up @@ -184,5 +163,4 @@ void UpgradeIsPerformedExecutingTwoScripts()
.PerformUpgrade();
}
}
}
#endif
}
1 change: 1 addition & 0 deletions src/dbup-tests/dbup-tests.csproj
Expand Up @@ -20,6 +20,7 @@
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="Shouldly" Version="2.8.1" />
<PackageReference Include="Assent" Version="1.0.1" />
<PackageReference Include="TestStack.BDDfy" Version="4.3.1" />
<PackageReference Include="xunit.core" Version="2.2.0" />
<PackageReference Include="NSubstitute" Version="2.0.3" />
Expand Down

0 comments on commit b61bfc0

Please sign in to comment.