diff --git a/src/NzbDrone.Core.Test/Datastore/Migration/070_delay_profileFixture.cs b/src/NzbDrone.Core.Test/Datastore/Migration/070_delay_profileFixture.cs new file mode 100644 index 000000000..7ade7e918 --- /dev/null +++ b/src/NzbDrone.Core.Test/Datastore/Migration/070_delay_profileFixture.cs @@ -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 + { + [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().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); + + } + } + +} \ No newline at end of file diff --git a/src/NzbDrone.Core.Test/Framework/DbTest.cs b/src/NzbDrone.Core.Test/Framework/DbTest.cs index 436368db3..3e81b68ad 100644 --- a/src/NzbDrone.Core.Test/Framework/DbTest.cs +++ b/src/NzbDrone.Core.Test/Framework/DbTest.cs @@ -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 @@ namespace NzbDrone.Core.Test.Framework { - public abstract class DbTest : DbTest where TSubject : class where TModel : ModelBase, new() @@ -85,7 +85,19 @@ protected ITestDatabase Db } } - private void WithTestDb() + protected virtual TestDatabase WithTestDb(Action beforeMigration) + { + var factory = Mocker.Resolve(); + var database = factory.Create(MigrationType); + Mocker.SetConstant(database); + + var testDb = new TestDatabase(database); + + return testDb; + } + + + protected void SetupContainer() { WithTempAsAppPath(); @@ -94,17 +106,13 @@ private void WithTestDb() Mocker.SetConstant(Mocker.Resolve()); MapRepository.Instance.EnableTraceLogging = true; - - var factory = Mocker.Resolve(); - 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] diff --git a/src/NzbDrone.Core.Test/Framework/MigrationTest.cs b/src/NzbDrone.Core.Test/Framework/MigrationTest.cs new file mode 100644 index 000000000..137f41983 --- /dev/null +++ b/src/NzbDrone.Core.Test/Framework/MigrationTest.cs @@ -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 : DbTest where TMigration : MigrationBase + { + protected override TestDatabase WithTestDb(Action beforeMigration) + { + var factory = Mocker.Resolve(); + + 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(); + } + } +} \ No newline at end of file diff --git a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 6f4bb2718..221b61979 100644 --- a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -116,6 +116,7 @@ + @@ -157,6 +158,7 @@ + diff --git a/src/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneMigrationBase.cs b/src/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneMigrationBase.cs index 2838adac4..a2144f0ec 100644 --- a/src/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneMigrationBase.cs +++ b/src/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneMigrationBase.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 @@ public MigrationContext Context { get { + if (_migrationContext == null) + { + _migrationContext = (MigrationContext)ApplicationContext; + } return _migrationContext; } }