Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into issue-16
  • Loading branch information
RikuVirtanen committed Aug 30, 2023
2 parents 9ed1438 + fbbd0cd commit b9cb8eb
Show file tree
Hide file tree
Showing 18 changed files with 801 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/ConvertToXML_build_and_test_on_main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: ConvertToXML build main

on:
push:
branches:
- main
paths:
- 'Frends.CSV.ConvertToXML/**'
workflow_dispatch:

jobs:
build:
uses: FrendsPlatform/FrendsTasks/.github/workflows/build_main.yml@main
with:
workdir: Frends.CSV.ConvertToXML
secrets:
badge_service_api_key: ${{ secrets.BADGE_SERVICE_API_KEY }}
18 changes: 18 additions & 0 deletions .github/workflows/ConvertToXML_build_and_test_on_push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: ConvertToXML build test

on:
push:
branches-ignore:
- main
paths:
- 'Frends.CSV.ConvertToXML/**'
workflow_dispatch:

jobs:
build:
uses: FrendsPlatform/FrendsTasks/.github/workflows/build_test.yml@main
with:
workdir: Frends.CSV.ConvertToXML
secrets:
badge_service_api_key: ${{ secrets.BADGE_SERVICE_API_KEY }}
test_feed_api_key: ${{ secrets.TASKS_TEST_FEED_API_KEY }}
12 changes: 12 additions & 0 deletions .github/workflows/ConvertToXML_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: ConvertToXML release

on:
workflow_dispatch:

jobs:
build:
uses: FrendsPlatform/FrendsTasks/.github/workflows/release.yml@main
with:
workdir: Frends.CSV.ConvertToXML
secrets:
feed_api_key: ${{ secrets.TASKS_FEED_API_KEY }}
4 changes: 4 additions & 0 deletions Frends.CSV.ConvertToXML/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*.cs]

# CS8602: Dereference of a possibly null reference.
dotnet_diagnostic.CS8602.severity = none
5 changes: 5 additions & 0 deletions Frends.CSV.ConvertToXML/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## [1.0.0] - 2023-08-24
### Added
- Initial implementation
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="System.Xml.ReaderWriter" Version="4.3.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Frends.CSV.ConvertToXML\Frends.CSV.ConvertToXML.csproj" />
</ItemGroup>

</Project>
270 changes: 270 additions & 0 deletions Frends.CSV.ConvertToXML/Frends.CSV.ConvertToXML.UnitTests/UnitTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
using Frends.CSV.ConvertToXML.Definitions;
using NUnit.Framework;

namespace Frends.CSV.ConvertToXML.UnitTests;

[TestFixture]
public class UnitTests
{
[Test]
public void Test_WithEmptyFields()
{
var csv = @"string,1,2023-01-01,2,200,3,true,N
,,,,,,,";

var input = new Input()
{
ColumnSpecifications = Array.Empty<ColumnSpecification>(),
Delimiter = ",",
Csv = csv
};

var options = new Options()
{
ContainsHeaderRow = false,
SkipRowsFromTop = 0,
SkipEmptyRows = false
};

var result = CSV.ConvertToXML(input, options, default);
Assert.IsNotNull(result.Xml);
}

[Test]
public void ConvertToXMLTest_SkipRowsWithAutomaticHeaders()
{
var csv = @"asdasd
Coolio
year;car;mark;price
1997;Ford;E350;2,34
2000;Mercury;Cougar;2,38";

var input = new Input()
{
ColumnSpecifications = Array.Empty<ColumnSpecification>(),
Delimiter = ";",
Csv = csv
};

var options = new Options()
{
ContainsHeaderRow = true,
SkipRowsFromTop = 2,
SkipEmptyRows = false
};

var result = CSV.ConvertToXML(input, options, default);
Assert.IsNotNull(result.Xml);
Assert.IsTrue(result.Xml.Contains("<year>2000</year>"));
}

[Test]
public void ConvertToXMLTest_WithColumnSpecAndMissingHeader()
{
var csv = @"1997;Ford;E350;2,34
2000;Mercury;Cougar;2,38";

var input = new Input()
{
ColumnSpecifications = new[]
{
new ColumnSpecification() {Name = "Year", Type = ColumnType.Int},
new ColumnSpecification() {Name = "Car", Type = ColumnType.String},
new ColumnSpecification() {Name = "Mark", Type = ColumnType.String},
new ColumnSpecification() {Name = "Price", Type = ColumnType.Decimal}
},
Delimiter = ";",
Csv = csv
};

var options = new Options()
{
ContainsHeaderRow = false,
CultureInfo = "fi-FI"
};

var result = CSV.ConvertToXML(input, options, default);
Assert.IsNotNull(result.Xml);
Assert.IsTrue(result.Xml.Contains("<Year>2000</Year>"));
}

[Test]
public void ConvertToXMLTest_WithNoColumnSpecAndNoHeader()
{
var csv = @"1997;Ford;E350;2,34
2000;Mercury;Cougar;2,38";

var input = new Input()
{
ColumnSpecifications = Array.Empty<ColumnSpecification>(),
Delimiter = ";",
Csv = csv
};

var options = new Options()
{
ContainsHeaderRow = false,
CultureInfo = "fi-FI"
};

var result = CSV.ConvertToXML(input, options, default);
Assert.IsNotNull(result.Xml);
Assert.IsTrue(result.Xml.Contains("<0>2000</0>"));
}

[Test]
public void ConvertToXMLTest_WillAllKindOfDataTypes()
{
var csv =
@"THIS;is;header;row;with;some;random;stuff ;yes
1997;""Fo;rd"";2,34;true;1;4294967296;f;2008-09-15;2008-05-01 7:34:42Z
2000;Mercury;2,38;false;0;4294967296;g;2009-09-15T06:30:41.7752486;Thu, 01 May 2008 07:34:42 GMT";

var input = new Input()
{
ColumnSpecifications = new[]
{
new ColumnSpecification() {Name = "Int", Type = ColumnType.Int},
new ColumnSpecification() {Name = "String", Type = ColumnType.String},
new ColumnSpecification() {Name = "Decimal", Type = ColumnType.Decimal},
new ColumnSpecification() {Name = "Bool", Type = ColumnType.Boolean},
new ColumnSpecification() {Name = "Bool2", Type = ColumnType.Boolean},
new ColumnSpecification() {Name = "Long", Type = ColumnType.Long},
new ColumnSpecification() {Name = "Char", Type = ColumnType.Char},
new ColumnSpecification() {Name = "DateTime", Type = ColumnType.DateTime},
new ColumnSpecification() {Name = "DateTime2", Type = ColumnType.DateTime},
},
Delimiter = ";",
Csv = csv
};

var options = new Options()
{
ContainsHeaderRow = true,
CultureInfo = "fi-FI"
};

var result = CSV.ConvertToXML(input, options, default);
Assert.IsNotNull(result.Xml);
}

[Test]
public void TestConvertToXMLTreatMissingFieldsAsNullSetToTrue()
{
var csv =
@"header1,header2,header3
value1,value2,value3
value1,value2,value3
value1,value2";

var input = new Input()
{
ColumnSpecifications = new ColumnSpecification[0],
Delimiter = ",",
Csv = csv
};

var options = new Options()
{
ContainsHeaderRow = true,
CultureInfo = "fi-FI",
TreatMissingFieldsAsNulls = true
};

var result = CSV.ConvertToXML(input, options, default);
Assert.IsTrue(condition: result.Xml.Contains("<header3 />"));
}

[Test]
public void TestConvertToXMLTreatMissingFieldsAsNullSetToTrueNoHeader()
{
var csv =
@"value1,value2,value3
value1,value2,value3
value1,value2";

var input = new Input()
{
ColumnSpecifications = new ColumnSpecification[0],
Delimiter = ",",
Csv = csv
};

var options = new Options()
{
ContainsHeaderRow = false,
CultureInfo = "fi-FI",
TreatMissingFieldsAsNulls = true
};

var result = CSV.ConvertToXML(input, options, default);
Assert.IsTrue(condition: result.Xml.Contains("<2 />"));
}

[Test]
public void TestConvertToXMLTreatMissingFieldsAsNullSetToTrueNoHeaderWithColumnSpecifications()
{
var csv =
@"string,1,2023-01-01,2,200,3,true,N
,,,,,,,";

var input = new Input()
{
ColumnSpecifications = new[]
{
new ColumnSpecification() { Name = "String", Type = ColumnType.String },
new ColumnSpecification() { Name = "Decimal", Type = ColumnType.Decimal},
new ColumnSpecification() { Name = "DateTime", Type = ColumnType.DateTime },
new ColumnSpecification() { Name = "Int", Type = ColumnType.Int },
new ColumnSpecification() { Name = "Long", Type = ColumnType.Long },
new ColumnSpecification() { Name = "Double", Type = ColumnType.Double },
new ColumnSpecification() { Name = "Boolean", Type = ColumnType.Boolean },
new ColumnSpecification() { Name = "Char", Type = ColumnType.Char }
},
Delimiter = ",",
Csv = csv
};

var options = new Options()
{
ContainsHeaderRow = false,
CultureInfo = "fi-FI",
TreatMissingFieldsAsNulls = true
};

var result = CSV.ConvertToXML(input, options, default);
Assert.IsTrue(result.Xml.Contains("<Decimal />"));
Assert.IsTrue(result.Xml.Contains("<DateTime />"));
Assert.IsTrue(result.Xml.Contains("<Int />"));
Assert.IsTrue(result.Xml.Contains("<Long />"));
Assert.IsTrue(result.Xml.Contains("<Double />"));
Assert.IsTrue(result.Xml.Contains("<Boolean />"));
Assert.IsTrue(result.Xml.Contains("<Char />"));
}

[Test]
public void TestConvertToXMLTreatMissingFieldsAsNullSetToFalse()
{
var csv =
@"header1,header2,header3
value1,value2,value3
value1,value2,value3
value1,value2";
var input = new Input()
{
ColumnSpecifications = new ColumnSpecification[0],
Delimiter = ",",
Csv = csv
};

var options = new Options()
{
ContainsHeaderRow = true,
CultureInfo = "fi-FI",
TreatMissingFieldsAsNulls = false
};

var ex = Assert.Throws<CsvHelper.MissingFieldException>(() => CSV.ConvertToXML(input, options, default));
Assert.IsTrue(ex.Message.StartsWith("Field at index '2' does not exist. You can ignore missing fields by setting MissingFieldFound to null."));
}
}
Loading

0 comments on commit b9cb8eb

Please sign in to comment.