1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/NzbDrone.Core.Test/Framework/DbTest.cs

137 lines
3.1 KiB
C#
Raw Normal View History

2013-02-04 03:10:15 +03:00
using System;
2013-02-23 22:38:25 +03:00
using System.Collections.Generic;
2013-03-24 07:16:00 +03:00
using System.Data;
2013-02-04 03:10:15 +03:00
using System.Linq;
using NUnit.Framework;
2013-02-04 07:18:59 +03:00
using NzbDrone.Common;
2013-02-04 03:10:15 +03:00
using NzbDrone.Core.Datastore;
2013-03-24 07:16:00 +03:00
using ServiceStack.OrmLite;
2013-02-04 03:10:15 +03:00
namespace NzbDrone.Core.Test.Framework
{
2013-03-24 07:16:00 +03:00
public abstract class DbTest<TSubject, TModel> : DbTest
2013-02-23 22:38:25 +03:00
where TSubject : class
where TModel : ModelBase, new()
{
2013-02-23 22:38:25 +03:00
private TSubject _subject;
protected BasicRepository<TModel> Storage { get; private set; }
2013-02-23 22:38:25 +03:00
protected IList<TModel> AllStoredModels
{
2013-02-23 22:38:25 +03:00
get
{
return Storage.All().ToList();
}
}
2013-02-23 22:38:25 +03:00
protected TModel StoredModel
{
get
{
return Storage.All().Single();
}
}
[SetUp]
public void CoreTestSetup()
{
_subject = null;
2013-02-23 22:38:25 +03:00
Storage = Mocker.Resolve<BasicRepository<TModel>>();
}
protected TSubject Subject
{
get
{
if (_subject == null)
{
_subject = Mocker.Resolve<TSubject>();
}
return _subject;
}
}
}
2013-03-24 07:16:00 +03:00
public abstract class DbTest : CoreTest
2013-02-04 03:10:15 +03:00
{
2013-03-24 07:16:00 +03:00
private IDatabase _db;
protected IDatabase Db
2013-02-04 03:10:15 +03: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 22:38:25 +03:00
private void WithObjectDb(bool memory = true)
2013-02-04 03:10:15 +03:00
{
2013-03-24 07:56:59 +03:00
var factory = new DbFactory();
var dbConnection = factory.Create();
_db = new TestDatabase(dbConnection);
Mocker.SetConstant(dbConnection);
2013-02-04 03:10:15 +03:00
}
2013-02-23 22:38:25 +03:00
[SetUp]
public void SetupReadDb()
{
WithObjectDb();
}
2013-03-24 07:16:00 +03:00
}
public interface IDatabase
{
void InsertMany<T>(IEnumerable<T> items) where T : new();
void Insert<T>(T item) where T : new();
IEnumerable<T> All<T>() where T : new();
void Update<T>(T childModel) where T : new();
void Delete<T>(T childModel) where T : new();
}
public class TestDatabase : IDatabase
{
private readonly IDbConnection _dbConnection;
public TestDatabase(IDbConnection dbConnection)
2013-02-04 03:10:15 +03:00
{
2013-03-24 07:16:00 +03:00
_dbConnection = dbConnection;
}
public void InsertMany<T>(IEnumerable<T> items) where T : new()
{
_dbConnection.InsertAll(items);
}
public void Insert<T>(T item) where T : new()
{
_dbConnection.Insert(item);
}
public IEnumerable<T> All<T>() where T : new()
{
return _dbConnection.Select<T>();
}
public void Update<T>(T childModel) where T : new()
{
_dbConnection.Update(childModel);
}
public void Delete<T>(T childModel) where T : new()
{
_dbConnection.Delete(childModel);
2013-02-04 03:10:15 +03:00
}
}
}