using System.Collections.Generic; using System.Data; using System.Linq; using Moq; using NzbDrone.Core.Datastore; using NzbDrone.Core.Messaging.Events; namespace NzbDrone.Core.Test.Framework { public interface ITestDatabase { void InsertMany(IEnumerable items) where T : ModelBase, new(); T Insert(T item) where T : ModelBase, new(); List All() where T : ModelBase, new(); T Single() where T : ModelBase, new(); void Update(T childModel) where T : ModelBase, new(); void Delete(T childModel) where T : ModelBase, new(); IDirectDataMapper GetDirectDataMapper(); IDbConnection OpenConnection(); DatabaseType DatabaseType { get; } } public class TestDatabase : ITestDatabase { private readonly IDatabase _dbConnection; private readonly IEventAggregator _eventAggregator; public DatabaseType DatabaseType => _dbConnection.DatabaseType; public TestDatabase(IDatabase dbConnection) { _eventAggregator = new Mock().Object; _dbConnection = dbConnection; } public void InsertMany(IEnumerable items) where T : ModelBase, new() { new BasicRepository(_dbConnection, _eventAggregator).InsertMany(items.ToList()); } public T Insert(T item) where T : ModelBase, new() { return new BasicRepository(_dbConnection, _eventAggregator).Insert(item); } public List All() where T : ModelBase, new() { return new BasicRepository(_dbConnection, _eventAggregator).All().ToList(); } public T Single() where T : ModelBase, new() { return All().SingleOrDefault(); } public void Update(T childModel) where T : ModelBase, new() { new BasicRepository(_dbConnection, _eventAggregator).Update(childModel); } public void Delete(T childModel) where T : ModelBase, new() { new BasicRepository(_dbConnection, _eventAggregator).Delete(childModel); } public IDirectDataMapper GetDirectDataMapper() { return new DirectDataMapper(_dbConnection); } public IDbConnection OpenConnection() { return _dbConnection.OpenConnection(); } } }