Skip to content

Commit

Permalink
minor refactorings and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dradovic committed Oct 27, 2011
1 parent 746d6c1 commit 33c0c0d
Show file tree
Hide file tree
Showing 14 changed files with 385 additions and 207 deletions.
3 changes: 2 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
2.0.0 - API:
2.0.0 - Duplicate migration exports are detected before actual migration execution
- API:
- IMigrationBatch lists:
- migrations scheduled for execution
- migrations executed server-side but unknown to the application
Expand Down
2 changes: 1 addition & 1 deletion MigSharp.NUnit/MigSharp.NUnit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@
<Compile Include="Migrate\MigrateProcess.cs" />
<Compile Include="Migrate\ConsoleTests.cs" />
<Compile Include="MigrationOptionTests.cs" />
<Compile Include="MigratorTests.cs" />
<Compile Include="Process\MigrationBatchTests.cs" />
<Compile Include="Process\MigrationSelectorTests.cs" />
<Compile Include="Process\ValidatorTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Providers\DataTypeTests.cs" />
Expand Down
103 changes: 0 additions & 103 deletions MigSharp.NUnit/MigratorTests.cs

This file was deleted.

51 changes: 28 additions & 23 deletions MigSharp.NUnit/Process/MigrationBatchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,49 +26,54 @@ public void VerifyValidationErrorsResultInException()
step,
};
IVersioning versioning = MockRepository.GenerateStub<IVersioning>();
MigrationBatch batch = new MigrationBatch(steps, steps, Enumerable.Empty<IMigrationMetadata>(), versioning, new MigrationOptions());
MigrationBatch batch = new MigrationBatch(steps, Enumerable.Empty<IMigrationMetadata>(), versioning, new MigrationOptions());

batch.Execute();
Assert.IsTrue(batch.IsExecuted);
}

[Test]
public void VerifyStepExecutingIsRaised()
public void VerifyStepExecutedAndStepExecutingAreRaised()
{
IMigrationStep step = MockRepository.GenerateStub<IMigrationStep>();
step.Expect(s => s.Metadata).Return(new Metadata1());
var metadata = new Metadata1();
step.Expect(s => s.Metadata).Return(metadata);
step.Expect(s => s.Report(null)).IgnoreArguments().Return(CreateMigrationReport());
IMigrationStep[] steps = new[]
{
step,
};
IVersioning versioning = MockRepository.GenerateStub<IVersioning>();
MigrationBatch batch = new MigrationBatch(steps, steps, Enumerable.Empty<IMigrationMetadata>(), versioning, new MigrationOptions());
int count = 0;
batch.StepExecuting += (sender, args) => count++;
var batch = new MigrationBatch(steps, Enumerable.Empty<IMigrationMetadata>(), versioning, new MigrationOptions());
Assert.AreSame(metadata, batch.ScheduledMigrations[0], "The batch should expose the metadata of the step."); // this is tested to allow for the undocumented feature test below
int countExecutingEvent = 0;
int countExecutedEvent = 0;
batch.StepExecuting += (sender, args) =>
{
// note: the following assertion tests an undocumented feature
Assert.AreSame(metadata, args.Metadata, "The event should carry the same metadata that is in the ScheduleMigrations collection.");
countExecutingEvent++;
};
batch.StepExecuted += (sender, args) =>
{
// note: the following assertion tests an undocumented feature
Assert.AreSame(metadata, args.Metadata, "The event should carry the same metadata that is in the ScheduleMigrations collection.");
countExecutedEvent++;
};

batch.Execute();

Assert.AreEqual(2 * steps.Length, count);
Assert.IsTrue(batch.IsExecuted);
Assert.AreEqual(steps.Length, countExecutingEvent);
Assert.AreEqual(steps.Length, countExecutedEvent);
}

[Test]
public void VerifyStepExecutedIsRaised()
[Test, ExpectedException(typeof(InvalidOperationException))]
public void VerifyCallingExecuteTwiceThrows()
{
IMigrationStep step = MockRepository.GenerateStub<IMigrationStep>();
step.Expect(s => s.Metadata).Return(new Metadata1());
step.Expect(s => s.Report(null)).IgnoreArguments().Return(CreateMigrationReport());
IMigrationStep[] steps = new[]
{
step,
};
IVersioning versioning = MockRepository.GenerateStub<IVersioning>();
MigrationBatch batch = new MigrationBatch(steps, steps, Enumerable.Empty<IMigrationMetadata>(), versioning, new MigrationOptions());
int count = 0;
batch.StepExecuted += (sender, args) => count++;

var batch = new MigrationBatch(Enumerable.Empty<IMigrationStep>(), Enumerable.Empty<IMigrationMetadata>(), MockRepository.GenerateStub<IVersioning>(), new MigrationOptions());
batch.Execute();
batch.Execute();

Assert.AreEqual(2 * steps.Length, count);
}

private static IMigrationReport CreateMigrationReport()
Expand Down
162 changes: 162 additions & 0 deletions MigSharp.NUnit/Process/MigrationSelectorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
using System.Collections.Generic;
using System.Linq;

using MigSharp.Core;
using MigSharp.Process;

using NUnit.Framework;

using Rhino.Mocks;

namespace MigSharp.NUnit.Process
{
[TestFixture, Category("smoke")]
public class MigrationSelectorTests
{
private const string DefaultModuleName = MigrationExportAttribute.DefaultModuleName;

[Test]
public void TestRegularCase()
{
var migration = MockRepository.GenerateStub<IMigration>();
ImportedMigration[] importedMigrations = {
new ImportedMigration(migration, new MigrationMetadata(1, DefaultModuleName, null)),
new ImportedMigration(migration, new MigrationMetadata(2, DefaultModuleName, null)),
new ImportedMigration(migration, new MigrationMetadata(3, DefaultModuleName, null)),
};
IMigrationMetadata[] executedMigrations = {
new MigrationMetadata(1, DefaultModuleName, null),
new MigrationMetadata(2, DefaultModuleName, null),
};

var selector = new MigrationSelector(importedMigrations, executedMigrations);

IEnumerable<ApplicableMigration> applicableMigrations;
IEnumerable<IMigrationMetadata> unidentifiedMigrations;
selector.GetMigrationsTo(long.MaxValue, s => true, out applicableMigrations, out unidentifiedMigrations);

Assert.AreEqual(1, applicableMigrations.Count());
Assert.AreEqual(3, applicableMigrations.First().Metadata.Timestamp);
Assert.AreEqual(MigrationDirection.Up, applicableMigrations.First().Metadata.Direction);
CollectionAssert.IsEmpty(unidentifiedMigrations);
}

[Test]
public void TestGapsAreFound()
{
var migration = MockRepository.GenerateStub<IMigration>();
ImportedMigration[] importedMigrations = {
new ImportedMigration(migration, new MigrationMetadata(1, DefaultModuleName, null)),
new ImportedMigration(migration, new MigrationMetadata(2, DefaultModuleName, null)),
new ImportedMigration(migration, new MigrationMetadata(3, DefaultModuleName, null)),
};
IMigrationMetadata[] executedMigrations = {
new MigrationMetadata(1, DefaultModuleName, null),
new MigrationMetadata(3, DefaultModuleName, null),
};

var selector = new MigrationSelector(importedMigrations, executedMigrations);

IEnumerable<ApplicableMigration> applicableMigrations;
IEnumerable<IMigrationMetadata> unidentifiedMigrations;
selector.GetMigrationsTo(long.MaxValue, s => true, out applicableMigrations, out unidentifiedMigrations);

Assert.AreEqual(1, applicableMigrations.Count());
Assert.AreEqual(2, applicableMigrations.First().Metadata.Timestamp);
Assert.AreEqual(MigrationDirection.Up, applicableMigrations.First().Metadata.Direction);
CollectionAssert.IsEmpty(unidentifiedMigrations);
}

[Test]
public void TestReverting()
{
var migration = MockRepository.GenerateStub<IReversibleMigration>();
ImportedMigration[] importedMigrations = {
new ImportedMigration(migration, new MigrationMetadata(1, DefaultModuleName, null)),
new ImportedMigration(migration, new MigrationMetadata(2, DefaultModuleName, null)),
new ImportedMigration(migration, new MigrationMetadata(3, DefaultModuleName, null)),
};
IMigrationMetadata[] executedMigrations = {
new MigrationMetadata(1, DefaultModuleName, null),
new MigrationMetadata(2, DefaultModuleName, null),
new MigrationMetadata(3, DefaultModuleName, null),
};

var selector = new MigrationSelector(importedMigrations, executedMigrations);

IEnumerable<ApplicableMigration> applicableMigrations;
IEnumerable<IMigrationMetadata> unidentifiedMigrations;
selector.GetMigrationsTo(2, s => true, out applicableMigrations, out unidentifiedMigrations);

Assert.AreEqual(1, applicableMigrations.Count());
Assert.AreEqual(3, applicableMigrations.First().Metadata.Timestamp);
Assert.AreEqual(MigrationDirection.Down, applicableMigrations.First().Metadata.Direction);
CollectionAssert.IsEmpty(unidentifiedMigrations);
}

[Test, ExpectedException(typeof(IrreversibleMigrationException))]
public void TestRevertingThrowsWhenImpossible()
{
var migration = MockRepository.GenerateStub<IMigration>();
ImportedMigration[] importedMigrations = {
new ImportedMigration(migration, new MigrationMetadata(1, DefaultModuleName, null)),
new ImportedMigration(migration, new MigrationMetadata(2, DefaultModuleName, null)),
new ImportedMigration(migration, new MigrationMetadata(3, DefaultModuleName, null)),
};
IMigrationMetadata[] executedMigrations = {
new MigrationMetadata(1, DefaultModuleName, null),
new MigrationMetadata(2, DefaultModuleName, null),
new MigrationMetadata(3, DefaultModuleName, null),
};

var selector = new MigrationSelector(importedMigrations, executedMigrations);

IEnumerable<ApplicableMigration> applicableMigrations;
IEnumerable<IMigrationMetadata> unidentifiedMigrations;
selector.GetMigrationsTo(2, s => true, out applicableMigrations, out unidentifiedMigrations);
}

[Test]
public void TestUnidentifiedMigrations()
{
var migration = MockRepository.GenerateStub<IReversibleMigration>();
ImportedMigration[] importedMigrations = {
new ImportedMigration(migration, new MigrationMetadata(1, DefaultModuleName, null)),
new ImportedMigration(migration, new MigrationMetadata(2, DefaultModuleName, null)),
new ImportedMigration(migration, new MigrationMetadata(3, DefaultModuleName, null)),
};
IMigrationMetadata[] executedMigrations = {
new MigrationMetadata(1, DefaultModuleName, null),
new MigrationMetadata(2, DefaultModuleName, null),
new MigrationMetadata(13, DefaultModuleName, null),
};

var selector = new MigrationSelector(importedMigrations, executedMigrations);

IEnumerable<ApplicableMigration> applicableMigrations;
IEnumerable<IMigrationMetadata> unidentifiedMigrations;
selector.GetMigrationsTo(long.MaxValue, s => true, out applicableMigrations, out unidentifiedMigrations);

Assert.AreEqual(1, applicableMigrations.Count());
Assert.AreEqual(3, applicableMigrations.First().Metadata.Timestamp);
Assert.AreEqual(MigrationDirection.Up, applicableMigrations.First().Metadata.Direction);
Assert.AreEqual(1, unidentifiedMigrations.Count());
Assert.AreEqual(13, unidentifiedMigrations.First().Timestamp);
}

[Test, ExpectedException(typeof(InvalidMigrationExportException), ExpectedMessage = "The migration with timestamp 1 and module name '" + MigrationExportAttribute.DefaultModuleName + "' is defined more than once.")]
public void TestDuplicateMigrationsThrowInvalidMigrationException()
{
var migration = MockRepository.GenerateStub<IMigration>();
ImportedMigration[] importedMigrations = {
new ImportedMigration(migration, new MigrationMetadata(1, DefaultModuleName, null)),
new ImportedMigration(migration, new MigrationMetadata(1, DefaultModuleName, null)),
};
IMigrationMetadata[] executedMigrations = {
};

var selector = new MigrationSelector(importedMigrations, executedMigrations);
Assert.IsNotNull(selector, "Just to satisfy R# and FxCop.");
}
}
}
10 changes: 8 additions & 2 deletions MigSharp/IMigrationBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ public interface IMigrationBatch
ReadOnlyCollection<IMigrationMetadata> UnidentifiedMigrations { get; }

/// <summary>
/// Performs the migrations contained in this batch.
/// Performs the migrations contained in this batch. This method can only be
/// called once (when IsExecuted is false).
/// </summary>
void Execute(); // FIXME: dr, 12.1, prevent multiple calls to Execute?
void Execute();

/// <summary>
/// Indicates if <see cref="Execute"/> was already called.
/// </summary>
bool IsExecuted { get; }
}
}
Loading

0 comments on commit 33c0c0d

Please sign in to comment.