You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
2.9 KiB
100 lines
2.9 KiB
2 years ago
|
using Recyclarr.Cli.Migration;
|
||
2 years ago
|
using Recyclarr.Cli.Migration.Steps;
|
||
2 years ago
|
using Spectre.Console.Testing;
|
||
3 years ago
|
|
||
2 years ago
|
namespace Recyclarr.Cli.Tests.Migration;
|
||
3 years ago
|
|
||
|
[TestFixture]
|
||
|
[Parallelizable(ParallelScope.All)]
|
||
1 year ago
|
public class MigrationExecutorTest
|
||
3 years ago
|
{
|
||
|
[Test]
|
||
|
public void Step_not_executed_if_check_returns_false()
|
||
|
{
|
||
2 years ago
|
using var console = new TestConsole();
|
||
3 years ago
|
var step = Substitute.For<IMigrationStep>();
|
||
3 years ago
|
var executor = new MigrationExecutor(new[] {step}, console);
|
||
3 years ago
|
|
||
|
step.CheckIfNeeded().Returns(false);
|
||
|
|
||
3 years ago
|
executor.PerformAllMigrationSteps(false);
|
||
3 years ago
|
|
||
|
step.Received().CheckIfNeeded();
|
||
3 years ago
|
step.DidNotReceive().Execute(null);
|
||
3 years ago
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void Step_executed_if_check_returns_true()
|
||
|
{
|
||
2 years ago
|
using var console = new TestConsole();
|
||
3 years ago
|
var step = Substitute.For<IMigrationStep>();
|
||
3 years ago
|
var executor = new MigrationExecutor(new[] {step}, console);
|
||
3 years ago
|
|
||
|
step.CheckIfNeeded().Returns(true);
|
||
|
|
||
3 years ago
|
executor.PerformAllMigrationSteps(false);
|
||
3 years ago
|
|
||
|
step.Received().CheckIfNeeded();
|
||
3 years ago
|
step.Received().Execute(null);
|
||
3 years ago
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void Steps_executed_in_ascending_order()
|
||
|
{
|
||
2 years ago
|
using var console = new TestConsole();
|
||
3 years ago
|
|
||
3 years ago
|
var steps = new[]
|
||
|
{
|
||
|
Substitute.For<IMigrationStep>(),
|
||
|
Substitute.For<IMigrationStep>(),
|
||
|
Substitute.For<IMigrationStep>()
|
||
|
};
|
||
|
|
||
|
steps[0].Order.Returns(20);
|
||
|
steps[1].Order.Returns(10);
|
||
|
steps[2].Order.Returns(30);
|
||
|
|
||
3 years ago
|
var executor = new MigrationExecutor(steps, console);
|
||
3 years ago
|
|
||
3 years ago
|
executor.PerformAllMigrationSteps(false);
|
||
3 years ago
|
|
||
|
Received.InOrder(() =>
|
||
|
{
|
||
|
steps[1].CheckIfNeeded();
|
||
|
steps[0].CheckIfNeeded();
|
||
|
steps[2].CheckIfNeeded();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void Exception_converted_to_migration_exception()
|
||
|
{
|
||
2 years ago
|
using var console = new TestConsole();
|
||
3 years ago
|
var step = Substitute.For<IMigrationStep>();
|
||
3 years ago
|
var executor = new MigrationExecutor(new[] {step}, console);
|
||
3 years ago
|
|
||
|
step.CheckIfNeeded().Returns(true);
|
||
3 years ago
|
step.When(x => x.Execute(null)).Throw(new ArgumentException("test message"));
|
||
3 years ago
|
|
||
3 years ago
|
var act = () => executor.PerformAllMigrationSteps(false);
|
||
3 years ago
|
|
||
3 years ago
|
act.Should().Throw<MigrationException>().Which.OriginalException.Message.Should().Be("test message");
|
||
3 years ago
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void Migration_exceptions_are_not_converted()
|
||
|
{
|
||
2 years ago
|
using var console = new TestConsole();
|
||
3 years ago
|
var step = Substitute.For<IMigrationStep>();
|
||
3 years ago
|
var executor = new MigrationExecutor(new[] {step}, console);
|
||
2 years ago
|
var exception = new MigrationException(new ArgumentException(), "a", new[] {"b"});
|
||
3 years ago
|
|
||
|
step.CheckIfNeeded().Returns(true);
|
||
3 years ago
|
step.When(x => x.Execute(null)).Throw(exception);
|
||
3 years ago
|
|
||
3 years ago
|
var act = () => executor.PerformAllMigrationSteps(false);
|
||
3 years ago
|
|
||
|
act.Should().Throw<MigrationException>().Which.Should().Be(exception);
|
||
|
}
|
||
|
}
|