2013-02-04 01:10:15 +01:00
|
|
|
|
using System;
|
2013-02-23 20:38:25 +01:00
|
|
|
|
using System.Collections.Generic;
|
2013-03-25 04:51:32 +01:00
|
|
|
|
using System.IO;
|
2013-02-04 01:10:15 +01:00
|
|
|
|
using System.Linq;
|
2013-03-25 04:51:32 +01:00
|
|
|
|
using Marr.Data;
|
2013-02-04 01:10:15 +01:00
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using NzbDrone.Core.Datastore;
|
2013-03-25 07:13:53 +01:00
|
|
|
|
using NzbDrone.Core.Datastore.Migration.Framework;
|
2013-02-04 01:10:15 +01:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Test.Framework
|
|
|
|
|
{
|
2013-03-24 05:16:00 +01:00
|
|
|
|
public abstract class DbTest<TSubject, TModel> : DbTest
|
2013-02-23 20:38:25 +01:00
|
|
|
|
where TSubject : class
|
2013-02-18 08:59:43 +01:00
|
|
|
|
where TModel : ModelBase, new()
|
|
|
|
|
{
|
2013-02-23 20:38:25 +01:00
|
|
|
|
private TSubject _subject;
|
2013-02-18 08:59:43 +01:00
|
|
|
|
|
|
|
|
|
protected BasicRepository<TModel> Storage { get; private set; }
|
|
|
|
|
|
2013-02-23 20:38:25 +01:00
|
|
|
|
protected IList<TModel> AllStoredModels
|
2013-02-18 08:59:43 +01:00
|
|
|
|
{
|
2013-02-23 20:38:25 +01:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Storage.All().ToList();
|
|
|
|
|
}
|
2013-02-18 08:59:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-23 20:38:25 +01:00
|
|
|
|
protected TModel StoredModel
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Storage.All().Single();
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-16 04:50:22 +01:00
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void CoreTestSetup()
|
|
|
|
|
{
|
|
|
|
|
_subject = null;
|
2013-02-23 20:38:25 +01:00
|
|
|
|
Storage = Mocker.Resolve<BasicRepository<TModel>>();
|
2013-02-16 04:50:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected TSubject Subject
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_subject == null)
|
|
|
|
|
{
|
|
|
|
|
_subject = Mocker.Resolve<TSubject>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _subject;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 05:16:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public abstract class DbTest : CoreTest
|
2013-02-04 01:10:15 +01:00
|
|
|
|
{
|
2013-02-16 04:50:22 +01:00
|
|
|
|
|
2013-03-25 04:51:32 +01:00
|
|
|
|
private string _dbName;
|
|
|
|
|
|
|
|
|
|
private ITestDatabase _db;
|
|
|
|
|
private IDatabase _database;
|
|
|
|
|
|
2013-03-27 05:03:02 +01:00
|
|
|
|
|
|
|
|
|
protected virtual MigrationType MigrationType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return MigrationType.Main;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-25 04:51:32 +01:00
|
|
|
|
protected ITestDatabase Db
|
2013-02-04 01:10:15 +01:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_db == null)
|
|
|
|
|
throw new InvalidOperationException("Test object database doesn't exists. Make sure you call WithRealDb() if you intend to use an actual database.");
|
|
|
|
|
|
|
|
|
|
return _db;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-23 20:38:25 +01:00
|
|
|
|
private void WithObjectDb(bool memory = true)
|
2013-02-04 01:10:15 +01:00
|
|
|
|
{
|
2013-03-25 04:51:32 +01:00
|
|
|
|
|
|
|
|
|
_dbName = DateTime.Now.Ticks.ToString() + ".db";
|
|
|
|
|
|
|
|
|
|
MapRepository.Instance.EnableTraceLogging = true;
|
|
|
|
|
|
2013-04-01 08:49:46 +02:00
|
|
|
|
var factory = new DbFactory(new MigrationController(new MigrationLogger(TestLogger)));
|
2013-03-27 05:03:02 +01:00
|
|
|
|
_database = factory.Create(_dbName, MigrationType);
|
2013-03-25 04:51:32 +01:00
|
|
|
|
_db = new TestTestDatabase(_database);
|
|
|
|
|
Mocker.SetConstant(_database);
|
2013-02-04 01:10:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-23 20:38:25 +01:00
|
|
|
|
[SetUp]
|
|
|
|
|
public void SetupReadDb()
|
|
|
|
|
{
|
|
|
|
|
WithObjectDb();
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-25 04:51:32 +01:00
|
|
|
|
[TearDown]
|
|
|
|
|
public void TearDown()
|
|
|
|
|
{
|
|
|
|
|
var files = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.db");
|
|
|
|
|
|
|
|
|
|
foreach (var file in files)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
File.Delete(file);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-03-24 05:16:00 +01:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-03-25 04:51:32 +01:00
|
|
|
|
public interface ITestDatabase
|
2013-03-24 05:16:00 +01:00
|
|
|
|
{
|
2013-03-25 04:51:32 +01:00
|
|
|
|
void InsertMany<T>(IEnumerable<T> items) where T : ModelBase, new();
|
2013-04-11 06:38:45 +02:00
|
|
|
|
T Insert<T>(T item) where T : ModelBase, new();
|
2013-03-27 01:51:37 +01:00
|
|
|
|
List<T> All<T>() where T : ModelBase, new();
|
2013-03-26 06:51:56 +01:00
|
|
|
|
T Single<T>() where T : ModelBase, new();
|
2013-03-25 04:51:32 +01:00
|
|
|
|
void Update<T>(T childModel) where T : ModelBase, new();
|
|
|
|
|
void Delete<T>(T childModel) where T : ModelBase, new();
|
2013-03-24 05:16:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-25 04:51:32 +01:00
|
|
|
|
public class TestTestDatabase : ITestDatabase
|
2013-03-24 05:16:00 +01:00
|
|
|
|
{
|
2013-03-25 04:51:32 +01:00
|
|
|
|
private readonly IDatabase _dbConnection;
|
2013-03-24 05:16:00 +01:00
|
|
|
|
|
2013-03-25 04:51:32 +01:00
|
|
|
|
public TestTestDatabase(IDatabase dbConnection)
|
2013-02-04 01:10:15 +01:00
|
|
|
|
{
|
2013-03-24 05:16:00 +01:00
|
|
|
|
_dbConnection = dbConnection;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-25 04:51:32 +01:00
|
|
|
|
public void InsertMany<T>(IEnumerable<T> items) where T : ModelBase, new()
|
2013-03-24 05:16:00 +01:00
|
|
|
|
{
|
2013-03-25 04:51:32 +01:00
|
|
|
|
new BasicRepository<T>(_dbConnection).InsertMany(items.ToList());
|
2013-03-24 05:16:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-11 06:38:45 +02:00
|
|
|
|
public T Insert<T>(T item) where T : ModelBase, new()
|
2013-03-24 05:16:00 +01:00
|
|
|
|
{
|
2013-04-11 06:38:45 +02:00
|
|
|
|
return new BasicRepository<T>(_dbConnection).Insert(item);
|
2013-03-24 05:16:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-27 01:51:37 +01:00
|
|
|
|
public List<T> All<T>() where T : ModelBase, new()
|
2013-03-24 05:16:00 +01:00
|
|
|
|
{
|
2013-03-27 01:51:37 +01:00
|
|
|
|
return new BasicRepository<T>(_dbConnection).All().ToList();
|
2013-03-24 05:16:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-26 06:51:56 +01:00
|
|
|
|
public T Single<T>() where T : ModelBase, new()
|
|
|
|
|
{
|
|
|
|
|
return All<T>().SingleOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-25 04:51:32 +01:00
|
|
|
|
public void Update<T>(T childModel) where T : ModelBase, new()
|
2013-03-24 05:16:00 +01:00
|
|
|
|
{
|
2013-03-25 04:51:32 +01:00
|
|
|
|
new BasicRepository<T>(_dbConnection).Update(childModel);
|
2013-03-24 05:16:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-25 04:51:32 +01:00
|
|
|
|
public void Delete<T>(T childModel) where T : ModelBase, new()
|
2013-03-24 05:16:00 +01:00
|
|
|
|
{
|
2013-03-25 04:51:32 +01:00
|
|
|
|
new BasicRepository<T>(_dbConnection).Delete(childModel);
|
2013-02-04 01:10:15 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|