cleaned up CoreTest base class.

pull/2/head
kay.one 12 years ago
parent 77bb790672
commit 64a3e1caf0

@ -1,7 +1,7 @@
using System;
using System.IO;
using NUnit.Framework;
using NzbDrone.Common;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Test.Common;
@ -11,30 +11,46 @@ namespace NzbDrone.Core.Test.Framework
{
public class CoreTest : TestBase
{
static CoreTest()
private string _dbTemplateName;
[SetUp]
public void CoreTestSetup()
{
_dbTemplateName = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()) + ".sdf";
CreateDataBaseTemplate();
}
private IDatabase GetEmptyDatabase(string fileName = "")
{
//Delete old db files
var oldDbFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.sdf", SearchOption.AllDirectories);
foreach (var file in oldDbFiles)
Console.WriteLine("====================DataBase====================");
Console.WriteLine("Cloning database from template.");
if (String.IsNullOrWhiteSpace(fileName))
{
try
{
File.Delete(file);
}
catch { }
fileName = Guid.NewGuid() + ".sdf";
}
/* //Delete App_data folder
var appData = new EnvironmentProvider().GetAppDataPath();
File.Copy(_dbTemplateName, fileName);
if (Directory.Exists(appData))
{
//Directory.Delete(appData, true);
}*/
var connectionString = ConnectionFactory.GetConnectionString(fileName);
var database = ConnectionFactory.GetPetaPocoDb(connectionString);
Console.WriteLine("====================DataBase====================");
Console.WriteLine();
Console.WriteLine();
TestDbHelper.CreateDataBaseTemplate();
return database;
}
private void CreateDataBaseTemplate()
{
Console.WriteLine("Creating an empty PetaPoco database");
var connectionString = ConnectionFactory.GetConnectionString(_dbTemplateName);
var database = ConnectionFactory.GetPetaPocoDb(connectionString);
database.Dispose();
}
private IDatabase _db;
protected IDatabase Db
@ -50,7 +66,7 @@ namespace NzbDrone.Core.Test.Framework
protected void WithRealDb()
{
_db = TestDbHelper.GetEmptyDatabase();
_db = GetEmptyDatabase();
Mocker.SetConstant(Db);
}
@ -71,6 +87,18 @@ namespace NzbDrone.Core.Test.Framework
public void CoreTestTearDown()
{
ConfigProvider.ClearCache();
if (_db != null && _db.Connection != null && File.Exists(_db.Connection.Database))
{
var file = _db.Connection.Database;
_db.Dispose();
try
{
File.Delete(file);
}
catch (IOException) { }
}
}
}
}

@ -16,44 +16,6 @@ namespace NzbDrone.Core.Test.Framework
{
internal static class TestDbHelper
{
private static readonly string dbTemplateName;
static TestDbHelper()
{
dbTemplateName = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()) + ".sdf";
}
internal static string ConnectionString { get; private set; }
internal static IDatabase GetEmptyDatabase(string fileName = "")
{
Console.WriteLine("====================DataBase====================");
Console.WriteLine("Cloning database from template.");
if (String.IsNullOrWhiteSpace(fileName))
{
fileName = Guid.NewGuid() + ".sdf";
}
File.Copy(dbTemplateName, fileName);
ConnectionString = ConnectionFactory.GetConnectionString(fileName);
var database = ConnectionFactory.GetPetaPocoDb(ConnectionString);
Console.WriteLine("====================DataBase====================");
Console.WriteLine();
Console.WriteLine();
return database;
}
internal static void CreateDataBaseTemplate()
{
Console.WriteLine("Creating an empty PetaPoco database");
var connectionString = ConnectionFactory.GetConnectionString(dbTemplateName);
var database = ConnectionFactory.GetPetaPocoDb(connectionString);
database.Dispose();
}
}
}

@ -27,7 +27,7 @@ namespace NzbDrone.Core.Test.ProviderTests.EpisodeProviderTests
All()
.With(l => l.Language = new TvdbLanguage(0, "eng", "a"))
.Build();
var fakeSeries = Builder<Series>.CreateNew()
.With(c => c.SeriesId = seriesId)
@ -38,19 +38,18 @@ namespace NzbDrone.Core.Test.ProviderTests.EpisodeProviderTests
.With(e => e.TvDbEpisodeId = tvDbSeries.First().Id)
.Build();
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
db.Insert(fakeSeries);
db.Insert(fakeEpisode);
WithRealDb();
Db.Insert(fakeSeries);
Db.Insert(fakeEpisode);
//Act
Mocker.Resolve<EpisodeProvider>().DeleteEpisodesNotInTvdb(fakeSeries, tvDbSeries);
//Assert
var result = db.Fetch<Episode>();
var result = Db.Fetch<Episode>();
result.Should().HaveCount(1);
}
@ -75,19 +74,18 @@ namespace NzbDrone.Core.Test.ProviderTests.EpisodeProviderTests
.With(e => e.TvDbEpisodeId = 0)
.Build();
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
db.Insert(fakeSeries);
db.Insert(fakeEpisode);
WithRealDb();
Db.Insert(fakeSeries);
Db.Insert(fakeEpisode);
//Act
Mocker.Resolve<EpisodeProvider>().DeleteEpisodesNotInTvdb(fakeSeries, tvDbSeries);
//Assert
var result = db.Fetch<Episode>();
var result = Db.Fetch<Episode>();
result.Should().HaveCount(1);
}
@ -112,19 +110,16 @@ namespace NzbDrone.Core.Test.ProviderTests.EpisodeProviderTests
.With(e => e.TvDbEpisodeId = null)
.Build();
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
WithRealDb();
db.Insert(fakeSeries);
db.Insert(fakeEpisode);
Db.Insert(fakeSeries);
Db.Insert(fakeEpisode);
//Act
Mocker.Resolve<EpisodeProvider>().DeleteEpisodesNotInTvdb(fakeSeries, tvDbSeries);
//Assert
var result = db.Fetch<Episode>();
var result = Db.Fetch<Episode>();
result.Should().HaveCount(1);
}
@ -151,19 +146,18 @@ namespace NzbDrone.Core.Test.ProviderTests.EpisodeProviderTests
.With(e => e.TvDbEpisodeId = 300)
.Build();
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
db.Insert(fakeSeries);
db.Insert(fakeEpisode);
WithRealDb();
Db.Insert(fakeSeries);
Db.Insert(fakeEpisode);
//Act
Mocker.Resolve<EpisodeProvider>().DeleteEpisodesNotInTvdb(fakeSeries, tvDbSeries);
//Assert
var result = db.Fetch<Episode>();
var result = Db.Fetch<Episode>();
result.Should().HaveCount(0);
}
@ -203,24 +197,23 @@ namespace NzbDrone.Core.Test.ProviderTests.EpisodeProviderTests
.With(e => e.TvDbEpisodeId = 300)
.Build();
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
db.Insert(fakeSeries);
db.Insert(fakeEpisode);
db.Insert(otherFakeSeries);
db.Insert(otherFakeEpisode);
WithRealDb();
Db.Insert(fakeSeries);
Db.Insert(fakeEpisode);
Db.Insert(otherFakeSeries);
Db.Insert(otherFakeEpisode);
//Act
Mocker.Resolve<EpisodeProvider>().DeleteEpisodesNotInTvdb(fakeSeries, tvDbSeries);
//Assert
var result = db.Fetch<Episode>();
var result = Db.Fetch<Episode>();
result.Should().HaveCount(1);
}
[Test]
public void should_not_do_anything_if_episode_list_is_empty()
{

@ -24,9 +24,7 @@ namespace NzbDrone.Core.Test.ProviderTests
[Test]
public void Init_indexer_test()
{
Mocker.SetConstant(TestDbHelper.GetEmptyDatabase());
WithRealDb();
Mocker.SetConstant<IEnumerable<IndexerBase>>(new List<IndexerBase> { Mocker.Resolve<MockIndexer>() });
@ -46,7 +44,7 @@ namespace NzbDrone.Core.Test.ProviderTests
[Test]
public void Init_indexer_with_disabled_job()
{
Mocker.SetConstant(TestDbHelper.GetEmptyDatabase());
WithRealDb();
Mocker.SetConstant<IEnumerable<IndexerBase>>(new List<IndexerBase> { Mocker.Resolve<MockIndexer>() });
@ -65,7 +63,7 @@ namespace NzbDrone.Core.Test.ProviderTests
[Test]
public void Init_indexer_should_enable_indexer_that_is_enabled_by_default()
{
Mocker.SetConstant(TestDbHelper.GetEmptyDatabase());
WithRealDb();
Mocker.SetConstant<IEnumerable<IndexerBase>>(new List<IndexerBase> { Mocker.Resolve<DefaultEnabledIndexer>() });
@ -82,7 +80,7 @@ namespace NzbDrone.Core.Test.ProviderTests
[Test]
public void Init_indexer_should_not_enable_indexer_that_is_not_enabled_by_default()
{
Mocker.SetConstant(TestDbHelper.GetEmptyDatabase());
WithRealDb();
Mocker.SetConstant<IEnumerable<IndexerBase>>(new List<IndexerBase> { Mocker.Resolve<MockIndexer>() });

@ -37,13 +37,11 @@ namespace NzbDrone.Core.Test.ProviderTests
var database = TestDbHelper.GetEmptyDatabase();
WithRealDb();
database.InsertMany(firstSeriesFiles);
database.InsertMany(secondSeriesFiles);
Mocker.SetConstant(database);
Db.InsertMany(firstSeriesFiles);
Db.InsertMany(secondSeriesFiles);
var result = Mocker.Resolve<MediaFileProvider>().GetSeriesFiles(12);
@ -69,13 +67,10 @@ namespace NzbDrone.Core.Test.ProviderTests
.Build();
WithRealDb();
var database = TestDbHelper.GetEmptyDatabase();
database.InsertMany(firstSeriesFiles);
database.InsertMany(secondSeriesFiles);
Mocker.SetConstant(database);
Db.InsertMany(firstSeriesFiles);
Db.InsertMany(secondSeriesFiles);
var result = Mocker.Resolve<MediaFileProvider>().GetSeasonFiles(12, 1);
@ -157,13 +152,12 @@ namespace NzbDrone.Core.Test.ProviderTests
.Build();
var database = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(database);
database.InsertMany(episodeFiles);
WithRealDb();
Db.InsertMany(episodeFiles);
//Act
Mocker.Resolve<MediaFileProvider>().Delete(1);
var result = database.Fetch<EpisodeFile>();
var result = Db.Fetch<EpisodeFile>();
//Assert
result.Should().HaveCount(9);

@ -16,14 +16,15 @@ namespace NzbDrone.Core.Test.ProviderTests
// ReSharper disable InconsistentNaming
public class QualityTypeProviderTest : CoreTest
{
[SetUp]
public void SetuUp()
{
WithRealDb();
}
[Test]
public void SetupDefault_should_add_all_profiles()
{
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
Mocker.Resolve<QualityTypeProvider>();
@ -45,11 +46,8 @@ namespace NzbDrone.Core.Test.ProviderTests
[Test]
public void SetupDefault_already_exists_should_insert_missing()
{
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
db.Insert(new QualityType { QualityTypeId = 1, Name = "SDTV", MinSize = 0, MaxSize = 100 });
Db.Insert(new QualityType { QualityTypeId = 1, Name = "SDTV", MinSize = 0, MaxSize = 100 });
Mocker.Resolve<QualityTypeProvider>();
@ -63,16 +61,12 @@ namespace NzbDrone.Core.Test.ProviderTests
[Test]
public void GetList_single_quality_type()
{
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
var fakeQualityTypes = Builder<QualityType>.CreateListOfSize(6)
.Build();
var ids = new List<int> { 1 };
db.InsertMany(fakeQualityTypes);
Db.InsertMany(fakeQualityTypes);
var result = Mocker.Resolve<QualityTypeProvider>().GetList(ids);
@ -84,16 +78,12 @@ namespace NzbDrone.Core.Test.ProviderTests
[Test]
public void GetList_multiple_quality_type()
{
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
var fakeQualityTypes = Builder<QualityType>.CreateListOfSize(6)
.Build();
var ids = new List<int> { 1, 2 };
db.InsertMany(fakeQualityTypes);
Db.InsertMany(fakeQualityTypes);
var result = Mocker.Resolve<QualityTypeProvider>().GetList(ids);

@ -25,6 +25,8 @@ namespace NzbDrone.Core.Test.ProviderTests
{
Mocker.GetMock<ConfigProvider>().SetupGet(s => s.ServiceRootUrl)
.Returns("http://services.nzbdrone.com");
WithRealDb();
}
private void WithValidJson()
@ -75,6 +77,7 @@ namespace NzbDrone.Core.Test.ProviderTests
.With(f => f.SceneName = "laworder")
.Build();
Db.Insert(fakeMap);
//Act
@ -96,6 +99,7 @@ namespace NzbDrone.Core.Test.ProviderTests
.With(f => f.SceneName = "laworder")
.Build();
Db.Insert(fakeMap);
//Act
@ -147,6 +151,8 @@ namespace NzbDrone.Core.Test.ProviderTests
.With(f => f.SeasonNumber = -1)
.Build();
Db.Insert(fakeMap);
Db.Insert(fakeMap2);
@ -177,8 +183,6 @@ namespace NzbDrone.Core.Test.ProviderTests
[Test]
public void UpdateMappings_should_add_all_mappings_to_database()
{
//Setup
WithRealDb();
WithValidJson();
//Act
@ -200,7 +204,6 @@ namespace NzbDrone.Core.Test.ProviderTests
.With(f => f.SceneName = "laworder")
.Build();
WithRealDb();
WithValidJson();
Db.Insert(fakeMap);
@ -223,7 +226,6 @@ namespace NzbDrone.Core.Test.ProviderTests
.With(f => f.SceneName = "laworder")
.Build();
WithRealDb();
WithErrorDownloadingJson();
Db.Insert(fakeMap);
@ -246,7 +248,6 @@ namespace NzbDrone.Core.Test.ProviderTests
.With(f => f.SceneName = "laworder")
.Build();
WithRealDb();
Db.Insert(fakeMap);
//Act
@ -260,7 +261,6 @@ namespace NzbDrone.Core.Test.ProviderTests
public void UpdateIfEmpty_should_update_if_count_is_zero()
{
//Setup
WithRealDb();
WithValidJson();
//Act

@ -17,11 +17,16 @@ namespace NzbDrone.Core.Test
// ReSharper disable InconsistentNaming
public class QualityProfileTest : CoreTest<QualityProvider>
{
[SetUp]
public void SetUp()
{
WithRealDb();
}
[Test]
public void Test_Storage()
{
//Arrange
var database = TestDbHelper.GetEmptyDatabase();
var testProfile = new QualityProfile
{
Name = Guid.NewGuid().ToString(),
@ -30,8 +35,8 @@ namespace NzbDrone.Core.Test
};
var id = Convert.ToInt32(database.Insert(testProfile));
var fetch = database.SingleOrDefault<QualityProfile>(id);
var id = Convert.ToInt32(Db.Insert(testProfile));
var fetch = Db.SingleOrDefault<QualityProfile>(id);
Assert.AreEqual(id, fetch.QualityProfileId);
@ -45,7 +50,6 @@ namespace NzbDrone.Core.Test
public void Test_Storage_no_allowed()
{
//Arrange
var database = TestDbHelper.GetEmptyDatabase();
var testProfile = new QualityProfile
{
Name = Guid.NewGuid().ToString(),
@ -53,8 +57,8 @@ namespace NzbDrone.Core.Test
};
var id = Convert.ToInt32(database.Insert(testProfile));
var fetch = database.SingleOrDefault<QualityProfile>(id);
var id = Convert.ToInt32(Db.Insert(testProfile));
var fetch = Db.SingleOrDefault<QualityProfile>(id);
Assert.AreEqual(id, fetch.QualityProfileId);
@ -67,11 +71,6 @@ namespace NzbDrone.Core.Test
[Test]
public void Update_Success()
{
//Arrange
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
var testProfile = new QualityProfile
{
Name = Guid.NewGuid().ToString(),
@ -79,8 +78,8 @@ namespace NzbDrone.Core.Test
};
var id = Convert.ToInt32(db.Insert(testProfile));
var currentProfile = db.SingleOrDefault<QualityProfile>(id);
var id = Convert.ToInt32(Db.Insert(testProfile));
var currentProfile = Db.SingleOrDefault<QualityProfile>(id);
//Update
@ -99,9 +98,6 @@ namespace NzbDrone.Core.Test
[Test]
public void Test_Series_Quality()
{
//Arrange
var database = TestDbHelper.GetEmptyDatabase();
var testProfile = new QualityProfile
{
Name = Guid.NewGuid().ToString(),
@ -110,18 +106,18 @@ namespace NzbDrone.Core.Test
};
var profileId = Convert.ToInt32(database.Insert(testProfile));
var profileId = Convert.ToInt32(Db.Insert(testProfile));
var series = Builder<Series>.CreateNew().Build();
series.QualityProfileId = profileId;
database.Insert(testProfile);
database.Insert(series);
Db.Insert(testProfile);
Db.Insert(series);
var result = database.Fetch<Series>();
var result = Db.Fetch<Series>();
result.Should().HaveCount(1);
var profile = database.SingleOrDefault<QualityProfile>(result[0].QualityProfileId);
var profile = Db.SingleOrDefault<QualityProfile>(result[0].QualityProfileId);
Assert.AreEqual(profileId, result[0].QualityProfileId);
Assert.AreEqual(testProfile.Name, profile.Name);
}
@ -131,10 +127,6 @@ namespace NzbDrone.Core.Test
public void SetupInitial_should_add_two_profiles()
{
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
Mocker.Resolve<QualityProvider>();
@ -152,8 +144,6 @@ namespace NzbDrone.Core.Test
//We don't want to keep adding them back if a user deleted them on purpose.
public void SetupInitial_should_skip_if_any_profile_exists()
{
WithRealDb();
InitiateSubject();
var profiles = Subject.All();

@ -32,7 +32,8 @@ namespace NzbDrone.Core.Test
[TestFixtureSetUp]
public void Setup()
{
db = TestDbHelper.GetEmptyDatabase();
WithRealDb();
int currentFileId = 0;
@ -42,7 +43,7 @@ namespace NzbDrone.Core.Test
Allowed = new List<QualityTypes> { QualityTypes.DVD, QualityTypes.Bluray1080p },
Cutoff = QualityTypes.DVD
};
db.Insert(qulityProfile);
Db.Insert(qulityProfile);
foreach (var _seriesId in seriesIds)
{
@ -52,7 +53,7 @@ namespace NzbDrone.Core.Test
.With(s => s.Monitored = true)
.Build();
db.Insert(series);
Db.Insert(series);
foreach (var _seasonNumber in seasonsNumbers)
{
@ -94,8 +95,8 @@ namespace NzbDrone.Core.Test
}
db.InsertMany(episodes);
db.InsertMany(files);
Db.InsertMany(episodes);
Db.InsertMany(files);
}

@ -100,6 +100,13 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\MiniProfiler.2.0.2\lib\net40\MiniProfiler.dll</HintPath>
</Reference>
<Reference Include="Nancy, Version=0.15.3.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Nancy.0.15.3\lib\net40\Nancy.dll</HintPath>
</Reference>
<Reference Include="Nancy.Hosting.Aspnet">
<HintPath>..\packages\Nancy.Hosting.Aspnet.0.15.3\lib\net40\Nancy.Hosting.Aspnet.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll</HintPath>

@ -41,6 +41,7 @@
<remove name="UrlRoutingHandler" />
<add name="MvcHttpHandler" preCondition="integratedMode" verb="" path=".mvc" type="System.Web.Mvc.MvcHttpHandler" />
<add name="CassetteHttpHandler" path="cassette.axd" preCondition="integratedMode" verb="*" allowPathInfo="true" type="Cassette.Aspnet.CassetteHttpHandler, Cassette.Aspnet" />
<add name="Nancy" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" />
</handlers>
<directoryBrowse enabled="false" />
<staticContent>
@ -81,4 +82,22 @@
<add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
</DbProviderFactories>
</system.data>
<location path="api" allowOverride="false">
<system.web>
<customErrors mode="Off" />
</system.web>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Cache-Control" value="no-cache, no-store, must-revalidate" />
<add name="Pragma" value="no-cache" />
<add name="Expires" value="0" />
</customHeaders>
</httpProtocol>
<handlers>
<add name="Nancy" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" />
</handlers>
<httpErrors errorMode="Detailed" />
</system.webServer>
</location>
</configuration>

@ -26,6 +26,8 @@
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" />
<package id="MiniProfiler" version="2.0.2" />
<package id="MiniProfiler.MVC3" version="2.0.2" />
<package id="Nancy" version="0.15.3" targetFramework="net40" />
<package id="Nancy.Hosting.Aspnet" version="0.15.3" targetFramework="net40" />
<package id="Newtonsoft.Json" version="4.5.11" targetFramework="net40" />
<package id="NLog" version="2.0.0.2000" />
<package id="ServiceStack.Text" version="3.9.27" targetFramework="net40" />

Loading…
Cancel
Save