parent
927ae86e44
commit
337d01e4ed
@ -0,0 +1,55 @@
|
|||||||
|
using FizzWare.NBuilder;
|
||||||
|
using FluentAssertions;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using NzbDrone.Core.Housekeeping.Housekeepers;
|
||||||
|
using NzbDrone.Core.ImportLists;
|
||||||
|
using NzbDrone.Core.ImportLists.ImportListMovies;
|
||||||
|
using NzbDrone.Core.Test.Framework;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Test.Housekeeping.Housekeepers
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class CleanupOrphanedImportListMoviesFixture : DbTest<CleanupOrphanedImportListMovies, ImportListMovie>
|
||||||
|
{
|
||||||
|
private ImportListDefinition _importList;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void Setup()
|
||||||
|
{
|
||||||
|
_importList = Builder<ImportListDefinition>.CreateNew()
|
||||||
|
.BuildNew();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenImportList()
|
||||||
|
{
|
||||||
|
Db.Insert(_importList);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_delete_orphaned_importlistmovies()
|
||||||
|
{
|
||||||
|
var status = Builder<ImportListMovie>.CreateNew()
|
||||||
|
.With(h => h.ListId = _importList.Id)
|
||||||
|
.BuildNew();
|
||||||
|
Db.Insert(status);
|
||||||
|
|
||||||
|
Subject.Clean();
|
||||||
|
AllStoredModels.Should().BeEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_not_delete_unorphaned_importlistmovies()
|
||||||
|
{
|
||||||
|
GivenImportList();
|
||||||
|
|
||||||
|
var status = Builder<ImportListMovie>.CreateNew()
|
||||||
|
.With(h => h.ListId = _importList.Id)
|
||||||
|
.BuildNew();
|
||||||
|
Db.Insert(status);
|
||||||
|
|
||||||
|
Subject.Clean();
|
||||||
|
AllStoredModels.Should().HaveCount(1);
|
||||||
|
AllStoredModels.Should().Contain(h => h.ListId == _importList.Id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
using Dapper;
|
||||||
|
using NzbDrone.Core.Datastore;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Housekeeping.Housekeepers
|
||||||
|
{
|
||||||
|
public class CleanupOrphanedImportListMovies : IHousekeepingTask
|
||||||
|
{
|
||||||
|
private readonly IMainDatabase _database;
|
||||||
|
|
||||||
|
public CleanupOrphanedImportListMovies(IMainDatabase database)
|
||||||
|
{
|
||||||
|
_database = database;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clean()
|
||||||
|
{
|
||||||
|
using var mapper = _database.OpenConnection();
|
||||||
|
|
||||||
|
mapper.Execute(@"DELETE FROM ""ImportListMovies""
|
||||||
|
WHERE ""Id"" IN (
|
||||||
|
SELECT ""ImportListMovies"".""Id"" FROM ""ImportListMovies""
|
||||||
|
LEFT OUTER JOIN ""ImportLists""
|
||||||
|
ON ""ImportListMovies"".""ListId"" = ""ImportLists"".""Id""
|
||||||
|
WHERE ""ImportLists"".""Id"" IS NULL)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue