예제 #1
0
        public void EmptyDataTable_YieldsEmpty()
        {
            var dataTable = new DataTable();

            var formatter = new CsvFormatter();
            var result = formatter.Format(dataTable).ToList();

            CollectionAssert.AreEqual(new string[] { "" }, result);
        }
예제 #2
0
        public void SingleResult_YieldsThatResult()
        {
            var dataTable = new DataTable();
            dataTable.Columns.Add("Result", typeof(string));
            dataTable.Rows.Add("Some Value");

            var formatter = new CsvFormatter();
            var result = formatter.Format(dataTable).ToList();
            Assert.AreEqual("Some Value", result.Single());
        }
예제 #3
0
            public void ThenTheResultStartsWithCsvHeaders(
                List <CommitmentAgreement> agreements,
                CsvFormatter sut)
            {
                var byteResult = sut.Format(TestHelper.Clone(agreements));

                var stringResult = Default.GetString(byteResult);

                stringResult.Should().StartWith(
                    $"{nameof(CommitmentAgreement.OrganisationName)},{nameof(CommitmentAgreement.CohortID)},{nameof(CommitmentAgreement.AgreementID)}");
            }
예제 #4
0
        public void Format_CreatesCsv()
        {
            var dataTable = new[]
            {
                new Person {Id = 1, FirstName = "John", LastName = "Doe"}
            }.ToDataTable();

            var formatter = new CsvFormatter();
            var result = formatter.Format(dataTable).ToList();

            CollectionAssert.AreEqual(new[] { "\"Id\";\"FirstName\";\"LastName\"", "\"1\";\"John\";\"Doe\"" }, result);
        }
예제 #5
0
            public void ThenTheResultContainsCsvAgreements(
                List <CommitmentAgreement> agreements,
                CsvFormatter sut)
            {
                var byteResult = sut.Format(TestHelper.Clone(agreements));

                var stringResult = Default.GetString(byteResult);

                stringResult.Should().Contain($"{agreements[0].OrganisationName},{agreements[0].CohortID},{agreements[0].AgreementID}");
                stringResult.Should().Contain($"{agreements[1].OrganisationName},{agreements[1].CohortID},{agreements[1].AgreementID}");
                stringResult.Should().Contain($"{agreements[2].OrganisationName},{agreements[2].CohortID},{agreements[2].AgreementID}");
            }
        /// <summary>
        /// Generates Output for the given Node.
        /// </summary>
        /// <param name="context">Writer Context.</param>
        /// <param name="n">Node.</param>
        /// <param name="segment">Triple Segment.</param>
        private void GenerateNodeOutput(BaseWriterContext context, INode n, TripleSegment segment)
        {
            switch (n.NodeType)
            {
            case NodeType.Blank:
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable("CSV"));
                }

                context.Output.Write(_formatter.Format(n));
                break;

            case NodeType.GraphLiteral:
                throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("CSV"));

            case NodeType.Literal:
                if (segment == TripleSegment.Subject)
                {
                    throw new RdfOutputException(WriterErrorMessages.LiteralSubjectsUnserializable("CSV"));
                }
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("CSV"));
                }

                context.Output.Write(_formatter.Format(n));
                break;

            case NodeType.Uri:
                context.Output.Write(_formatter.Format(n));
                break;

            default:
                throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("CSV"));
            }
        }
예제 #7
0
        public void FormatCommaWithCSV()
        {
            var formatter = new CsvFormatter();
            var response  = new PlacesResponse()
            {
                results = new PlacesResult[] {
                    new PlacesResult()
                    {
                        name = "Name, with, commas", opening_hours = new OpeningHours {
                            open_now = false
                        }, rating = 4.8, vicinity = "near by"
                    },
                }
            };
            string formattedResult = formatter.Format(response);
            var    lines           = formattedResult.Split(Environment.NewLine);

            Assert.Equal(2, lines[1].Count(x => x == ','));
        }
예제 #8
0
        public async Task <FileContentResult> Get()
        {
            // get the film list
            var filmList = await _starWarsService.GetFilms();

            // get the characters for these films
            Character[] characterArray = new Character[0];
            foreach (Film film in filmList)
            {
                var result = await _starWarsService.GetCharactersByFilm(film.ObjectId);

                // combine and sort characters, no duplicates
                foreach (var character in result)
                {
                    character.EpisodeId = film.EpisodeId;
                }
                characterArray = characterArray.Union(result).ToArray();
            }
            Array.Sort(characterArray, new StarWarsComparer());
            // convert names into last name, first name format
            foreach (var character in characterArray)
            {
                var names = character.Name.Split(" ");
                if (names.Length > 1)
                {
                    character.Name = $"{names[1]}-{names[0]}";
                }
            }
            // convert into csv file and return it.
            var characterList = new List <Character>(characterArray);
            var formatter     = new CsvFormatter();
            var csv           = formatter.Format(characterList);

            byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(csv);
            // return csv file
            return(File(fileBytes, "text/csv", "StarWarsCharacters.csv"));
        }
예제 #9
0
        /// <summary>
        /// Saves a SPARQL Result Set to CSV format
        /// </summary>
        /// <param name="results">Result Set</param>
        /// <param name="output">Writer to save to</param>
        public void Save(SparqlResultSet results, TextWriter output)
        {
            try
            {
                if (results.ResultsType == SparqlResultsType.VariableBindings)
                {
                    // Output Variables first
                    String[] vars = results.Variables.ToArray();
                    for (int i = 0; i < vars.Length; i++)
                    {
                        output.Write(vars[i]);
                        if (i < vars.Length - 1)
                        {
                            output.Write(',');
                        }
                    }
                    output.Write("\r\n");

                    foreach (SparqlResult result in results)
                    {
                        for (int i = 0; i < vars.Length; i++)
                        {
                            if (result.HasValue(vars[i]))
                            {
                                INode temp = result[vars[i]];
                                if (temp != null)
                                {
                                    switch (temp.NodeType)
                                    {
                                    case NodeType.Blank:
                                    case NodeType.Uri:
                                    case NodeType.Literal:
                                        output.Write(_formatter.Format(temp));
                                        break;

                                    case NodeType.GraphLiteral:
                                        throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("SPARQL CSV"));

                                    default:
                                        throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("SPARQL CSV"));
                                    }
                                }
                            }
                            if (i < vars.Length - 1)
                            {
                                output.Write(',');
                            }
                        }
                        output.Write("\r\n");
                    }
                }
                else
                {
                    output.Write(results.Result.ToString());
                }

                output.Close();
            }
            catch
            {
                try
                {
                    output.Close();
                }
                catch
                {
                    // No error handling, just trying to clean up
                }
                throw;
            }
        }