| 
									
										
										
										
											2013-02-03 16:10:15 -08:00
										 |  |  | using System; | 
					
						
							| 
									
										
										
										
											2013-02-23 11:38:25 -08:00
										 |  |  | using System.Collections.Generic; | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  | using System.IO; | 
					
						
							| 
									
										
										
										
											2013-02-03 16:10:15 -08:00
										 |  |  | using System.Linq; | 
					
						
							| 
									
										
										
										
											2013-07-04 20:56:27 -07:00
										 |  |  | using FluentMigrator.Runner; | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  | using Marr.Data; | 
					
						
							| 
									
										
										
										
											2013-05-05 14:24:33 -07:00
										 |  |  | using Moq; | 
					
						
							| 
									
										
										
										
											2013-02-03 16:10:15 -08:00
										 |  |  | using NUnit.Framework; | 
					
						
							| 
									
										
										
										
											2013-05-05 14:24:33 -07:00
										 |  |  | using NzbDrone.Common.Messaging; | 
					
						
							| 
									
										
										
										
											2013-02-03 16:10:15 -08:00
										 |  |  | using NzbDrone.Core.Datastore; | 
					
						
							| 
									
										
										
										
											2013-03-24 23:13:53 -07:00
										 |  |  | using NzbDrone.Core.Datastore.Migration.Framework; | 
					
						
							| 
									
										
										
										
											2013-02-03 16:10:15 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace NzbDrone.Core.Test.Framework | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-06-02 20:15:56 -07:00
										 |  |  |     [Category("DbTest")] | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |     public abstract class DbTest<TSubject, TModel> : DbTest | 
					
						
							| 
									
										
										
										
											2013-02-23 11:38:25 -08:00
										 |  |  |         where TSubject : class | 
					
						
							| 
									
										
										
										
											2013-02-17 23:59:43 -08:00
										 |  |  |         where TModel : ModelBase, new() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-02-23 11:38:25 -08:00
										 |  |  |         private TSubject _subject; | 
					
						
							| 
									
										
										
										
											2013-02-17 23:59:43 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         protected BasicRepository<TModel> Storage { get; private set; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-23 11:38:25 -08:00
										 |  |  |         protected IList<TModel> AllStoredModels | 
					
						
							| 
									
										
										
										
											2013-02-17 23:59:43 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-02-23 11:38:25 -08:00
										 |  |  |             get | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 return Storage.All().ToList(); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2013-02-17 23:59:43 -08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-23 11:38:25 -08:00
										 |  |  |         protected TModel StoredModel | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             get | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 return Storage.All().Single(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-02-15 19:50:22 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         [SetUp] | 
					
						
							|  |  |  |         public void CoreTestSetup() | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             _subject = null; | 
					
						
							| 
									
										
										
										
											2013-02-23 11:38:25 -08:00
										 |  |  |             Storage = Mocker.Resolve<BasicRepository<TModel>>(); | 
					
						
							| 
									
										
										
										
											2013-02-15 19:50:22 -08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         protected TSubject Subject | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             get | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 if (_subject == null) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     _subject = Mocker.Resolve<TSubject>(); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return _subject; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public abstract class DbTest : CoreTest | 
					
						
							| 
									
										
										
										
											2013-02-03 16:10:15 -08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |         private ITestDatabase _db; | 
					
						
							| 
									
										
										
										
											2013-03-26 21:03:02 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         protected virtual MigrationType MigrationType | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             get | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 return MigrationType.Main; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |         protected ITestDatabase Db | 
					
						
							| 
									
										
										
										
											2013-02-03 16:10:15 -08: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-06-27 18:03:04 -07:00
										 |  |  |         private void WithTestDb() | 
					
						
							| 
									
										
										
										
											2013-02-03 16:10:15 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-06-27 18:03:04 -07:00
										 |  |  |             WithTempAsAppPath(); | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-04 20:56:27 -07:00
										 |  |  |             Mocker.SetConstant<IAnnouncer>(Mocker.Resolve<MigrationLogger>()); | 
					
						
							|  |  |  |             Mocker.SetConstant<IConnectionStringFactory>(Mocker.Resolve<ConnectionStringFactory>()); | 
					
						
							|  |  |  |             Mocker.SetConstant<ISQLiteMigrationHelper>(Mocker.Resolve<SQLiteMigrationHelper>()); | 
					
						
							|  |  |  |             Mocker.SetConstant<ISQLiteAlter>(Mocker.Resolve<SQLiteAlter>()); | 
					
						
							|  |  |  |             Mocker.SetConstant<IMigrationController>(Mocker.Resolve<MigrationController>()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |             MapRepository.Instance.EnableTraceLogging = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-04 20:56:27 -07:00
										 |  |  |             var factory = Mocker.Resolve<DbFactory>(); | 
					
						
							|  |  |  |             var _database = factory.Create(MigrationType); | 
					
						
							| 
									
										
										
										
											2013-07-03 18:01:49 -07:00
										 |  |  |             _db = new TestDatabase(_database); | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |             Mocker.SetConstant(_database); | 
					
						
							| 
									
										
										
										
											2013-02-03 16:10:15 -08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-23 11:38:25 -08:00
										 |  |  |         [SetUp] | 
					
						
							|  |  |  |         public void SetupReadDb() | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-06-27 18:03:04 -07:00
										 |  |  |             WithTestDb(); | 
					
						
							| 
									
										
										
										
											2013-02-23 11:38:25 -08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |         [TearDown] | 
					
						
							|  |  |  |         public void TearDown() | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-07-09 15:06:41 -07:00
										 |  |  |             if (TestFolderInfo != null && Directory.Exists(TestFolderInfo.AppDataFolder)) | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |             { | 
					
						
							| 
									
										
										
										
											2013-07-04 21:43:28 -07:00
										 |  |  |                 var files = Directory.GetFiles(TestFolderInfo.AppDataFolder); | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-27 18:03:04 -07:00
										 |  |  |                 foreach (var file in files) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     try | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         File.Delete(file); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     catch (Exception) | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |     public interface ITestDatabase | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |         void InsertMany<T>(IEnumerable<T> items) where T : ModelBase, new(); | 
					
						
							| 
									
										
										
										
											2013-04-10 21:38:45 -07:00
										 |  |  |         T Insert<T>(T item) where T : ModelBase, new(); | 
					
						
							| 
									
										
										
										
											2013-03-26 17:51:37 -07:00
										 |  |  |         List<T> All<T>() where T : ModelBase, new(); | 
					
						
							| 
									
										
										
										
											2013-03-25 22:51:56 -07:00
										 |  |  |         T Single<T>() where T : ModelBase, new(); | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |         void Update<T>(T childModel) where T : ModelBase, new(); | 
					
						
							|  |  |  |         void Delete<T>(T childModel) where T : ModelBase, new(); | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-03 18:01:49 -07:00
										 |  |  |     public class TestDatabase : ITestDatabase | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |         private readonly IDatabase _dbConnection; | 
					
						
							| 
									
										
										
										
											2013-05-05 14:24:33 -07:00
										 |  |  |         private IMessageAggregator _messageAggregator; | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-03 18:01:49 -07:00
										 |  |  |         public TestDatabase(IDatabase dbConnection) | 
					
						
							| 
									
										
										
										
											2013-02-03 16:10:15 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-05-05 14:24:33 -07:00
										 |  |  |             _messageAggregator = new Mock<IMessageAggregator>().Object; | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |             _dbConnection = dbConnection; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |         public void InsertMany<T>(IEnumerable<T> items) where T : ModelBase, new() | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-05-05 14:24:33 -07:00
										 |  |  |             new BasicRepository<T>(_dbConnection, _messageAggregator).InsertMany(items.ToList()); | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-10 21:38:45 -07:00
										 |  |  |         public T Insert<T>(T item) where T : ModelBase, new() | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-05-05 14:24:33 -07:00
										 |  |  |             return new BasicRepository<T>(_dbConnection, _messageAggregator).Insert(item); | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-26 17:51:37 -07:00
										 |  |  |         public List<T> All<T>() where T : ModelBase, new() | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-05-05 14:24:33 -07:00
										 |  |  |             return new BasicRepository<T>(_dbConnection, _messageAggregator).All().ToList(); | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-25 22:51:56 -07:00
										 |  |  |         public T Single<T>() where T : ModelBase, new() | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             return All<T>().SingleOrDefault(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |         public void Update<T>(T childModel) where T : ModelBase, new() | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-05-05 14:24:33 -07:00
										 |  |  |             new BasicRepository<T>(_dbConnection, _messageAggregator).Update(childModel); | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |         public void Delete<T>(T childModel) where T : ModelBase, new() | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-05-05 14:24:33 -07:00
										 |  |  |             new BasicRepository<T>(_dbConnection, _messageAggregator).Delete(childModel); | 
					
						
							| 
									
										
										
										
											2013-02-03 16:10:15 -08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |