Пример #1
0
        public async Task RunTests_GivenNoTestResults_LogsAndreturnsEmptyResults()
        {
            //Arrange
            IEnumerable <ProviderResult> providerResults = new[] { new ProviderResult {
                                                                       Provider = new ProviderSummary {
                                                                           Id = ProviderId
                                                                       }, SpecificationId = SpecificationId
                                                                   } };
            IEnumerable <TestScenario>          scenarios = new[] { new TestScenario() };
            IEnumerable <ProviderSourceDataset> providerSourceDatasets = new ProviderSourceDataset[0];
            IEnumerable <TestScenarioResult>    testScenarioResults    = new TestScenarioResult[0];
            SpecificationSummary specificationSummary = new SpecificationSummary();
            BuildProject         buildProject         = new BuildProject();

            ILogger logger = CreateLogger();

            TestEngine testEngine = CreateTestEngine(logger: logger);

            //Act
            IEnumerable <TestScenarioResult> results = await testEngine.RunTests(scenarios, providerResults, providerSourceDatasets,
                                                                                 testScenarioResults, specificationSummary, buildProject);

            results
            .Count()
            .Should()
            .Be(0);

            logger
            .Received(1)
            .Warning(Arg.Is($"No test results generated for provider: {ProviderId} on specification: {SpecificationId}"));
        }
Пример #2
0
        public async Task RunTests_GivenNoTestScenarios_LogsAndreturnsEmptyResults()
        {
            //Arrange
            IEnumerable <ProviderResult>        providerResults        = new[] { new ProviderResult() };
            IEnumerable <TestScenario>          scenarios              = new TestScenario[0];
            IEnumerable <ProviderSourceDataset> providerSourceDatasets = new ProviderSourceDataset[0];
            IEnumerable <TestScenarioResult>    testScenarioResults    = new TestScenarioResult[0];
            SpecificationSummary specificationSummary = new SpecificationSummary();
            BuildProject         buildProject         = new BuildProject();

            ILogger logger = CreateLogger();

            TestEngine testEngine = CreateTestEngine(logger: logger);

            //Act
            IEnumerable <TestScenarioResult> results = await testEngine.RunTests(scenarios, providerResults, providerSourceDatasets,
                                                                                 testScenarioResults, specificationSummary, buildProject);

            results
            .Count()
            .Should()
            .Be(0);

            logger
            .Received(1)
            .Warning(Arg.Is("No test scenarios were supplied to execute tests"));
        }
        private static void AddProviderSourceDatasetToResults(Dictionary <string, Dictionary <string, ProviderSourceDataset> > results, DocumentEntity <ProviderSourceDataset> providerSourceDatasetDocument)
        {
            ProviderSourceDataset providerSourceDatasetResult = providerSourceDatasetDocument.Content;

            string providerId         = providerSourceDatasetDocument.Content.ProviderId;
            string dataRelationshipId = providerSourceDatasetDocument.Content.DataRelationship.Id;

            results[providerId].Add(dataRelationshipId, providerSourceDatasetResult);
        }
Пример #4
0
        public async Task UpdateCurrentProviderSourceDatasetsFloodsCosmosAndWaitsForAllTasks()
        {
            ProviderSourceDataset one   = NewProviderSourceDataset();
            ProviderSourceDataset two   = NewProviderSourceDataset();
            ProviderSourceDataset three = NewProviderSourceDataset();

            await WhenTheCurrentProviderSourceDatasetsAreUpdated(one, two, three);

            ThenTheProviderSourceDatasetsWereUpserted(one, two, three);
        }
Пример #5
0
        public async Task RunTests_GivenTestResultsReturnedFromExecutorWhereNoStepsExecuted_ReturnsOneIgnoreTestResult()
        {
            //Arrange
            ProviderResult providerResult = new ProviderResult {
                Provider = new ProviderSummary {
                    Id = ProviderId, Name = "any provider"
                }, SpecificationId = SpecificationId
            };

            IEnumerable <ProviderResult>        providerResults        = new[] { providerResult };
            IEnumerable <TestScenario>          scenarios              = new[] { new TestScenario() };
            IEnumerable <ProviderSourceDataset> providerSourceDatasets = new ProviderSourceDataset[0];
            IEnumerable <TestScenarioResult>    testScenarioResults    = new TestScenarioResult[0];

            SpecModel.SpecificationSummary specificationSummary = new SpecModel.SpecificationSummary {
                Id = SpecificationId, Name = "spec-name"
            };

            BuildProject buildProject = new BuildProject();

            IEnumerable <ScenarioResult> scenarioResults = new[]
            {
                new ScenarioResult  {
                    Scenario = new Reference("sceanrio=id", "scenario name")
                }
            };

            ILogger logger = CreateLogger();

            IGherkinExecutor gherkinExecutor = CreateGherkinExecutor();

            gherkinExecutor
            .Execute(Arg.Any <ProviderResult>(), Arg.Any <IEnumerable <ProviderSourceDataset> >(), Arg.Any <IEnumerable <TestScenario> >(), Arg.Any <BuildProject>())
            .Returns(scenarioResults);

            TestEngine testEngine = CreateTestEngine(gherkinExecutor, logger);

            //Act
            IEnumerable <TestScenarioResult> results = await testEngine.RunTests(scenarios, providerResults, providerSourceDatasets,
                                                                                 testScenarioResults, specificationSummary, buildProject);

            results
            .Count()
            .Should()
            .Be(1);

            results
            .First()
            .TestResult
            .Should()
            .Be(TestResult.Ignored);
        }
Пример #6
0
        private void AndTheProviderSourceDataset(
            string specificationId,
            string providerId,
            string dataRelationshipId,
            ProviderSourceDataset providerSourceDataset)
        {
            string documentKey = $"{specificationId}_{dataRelationshipId}_{providerId}";

            _cosmosRepository
            .TryReadDocumentByIdPartitionedAsync <ProviderSourceDataset>(Arg.Is(documentKey), Arg.Is(providerId))
            .Returns(new DocumentEntity <ProviderSourceDataset>
            {
                Deleted = false,
                Content = providerSourceDataset
            });
        }
Пример #7
0
        protected static object GetActualValue(IEnumerable <ProviderSourceDataset> datasets, string datasetRelationshipName, string fieldName)
        {
            object actualValue = null;

            ProviderSourceDataset providerSourceDataset = datasets.Where(d => d.DataRelationship.Name.Equals(datasetRelationshipName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();

            if (providerSourceDataset != null)
            {
                List <Dictionary <string, object> > rows = providerSourceDataset.Current.Rows;

                if (rows.Count > 0)
                {
                    actualValue = rows.First().First(r => r.Key.ToUpperInvariant() == fieldName.ToUpperInvariant()).Value;
                }
            }

            return(actualValue);
        }
Пример #8
0
        private async Task PersistDataset(TableLoadResult loadResult, Dataset dataset, DatasetDefinition datasetDefinition, BuildProject buildProject, string specificationId, string relationshipId, int version, Reference user)
        {
            IEnumerable <ProviderSummary> providerSummaries = await _providerService.FetchCoreProviderData();

            Guard.IsNullOrWhiteSpace(relationshipId, nameof(relationshipId));

            IList <ProviderSourceDataset> providerSourceDatasets = new List <ProviderSourceDataset>();

            if (buildProject.DatasetRelationships == null)
            {
                _logger.Error($"No dataset relationships found for build project with id : '{buildProject.Id}' for specification '{specificationId}'");
                return;
            }

            DatasetRelationshipSummary relationshipSummary = buildProject.DatasetRelationships.FirstOrDefault(m => m.Relationship.Id == relationshipId);

            if (relationshipSummary == null)
            {
                _logger.Error($"No dataset relationship found for build project with id : {buildProject.Id} with data definition id {datasetDefinition.Id} and relationshipId '{relationshipId}'");
                return;
            }

            ConcurrentDictionary <string, ProviderSourceDataset> existingCurrent = new ConcurrentDictionary <string, ProviderSourceDataset>();

            IEnumerable <ProviderSourceDataset> existingCurrentDatasets = await _providerResultsRepositoryPolicy.ExecuteAsync(() =>
                                                                                                                              _providersResultsRepository.GetCurrentProviderSourceDatasets(specificationId, relationshipId));

            if (existingCurrentDatasets.AnyWithNullCheck())
            {
                foreach (ProviderSourceDataset currentDataset in existingCurrentDatasets)
                {
                    existingCurrent.TryAdd(currentDataset.ProviderId, currentDataset);
                }
            }

            ConcurrentDictionary <string, ProviderSourceDataset> resultsByProviderId = new ConcurrentDictionary <string, ProviderSourceDataset>();

            ConcurrentDictionary <string, ProviderSourceDataset> updateCurrentDatasets = new ConcurrentDictionary <string, ProviderSourceDataset>();

            Parallel.ForEach(loadResult.Rows, (RowLoadResult row) =>
            {
                IEnumerable <string> allProviderIds = GetProviderIdsForIdentifier(datasetDefinition, row, providerSummaries);

                foreach (string providerId in allProviderIds)
                {
                    if (!resultsByProviderId.TryGetValue(providerId, out ProviderSourceDataset sourceDataset))
                    {
                        sourceDataset = new ProviderSourceDataset
                        {
                            DataGranularity            = relationshipSummary.DataGranularity,
                            SpecificationId            = specificationId,
                            DefinesScope               = relationshipSummary.DefinesScope,
                            DataRelationship           = new Reference(relationshipSummary.Relationship.Id, relationshipSummary.Relationship.Name),
                            DatasetRelationshipSummary = new Reference(relationshipSummary.Id, relationshipSummary.Name),
                            ProviderId = providerId
                        };

                        sourceDataset.Current = new ProviderSourceDatasetVersion
                        {
                            Rows                    = new List <Dictionary <string, object> >(),
                            Dataset                 = new VersionReference(dataset.Id, dataset.Name, version),
                            Date                    = DateTimeOffset.Now.ToLocalTime(),
                            ProviderId              = providerId,
                            Version                 = 1,
                            PublishStatus           = Models.Versioning.PublishStatus.Draft,
                            ProviderSourceDatasetId = sourceDataset.Id,
                            Author                  = user
                        };

                        if (!resultsByProviderId.TryAdd(providerId, sourceDataset))
                        {
                            resultsByProviderId.TryGetValue(providerId, out sourceDataset);
                        }
                    }

                    if (_featureToggle.IsUseFieldDefinitionIdsInSourceDatasetsEnabled())
                    {
                        sourceDataset.DataDefinitionId = relationshipSummary.DatasetDefinition.Id;

                        Dictionary <string, object> rows = new Dictionary <string, object>();

                        foreach (KeyValuePair <string, object> rowField in row.Fields)
                        {
                            foreach (TableDefinition tableDefinition in datasetDefinition.TableDefinitions)
                            {
                                FieldDefinition fieldDefinition = tableDefinition.FieldDefinitions.FirstOrDefault(m => m.Name == rowField.Key);
                                if (fieldDefinition != null)
                                {
                                    rows.Add(fieldDefinition.Id, rowField.Value);
                                }
                            }
                        }

                        sourceDataset.Current.Rows.Add(rows);
                    }
                    else
                    {
                        sourceDataset.DataDefinition = new Reference(relationshipSummary.DatasetDefinition.Id, relationshipSummary.DatasetDefinition.Name);

                        sourceDataset.Current.Rows.Add(row.Fields);
                    }
                }
            });

            ConcurrentBag <ProviderSourceDatasetVersion> historyToSave = new ConcurrentBag <ProviderSourceDatasetVersion>();

            List <Task> historySaveTasks = new List <Task>(resultsByProviderId.Count);

            SemaphoreSlim throttler = new SemaphoreSlim(initialCount: 15);

            foreach (KeyValuePair <string, ProviderSourceDataset> providerSourceDataset in resultsByProviderId)
            {
                await throttler.WaitAsync();

                historySaveTasks.Add(
                    Task.Run(async() =>
                {
                    try
                    {
                        string providerId = providerSourceDataset.Key;
                        ProviderSourceDataset sourceDataset = providerSourceDataset.Value;

                        ProviderSourceDatasetVersion newVersion = null;

                        if (existingCurrent.ContainsKey(providerId))
                        {
                            newVersion = existingCurrent[providerId].Current.Clone() as ProviderSourceDatasetVersion;

                            string existingDatasetJson = JsonConvert.SerializeObject(existingCurrent[providerId].Current.Rows);
                            string latestDatasetJson   = JsonConvert.SerializeObject(sourceDataset.Current.Rows);

                            if (existingDatasetJson != latestDatasetJson)
                            {
                                newVersion        = await _sourceDatasetsVersionRepository.CreateVersion(newVersion, existingCurrent[providerId].Current, providerId);
                                newVersion.Author = user;
                                newVersion.Rows   = sourceDataset.Current.Rows;

                                sourceDataset.Current = newVersion;

                                updateCurrentDatasets.TryAdd(providerId, sourceDataset);

                                historyToSave.Add(newVersion);
                            }

                            existingCurrent.TryRemove(providerId, out ProviderSourceDataset existingProviderSourceDataset);
                        }
                        else
                        {
                            newVersion = sourceDataset.Current;

                            updateCurrentDatasets.TryAdd(providerId, sourceDataset);

                            historyToSave.Add(newVersion);
                        }
                    }
                    finally
                    {
                        throttler.Release();
                    }
                }));
            }

            await TaskHelper.WhenAllAndThrow(historySaveTasks.ToArray());

            if (updateCurrentDatasets.Count > 0)
            {
                _logger.Information($"Saving {updateCurrentDatasets.Count()} updated source datasets");

                await _providerResultsRepositoryPolicy.ExecuteAsync(() =>
                                                                    _providersResultsRepository.UpdateCurrentProviderSourceDatasets(updateCurrentDatasets.Values));
            }

            if (_featureToggle.IsProviderResultsSpecificationCleanupEnabled() && existingCurrent.Any())
            {
                _logger.Information($"Removing {existingCurrent.Count()} missing source datasets");

                await _providerResultsRepositoryPolicy.ExecuteAsync(() =>
                                                                    _providersResultsRepository.DeleteCurrentProviderSourceDatasets(existingCurrent.Values));

                foreach (IEnumerable <ProviderSourceDataset> providerSourceDataSets in existingCurrent.Values.Partition <ProviderSourceDataset>(1000))
                {
                    await SendProviderSourceDatasetCleanupMessageToTopic(specificationId, ServiceBusConstants.TopicNames.ProviderSourceDatasetCleanup, providerSourceDataSets);
                }
            }

            if (historyToSave.Any())
            {
                _logger.Information($"Saving {historyToSave.Count()} items to history");
                await _sourceDatasetsVersionRepository.SaveVersions(historyToSave);
            }

            Reference relationshipReference = new Reference(relationshipSummary.Relationship.Id, relationshipSummary.Relationship.Name);

            DatasetAggregations datasetAggregations = GenerateAggregations(datasetDefinition, loadResult, specificationId, relationshipReference);

            if (!datasetAggregations.Fields.IsNullOrEmpty())
            {
                await _datasetsAggregationsRepository.CreateDatasetAggregations(datasetAggregations);
            }

            await _cacheProvider.RemoveAsync <List <CalculationAggregation> >($"{CacheKeys.DatasetAggregationsForSpecification}{specificationId}");


            await PopulateProviderSummariesForSpecification(specificationId, providerSummaries);
        }
Пример #9
0
        public async Task ReturnsCorrectDatasets_GivenValidSetOfProviders()
        {
            string relationshipIdOne = NewRandomString();
            string relationshipIdTwo = NewRandomString();

            Guid relationshipOneVersionKey = Guid.NewGuid();

            ProviderSourceDataset dataSetOne   = NewProviderSourceDataset();
            ProviderSourceDataset dataSetTwo   = NewProviderSourceDataset();
            ProviderSourceDataset dataSetThree = NewProviderSourceDataset();
            ProviderSourceDataset dataSetFour  = NewProviderSourceDataset();
            ProviderSourceDataset dataSetFive  = NewProviderSourceDataset();
            ProviderSourceDataset dataSetSix   = NewProviderSourceDataset();
            ProviderSourceDataset dataSetSeven = NewProviderSourceDataset();
            ProviderSourceDataset dataSetEight = NewProviderSourceDataset();

            GivenTheDatasetVersionKey(relationshipIdOne, relationshipOneVersionKey);
            AndTheProviderSourceDataset(_specificationId, dataSetOne.ProviderId, relationshipIdTwo, dataSetOne);
            AndTheProviderSourceDataset(_specificationId, dataSetTwo.ProviderId, relationshipIdTwo, dataSetTwo);
            AndTheProviderSourceDataset(_specificationId, dataSetThree.ProviderId, relationshipIdTwo, dataSetThree);
            AndTheProviderSourceDataset(_specificationId, dataSetFour.ProviderId, relationshipIdTwo, dataSetFour);
            AndTheProviderSourceDataset(_specificationId, dataSetFive.ProviderId, relationshipIdTwo, dataSetFive);
            AndTheProviderSourceDataset(_specificationId, dataSetSix.ProviderId, relationshipIdTwo, dataSetSix);
            AndTheProviderSourceDataset(_specificationId, dataSetSeven.ProviderId, relationshipIdTwo, dataSetSeven);
            AndTheProviderSourceDataset(_specificationId, dataSetEight.ProviderId, relationshipIdTwo, dataSetEight);

            Dictionary <string, Dictionary <string, ProviderSourceDataset> > datasets =
                await _repository.GetProviderSourceDatasetsByProviderIdsAndRelationshipIds(
                    _specificationId,
                    new[]
            {
                dataSetOne.ProviderId,
                dataSetTwo.ProviderId,
                dataSetThree.ProviderId,
                dataSetFour.ProviderId,
                dataSetFive.ProviderId,
                dataSetSix.ProviderId,
                dataSetSeven.ProviderId,
                dataSetEight.ProviderId
            },
                    new[]
            {
                relationshipIdOne,
                relationshipIdTwo
            });

            datasets.Values.SelectMany(x => x.Values).Count()
            .Should()
            .Be(8);

            datasets.Values.SelectMany(x => x.Values).Select(_ => _.Id)
            .Should()
            .BeEquivalentTo(new[]
            {
                dataSetOne,
                dataSetTwo,
                dataSetThree,
                dataSetFour,
                dataSetFive,
                dataSetSix,
                dataSetSeven,
                dataSetEight
            }.Select(_ => _.Id));
        }