Our first data migration test :D

pull/2/head
Keivan Beigi 10 years ago committed by Mark McDowall
parent c7ed76f6d3
commit be81bf322a

@ -0,0 +1,52 @@
using System;
using System.Linq;
using FluentAssertions;
using FluentMigrator;
using NUnit.Framework;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Datastore.Migration;
using NzbDrone.Core.Datastore.Migration.Framework;
using NzbDrone.Core.Indexers;
using NzbDrone.Core.Profiles.Delay;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.Datastore.Migration
{
[TestFixture]
public class delay_profileFixture : MigrationTest<delay_profile>
{
[TestCase]
public void should_migrate_old_delays()
{
WithTestDb(c =>
{
c.Insert.IntoTable("Profiles").Row(new
{
GrabDelay = 1,
Name = "OneHour",
Cutoff = "{}",
Items = "{}"
});
c.Insert.IntoTable("Profiles").Row(new
{
GrabDelay = 2,
Name = "TwoHours",
Cutoff = "{}",
Items = "[]"
});
});
var allProfiles = Mocker.Resolve<DelayProfileRepository>().All().ToList();
allProfiles.Should().HaveCount(3);
allProfiles.Should().OnlyContain(c => c.PreferredProtocol == DownloadProtocol.Usenet);
allProfiles.Should().OnlyContain(c => c.TorrentDelay == 0);
allProfiles.Should().Contain(c => c.UsenetDelay == 60);
allProfiles.Should().Contain(c => c.UsenetDelay == 120);
}
}
}

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using FluentMigrator;
using FluentMigrator.Runner;
using Marr.Data;
using Moq;
@ -13,7 +14,6 @@ using NzbDrone.Core.Messaging.Events;
namespace NzbDrone.Core.Test.Framework
{
public abstract class DbTest<TSubject, TModel> : DbTest
where TSubject : class
where TModel : ModelBase, new()
@ -85,7 +85,19 @@ namespace NzbDrone.Core.Test.Framework
}
}
private void WithTestDb()
protected virtual TestDatabase WithTestDb(Action<MigrationBase> beforeMigration)
{
var factory = Mocker.Resolve<DbFactory>();
var database = factory.Create(MigrationType);
Mocker.SetConstant(database);
var testDb = new TestDatabase(database);
return testDb;
}
protected void SetupContainer()
{
WithTempAsAppPath();
@ -94,17 +106,13 @@ namespace NzbDrone.Core.Test.Framework
Mocker.SetConstant<IMigrationController>(Mocker.Resolve<MigrationController>());
MapRepository.Instance.EnableTraceLogging = true;
var factory = Mocker.Resolve<DbFactory>();
var database = factory.Create(MigrationType);
_db = new TestDatabase(database);
Mocker.SetConstant(database);
}
[SetUp]
public void SetupReadDb()
public virtual void SetupDb()
{
WithTestDb();
SetupContainer();
_db = WithTestDb(null);
}
[TearDown]

@ -0,0 +1,36 @@
using System;
using FluentMigrator;
using NUnit.Framework;
using NzbDrone.Core.Datastore;
namespace NzbDrone.Core.Test.Framework
{
[Category("DbMigrationTest")]
[Category("DbTest")]
public abstract class MigrationTest<TMigration> : DbTest where TMigration : MigrationBase
{
protected override TestDatabase WithTestDb(Action<MigrationBase> beforeMigration)
{
var factory = Mocker.Resolve<DbFactory>();
var database = factory.Create(MigrationType, m =>
{
if (m.GetType() == typeof(TMigration))
{
beforeMigration(m);
}
});
var testDb = new TestDatabase(database);
Mocker.SetConstant(database);
return testDb;
}
[SetUp]
public override void SetupDb()
{
SetupContainer();
}
}
}

@ -116,6 +116,7 @@
<Compile Include="Datastore\DatabaseRelationshipFixture.cs" />
<Compile Include="Datastore\MappingExtentionFixture.cs" />
<Compile Include="Datastore\MarrDataLazyLoadingFixture.cs" />
<Compile Include="Datastore\Migration\070_delay_profileFixture.cs" />
<Compile Include="Datastore\ObjectDatabaseFixture.cs" />
<Compile Include="Datastore\PagingSpecExtensionsTests\PagingOffsetFixture.cs" />
<Compile Include="Datastore\PagingSpecExtensionsTests\ToSortDirectionFixture.cs" />
@ -157,6 +158,7 @@
<Compile Include="FluentTest.cs" />
<Compile Include="Framework\CoreTest.cs" />
<Compile Include="Framework\DbTest.cs" />
<Compile Include="Framework\MigrationTest.cs" />
<Compile Include="Framework\NBuilderExtensions.cs" />
<Compile Include="Framework\TestDbHelper.cs" />
<Compile Include="HealthCheck\Checks\AppDataLocationFixture.cs" />

@ -8,12 +8,11 @@ namespace NzbDrone.Core.Datastore.Migration.Framework
public abstract class NzbDroneMigrationBase : FluentMigrator.Migration
{
protected readonly Logger _logger;
private readonly MigrationContext _migrationContext;
private MigrationContext _migrationContext;
protected NzbDroneMigrationBase()
{
_logger = NzbDroneLogger.GetLogger(this);
_migrationContext = (MigrationContext)ApplicationContext;
}
protected virtual void MainDbUpgrade()
@ -37,6 +36,10 @@ namespace NzbDrone.Core.Datastore.Migration.Framework
{
get
{
if (_migrationContext == null)
{
_migrationContext = (MigrationContext)ApplicationContext;
}
return _migrationContext;
}
}

Loading…
Cancel
Save