public void Should_extract_value_as_json()
        {
            var sut = new Squidex2CsvConverter("data.products");

            var values = sut.GetValues(content).ToArray();

            Assert.Equal(new object[] { "[\"Squidex\",\"CLI\"]" }, values);
        }
        public void Should_extract_value_from_property()
        {
            var sut = new Squidex2CsvConverter("created");

            var values = sut.GetValues(content).ToArray();

            Assert.Equal(new object[] { content.Created }, values);
        }
        public void Should_extract_value_from_nested_array()
        {
            var sut = new Squidex2CsvConverter("data.products.iv.1");

            var values = sut.GetValues(content).ToArray();

            Assert.Equal(new object[] { "CLI" }, values);
        }
        public void Should_return_invalid_if_index_out_of_range()
        {
            var sut = new Squidex2CsvConverter("data.products.iv.3");

            var values = sut.GetValues(content).ToArray();

            Assert.Equal(new object[] { "<INVALID>" }, values);
        }
        public void Should_extract_value_from_nested_object()
        {
            var sut = new Squidex2CsvConverter("data.user.iv.name");

            var values = sut.GetValues(content).ToArray();

            Assert.Equal(new object[] { "Squidex" }, values);
        }
        public void Should_return_invalid_if_property_in_nested_object_not_found()
        {
            var sut = new Squidex2CsvConverter("data.invalid");

            var values = sut.GetValues(content).ToArray();

            Assert.Equal(new object[] { "<INVALID>" }, values);
        }
        public void Should_extract_value_from_invariant_data_and_escape()
        {
            var sut = new Squidex2CsvConverter("data.multilineText");

            var values = sut.GetValues(content).ToArray();

            Assert.Equal(new object[] { "Hello\\nWorld" }, values);
        }
        public void Should_extract_value_from_localized_data()
        {
            var sut = new Squidex2CsvConverter("data.localizedText.en");

            var values = sut.GetValues(content).ToArray();

            Assert.Equal(new object[] { "Hello World" }, values);
        }
        public void Should_extract_value_from_invariant_data_draft()
        {
            var sut = new Squidex2CsvConverter("dataDraft.name");

            var values = sut.GetValues(content).ToArray();

            Assert.Equal(new object[] { "Hello World" }, values);
        }
        public void Should_use_alias_field_names()
        {
            var sut = new Squidex2CsvConverter("created,name=data.name.iv");

            Assert.Equal(new[] { "created", "name" }, sut.FieldNames);
        }
        public void Should_use_direct_field_names()
        {
            var sut = new Squidex2CsvConverter("created,lastModified,data.name.iv");

            Assert.Equal(new[] { "created", "lastModified", "data.name.iv" }, sut.FieldNames);
        }
Esempio n. 12
0
            public async Task Export(ExportArguments arguments)
            {
                var ctx = QueryContext.Default.Unpublished(arguments.Unpublished);

                var(_, service) = Configuration.Setup();

                var client = service.CreateContentsClient <DummyEntity, DummyData>(arguments.Schema);

                if (arguments.Format == Format.JSON)
                {
                    if (!string.IsNullOrWhiteSpace(arguments.Fields))
                    {
                        throw new SquidexException("Fields are not used for JSON export.");
                    }

                    var fileOrFolder = arguments.Output;

                    if (arguments.FilePerContent)
                    {
                        if (string.IsNullOrWhiteSpace(fileOrFolder))
                        {
                            fileOrFolder = $"{arguments.Schema}_{DateTime.UtcNow:yyyy-MM-dd-hh-mm-ss}";
                        }

                        if (!Directory.Exists(fileOrFolder))
                        {
                            Directory.CreateDirectory(fileOrFolder);
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(fileOrFolder))
                        {
                            fileOrFolder = $"{arguments.Schema}_{DateTime.UtcNow:yyyy-MM-dd-hh-mm-ss}.json";
                        }

                        if (File.Exists(fileOrFolder))
                        {
                            File.Delete(fileOrFolder);
                        }
                    }

                    await ExportAsync(arguments, entity =>
                    {
                        if (arguments.FilePerContent)
                        {
                            File.WriteAllText(Path.Combine(fileOrFolder, $"{arguments.Schema}_{entity.Id}.json"), entity.JsonPrettyString());
                        }
                        else
                        {
                            File.AppendAllText(fileOrFolder, entity.JsonPrettyString());
                        }
                    });
                }
                else
                {
                    if (arguments.FilePerContent)
                    {
                        throw new SquidexException("Multiple files are not supported for CSV export.");
                    }

                    if (string.IsNullOrWhiteSpace(arguments.Fields))
                    {
                        throw new SquidexException("Fields must be defined for CSV export.");
                    }

                    var file = arguments.Output;

                    if (string.IsNullOrWhiteSpace(file))
                    {
                        file = $"{arguments.Schema}_{DateTime.UtcNow:yyyy-MM-dd-hh-mm-ss}.csv";
                    }

                    var converter = new Squidex2CsvConverter(arguments.Fields);

                    using (var stream = new FileStream(file, FileMode.Create, FileAccess.Write))
                    {
                        using (var streamWriter = new StreamWriter(stream))
                        {
                            var csvOptions = new CsvConfiguration(CultureInfo.InvariantCulture)
                            {
                                Delimiter = ";"
                            };

                            using (var writer = new CsvWriter(streamWriter, csvOptions))
                            {
                                foreach (var fieldName in converter.FieldNames)
                                {
                                    writer.WriteField(fieldName);
                                }

                                writer.NextRecord();

                                await ExportAsync(arguments, entity =>
                                {
                                    foreach (var value in converter.GetValues(entity))
                                    {
                                        if (value is string text)
                                        {
                                            writer.WriteField(text, true);
                                        }
                                        else
                                        {
                                            writer.WriteField(value);
                                        }
                                    }

                                    writer.NextRecord();
                                });
                            }
                        }
                    }
                }
            }
Esempio n. 13
0
            public async Task Export(ExportArguments arguments)
            {
                var session = configuration.StartSession();

                if (arguments.Format == Format.JSON)
                {
                    var fileOrFolder = arguments.Output;

                    if (arguments.FilePerContent)
                    {
                        if (string.IsNullOrWhiteSpace(fileOrFolder))
                        {
                            fileOrFolder = $"{arguments.Schema}_{DateTime.UtcNow:yyyy-MM-dd-hh-mm-ss}";
                        }

                        Directory.CreateDirectory(fileOrFolder);
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(fileOrFolder))
                        {
                            fileOrFolder = $"{arguments.Schema}_{DateTime.UtcNow:yyyy-MM-dd-hh-mm-ss}.json";
                        }

                        if (File.Exists(fileOrFolder))
                        {
                            File.Delete(fileOrFolder);
                        }
                    }

                    if (arguments.FilePerContent)
                    {
                        await session.ExportAsync(arguments, log, async entity =>
                        {
                            var fileName = $"{arguments.Schema}_{entity.Id}.json";
                            var filePath = Path.Combine(fileOrFolder, fileName);

                            if (arguments.FullEntities)
                            {
                                await Helper.WriteJsonToFileAsync(entity, filePath);
                            }
                            else
                            {
                                await Helper.WriteJsonToFileAsync(entity.Data, filePath);
                            }
                        });
                    }
                    else if (arguments.JsonArray)
                    {
                        var allRecords = new List <DynamicContent>();

                        await session.ExportAsync(arguments, log, entity =>
                        {
                            allRecords.Add(entity);

                            return(Task.CompletedTask);
                        });

                        if (arguments.FullEntities)
                        {
                            await Helper.WriteJsonToFileAsync(allRecords, fileOrFolder);
                        }
                        else
                        {
                            await Helper.WriteJsonToFileAsync(allRecords.Select(x => x.Data), fileOrFolder);
                        }
                    }
                    else
                    {
                        await using (var stream = new FileStream(fileOrFolder, FileMode.Create, FileAccess.Write))
                        {
                            await using (var writer = new StreamWriter(stream))
                            {
                                await session.ExportAsync(arguments, log, async entity =>
                                {
                                    if (arguments.FullEntities)
                                    {
                                        await writer.WriteJsonAsync(entity);
                                    }
                                    else
                                    {
                                        await writer.WriteJsonAsync(entity.Data);
                                    }

                                    await writer.WriteLineAsync();
                                    await writer.WriteLineAsync(JsonSeparator);
                                });
                            }
                        }
                    }
                }
                else
                {
                    if (arguments.FilePerContent)
                    {
                        throw new CLIException("Multiple files are not supported for CSV export.");
                    }

                    var file = arguments.Output;

                    if (string.IsNullOrWhiteSpace(file))
                    {
                        file = $"{arguments.Schema}_{DateTime.UtcNow:yyyy-MM-dd-hh-mm-ss}.csv";
                    }

                    var converter = new Squidex2CsvConverter(arguments.Fields);

                    await using (var stream = new FileStream(file, FileMode.Create, FileAccess.Write))
                    {
                        await using (var streamWriter = new StreamWriter(stream))
                        {
                            var csvOptions = new CsvConfiguration(CultureInfo.InvariantCulture)
                            {
                                Delimiter = ";"
                            };

                            await using (var writer = new CsvWriter(streamWriter, csvOptions))
                            {
                                foreach (var fieldName in converter.FieldNames)
                                {
                                    writer.WriteField(fieldName);
                                }

                                await writer.NextRecordAsync();

                                await session.ExportAsync(arguments, log, async entity =>
                                {
                                    foreach (var value in converter.GetValues(entity))
                                    {
                                        if (value is string text)
                                        {
                                            writer.WriteField(text, true);
                                        }
                                        else
                                        {
                                            writer.WriteField(value);
                                        }
                                    }

                                    await writer.NextRecordAsync();
                                });
                            }
                        }
                    }
                }
            }