From 1c6ddc74cb2a5ed13491dea0b1c29a8d24ede4e6 Mon Sep 17 00:00:00 2001 From: "Jamie.Rees" Date: Tue, 20 Jun 2017 09:14:39 +0100 Subject: [PATCH] Small fixes around the searching --- .../Rule/Search/ExistingRequestRuleTests.cs | 78 ++++++++++++++++--- src/Ombi.Core/Engine/MovieRequestEngine.cs | 1 + .../Models/Search/SearchTvShowViewModel.cs | 1 - src/Ombi.Core/MovieSender.cs | 3 +- .../Rule/Rules/Search/ExistingRequestRule.cs | 17 ++-- .../app/requests/movierequests.component.ts | 17 ++-- .../app/services/notification.service.ts | 1 + 7 files changed, 91 insertions(+), 27 deletions(-) diff --git a/src/Ombi.Core.Tests/Rule/Search/ExistingRequestRuleTests.cs b/src/Ombi.Core.Tests/Rule/Search/ExistingRequestRuleTests.cs index 8992c00f6..d20db35b2 100644 --- a/src/Ombi.Core.Tests/Rule/Search/ExistingRequestRuleTests.cs +++ b/src/Ombi.Core.Tests/Rule/Search/ExistingRequestRuleTests.cs @@ -1,13 +1,11 @@ -using System.Threading.Tasks; +using System.Collections.Generic; +using System.Threading.Tasks; using Moq; using Ombi.Core.Models.Requests; using Ombi.Core.Models.Requests.Movie; using Ombi.Core.Models.Search; using Ombi.Core.Requests.Models; using Ombi.Core.Rule.Rules.Search; -using Ombi.Store.Context; -using Ombi.Store.Entities; -using Ombi.Store.Repository; using Xunit; namespace Ombi.Core.Tests.Rule.Search @@ -25,20 +23,20 @@ namespace Ombi.Core.Tests.Rule.Search private Mock> MovieMock { get; } private Mock> TvMock { get; } - // TODO continue tests - // https://stackoverflow.com/questions/27483709/testing-ef-async-methods-with-sync-methods-with-moq + + [Fact] public async Task ShouldBe_Requested_WhenExisitngMovie() { - var list = DbHelper.GetQueryableMockDbSet(new MovieRequestModel + var list = new List{new MovieRequestModel { ProviderId = 123, Approved = true - }); - MovieMock.Setup(x => x.GetAllQueryable()).Returns(list); + }}; + MovieMock.Setup(x => x.GetAllAsync()).ReturnsAsync(list); var search = new SearchMovieViewModel { Id = 123, - + }; var result = await Rule.Execute(search); @@ -46,6 +44,66 @@ namespace Ombi.Core.Tests.Rule.Search Assert.Equal(search.Approved, true); } + [Fact] + public async Task ShouldBe_NotRequested_WhenNewMovie() + { + var list = new List{new MovieRequestModel + { + ProviderId = 123, + Approved = true + }}; + MovieMock.Setup(x => x.GetAllAsync()).ReturnsAsync(list); + var search = new SearchMovieViewModel + { + Id = 999, + + }; + var result = await Rule.Execute(search); + + Assert.True(result.Success); + Assert.Equal(search.Approved, false); + } + + [Fact] + public async Task ShouldBe_Requested_WhenExisitngTv() + { + var list = new List{new TvRequestModel + { + ProviderId = 123, + Approved = true + }}; + TvMock.Setup(x => x.GetAllAsync()).ReturnsAsync(list); + var search = new SearchTvShowViewModel + { + Id = 123, + + }; + var result = await Rule.Execute(search); + + Assert.True(result.Success); + Assert.Equal(search.Approved, true); + } + + [Fact] + public async Task ShouldBe_NotRequested_WhenNewTv() + { + var list = new List{new TvRequestModel + { + ProviderId = 123, + Approved = true + }}; + TvMock.Setup(x => x.GetAllAsync()).ReturnsAsync(list); + var search = new SearchTvShowViewModel() + { + Id = 999, + + }; + var result = await Rule.Execute(search); + + Assert.True(result.Success); + Assert.Equal(search.Approved, false); + } + } } \ No newline at end of file diff --git a/src/Ombi.Core/Engine/MovieRequestEngine.cs b/src/Ombi.Core/Engine/MovieRequestEngine.cs index c729ae407..ab9a39b79 100644 --- a/src/Ombi.Core/Engine/MovieRequestEngine.cs +++ b/src/Ombi.Core/Engine/MovieRequestEngine.cs @@ -132,6 +132,7 @@ namespace Ombi.Core.Engine RequestAdded = false }; } + // If there are no providers then it's successful but movie has not been sent } return await AddMovieRequest(requestModel, /*settings,*/ diff --git a/src/Ombi.Core/Models/Search/SearchTvShowViewModel.cs b/src/Ombi.Core/Models/Search/SearchTvShowViewModel.cs index fab72e420..83dcf28f4 100644 --- a/src/Ombi.Core/Models/Search/SearchTvShowViewModel.cs +++ b/src/Ombi.Core/Models/Search/SearchTvShowViewModel.cs @@ -5,7 +5,6 @@ namespace Ombi.Core.Models.Search { public class SearchTvShowViewModel : SearchViewModel { - public int Id { get; set; } public string Title { get; set; } public List Aliases { get; set; } public string Banner { get; set; } diff --git a/src/Ombi.Core/MovieSender.cs b/src/Ombi.Core/MovieSender.cs index 8db810101..6a2919bc9 100644 --- a/src/Ombi.Core/MovieSender.cs +++ b/src/Ombi.Core/MovieSender.cs @@ -45,9 +45,8 @@ namespace Ombi.Core return new MovieSenderResult { - Success = false, + Success = true, MovieSent = false, - Message = "There are no movie providers enabled!" }; } diff --git a/src/Ombi.Core/Rule/Rules/Search/ExistingRequestRule.cs b/src/Ombi.Core/Rule/Rules/Search/ExistingRequestRule.cs index 1492caa8d..bdb964b3d 100644 --- a/src/Ombi.Core/Rule/Rules/Search/ExistingRequestRule.cs +++ b/src/Ombi.Core/Rule/Rules/Search/ExistingRequestRule.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Linq; +using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Ombi.Core.Models.Requests; using Ombi.Core.Models.Requests.Movie; @@ -21,8 +22,8 @@ namespace Ombi.Core.Rule.Rules.Search public async Task Execute(SearchViewModel obj) { - var movieRequests = Movie.GetAllQueryable(); - var existing = await movieRequests.FirstOrDefaultAsync(x => x.ProviderId == obj.Id); + var movieRequests = await Movie.GetAllAsync(); + var existing = movieRequests.FirstOrDefault(x => x.ProviderId == obj.Id); if (existing != null) // Do we already have a request for this? { @@ -33,14 +34,14 @@ namespace Ombi.Core.Rule.Rules.Search return Success(); } - var tvRequests = Tv.GetAllQueryable(); - var movieExisting = await tvRequests.FirstOrDefaultAsync(x => x.ProviderId == obj.Id); - if (movieExisting != null) // Do we already have a request for this? + var tvRequests = await Tv.GetAllAsync(); + var tv = tvRequests.FirstOrDefault(x => x.ProviderId == obj.Id); + if (tv != null) // Do we already have a request for this? { obj.Requested = true; - obj.Approved = movieExisting.Approved; - obj.Available = movieExisting.Available; + obj.Approved = tv.Approved; + obj.Available = tv.Available; return Success(); } diff --git a/src/Ombi/ClientApp/app/requests/movierequests.component.ts b/src/Ombi/ClientApp/app/requests/movierequests.component.ts index 1b88c75fe..cd4f5c838 100644 --- a/src/Ombi/ClientApp/app/requests/movierequests.component.ts +++ b/src/Ombi/ClientApp/app/requests/movierequests.component.ts @@ -59,12 +59,7 @@ export class MovieRequestsComponent implements OnInit, OnDestroy { loadMore() { - this.requestService.getMovieRequests(this.amountToLoad, this.currentlyLoaded + 1) - .takeUntil(this.subscriptions) - .subscribe(x => { - this.movieRequests.push.apply(this.movieRequests, x); - this.currentlyLoaded = this.currentlyLoaded + this.amountToLoad; - }); + this.loadRequests(this.amountToLoad, this.currentlyLoaded); } search(text: any) { @@ -74,6 +69,7 @@ export class MovieRequestsComponent implements OnInit, OnDestroy { removeRequest(request: IMovieRequestModel) { this.requestService.removeMovieRequest(request); this.removeRequestFromUi(request); + this.loadRequests(1, this.currentlyLoaded); } changeAvailability(request: IMovieRequestModel, available: boolean) { @@ -94,6 +90,15 @@ export class MovieRequestsComponent implements OnInit, OnDestroy { this.updateRequest(request); } + private loadRequests(amountToLoad: number, currentlyLoaded: number) { + this.requestService.getMovieRequests(amountToLoad, currentlyLoaded + 1) + .takeUntil(this.subscriptions) + .subscribe(x => { + this.movieRequests.push.apply(this.movieRequests, x); + this.currentlyLoaded = currentlyLoaded + amountToLoad; + }); + } + private updateRequest(request: IMovieRequestModel) { this.requestService.updateMovieRequest(request) .takeUntil(this.subscriptions) diff --git a/src/Ombi/ClientApp/app/services/notification.service.ts b/src/Ombi/ClientApp/app/services/notification.service.ts index 04ef56989..ab1cb7417 100644 --- a/src/Ombi/ClientApp/app/services/notification.service.ts +++ b/src/Ombi/ClientApp/app/services/notification.service.ts @@ -5,6 +5,7 @@ import { Message } from 'primeng/components/common/api'; export class NotificationService { messages: Message[] = []; public addMessage(message: Message) { + this.clearMessages(); this.messages.push(message); this.messages = JSON.parse(JSON.stringify(this.messages)); // NOTE: THIS IS A HACK AROUND A BUG https://github.com/primefaces/primeng/issues/2943 }