Esempio n. 1
0
        private dynamic GetRandomValue(DatasetPrototype datasetPrototype)
        {
            var random = new Random();
            var index  = random.Next(0, datasetPrototype.Values.Count - 1);

            return(datasetPrototype.Values.ElementAt(index));
        }
Esempio n. 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("-", "")));
        }
Esempio n. 3
0
 public static string Pick(this IRobustRandom random, DatasetPrototype prototype)
 {
     return(random.Pick(prototype.Values));
 }
 public dynamic Generate(SupportedTypes type, IEnumerable <ConstraintPrototype> constraints, DatasetPrototype datasetPrototype = null)
 {
     if (datasetPrototype != null)
     {
         return(type == SupportedTypes.Enum
             ? _datasetValueGenerator.GenerateMany(datasetPrototype.Values)
             : _datasetValueGenerator.Generate(datasetPrototype.Values));
     }
     try
     {
         return(_fieldsGenerators[type].Generate(constraints));
     }
     catch (ConstraintNotSupportedException ex)
     {
         ex.FieldId = (int)type;
         throw;
     }
 }
Esempio n. 5
0
 private void ConfigureFieldGeneratorMock(DatasetPrototype datasetPrototype, SupportedTypes supportedType)
 {
     _fieldGeneratorMock.Setup(generator =>
                               generator.Generate(supportedType, new List <ConstraintPrototype>(), datasetPrototype)).Returns(GetRandomValue(datasetPrototype));
 }