Пример #1
0
        public void GenerateMany_CorrectResultsCount(int objectsCount, int resultCount)
        {
            var configurableObjectPrototype = GetConfigurablePrototype("test");

            var results = _dataGeneratorService.GenerateMany(configurableObjectPrototype, objectsCount);

            Assert.Equal(results.Count(), resultCount);
        }
Пример #2
0
        public async Task <IActionResult> GenerateObjectAsync([FromBody] ConfigurableObjectViewModel configurableObject,
                                                              string resultType, int?count)
        {
            var datasetIds = configurableObject.Fields
                             .Where(f => f.DatasetId != null)
                             .Select(f => f.DatasetId.GetValueOrDefault())
                             .Distinct().ToArray();

            var datasetPrototypes = datasetIds.Any() ? new List <DatasetPrototype>() : null;

            foreach (var datasetId in datasetIds)
            {
                var datasetValues = await _datasetService.GetValuesByDatasetIdAsync(datasetId);

                var prototypeValues = datasetValues.Select(v => v.Value).ToList <dynamic>();
                var prototype       = new DatasetPrototype(datasetId, prototypeValues);
                datasetPrototypes?.Add(prototype);
            }

            var modelForGenerate = _mapper.Map <ConfigurablePrototype>(configurableObject);
            var result           = _mapper.Map <IEnumerable <GeneratedObjectViewModel> >(_dataGenerator.GenerateMany(modelForGenerate, count ?? 1, datasetPrototypes)).ToList();

            SaveGeneratedObjects(result);

            if (resultType == null)
            {
                return(Ok(result));
            }

            var fileContents = _converters[resultType]?.Invoke(result) ?? throw new ArgumentException();

            return(File(Encoding.ASCII.GetBytes(fileContents), $"application/{resultType}",
                        $"{result.First().Name}_{result.First().DateCreated:s}.{resultType}".Replace(":", "").Replace("-", "")));
        }