From 04f21b250af29917b23bee60574a19f4188546f0 Mon Sep 17 00:00:00 2001 From: bruno-ref Date: Wed, 29 May 2024 20:17:46 +0200 Subject: [PATCH] Implemented Filter Methods, Mapper and Tests Signed-off by: Bruno Refenner --- .../Query/EntityPropertyFilterMapper.cs | 28 ++ .../Query/GetPhotosQueryHandler.cs | 14 +- .../Query/PhotoBeginsWithHandler.cs | 36 ++ .../Query/PhotoContainsHandler.cs | 38 ++ .../Query/PhotoEndsWithHandler.cs | 39 ++ .../PhotoUseCases/Query/PhotoEqualsHandler.cs | 41 ++ .../Query/PhotoGreaterThanFilterHandler.cs | 36 ++ .../Query/PhotoLowerThanHandler.cs | 37 ++ .../Query/PhotoPlusMinus100Handler.cs | 37 ++ .../Repositories/IPhotoFilterBuilder.cs | 9 + .../Builder/PhotoFilterBuilder.cs | 48 +++ .../PhotoFilterBuilderTest.cs | 391 ++++++++++++++++++ .../Spg.AloMalo.Api.Test.csproj | 7 + 13 files changed, 753 insertions(+), 8 deletions(-) create mode 100644 src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/EntityPropertyFilterMapper.cs create mode 100644 src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoBeginsWithHandler.cs create mode 100644 src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoContainsHandler.cs create mode 100644 src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoEndsWithHandler.cs create mode 100644 src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoEqualsHandler.cs create mode 100644 src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoGreaterThanFilterHandler.cs create mode 100644 src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoLowerThanHandler.cs create mode 100644 src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoPlusMinus100Handler.cs create mode 100644 test/Spg.AloMalo.Api.Test/PhotoFilterBuilderTest.cs diff --git a/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/EntityPropertyFilterMapper.cs b/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/EntityPropertyFilterMapper.cs new file mode 100644 index 0000000..0c21dad --- /dev/null +++ b/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/EntityPropertyFilterMapper.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Spg.AloMalo.Application.Services.PhotoUseCases.Query +{ + public class EntityPropertyFilterMapper + { + public string _propertyName; + public Func _filter; + + public EntityPropertyFilterMapper(string propertyName, Func filter) + { + _propertyName = propertyName; + _filter = filter; + } + public void ExecuteDeligate(TProperty property, string propertyName) + { + if (_propertyName == propertyName) + { + _filter.Invoke(property); + } + } + } +} + diff --git a/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/GetPhotosQueryHandler.cs b/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/GetPhotosQueryHandler.cs index 84fed9d..02dca02 100644 --- a/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/GetPhotosQueryHandler.cs +++ b/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/GetPhotosQueryHandler.cs @@ -19,14 +19,12 @@ public Task> Handle(GetPhotosQueryModel request, CancellationToke _photoRepository .FilterBuilder; - builder = new LastNameContainsParameter(builder) - .Compile(request.Query.Filter); - builder = new LastNameBeginsWithParameter(builder) - .Compile(request.Query.Filter); - builder = new LastNameEndsWithParameter(builder) - .Compile(request.Query.Filter); - // builder = new ... - + builder = new PhotoBeginsWithHandler(builder).WithQuery(request.Query.Filter); + builder = new PhotoContainsHandler(builder).WithQuery(request.Query.Filter); + builder = new PhotoEndsWithHandler(builder).WithQuery(request.Query.Filter); + builder = new PhotoGreaterThanFilterHandler(builder).WithQuery(request.Query.Filter); + builder = new PhotoLowerThanHandler(builder).WithQuery(request.Query.Filter); + builder = new PhotoPlusMinus100Handler(builder).WithQuery(request.Query.Filter); return Task.FromResult( builder .Build() diff --git a/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoBeginsWithHandler.cs b/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoBeginsWithHandler.cs new file mode 100644 index 0000000..7e94936 --- /dev/null +++ b/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoBeginsWithHandler.cs @@ -0,0 +1,36 @@ +using Spg.AloMalo.DomainModel.Interfaces.Repositories; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Spg.AloMalo.Application.Services.PhotoUseCases.Query +{ + public class PhotoBeginsWithHandler + { + private IPhotoFilterBuilder _builder; + + public PhotoBeginsWithHandler(IPhotoFilterBuilder builder) + { + _builder = builder; + } + + public IPhotoFilterBuilder WithQuery(string query) + { + string[] queryParts = query.Split(' '); + + if (queryParts[1] != "bw") + { + return _builder; + } + + new EntityPropertyFilterMapper("Name", _builder.ApplyNameBeginsWithFilter) + .ExecuteDeligate(queryParts[0], queryParts[2]); + new EntityPropertyFilterMapper("Description", _builder.ApplyDescriptionBeginsWithFilter) + .ExecuteDeligate(queryParts[0], queryParts[2]); + + return _builder; + } + } +} diff --git a/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoContainsHandler.cs b/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoContainsHandler.cs new file mode 100644 index 0000000..2a937d6 --- /dev/null +++ b/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoContainsHandler.cs @@ -0,0 +1,38 @@ +using Spg.AloMalo.DomainModel.Interfaces.Repositories; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Spg.AloMalo.Application.Services.PhotoUseCases.Query +{ + public class PhotoContainsHandler + { + private IPhotoFilterBuilder _builder; + + public PhotoContainsHandler(IPhotoFilterBuilder builder) + { + _builder = builder; + } + + public IPhotoFilterBuilder WithQuery(string query) + { + string[] queryParts = query.Split(' '); + if (queryParts.Length > 3) { + throw new Exception("Query has to many Parameters!"); + } + if (queryParts[1] != "ct") + { + return _builder; + } + + new EntityPropertyFilterMapper("Name", _builder.ApplyNameContainsFilter) + .ExecuteDeligate(queryParts[0], queryParts[2]); + new EntityPropertyFilterMapper("Description", _builder.ApplyDescriptionContainsFilter) + .ExecuteDeligate(queryParts[0], queryParts[2]); + + return _builder; + } + } +} diff --git a/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoEndsWithHandler.cs b/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoEndsWithHandler.cs new file mode 100644 index 0000000..a327c82 --- /dev/null +++ b/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoEndsWithHandler.cs @@ -0,0 +1,39 @@ +using Spg.AloMalo.DomainModel.Interfaces.Repositories; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Spg.AloMalo.Application.Services.PhotoUseCases.Query +{ + public class PhotoEndsWithHandler + { + private IPhotoFilterBuilder _builder; + + public PhotoEndsWithHandler(IPhotoFilterBuilder builder) + { + _builder = builder; + } + + public IPhotoFilterBuilder WithQuery(string query) + { + string[] queryParts = query.Split(' '); + if (queryParts.Length > 3) + { + throw new Exception("Query has to many Parameters!"); + } + if (queryParts[1] != "ew") + { + return _builder; + } + + new EntityPropertyFilterMapper("Name", _builder.ApplyNameEndsWithFilter) + .ExecuteDeligate(queryParts[0], queryParts[2]); + new EntityPropertyFilterMapper("Description", _builder.ApplyDescriptionEndsWithFilter) + .ExecuteDeligate(queryParts[0], queryParts[2]); + + return _builder; + } + } +} diff --git a/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoEqualsHandler.cs b/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoEqualsHandler.cs new file mode 100644 index 0000000..5a421c8 --- /dev/null +++ b/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoEqualsHandler.cs @@ -0,0 +1,41 @@ +using Spg.AloMalo.DomainModel.Interfaces.Repositories; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Spg.AloMalo.Application.Services.PhotoUseCases.Query +{ + public class PhotoEqualsHandler + { + private IPhotoFilterBuilder _builder; + + public PhotoEqualsHandler(IPhotoFilterBuilder builder) + { + _builder = builder; + } + + public IPhotoFilterBuilder WithQuery(string query) + { + string[] queryParts = query.Split(' '); + if (queryParts.Length > 3) + { + throw new Exception("Query has to many Parameters!"); + } + if (queryParts[1] != "eq") + { + return _builder; + } + + new EntityPropertyFilterMapper("Name", _builder.ApplyNameEqualsFilter) + .ExecuteDeligate(queryParts[0], queryParts[2]); + new EntityPropertyFilterMapper("Description", _builder.ApplyDescriptionEqualsFilter) + .ExecuteDeligate(queryParts[0], queryParts[2]); + new EntityPropertyFilterMapper("Height", _builder.ApplyHeightEqualsFilter) + .ExecuteDeligate(queryParts[0], queryParts[2]); + + return _builder; + } + } +} diff --git a/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoGreaterThanFilterHandler.cs b/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoGreaterThanFilterHandler.cs new file mode 100644 index 0000000..a8519e5 --- /dev/null +++ b/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoGreaterThanFilterHandler.cs @@ -0,0 +1,36 @@ +using Spg.AloMalo.DomainModel.Interfaces.Repositories; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Spg.AloMalo.Application.Services.PhotoUseCases.Query +{ + public class PhotoGreaterThanFilterHandler + { + private IPhotoFilterBuilder _builder; + + public PhotoGreaterThanFilterHandler(IPhotoFilterBuilder builder) + { + _builder = builder; + } + + public IPhotoFilterBuilder WithQuery(string query) + { + string[] queryParts = query.Split(' '); + if (queryParts.Length < 3) { + throw new Exception("Query has too many Parameters!"); + } + if (queryParts[1] != "gt") + { + return _builder; + } + + new EntityPropertyFilterMapper("Name", _builder.ApplyHeightGreaterThanFilter) + .ExecuteDeligate(queryParts[0], queryParts[2]); + + return _builder; + } + } +} diff --git a/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoLowerThanHandler.cs b/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoLowerThanHandler.cs new file mode 100644 index 0000000..297cd02 --- /dev/null +++ b/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoLowerThanHandler.cs @@ -0,0 +1,37 @@ +using Spg.AloMalo.DomainModel.Interfaces.Repositories; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Spg.AloMalo.Application.Services.PhotoUseCases.Query +{ + public class PhotoLowerThanHandler + { + private IPhotoFilterBuilder _builder; + + public PhotoLowerThanHandler(IPhotoFilterBuilder builder) + { + _builder = builder; + } + + public IPhotoFilterBuilder WithQuery(string query) + { + string[] queryParts = query.Split(' '); + if (queryParts.Length < 3) + { + throw new Exception("Query has too many Parameters!"); + } + if (queryParts[1] != "lt") + { + return _builder; + } + + new EntityPropertyFilterMapper("Name", _builder.ApplyHeightLowerThanFilter) + .ExecuteDeligate(queryParts[0], queryParts[2]); + + return _builder; + } + } +} diff --git a/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoPlusMinus100Handler.cs b/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoPlusMinus100Handler.cs new file mode 100644 index 0000000..cfa3a91 --- /dev/null +++ b/src/Spg.AloMalo.Application/Services/PhotoUseCases/Query/PhotoPlusMinus100Handler.cs @@ -0,0 +1,37 @@ +using Spg.AloMalo.DomainModel.Interfaces.Repositories; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Spg.AloMalo.Application.Services.PhotoUseCases.Query +{ + public class PhotoPlusMinus100Handler + { + private IPhotoFilterBuilder _builder; + + public PhotoPlusMinus100Handler(IPhotoFilterBuilder builder) + { + _builder = builder; + } + + public IPhotoFilterBuilder WithQuery(string query) + { + string[] queryParts = query.Split(' '); + if (queryParts.Length < 3) + { + throw new Exception("Query has too many Parameters!"); + } + if (queryParts[1] != "pm100") + { + return _builder; + } + + new EntityPropertyFilterMapper("Height", _builder.ApplyHeightPlusMinus100Filter) + .ExecuteDeligate(queryParts[0], queryParts[2]); + + return _builder; + } + } +} diff --git a/src/Spg.AloMalo.DomainModel/Interfaces/Repositories/IPhotoFilterBuilder.cs b/src/Spg.AloMalo.DomainModel/Interfaces/Repositories/IPhotoFilterBuilder.cs index 34e6727..b627805 100644 --- a/src/Spg.AloMalo.DomainModel/Interfaces/Repositories/IPhotoFilterBuilder.cs +++ b/src/Spg.AloMalo.DomainModel/Interfaces/Repositories/IPhotoFilterBuilder.cs @@ -11,5 +11,14 @@ public interface IPhotoFilterBuilder : IEntityFilterBuilder IPhotoFilterBuilder ApplyOrientationFilter(Orientations orientation); IPhotoFilterBuilder ApplyAiFilter(bool @is); //IPhotoFilterBuilder ApplyPaging(int page, int size); + IPhotoFilterBuilder ApplyDescriptionBeginsWithFilter(string description); + IPhotoFilterBuilder ApplyDescriptionContainsFilter(string description); + IPhotoFilterBuilder ApplyDescriptionEndsWithFilter(string description); + IPhotoFilterBuilder ApplyDescriptionEqualsFilter(string description); + IPhotoFilterBuilder ApplyNameEqualsFilter(string name); + IPhotoFilterBuilder ApplyHeightGreaterThanFilter(string filter); + IPhotoFilterBuilder ApplyHeightLowerThanFilter(string filter); + IPhotoFilterBuilder ApplyHeightEqualsFilter(string filter); + IPhotoFilterBuilder ApplyHeightPlusMinus100Filter(string filter); } } diff --git a/src/Spg.AloMalo.Repository/Builder/PhotoFilterBuilder.cs b/src/Spg.AloMalo.Repository/Builder/PhotoFilterBuilder.cs index 617dc37..49ecf96 100644 --- a/src/Spg.AloMalo.Repository/Builder/PhotoFilterBuilder.cs +++ b/src/Spg.AloMalo.Repository/Builder/PhotoFilterBuilder.cs @@ -47,5 +47,53 @@ public IPhotoFilterBuilder ApplyAiFilter(bool @is) EntityList = EntityList.Where(x => x.AiGenerated == @is); return this; } + public IPhotoFilterBuilder ApplyDescriptionBeginsWithFilter(string filter) + { + EntityList = EntityList.Where(x => x.Description.StartsWith(filter)); + return this; + } + public IPhotoFilterBuilder ApplyDescriptionContainsFilter(string filter) + { + EntityList = EntityList.Where(x => x.Description.Contains(filter)); + return this; + } + public IPhotoFilterBuilder ApplyDescriptionEndsWithFilter(string filter) + { + EntityList = EntityList.Where(x => x.Description.EndsWith(filter)); + return this; + } + public IPhotoFilterBuilder ApplyDescriptionEqualsFilter(string description) + { + EntityList = EntityList.Where(x => x.Description == description); + return this; + } + public IPhotoFilterBuilder ApplyNameEqualsFilter(string name) + { + EntityList = EntityList.Where(x => x.Name == name); + return this; + } + public IPhotoFilterBuilder ApplyHeightGreaterThanFilter(string filter) + { + + EntityList = EntityList.Where(x => x.Height > int.Parse(filter)); + return this; + } + public IPhotoFilterBuilder ApplyHeightLowerThanFilter(string filter) + { + + EntityList = EntityList.Where(x => x.Height < int.Parse(filter)); + return this; + } + public IPhotoFilterBuilder ApplyHeightEqualsFilter(string filter) + { + EntityList = EntityList.Where(x => x.Height == int.Parse(filter)); + return this; + } + public IPhotoFilterBuilder ApplyHeightPlusMinus100Filter(string filter) + { + EntityList = EntityList.Where(x => x.Height < int.Parse(filter)+100 && x.Height > int.Parse(filter)-100); + return this; + } + } } diff --git a/test/Spg.AloMalo.Api.Test/PhotoFilterBuilderTest.cs b/test/Spg.AloMalo.Api.Test/PhotoFilterBuilderTest.cs new file mode 100644 index 0000000..260ea96 --- /dev/null +++ b/test/Spg.AloMalo.Api.Test/PhotoFilterBuilderTest.cs @@ -0,0 +1,391 @@ +using Microsoft.EntityFrameworkCore; +using System.Linq; +using System.Collections.Generic; +using Spg.AloMalo.DomainModel.Model; +using Spg.AloMalo.DomainModel.Interfaces.Repositories; +using Spg.AloMalo.Repository.Builder; +using Spg.AloMalo.Infrastructure; +using Xunit; +using System.Linq; +using System.Collections.Generic; +using Spg.AloMalo.Api.Test.Helpers; +using Spg.AloMalo.DomainModel; +using Spg.AloMalo.Repository.Repositories; + +namespace Spg.AloMalo.Application.Services.PhotoUseCases.Query.Tests +{ + public class PhotoFilterBuilderTests + { + private void SeedDatabase(PhotoContext context) + { + var photos = new List { + + }; + context.Photos.AddRange(photos); + context.SaveChanges(); + } + [Fact] + public void GetPhotosQueryHandler_ShouldReturnPhotos_WithNameContainsT() + { + using (PhotoContext db = DatabaseUtilities.CreateDb()) + { + Photographer newPhotographer1 = DatabaseUtilities.GetSeedingPhotographers()[0]; + Photo newPhoto1 = new Photo( + new Guid("11111111-1111-1111-1111-111111111111"), + "Test Photo 01", + "Beschreibung Test Photo 01...", + DateTime.Now, + ImageTypes.Png, + new Location(12, 17), + 400, 800, + false, + newPhotographer1 + ).AddAlbums(new Album( + "Test Album 01", "Beschreibung...", + true, + newPhotographer1, + new TimeStampProvider() + )); + Photographer newPhotographer2 = DatabaseUtilities.GetSeedingPhotographers()[1]; + Photo newPhoto2 = new Photo( + new Guid("22222222-2222-2222-2222-222222222222"), + "Test Photo blabla 02", + "Beschreibung Test Photo 01...", + DateTime.Now, + ImageTypes.Png, + new Location(12, 17), + 400, 800, + false, + newPhotographer2 + ).AddAlbums(new Album( + "Test Album 02", "Beschreibung...", + true, + newPhotographer2, + new TimeStampProvider() + )); + + db.Photos.Add(newPhoto1); + db.Photos.Add(newPhoto2); + db.SaveChanges(); + var handler = new GetPhotosQueryHandler(new PhotoRepository(db, new PhotoFilterBuilder(db.Photos), new PhotoUpdateBuilder(db))); + var query = new GetPhotosQueryModel(new DomainModel.Queries.GetPhotosQuery("name ct T", "")); + var builder = new PhotoFilterBuilder(db.Photos); + var result = handler.Handle(query, CancellationToken.None).Result; + Console.WriteLine(result); + Assert.Equal(2, result.Count); + } + } + + + + [Fact] + public void GetPhotosQueryHandler_ShouldReturnPhotos_WithHeightGreaterThan100() + { + using (PhotoContext db = DatabaseUtilities.CreateDb()) + { + Photographer newPhotographer1 = DatabaseUtilities.GetSeedingPhotographers()[0]; + Photo newPhoto1 = new Photo( + new Guid("11111111-1111-1111-1111-111111111111"), + "Test Photo 01", + "Beschreibung Test Photo 01...", + DateTime.Now, + ImageTypes.Png, + new Location(12, 17), + 400, 800, + false, + newPhotographer1 + ).AddAlbums(new Album( + "Test Album 01", "Beschreibung...", + true, + newPhotographer1, + new TimeStampProvider() + )); + Photographer newPhotographer2 = DatabaseUtilities.GetSeedingPhotographers()[1]; + Photo newPhoto2 = new Photo( + new Guid("22222222-2222-2222-2222-222222222222"), + "Test Photo blabla 02", + "Beschreibung Test Photo 01...", + DateTime.Now, + ImageTypes.Png, + new Location(12, 17), + 400, 800, + false, + newPhotographer2 + ).AddAlbums(new Album( + "Test Album 02", "Beschreibung...", + true, + newPhotographer2, + new TimeStampProvider() + )); + + db.Photos.Add(newPhoto1); + db.Photos.Add(newPhoto2); + db.SaveChanges(); + var handler = new GetPhotosQueryHandler(new PhotoRepository(db, new PhotoFilterBuilder(db.Photos), new PhotoUpdateBuilder(db))); + var query = new GetPhotosQueryModel(new DomainModel.Queries.GetPhotosQuery("Height gt 100", "")); + var builder = new PhotoFilterBuilder(db.Photos); + var result = handler.Handle(query, CancellationToken.None).Result; + Console.WriteLine(result); + Assert.Equal(2, result.Count); + } + } + [Fact] + public void GetPhotosQueryHandler_ShouldReturnPhotos_WithHeightLowerThan1000() + { + using (PhotoContext db = DatabaseUtilities.CreateDb()) + { + Photographer newPhotographer1 = DatabaseUtilities.GetSeedingPhotographers()[0]; + Photo newPhoto1 = new Photo( + new Guid("11111111-1111-1111-1111-111111111111"), + "Test Photo 01", + "Beschreibung Test Photo 01...", + DateTime.Now, + ImageTypes.Png, + new Location(12, 17), + 400, 800, + false, + newPhotographer1 + ).AddAlbums(new Album( + "Test Album 01", "Beschreibung...", + true, + newPhotographer1, + new TimeStampProvider() + )); + Photographer newPhotographer2 = DatabaseUtilities.GetSeedingPhotographers()[1]; + Photo newPhoto2 = new Photo( + new Guid("22222222-2222-2222-2222-222222222222"), + "Test Photo blabla 02", + "Beschreibung Test Photo 01...", + DateTime.Now, + ImageTypes.Png, + new Location(12, 17), + 400, 800, + false, + newPhotographer2 + ).AddAlbums(new Album( + "Test Album 02", "Beschreibung...", + true, + newPhotographer2, + new TimeStampProvider() + )); + + db.Photos.Add(newPhoto1); + db.Photos.Add(newPhoto2); + db.SaveChanges(); + var handler = new GetPhotosQueryHandler(new PhotoRepository(db, new PhotoFilterBuilder(db.Photos), new PhotoUpdateBuilder(db))); + var query = new GetPhotosQueryModel(new DomainModel.Queries.GetPhotosQuery("Height lt 1000", "")); + var builder = new PhotoFilterBuilder(db.Photos); + var result = handler.Handle(query, CancellationToken.None).Result; + Console.WriteLine(result); + Assert.Equal(2, result.Count); + } + } + [Fact] + public void GetPhotosQueryHandler_ShouldReturnPhotos_WithHeightPlusMinus100From750() + { + using (PhotoContext db = DatabaseUtilities.CreateDb()) + { + Photographer newPhotographer1 = DatabaseUtilities.GetSeedingPhotographers()[0]; + Photo newPhoto1 = new Photo( + new Guid("11111111-1111-1111-1111-111111111111"), + "Test Photo 01", + "Beschreibung Test Photo 01...", + DateTime.Now, + ImageTypes.Png, + new Location(12, 17), + 400, 800, + false, + newPhotographer1 + ).AddAlbums(new Album( + "Test Album 01", "Beschreibung...", + true, + newPhotographer1, + new TimeStampProvider() + )); + Photographer newPhotographer2 = DatabaseUtilities.GetSeedingPhotographers()[1]; + Photo newPhoto2 = new Photo( + new Guid("22222222-2222-2222-2222-222222222222"), + "Test Photo blabla 02", + "Beschreibung Test Photo 01...", + DateTime.Now, + ImageTypes.Png, + new Location(12, 17), + 400, 800, + false, + newPhotographer2 + ).AddAlbums(new Album( + "Test Album 02", "Beschreibung...", + true, + newPhotographer2, + new TimeStampProvider() + )); + + db.Photos.Add(newPhoto1); + db.Photos.Add(newPhoto2); + db.SaveChanges(); + var handler = new GetPhotosQueryHandler(new PhotoRepository(db, new PhotoFilterBuilder(db.Photos), new PhotoUpdateBuilder(db))); + var query = new GetPhotosQueryModel(new DomainModel.Queries.GetPhotosQuery("Height pm100 750", "")); + var builder = new PhotoFilterBuilder(db.Photos); + var result = handler.Handle(query, CancellationToken.None).Result; + Console.WriteLine(result); + Assert.Equal(2, result.Count); + } + } + [Fact] + public void GetPhotosQueryHandler_ShouldReturnPhotos_WithDescriptionBeginsWithBesch() + { + using (PhotoContext db = DatabaseUtilities.CreateDb()) + { + Photographer newPhotographer1 = DatabaseUtilities.GetSeedingPhotographers()[0]; + Photo newPhoto1 = new Photo( + new Guid("11111111-1111-1111-1111-111111111111"), + "Test Photo 01", + "Beschreibung Test Photo 01...", + DateTime.Now, + ImageTypes.Png, + new Location(12, 17), + 400, 800, + false, + newPhotographer1 + ).AddAlbums(new Album( + "Test Album 01", "Beschreibung...", + true, + newPhotographer1, + new TimeStampProvider() + )); + Photographer newPhotographer2 = DatabaseUtilities.GetSeedingPhotographers()[1]; + Photo newPhoto2 = new Photo( + new Guid("22222222-2222-2222-2222-222222222222"), + "Test Photo blabla 02", + "Beschreibung Test Photo 01...", + DateTime.Now, + ImageTypes.Png, + new Location(12, 17), + 400, 800, + false, + newPhotographer2 + ).AddAlbums(new Album( + "Test Album 02", "Beschreibung...", + true, + newPhotographer2, + new TimeStampProvider() + )); + + db.Photos.Add(newPhoto1); + db.Photos.Add(newPhoto2); + db.SaveChanges(); + var handler = new GetPhotosQueryHandler(new PhotoRepository(db, new PhotoFilterBuilder(db.Photos), new PhotoUpdateBuilder(db))); + var query = new GetPhotosQueryModel(new DomainModel.Queries.GetPhotosQuery("Description bw Besch", "")); + var builder = new PhotoFilterBuilder(db.Photos); + var result = handler.Handle(query, CancellationToken.None).Result; + Console.WriteLine(result); + Assert.Equal(2, result.Count); + } + } + [Fact] + public void GetPhotosQueryHandler_ShouldReturnPhotos_WithNameEqualsTest() + { + using (PhotoContext db = DatabaseUtilities.CreateDb()) + { + Photographer newPhotographer1 = DatabaseUtilities.GetSeedingPhotographers()[0]; + Photo newPhoto1 = new Photo( + new Guid("11111111-1111-1111-1111-111111111111"), + "Test", + "Beschreibung Test Photo 1...", + DateTime.Now, + ImageTypes.Png, + new Location(12, 17), + 400, 800, + false, + newPhotographer1 + ).AddAlbums(new Album( + "Test Album 01", "Beschreibung...", + true, + newPhotographer1, + new TimeStampProvider() + )); + Photographer newPhotographer2 = DatabaseUtilities.GetSeedingPhotographers()[1]; + Photo newPhoto2 = new Photo( + new Guid("22222222-2222-2222-2222-222222222222"), + "Test Photo blabla 02", + "Beschreibung Test Photo 01...", + DateTime.Now, + ImageTypes.Png, + new Location(12, 17), + 400, 800, + false, + newPhotographer2 + ).AddAlbums(new Album( + "Test Album 02", "Beschreibung...", + true, + newPhotographer2, + new TimeStampProvider() + )); + + db.Photos.Add(newPhoto1); + db.Photos.Add(newPhoto2); + db.SaveChanges(); + var handler = new GetPhotosQueryHandler(new PhotoRepository(db, new PhotoFilterBuilder(db.Photos), new PhotoUpdateBuilder(db))); + var query = new GetPhotosQueryModel(new DomainModel.Queries.GetPhotosQuery("Name eq Test", "")); + var builder = new PhotoFilterBuilder(db.Photos); + var result = handler.Handle(query, CancellationToken.None).Result; + Console.WriteLine(result); + Assert.Equal(2, result.Count()); + } + } + + + [Fact] + public void GetPhotosQueryHandler_ShouldReturnPhotos_WithNamesEndingWith02() + { + using (PhotoContext db = DatabaseUtilities.CreateDb()) + { + Photographer newPhotographer1 = DatabaseUtilities.GetSeedingPhotographers()[0]; + Photo newPhoto1 = new Photo( + new Guid("11111111-1111-1111-1111-111111111111"), + "Test Photo 01", + "Beschreibung Test Photo 01...", + DateTime.Now, + ImageTypes.Png, + new Location(12, 17), + 400, 800, + false, + newPhotographer1 + ).AddAlbums(new Album( + "Test Album 01", "Beschreibung...", + true, + newPhotographer1, + new TimeStampProvider() + )); + Photographer newPhotographer2 = DatabaseUtilities.GetSeedingPhotographers()[1]; + Photo newPhoto2 = new Photo( + new Guid("22222222-2222-2222-2222-222222222222"), + "Test Photo blabla 01", + "Beschreibung Test Photo 01...", + DateTime.Now, + ImageTypes.Png, + new Location(12, 17), + 400, 800, + false, + newPhotographer2 + ).AddAlbums(new Album( + "Test Album 02", "Beschreibung...", + true, + newPhotographer2, + new TimeStampProvider() + )); + + db.Photos.Add(newPhoto1); + db.Photos.Add(newPhoto2); + db.SaveChanges(); + var handler = new GetPhotosQueryHandler(new PhotoRepository(db, new PhotoFilterBuilder(db.Photos), new PhotoUpdateBuilder(db))); + var query = new GetPhotosQueryModel(new DomainModel.Queries.GetPhotosQuery("Name ew 01", "")); + var builder = new PhotoFilterBuilder(db.Photos); + var result = handler.Handle(query, CancellationToken.None).Result; + Console.WriteLine(result); + Assert.Equal(2, result.Count()); + } + } + } + } + diff --git a/test/Spg.AloMalo.Api.Test/Spg.AloMalo.Api.Test.csproj b/test/Spg.AloMalo.Api.Test/Spg.AloMalo.Api.Test.csproj index 1a35739..3c7a85b 100644 --- a/test/Spg.AloMalo.Api.Test/Spg.AloMalo.Api.Test.csproj +++ b/test/Spg.AloMalo.Api.Test/Spg.AloMalo.Api.Test.csproj @@ -25,9 +25,16 @@ + + + + + + +