public async Task Replace_AllDataIncluded_WithFakeExifTool() { var item = await _query.AddItemAsync(new FileIndexItem { FileName = "test09.jpg", ParentDirectory = "/", Tags = "7test" }); var selectorStorage = new FakeSelectorStorage(new FakeIStorage(new List <string> { "/" }, new List <string> { "/test09.jpg" })); var metaReplaceService = new MetaReplaceService(_query, _appSettings, selectorStorage, new FakeIWebLogger()); var controller = new MetaReplaceController(metaReplaceService, _bgTaskQueue, new FakeIRealtimeConnectionsService(), new FakeIWebLogger(), NewScopeFactory()); var jsonResult = await controller.Replace("/test09.jpg", "Tags", "test", string.Empty) as JsonResult; if (jsonResult == null) { throw new NullReferenceException(nameof(jsonResult)); } var fileModel = jsonResult.Value as List <FileIndexItem>; if (fileModel == null) { throw new NullReferenceException(nameof(fileModel)); } Assert.AreNotEqual(null, fileModel.FirstOrDefault()?.Tags); Assert.AreEqual("7", fileModel.FirstOrDefault()?.Tags); await _query.RemoveItemAsync(item); }
public async Task DownloadPhoto_isThumbnailTrue_ReturnAThumb_ReturnFileStream_Test() { // Arrange var fileIndexItem = InsertSearchData(); var selectorStorage = new FakeSelectorStorage(ArrangeStorage()); // Act var controller = new DownloadPhotoController(_query, selectorStorage, new FakeIWebLogger()); controller.ControllerContext.HttpContext = new DefaultHttpContext(); // Run once var actionResult1 = await controller.DownloadPhoto(fileIndexItem.FilePath) as FileStreamResult; actionResult1.FileStream.Dispose(); // Run twice var actionResult2 = await controller.DownloadPhoto(fileIndexItem.FilePath) as FileStreamResult; Assert.AreNotEqual(null, actionResult2); actionResult2.FileStream.Dispose(); }
public async Task Update_SourceImageMissingOnDisk_WithFakeExifTool() { await _query.AddItemAsync(new FileIndexItem { FileName = "ApiController_Update_SourceImageMissingOnDisk_WithFakeExifTool.jpg", ParentDirectory = "/", FileHash = "ApiController_Update_SourceImageMissingOnDisk_WithFakeExifTool" }); var selectorStorage = new FakeSelectorStorage(new StorageSubPathFilesystem(_appSettings, new FakeIWebLogger())); var metaPreflight = new MetaPreflight(_query, _appSettings, selectorStorage, new FakeIWebLogger()); var metaUpdateService = new MetaUpdateService(_query, _exifTool, selectorStorage, new FakeMetaPreflight(), new FakeIWebLogger(), new FakeReadMetaSubPathStorage()); var controller = new MetaUpdateController(metaPreflight, metaUpdateService, _bgTaskQueue, new FakeIWebLogger(), NewScopeFactory()) { ControllerContext = { HttpContext = new DefaultHttpContext() } }; // Not Found --> 404 var testElement = new FileIndexItem(); var notFoundResult = await controller.UpdateAsync(testElement, "/ApiController_Update_SourceImageMissingOnDisk_WithFakeExifTool.jpg", false, false) as NotFoundObjectResult; if (notFoundResult == null) { throw new NullReferenceException(nameof(notFoundResult)); } Assert.AreEqual(404, notFoundResult.StatusCode); await _query.RemoveItemAsync(_query.SingleItem("/ApiController_Update_SourceImageMissingOnDisk_WithFakeExifTool.jpg").FileIndexItem); }
public async Task GeoBackgroundTask_WithResults_AlreadyHasGps() { var storage = new FakeIStorage(new List <string> { "/" }, new List <string> { "/2QOYZWMPACZAJ2MABGMOZ6CCPY.jpg" }, new List <byte[]> { CreateAnImage.Bytes } ); var storageSelector = new FakeSelectorStorage(storage); var geoReverseLookup = new GeoReverseLookup(_appSettings, _geoFileDownload, _memoryCache); var controller = new GeoBackgroundTask(_appSettings, storageSelector, _geoLocationWrite, _memoryCache, new FakeIWebLogger(), geoReverseLookup); // var is used var results = await controller.GeoBackgroundTaskAsync(); Assert.AreEqual(0, results.Count); }
public async Task DownloadPhoto_Thumb_base_folder_not_found_Test() { // Arrange var fileIndexItem = InsertSearchData(); var storage = new FakeIStorage(null, new List <string> { "/test.jpg" }, new List <byte[]> { FakeCreateAn.CreateAnImage.Bytes }); var selectorStorage = new FakeSelectorStorage(storage); // Act var controller = new DownloadPhotoController(_query, selectorStorage, new FakeIWebLogger()); controller.ControllerContext.HttpContext = new DefaultHttpContext(); var actionResult = await controller.DownloadPhoto(fileIndexItem.FilePath) as NotFoundObjectResult; Assert.AreNotEqual(null, actionResult); Assert.AreEqual(404, actionResult.StatusCode); Assert.AreEqual("ThumbnailTempFolder not found", actionResult.Value); }
public async Task ManualSync_test() { var provider = new ServiceCollection() .AddMemoryCache(); var appSettings = new AppSettings { DatabaseType = AppSettings.DatabaseTypeList.InMemoryDatabase, Verbose = false }; new SetupDatabaseTypes(appSettings, provider).BuilderDb(); provider.AddSingleton(appSettings); provider.AddScoped <IQuery, Query>(); provider.AddScoped <IWebLogger, FakeIWebLogger>(); var buildServiceProvider = provider.BuildServiceProvider(); var memoryCache = buildServiceProvider.GetService <IMemoryCache>(); var query = buildServiceProvider.GetService <IQuery>(); var cacheDbName = new Query(null, null, null, null).CachingDbName(nameof(FileIndexItem), "/"); memoryCache.Remove(cacheDbName); var cachedContent = new List <FileIndexItem> { new FileIndexItem("/test999.jpg") }; memoryCache.Set(cacheDbName, cachedContent); await query.AddItemAsync(new FileIndexItem("/test999.jpg")); var item = new FakeSelectorStorage( new FakeIStorage(new List <string> { "/" }, new List <string> { "/test2__1234.jpg", "/test3__1234.jpg" }, new List <byte[]> { FakeCreateAn.CreateAnImage.Bytes, FakeCreateAn.CreateAnImage.Bytes })); await new ManualBackgroundSyncService( new Synchronize(appSettings, query, item, new FakeIWebLogger(), memoryCache), query, new FakeIWebSocketConnectionsService(), memoryCache, new FakeIWebLogger(), new FakeIUpdateBackgroundTaskQueue(), GetScope()) .BackgroundTask("/", string.Empty); var content = query.DisplayFileFolders().Where(p => p.FilePath != "/").ToList(); foreach (var itemContent in content) { Console.WriteLine(itemContent.FilePath); } Assert.AreEqual(1, content.Count(p => p.FilePath == "/test2__1234.jpg")); Assert.AreEqual(1, content.Count(p => p.FilePath == "/test3__1234.jpg")); Assert.AreEqual(2, content.Count); }
public RedirectControllerTest() { _fakeSelectorStorage = new FakeSelectorStorage(); }
public async Task ExportController_TestZipping() { // to avoid skip of adding zip var zipFilesList = Directory.GetFiles(_createAnImage.BasePath, "*.*", SearchOption.AllDirectories) .Where(p => ".zip" == Path.GetExtension(p)); foreach (var toDelPath in zipFilesList) { new StorageHostFullPathFilesystem().FileDelete(toDelPath); } IServiceCollection services = new ServiceCollection(); services.AddHostedService <UpdateBackgroundQueuedHostedService>(); services.AddSingleton <IUpdateBackgroundTaskQueue, UpdateBackgroundTaskQueue>(); services.AddSingleton <IWebLogger, FakeIWebLogger>(); services.AddSingleton <ITelemetryService, FakeTelemetryService>(); var serviceProvider = services.BuildServiceProvider(); var service = serviceProvider.GetService <IHostedService>() as UpdateBackgroundQueuedHostedService; var backgroundQueue = serviceProvider.GetService <IUpdateBackgroundTaskQueue>(); if (service == null) { throw new Exception("service should not be null"); } await service.StartAsync(CancellationToken.None); // the test _appSettings.DatabaseType = AppSettings.DatabaseTypeList.InMemoryDatabase; var fakeStorage = new FakeIStorage(new List <string> { "/" }, new List <string> { _createAnImage.DbPath }, new List <byte[]> { CreateAnImage.Bytes }); var storageSelector = new FakeSelectorStorage(fakeStorage); var fakeQuery = new FakeIQuery(new List <FileIndexItem> { new FileIndexItem { FileName = _createAnImage.DbPath, ParentDirectory = "/", FileHash = "file-hash", ColorClass = ColorClassParser.Color.Winner, // 1 } }); var appSettings = new AppSettings { TempFolder = _createAnImage.BasePath, Verbose = true }; var export = new ExportService(fakeQuery, appSettings, storageSelector, new FakeIWebLogger()); var controller = new ExportController( backgroundQueue, storageSelector, export) { ControllerContext = { HttpContext = new DefaultHttpContext() } }; var actionResult = controller.CreateZip(_createAnImage.DbPath, true, false) as JsonResult; Assert.AreNotEqual(actionResult, null); var zipHash = actionResult.Value as string; Assert.AreEqual(true, zipHash.Contains("SR")); await Task.Delay(150); // Get from real fs in to fake memory var sourceFullPath = Path.Join(appSettings.TempFolder, zipHash) + ".zip"; await fakeStorage.WriteStreamAsync(new StorageHostFullPathFilesystem().ReadStream(sourceFullPath), sourceFullPath); var actionResult2zip = controller.Status(zipHash, true) as JsonResult; Assert.AreNotEqual(actionResult2zip, null); var resultValue = ( string )actionResult2zip.Value; if (resultValue != "OK" && resultValue != "Not Ready") { throw new Exception(actionResult2zip.StatusCode.ToString()); } // Don't check if file exist due async await service.StopAsync(CancellationToken.None); }
public UploadControllerTest() { var provider = new ServiceCollection() .AddMemoryCache() .BuildServiceProvider(); _memoryCache = provider.GetService <IMemoryCache>(); var builderDb = new DbContextOptionsBuilder <ApplicationDbContext>(); builderDb.UseInMemoryDatabase(nameof(ExportControllerTest)); var options = builderDb.Options; var context = new ApplicationDbContext(options); var scopeFactory = provider.GetService <IServiceScopeFactory>(); var services = new ServiceCollection(); // Fake the readMeta output services.AddSingleton <IReadMeta, FakeReadMeta>(); // Inject Config helper services.AddSingleton <IConfiguration>(new ConfigurationBuilder().Build()); // random config var createAnImage = new CreateAnImage(); _appSettings = new AppSettings { TempFolder = createAnImage.BasePath }; _query = new Query(context, _appSettings, scopeFactory, new FakeIWebLogger(), _memoryCache); _iStorage = new FakeIStorage(new List <string> { "/", "/test" }, new List <string> { createAnImage.DbPath }, new List <byte[]> { CreateAnImage.Bytes }); var readMeta = new ReadMeta(_iStorage, _appSettings); var selectorStorage = new FakeSelectorStorage(_iStorage); _import = new Import(selectorStorage, _appSettings, new FakeIImportQuery(), new FakeExifTool(_iStorage, _appSettings), _query, new ConsoleWrapper(), new FakeIMetaExifThumbnailService(), new FakeIWebLogger(), _memoryCache); // Start using dependency injection var builder = new ConfigurationBuilder(); // Add random config to dependency injection // build config var configuration = builder.Build(); // inject config as object to a service // Add Background services services.AddSingleton <IHostedService, UpdateBackgroundQueuedHostedService>(); services.AddSingleton <IUpdateBackgroundTaskQueue, UpdateBackgroundTaskQueue>(); // build the service var serviceProvider = services.BuildServiceProvider(); // get the service serviceProvider.GetRequiredService <IServiceScopeFactory>(); }