From 2424d28015fc6615faf60224c6c61a062bca4892 Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Sat, 30 Apr 2022 12:06:16 -0500 Subject: [PATCH] test: Unit tests for new command code --- src/Trash.Tests/Command/SonarrCommandTest.cs | 68 ++++++++++++++ .../Sonarr/ReleaseProfileListerTest.cs | 94 +++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 src/Trash.Tests/Command/SonarrCommandTest.cs create mode 100644 src/TrashLib.Tests/Sonarr/ReleaseProfileListerTest.cs diff --git a/src/Trash.Tests/Command/SonarrCommandTest.cs b/src/Trash.Tests/Command/SonarrCommandTest.cs new file mode 100644 index 00000000..6edf6ed6 --- /dev/null +++ b/src/Trash.Tests/Command/SonarrCommandTest.cs @@ -0,0 +1,68 @@ +using AutoFixture.NUnit3; +using CliFx.Exceptions; +using CliFx.Infrastructure; +using FluentAssertions; +using NSubstitute; +using NUnit.Framework; +using TestLibrary.AutoFixture; +using Trash.Command; +using TrashLib.Sonarr; + +namespace Trash.Tests.Command; + +[TestFixture] +[Parallelizable(ParallelScope.All)] +public class SonarrCommandTest +{ + [Test, AutoMockData] + public async Task List_terms_without_value_fails( + IConsole console, + SonarrCommand sut) + { + // When `--list-terms` is specified on the command line without a value, it gets a `null` value assigned. + sut.ListTerms = null; + + var act = () => sut.ExecuteAsync(console).AsTask(); + + await act.Should().ThrowAsync(); + } + + [Test, AutoMockData] + public async Task List_terms_with_empty_value_fails( + IConsole console, + SonarrCommand sut) + { + // If the user specifies a blank string as the value, it should still fail. + sut.ListTerms = ""; + + var act = () => sut.ExecuteAsync(console).AsTask(); + + await act.Should().ThrowAsync(); + } + + [Test, AutoMockData] + public async Task List_terms_uses_specified_trash_id( + [Frozen] IReleaseProfileLister lister, + IConsole console, + SonarrCommand sut) + { + sut.ListTerms = "some_id"; + + await sut.ExecuteAsync(console); + + lister.Received().ListTerms("some_id"); + } + + [Test, AutoMockData] + public async Task List_release_profiles_is_invoked( + [Frozen] IReleaseProfileLister lister, + IConsole console, + SonarrCommand sut) + { + sut.ListReleaseProfiles = true; + + await sut.ExecuteAsync(console); + + lister.Received().ListReleaseProfiles(); + } +} diff --git a/src/TrashLib.Tests/Sonarr/ReleaseProfileListerTest.cs b/src/TrashLib.Tests/Sonarr/ReleaseProfileListerTest.cs new file mode 100644 index 00000000..af42dd77 --- /dev/null +++ b/src/TrashLib.Tests/Sonarr/ReleaseProfileListerTest.cs @@ -0,0 +1,94 @@ +using AutoFixture.NUnit3; +using CliFx.Infrastructure; +using FluentAssertions; +using NSubstitute; +using NUnit.Framework; +using TestLibrary.AutoFixture; +using TrashLib.Sonarr; +using TrashLib.Sonarr.ReleaseProfile; +using TrashLib.Sonarr.ReleaseProfile.Guide; + +namespace TrashLib.Tests.Sonarr; + +[TestFixture] +[Parallelizable(ParallelScope.All)] +public class ReleaseProfileListerTest +{ + [Test, AutoMockData] + public void Release_profiles_appear_in_console_output( + [Frozen] ISonarrGuideService guide, + [Frozen(Matching.ImplementedInterfaces)] FakeInMemoryConsole console, + ReleaseProfileLister sut) + { + var testData = new[] + { + new ReleaseProfileData {Name = "First", TrashId = "123"}, + new ReleaseProfileData {Name = "Second", TrashId = "456"} + }; + + guide.GetReleaseProfileData().Returns(testData); + + sut.ListReleaseProfiles(); + + console.ReadOutputString().Should().ContainAll( + testData.SelectMany(x => new[] {x.Name, x.TrashId})); + } + + [Test, AutoMockData] + public void Terms_appear_in_console_output( + [Frozen] ISonarrGuideService guide, + [Frozen(Matching.ImplementedInterfaces)] FakeInMemoryConsole console, + ReleaseProfileLister sut) + { + var requiredData = new[] + { + new TermData {Name = "First", TrashId = "111", Term = "term1"}, + new TermData {Name = "Second", TrashId = "222", Term = "term2"} + }; + + var ignoredData = new[] + { + new TermData {Name = "Third", TrashId = "333", Term = "term3"}, + new TermData {Name = "Fourth", TrashId = "444", Term = "term4"} + }; + + var preferredData = new[] + { + new TermData {Name = "Fifth", TrashId = "555", Term = "term5"}, + new TermData {Name = "Sixth", TrashId = "666", Term = "term6"} + }; + + guide.GetUnfilteredProfileById(Arg.Any()).Returns(new ReleaseProfileData + { + Name = "Release Profile", + TrashId = "098", + Required = requiredData, + Ignored = ignoredData, + Preferred = new PreferredTermData[] + { + new() {Score = 100, Terms = preferredData} + } + }); + + sut.ListTerms("098"); + + var expectedOutput = new[] + { + requiredData.SelectMany(x => new[] {x.Name, x.TrashId}), + ignoredData.SelectMany(x => new[] {x.Name, x.TrashId}), + preferredData.SelectMany(x => new[] {x.Name, x.TrashId}) + }; + + console.ReadOutputString().Should().ContainAll(expectedOutput.SelectMany(x => x)); + } + + + [Test, AutoMockData] + public void Release_profile_trash_id_is_used_to_look_up_data( + [Frozen] ISonarrGuideService guide, + ReleaseProfileLister sut) + { + sut.ListTerms("098"); + guide.Received().GetUnfilteredProfileById("098"); + } +}