public async Task Handle_ReturnsAllParents_FromSitesSharingEntirePath()
        {
            // Arrange
            var query = new LocationsParentsQuery(new[] { "site1", "trust1", "region1", "national1" });

            // Act
            var results = await Service.Handle(query);

            // Assert
            results.Should().NotBeNull();
            results.Count.Should().Be(4);

            results["site1"].Count().Should().Be(4);
            results["site1"].Count(l => l.Type == LocationType.National).Should().Be(1);
            results["site1"].Count(l => l.Type == LocationType.Region).Should().Be(1);
            results["site1"].Count(l => l.Type == LocationType.Trust).Should().Be(1);
            results["site1"].Count(l => l.Type == LocationType.Site).Should().Be(1);

            results["trust1"].Count().Should().Be(3);
            results["trust1"].Count(l => l.Type == LocationType.National).Should().Be(1);
            results["trust1"].Count(l => l.Type == LocationType.Region).Should().Be(1);
            results["trust1"].Count(l => l.Type == LocationType.Trust).Should().Be(1);

            results["region1"].Count().Should().Be(2);
            results["region1"].Count(l => l.Type == LocationType.National).Should().Be(1);
            results["region1"].Count(l => l.Type == LocationType.Region).Should().Be(1);

            results["national1"].Count().Should().Be(1);
            results["national1"].Count(l => l.Type == LocationType.National).Should().Be(1);
        }
        public async Task Handle_ReturnsAllParents_FromMultipleSites()
        {
            // Arrange
            var query = new LocationsParentsQuery(new[] { "site1", "site2" });

            // Act
            var results = await Service.Handle(query);

            // Assert
            results.Should().NotBeNull();
            results.Count.Should().Be(2);

            results["site1"].Count().Should().Be(4);
            results["site1"].Count(l => l.Type == LocationType.National).Should().Be(1);
            results["site1"].Count(l => l.Type == LocationType.Region).Should().Be(1);
            results["site1"].Count(l => l.Type == LocationType.Trust).Should().Be(1);
            results["site1"].Count(l => l.Type == LocationType.Site).Should().Be(1);

            results["site2"].Count().Should().Be(4);
            results["site2"].Count(l => l.Type == LocationType.National).Should().Be(1);
            results["site2"].Count(l => l.Type == LocationType.Region).Should().Be(1);
            results["site2"].Count(l => l.Type == LocationType.Trust).Should().Be(1);
            results["site2"].Count(l => l.Type == LocationType.Site).Should().Be(1);
        }
        /// <inheritdoc/>
        public override async Task <IDictionary <string, IEnumerable <Models.Location> > > Handle(LocationsParentsQuery param)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }

            // Initialise with the key and value the same
            var locationIds = param.LocationIds.ToList();

            var maxLoops = MaxLoopIterations;
            var result   = param.LocationIds.Distinct().ToDictionary(
                k => k,
                v => new List <Models.Location>());

            while (locationIds.Any() && (maxLoops--) > 0)
            {
                var ids = locationIds.ToList();

                var items = (await DatabaseAccess.GetItemsAsync(
                                 ConnectionSettings,
                                 (Models.Location location) => ids.Contains(location.LocationId))).ToList();

                if (!items.Any())
                {
                    break;
                }

                foreach (var item in items)
                {
                    var keys = result
                               .Where(kvp =>
                                      (!kvp.Value.Any() && kvp.Key == item.LocationId) ||
                                      (kvp.Value.Any() && kvp.Value.Last().ParentId == item.LocationId))
                               .Select(kvp => kvp.Key);

                    foreach (var key in keys)
                    {
                        result[key].Add(item);
                    }
                }

                locationIds = result
                              .Where(r => r.Value.Count > 0)
                              .Select(r => r.Value.Last().ParentId).ToList();
            }

            // Stop the value being a list
            return(result.ToDictionary(
                       k => k.Key,
                       v => v.Value.AsEnumerable()));
        }