示例#1
0
        public void ExportUserDataIntoJsonFormatFile()
        {
            string expectedDataResult = @"{""Name"":""Jack"",""LastName"":""Stilson"",""Age"":28,""Email"":""*****@*****.**""}" + Environment.NewLine;
            var    user = new User {
                Name     = "Jack",
                LastName = "Stilson",
                Age      = 28,
                Email    = "*****@*****.**"
            };

            _dataExporter.Export(user, _writableDestination);

            string dataFromFile = File.ReadAllText(FILEPATH);

            Assert.That(dataFromFile, Is.EqualTo(expectedDataResult));
        }
        private void Test <T>(string filename)
        {
            IEnumerable <T> records           = null;
            var             sampleFilename    = $"{_directory}sample\\{filename}";
            var             generatedFilename = string.Empty;

            using (var reader = new StreamReader(sampleFilename))
                using (var csv = new CsvReader(reader))
                {
                    records           = csv.GetRecords <T>();
                    generatedFilename = _objectToTest.Export(records);
                }
            Assert.That(File.ReadAllBytes(generatedFilename), Is.EquivalentTo(File.ReadAllBytes(sampleFilename)));
        }
示例#3
0
        public void Diff()
        {
            var dataTableFromLeftSource  = ReaderOfLeftDataSource.Read(ReadOptionsOfLeftSource);
            var dataTableFromRightSource = ReaderOfRightDataSource.Read(ReadOptionsOfRightSource);

            DataMerger = new DataMerger(dataTableFromLeftSource, dataTableFromRightSource
                                        , MergeOptions
                                        , ColumnNameBuilder);
            var mergeResult = DataMerger.Merge();

            lock (ExportLockObject)
            {
                DataExporter.Export(mergeResult, ExportOptions, ExcelHighlighter);
            }
        }
示例#4
0
        void DoExport(Portion portion = null)
        {
            exportTabs.SelectedTabPage = logTabPage;
            log.Clear();

            if (portion == null && !exporter.AnyNewDataToExport())
            {
                exporter_OnMessage(Program.LanguageManager.GetString(StringResources.Export_NoData));
                return;
            }

            SaveFileDialog dlg = new SaveFileDialog();

            dlg.Filter = Program.LanguageManager.GetString(StringResources.Export_Filter) + Resources.prizm;

            if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            exporter.ArchiveName = dlg.FileName;

            exporter.OnMessage -= exporter_OnMessage;
            exporter.OnDone    -= exporter_OnDone;
            exporter.OnError   -= exporter_OnError;

            exporter.OnMessage += exporter_OnMessage;
            exporter.OnDone    += exporter_OnDone;
            exporter.OnError   += exporter_OnError;

            progressPanel.Visible = true;

            Task task = portion == null ? new Task(() => exporter.Export()) : new Task(() => exporter.Export(portion));

            task.ContinueWith((_) => {
                if (progressPanel.InvokeRequired)
                {
                    progressPanel.Invoke(new MethodInvoker(() => { progressPanel.Visible = false; }));
                }
                else
                {
                    progressPanel.Visible = false;
                }

                LoadPortions();
            });
            task.Start();
        }
        public void Export_MethodCall_ShouldReturnTrue()
        {
            // Simulamos la serialización de los datos
            string serializerDataSimulation = "Estos son los datos serializados, me da igual el formato que sea";

            _serializer.Serialize(Arg.Any <User>()).Returns(serializerDataSimulation);

            // Simulamos la escritura de los datos en un destino
            _writableDestination.Write(serializerDataSimulation).Returns(true);

            // Actuamos sobre el método que queremos probar
            var result = _dataExporter.Export(new User(), _writableDestination);

            // Afirmamos que la prueba pase
            Assert.That(result, Is.True);
        }
示例#6
0
        public Task PriceJsonExport()
        {
            //Arrange
            IKnownExportTypesRegistrar registrar = new KnownExportTypesService();

            var searchServiceMock = new Mock <IPricingSearchService>();

            searchServiceMock.Setup(x => x.SearchPrices(It.Is <PricesSearchCriteria>(y => y.Skip == 0))).Returns(GetTestPriceResult <Price>());
            searchServiceMock.Setup(x => x.SearchPrices(It.Is <PricesSearchCriteria>(y => y.Skip > 0))).Returns(new PricingSearchResult <Price>());

            var priceServiceMock = new Mock <IPricingService>();

            var itemServiceMock = new Mock <IItemService>();

            var metadata = typeof(ExportablePrice).GetPropertyNames();
            var resolver = (IKnownExportTypesResolver)registrar;

            registrar.RegisterType(ExportedTypeDefinitionBuilder.Build <ExportablePrice, PriceExportDataQuery>()
                                   .WithDataSourceFactory(new PriceExportPagedDataSourceFactory(searchServiceMock.Object, priceServiceMock.Object, itemServiceMock.Object, null))
                                   .WithMetadata(metadata));

            var includedPropertyNames = new string[] { "Currency", "ProductId", "Sale", "List", "MinQuantity", "StartDate", "EndDate", "EffectiveValue" };
            var IncludedProperties    = metadata.PropertyInfos.Where(x => includedPropertyNames.Contains(x.FullName, StringComparer.OrdinalIgnoreCase)).ToArray();

            var exportProviderFactories = new[] {
                new Func <ExportDataRequest, IExportProvider>((request) => new JsonExportProvider(request)),
            };

            var dataExporter = new DataExporter(resolver, new ExportProviderFactory(exportProviderFactories));

            //Act
            var result = string.Empty;

            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream, Encoding.UTF8)
                {
                    AutoFlush = true
                })
                {
                    dataExporter.Export(
                        stream,
                        new ExportDataRequest()
                    {
                        DataQuery = new PriceExportDataQuery()
                        {
                            IncludedProperties = IncludedProperties
                        },
                        ExportTypeName = typeof(ExportablePrice).FullName,
                        ProviderName   = nameof(JsonExportProvider)
                    },
                        new Action <ExportProgressInfo>(x => Console.WriteLine(x.Description)),
                        new CancellationTokenWrapper(CancellationToken.None));

                    stream.Seek(0, SeekOrigin.Begin);

                    var reader = new StreamReader(stream);
                    result = reader.ReadToEnd();
                }

            //Assert
            var prices = JsonConvert.DeserializeObject <ExportablePrice[]>(result);

            Assert.NotNull(prices);
            Assert.Equal(3, prices.Length);
            Assert.Equal(2, prices.Count(x => x.List == 3.99m));
            Assert.Equal(1, prices.Count(x => x.ProductId == "d029526eab5948b189694f1bddba8e68"));

            return(Task.CompletedTask);
        }
示例#7
0
        public Task PriceCsvExport()
        {
            IKnownExportTypesRegistrar registrar = new KnownExportTypesService();

            var resolver = (IKnownExportTypesResolver)registrar;

            var searchServiceMock = new Mock <IPricingSearchService>();

            searchServiceMock.Setup(x => x.SearchPrices(It.Is <PricesSearchCriteria>(y => y.Skip == 0))).Returns(GetTestPriceResult <Price>());
            searchServiceMock.Setup(x => x.SearchPrices(It.Is <PricesSearchCriteria>(y => y.Skip > 0))).Returns(new PricingSearchResult <Price>());

            var priceServiceMock = new Mock <IPricingService>();

            var itemServiceMock = new Mock <IItemService>();

            var metadata = typeof(ExportablePrice).GetPropertyNames();

            registrar.RegisterType(ExportedTypeDefinitionBuilder.Build <ExportablePrice, PriceExportDataQuery>()
                                   .WithDataSourceFactory(new PriceExportPagedDataSourceFactory(searchServiceMock.Object, priceServiceMock.Object, itemServiceMock.Object, null))
                                   .WithMetadata(metadata)
                                   .WithTabularMetadata(typeof(TabularPrice).GetPropertyNames()));

            var exportProviderFactories = new[]
            {
                new Func <ExportDataRequest, IExportProvider>((request) => new JsonExportProvider(request)),
                new Func <ExportDataRequest, IExportProvider>((request) => new CsvExportProvider(request)),
            };


            var includedPropertyNames = new string[] { "Currency", "ProductId" };
            var IncludedProperties    = metadata.PropertyInfos.Where(x => includedPropertyNames.Contains(x.FullName, StringComparer.OrdinalIgnoreCase)).ToArray();
            var dataExporter          = new DataExporter(resolver, new ExportProviderFactory(exportProviderFactories));

            var result = string.Empty;

            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream, Encoding.UTF8)
                {
                    AutoFlush = true
                })
                {
                    dataExporter.Export(
                        stream,
                        new ExportDataRequest()
                    {
                        DataQuery = new PriceExportDataQuery()
                        {
                            IncludedProperties = IncludedProperties,
                        },
                        ExportTypeName = typeof(ExportablePrice).FullName,
                        ProviderName   = nameof(CsvExportProvider)
                    },
                        new Action <ExportProgressInfo>(x => Console.WriteLine(x.Description)),
                        new CancellationTokenWrapper(CancellationToken.None));

                    stream.Seek(0, SeekOrigin.Begin);

                    var reader = new StreamReader(stream);
                    result = reader.ReadToEnd();
                }

            Assert.Equal("Currency,ProductId\r\nUSD,d029526eab5948b189694f1bddba8e68\r\nEUR,85e7aa089a4e4a97a4394d668e37e3f8\r\nEUR,f427108e75ed4676923ddc47632111e3\r\n", result);

            return(Task.CompletedTask);
        }
示例#8
0
        public Task PricelistAssignmentJsonExport()
        {
            IKnownExportTypesRegistrar registrar = new KnownExportTypesService();

            var searchServiceMock = new Mock <IPricingSearchService>();

            searchServiceMock.Setup(x => x.SearchPricelistAssignments(It.Is <PricelistAssignmentsSearchCriteria>(y => y.Skip == 0))).Returns(GetPricelistAssignmentSearchResult <PricelistAssignment>());
            searchServiceMock.Setup(x => x.SearchPricelistAssignments(It.Is <PricelistAssignmentsSearchCriteria>(y => y.Skip > 0))).Returns(new PricingSearchResult <PricelistAssignment>());

            var priceServiceMock   = new Mock <IPricingService>();
            var catalogServiceMock = new Mock <ICatalogService>();

            var metadata = typeof(ExportablePricelistAssignment).GetPropertyNames();
            var resolver = (IKnownExportTypesResolver)registrar;

            registrar.RegisterType(ExportedTypeDefinitionBuilder.Build <ExportablePricelistAssignment, PricelistAssignmentExportDataQuery>()
                                   .WithDataSourceFactory(new PricelistAssignmentExportPagedDataSourceFactory(searchServiceMock.Object, priceServiceMock.Object, catalogServiceMock.Object))
                                   .WithMetadata(metadata));

            var exportProviderFactories = new[] {
                new Func <ExportDataRequest, IExportProvider>((request) => new JsonExportProvider(request)),
            };

            var dataExporter          = new DataExporter(resolver, new ExportProviderFactory(exportProviderFactories));
            var includedPropertyNames = new string[] { "CatalogId", "PricelistId", "Priority", "Id" };
            var IncludedProperties    = metadata.PropertyInfos.Where(x => includedPropertyNames.Contains(x.FullName, StringComparer.OrdinalIgnoreCase)).ToArray();

            //Act
            var result = string.Empty;

            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream, Encoding.UTF8)
                {
                    AutoFlush = true
                })
                {
                    dataExporter.Export(
                        stream,
                        new ExportDataRequest()
                    {
                        DataQuery = new PricelistAssignmentExportDataQuery()
                        {
                            IncludedProperties = IncludedProperties
                        },
                        ExportTypeName = typeof(ExportablePricelistAssignment).FullName,
                        ProviderName   = nameof(JsonExportProvider)
                    },
                        new Action <ExportProgressInfo>(x => Console.WriteLine(x.Description)),
                        new CancellationTokenWrapper(CancellationToken.None));

                    stream.Seek(0, SeekOrigin.Begin);

                    var reader = new StreamReader(stream);
                    result = reader.ReadToEnd();
                }

            //Assert
            var pricelistsAssigments = JsonConvert.DeserializeObject <ExportablePricelistAssignment[]>(result);

            Assert.NotNull(pricelistsAssigments);
            Assert.NotEmpty(pricelistsAssigments);

            var assigment = pricelistsAssigments.FirstOrDefault(x => x.Id == "d4a4e5271046497eaef61ee47efe6215");

            Assert.NotNull(assigment);
            Assert.Null(assigment.Name);
            Assert.Equal("80873a35a34a4cf8997ac87e69cac6d6", assigment.PricelistId);
            Assert.Equal("4974648a41df4e6ea67ef2ad76d7bbd4", assigment.CatalogId);
            Assert.Equal(10, assigment.Priority);

            return(Task.CompletedTask);
        }
示例#9
0
        public Task PricelistJsonExport()
        {
            //Arrange
            IKnownExportTypesRegistrar registrar = new KnownExportTypesService();

            var searchServiceMock = new Mock <IPricingSearchService>();

            searchServiceMock.Setup(x => x.SearchPricelists(It.Is <PricelistSearchCriteria>(y => y.Skip == 0))).Returns(GetTestPricelistResult <Pricelist>());
            searchServiceMock.Setup(x => x.SearchPricelists(It.Is <PricelistSearchCriteria>(y => y.Skip > 0))).Returns(new PricingSearchResult <Pricelist>());

            searchServiceMock.Setup(x => x.SearchPrices(It.IsAny <PricesSearchCriteria>())).Returns(new PricingSearchResult <Price>()
            {
                TotalCount = 0,
                Results    = new List <Price>(),
            });

            var priceServiceMock = new Mock <IPricingService>();

            var metadata = typeof(ExportablePricelist).GetPropertyNames();
            var resolver = (IKnownExportTypesResolver)registrar;

            registrar.RegisterType(ExportedTypeDefinitionBuilder.Build <ExportablePricelist, PricelistExportDataQuery>()
                                   .WithDataSourceFactory(new PricelistExportPagedDataSourceFactory(searchServiceMock.Object, priceServiceMock.Object))
                                   .WithMetadata(metadata));

            var exportProviderFactories = new[] {
                new Func <ExportDataRequest, IExportProvider>((request) => new JsonExportProvider(request)),
            };

            var dataExporter          = new DataExporter(resolver, new ExportProviderFactory(exportProviderFactories));
            var includedPropertyNames = new string[] { "Currency", "Id", "Name" };
            var IncludedProperties    = metadata.PropertyInfos.Where(x => includedPropertyNames.Contains(x.FullName, StringComparer.OrdinalIgnoreCase)).ToArray();

            //Act
            var result = string.Empty;

            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream, Encoding.UTF8)
                {
                    AutoFlush = true
                })
                {
                    dataExporter.Export(
                        stream,
                        new ExportDataRequest()
                    {
                        DataQuery = new PricelistExportDataQuery()
                        {
                            IncludedProperties = IncludedProperties
                        },
                        ExportTypeName = typeof(ExportablePricelist).FullName,
                        ProviderName   = nameof(JsonExportProvider)
                    },
                        new Action <ExportProgressInfo>(x => Console.WriteLine(x.Description)),
                        new CancellationTokenWrapper(CancellationToken.None));

                    stream.Seek(0, SeekOrigin.Begin);

                    var reader = new StreamReader(stream);
                    result = reader.ReadToEnd();
                }

            //Assert
            var pricelists = JsonConvert.DeserializeObject <ExportablePricelist[]>(result);

            Assert.NotNull(pricelists);
            Assert.NotEmpty(pricelists);

            var pricelist = pricelists.FirstOrDefault(x => x.Name.Equals("BoltsUSD", StringComparison.InvariantCultureIgnoreCase));

            Assert.NotNull(pricelist);
            Assert.Equal("0456b3ebc0a24c0ab7054ec9a5cea78e", pricelist.Id);
            Assert.Equal("USD", pricelist.Currency);

            return(Task.CompletedTask);
        }
示例#10
0
        /// <summary>
        /// В очереди появилась задача - выполняем.
        /// Метод всегда запускается в отдельном потоке (new Thread(ReceiveRetroProcess)) - не изменять это!
        /// </summary>
        private void ReceiveRetroProcess()
        {
            QueueObjectAction objectAction = null;

            try
            {
                if (_queueAction == null || _queueAction.IsEmptyQueue())
                {
                    return;
                }
#if DEBUG
                Log.Info("По таймеру разбираем очередь ");
#endif

                objectAction = _queueAction.GetFirst();

                // Обрабатываем только новые действия
                if (objectAction == null || _listActionsRunning.ContainsKey(objectAction.Guid))
                {
                    return;
                }

                if (!IsReceivedObject(objectAction))
                {
                    Log.Info("Выполнение действия прервано " + objectAction.Action.GuidAction);
                    return;
                }

                _listActionsRunning[objectAction.Guid] = 0;

                DateTime dtNow = DateTime.Now;

                // Отложим время выполнения действия если задана соответствующая настройка
                if (objectAction.Action.RunTimeOffset > 0)
                {
                    Log.Info("Выполнение действия отложено " + objectAction.Action.GuidAction);
                    Thread.Sleep(TimeSpan.FromSeconds(objectAction.Action.RunTimeOffset));
                    Log.Info("Выполнение действия возобновлено " + objectAction.Action.GuidAction);
                }

                var dataRegistry = Registry.GetNewRegistry(_rsduDbConf.DSN, _rsduDbConf.Login, _rsduDbConf.Password);
                dataRegistry.SetAppName(_rsduDbConf.AppName);

                lock (dataRegistry.Locker)
                {
                    Log.Info("Количество задач в очереди до обработки " + _queueAction.Count());
                    _timerThread.Start();
                    var archives = new List <ParamData>();

                    // Получение кадра из бд
                    RetroKadr kadr = GetKadr(objectAction.Action.KadrId, dataRegistry);
                    if (kadr == null)
                    {
                        _timerThread.Stop();
                        RemoveActionRunning(objectAction);
                        return;
                    }

                    var conditionsDates = new List <QueryConditionsDates>();

                    // Расчет периода и запрос архивов
                    Dictionary <DateTime, DateTime> startFinish = new Dictionary <DateTime, DateTime>();
                    foreach (var queryCondition in objectAction.Action.QueryConditions)
                    {
                        DateTime start  = GetDateStart(queryCondition, dtNow);
                        DateTime finish = GetDateFinish(queryCondition.Interval, queryCondition.IntervalCount, start);

                        conditionsDates.Add(new QueryConditionsDates(queryCondition, start, finish));

                        List <ParamData> archive = GetDataArc(start, finish, kadr, dataRegistry);

                        if (archive != null)
                        {
                            archives.AddRange(archive);
                        }

                        startFinish.Add(start, finish);
                    }

                    IDataExporter dataExporter = new DataExporter(objectAction.Action, kadr, archives, conditionsDates);
                    Log.Info("Выполняется задача " + objectAction.Guid);
                    bool exported = dataExporter.Export(startFinish, dtNow);
                    Log.Info("Выполнена задача " + objectAction.Guid + " - " + exported);

                    if (exported)
                    {
                        _queueAction.DeleteRecord(objectAction);
                    }
                    else
                    {
                        _queueAction.DeleteRecord(objectAction);
                        objectAction.CountError = objectAction.CountError + 1;
                        _queueAction.EnqueueObject(objectAction);
                    }
                    _evGetRetro.Set();
                    _timerThread.Stop();
                    RemoveActionRunning(objectAction);
#if DEBUG
                    Log.Info("Количество задач в очереди после обработки " + _queueAction.Count());
#endif
                }
                dataRegistry = null;
            }
            catch (ThreadAbortException)
            {
                // При остановке сервиса все потоки грохаются с помощью Thread.Abort.
                // Предотвращаем появление исключения "Поток находился в процессе ожидания" из-за того,
                // что некоторые потоки находятся в приостановленном состоянии т.к. мы Thread.Sleep делаем
                Thread.ResetAbort();
            }
            catch (Exception ex)
            {
                Log.Error("Не удалось выполнить задачу из очереди", ex);

                if (objectAction != null)
                {
                    RemoveActionRunning(objectAction);
                }
            }
        }