using FluentAssertions; using Moq; using NUnit.Framework; using Strata.ContinuousImprovement.Biz.Filter; using Strata.ContinuousImprovement.Biz.Pagination; using Strata.ContinuousImprovement.Biz.QualityVariation.Events; using Strata.ContinuousImprovement.Biz.QualityVariation.Events.Filters; using Strata.ContinuousImprovement.JazzEntityFrameworkStub; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace Strata.ContinuousImprovement.Biz.Test.Unit.QualityVariation.Events { [TestFixture, Category("Unit")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Minor Code Smell", "S101:Types should be named in PascalCase", Justification = "Standard abbreviation")] public class QVIEventServiceTest { private Mock _qviEventSeriveMock; [SetUp] public void TestSetUp() { _qviEventSeriveMock = new Mock(); } [TestCaseSource(nameof(GetFrameworkWithFilterData))] public async Task TestGetPagedAsyncWithFramework(JazzEntityFrameworkFactory jazzEntityFrameworkFactory, QVIEventFilters filters, CancellationToken cancellationToken) { // arrange var qviEventService = new QVIEventService(jazzEntityFrameworkFactory, null, null); try { // act var taskResult = await qviEventService.GetPagedAsync(new PagingOptions(), filters, cancellationToken); // assert var result = taskResult.Data; if (string.IsNullOrEmpty(jazzEntityFrameworkFactory.TestName)) { result.Should().BeNullOrEmpty(); return; } result.Should().NotBeNullOrEmpty().And.AllBeOfType(); if (filters.QualityVariationEventIds.Any()) result.Should() .Contain(vo => filters.QualityVariationEventIds.Contains(vo.QualityVariationEventName.ToString())); } catch (AssertionException) { // Intentionally left empty } catch (Exception e) { e.Should().BeOfType(); } } [Test] public async Task TestFirstPage_Sorted_Asc() { // arrange var qviEventService = new QVIEventService(new JazzEntityFrameworkFactory(), null, null); var filters = new QVIEventFilters() { QualityVariationEventIds = new List(), IdentifiedSavings = 0, QVICases = 0, ConfigurationGuid = new Guid("e96abe3e-1526-43ca-815e-ee30c5b34345") }; var pagingOptions = new PagingOptions() { SortField = "QVICasesTotal", SortOrder = SortOrder.Asc }; // act var taskResult = await qviEventService.GetPagedAsync(pagingOptions, filters, CancellationToken.None); // assert taskResult.Data.Should().NotBeNullOrEmpty() .And.AllBeOfType() .And.HaveCountGreaterThan(1); taskResult.Data.First().QVICasesTotal.Should().BeLessThan(taskResult.Data.Last().QVICasesTotal); } [Test] public async Task TestFirstPage_Sorted_Desc() { // arrange var qviEventService = new QVIEventService(new JazzEntityFrameworkFactory(), null, null); var filters = new QVIEventFilters() { QualityVariationEventIds = new List(), IdentifiedSavings = 0, QVICases = 0, ConfigurationGuid = new Guid("e96abe3e-1526-43ca-815e-ee30c5b34345") }; var pagingOptions = new PagingOptions() { SortField = "QVICasesTotal", SortOrder = SortOrder.Desc }; // act var taskResult = await qviEventService .GetPagedAsync(pagingOptions, filters, CancellationToken.None); // assert taskResult.Data.Should().NotBeNullOrEmpty() .And.AllBeOfType() .And.HaveCountGreaterThan(1); taskResult.Data.First().QVICasesTotal.Should().BeGreaterThan(taskResult.Data.Last().QVICasesTotal); } [TestCaseSource(nameof(GetFilterData))] public void TestValidateFilters(QVIEventFilters filters) { if (filters == null) return; if (filters.QualityVariationEventIds.Any()) filters.QualityVariationEventIds.Should() .AllBeOfType() .And.NotBeNullOrEmpty(); } [TestCaseSource(typeof(JazzEntityFrameworkFactory), nameof(JazzEntityFrameworkFactory.GetFrameworksWithCancellation))] public async Task TestGetFilterOptionsAsync(JazzEntityFrameworkFactory jazzEntityFrameworkFactory, CancellationToken cancellationToken) { // arrange var qviEventService = new QVIEventService(jazzEntityFrameworkFactory, null, null); try { // act var taskResult = await qviEventService .GetFilterOptionsAsync(new Guid("e96abe3e-1526-43ca-815e-ee30c5b34345"), cancellationToken); // assert if (string.IsNullOrEmpty(jazzEntityFrameworkFactory.TestName)) { taskResult.Should().BeNull(); return; } taskResult.Should().BeOfType().And.NotBeNull(); taskResult.QualityVariationEvents.Should().AllBeOfType() .And.NotBeNullOrEmpty() .And.NotContain(cd => string.IsNullOrEmpty(cd.Id)) .And.NotContain(cd => string.IsNullOrEmpty(cd.Text)); } catch (NotImplementedException nie) { nie.Should().BeOfType(); } catch (Exception e) { e.Should().BeOfType(); } } #region JazzDbService Tests [TestCaseSource(typeof(JazzEntityFrameworkFactory), nameof(JazzEntityFrameworkFactory.GetFrameworks))] public async Task TestGetAllAsync(JazzEntityFrameworkFactory jazzEntityFrameworkFactory) { // arrange var qviEventService = new QVIEventService(jazzEntityFrameworkFactory, null, null); // act var taskResult = await qviEventService.GetAllAsync(CancellationToken.None); // assert if (string.IsNullOrEmpty(jazzEntityFrameworkFactory.TestName)) { taskResult.Should().BeNullOrEmpty(); } else { taskResult.Should().NotBeNullOrEmpty(); } } [Test, Ignore("Broken due to removal of Key from VariationCaseType")] public async Task TestGetAllAsync_Filtered() { // arrange var factory = new JazzEntityFrameworkFactory(); var filters = factory.QVIOpportunities; var qviEventService = new QVIEventService(factory, null, null); // act var taskResult = await qviEventService.GetAllAsync(CancellationToken.None); // assert taskResult.Should().NotBeNullOrEmpty() .And.AllBeOfType() .And.HaveCountGreaterThan(0) .And.NotContain(o => string.IsNullOrEmpty(o.ToString())) .And.Contain(o => filters.Select(f => f.QualityVariationEventName).Contains(o.QualityVariationEventName)) .And.Contain(o => filters.Select(f => f.ConfigurationGuid).Contains(o.ConfigurationGuid)); } [TestCaseSource(typeof(JazzEntityFrameworkFactory), nameof(JazzEntityFrameworkFactory.GetFrameworks)), Ignore("Complex key issue")] public async Task TestGetById(JazzEntityFrameworkFactory jazzEntityFrameworkFactory) { // arrange var qviEventService = new QVIEventService(jazzEntityFrameworkFactory, null, null); var tempResult = await qviEventService.GetAllAsync(CancellationToken.None); // act var taskResult = await qviEventService.GetById(tempResult.FirstOrDefault()?.QualityVariationEventId ?? 1, CancellationToken.None); // assert if (string.IsNullOrEmpty(jazzEntityFrameworkFactory.TestName)) { taskResult.Should().BeNull(); } else { taskResult.Should().NotBeNull() .And.BeOfType(); } } [TestCaseSource(typeof(JazzEntityFrameworkFactory), nameof(JazzEntityFrameworkFactory.GetFrameworks)), Ignore("Complex key issue")] public async Task TestGetByIds(JazzEntityFrameworkFactory jazzEntityFrameworkFactory) { // arrange var qviEventService = new QVIEventService(jazzEntityFrameworkFactory, null, null); var tempResult = await qviEventService.GetAllAsync(CancellationToken.None); var ids = tempResult.Select(o => o.QualityVariationEventId); // act var taskResult = await qviEventService.GetByIds(ids, CancellationToken.None); // assert if (string.IsNullOrEmpty(jazzEntityFrameworkFactory.TestName)) { taskResult.Should().BeNullOrEmpty(); } else { taskResult.Should().NotBeNullOrEmpty() .And.AllBeOfType(); } } #endregion [TearDown] public void TestTearDown() { // Method intentionally left empty. } #region TestCaseData public static IEnumerable GetFilterData() { yield return new TestCaseData(new QVIEventFilters { ConfigurationGuid = new Guid("e96abe3e-1526-43ca-815e-ee30c5b34345") }).SetName("No Filters"); yield return new TestCaseData(new QVIEventFilters { IdentifiedSavings = 1999, ConfigurationGuid = new Guid("e96abe3e-1526-43ca-815e-ee30c5b34345") }).SetName( "By Identified Savings"); yield return new TestCaseData(new QVIEventFilters { QualityVariationEventIds = new[] { "1" }, ConfigurationGuid = new Guid("e96abe3e-1526-43ca-815e-ee30c5b34345") }) .SetName("By Event ID"); } public static IEnumerable GetFrameworkWithFilterData() { var testCases = GetFilterData().ToList(); var jeffs = JazzEntityFrameworkFactory.GetFrameworksWithCancellation() .Select(tcd => tcd.Arguments); foreach (var tcd in jeffs) { var jeff = (JazzEntityFrameworkFactory)tcd[0]; var ct = (CancellationToken)tcd[1]; foreach (var testCase in testCases) { yield return new TestCaseData(jeff, testCase.Arguments.GetValue(0), ct) .SetName($"{testCase.TestName} {(string.IsNullOrEmpty(jeff.TestName) ? "w/o Jeff" : "w/Jeff")}") .SetCategory(jeff.TestName); } } } #endregion } }