2024-05-05 10:20:54 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2024-05-05 18:23:17 +00:00
|
|
|
using CSV_Metrics_Logger;
|
2024-05-05 10:20:54 +00:00
|
|
|
|
|
|
|
namespace Tests;
|
|
|
|
|
|
|
|
[ExcludeFromCodeCoverage]
|
|
|
|
public sealed class Tests
|
|
|
|
{
|
|
|
|
[Test]
|
|
|
|
public void TestBasicImplementation()
|
|
|
|
{
|
|
|
|
var testData = new TestDataRecordStructMixed("Area1", 1, 1.1f, 7);
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
{
|
|
|
|
Assert.That(typeof(TestDataRecordStructMixed).GetInterface("IConvertToCSV"), Is.Not.Null);
|
|
|
|
Assert.That(typeof(TestDataRecordStructMixed).GetProperties(), Has.Length.EqualTo(5));
|
|
|
|
|
|
|
|
var numberColumns = testData.GetCSVColumnCount();
|
|
|
|
Assert.That(numberColumns, Is.EqualTo(5));
|
|
|
|
|
|
|
|
var header = testData.GetCSVHeaders();
|
|
|
|
Assert.That(header, Is.EquivalentTo(new[] { "Area", "Step", "Value", "Z", "State" }));
|
|
|
|
|
|
|
|
var dataLine = testData.ConvertToCSVDataLine();
|
|
|
|
Assert.That(dataLine, Is.EquivalentTo(new[] { "Area1", "1", "1.1", "7", "False" }));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestLongText()
|
|
|
|
{
|
|
|
|
var text = """
|
|
|
|
This is a long text that should be split into multiple lines
|
|
|
|
and should be able to be used as a multiline string
|
|
|
|
...
|
|
|
|
and on and on...
|
|
|
|
""";
|
|
|
|
var testData = new TestDataRecordStructMixed(text, 500_000, 0.00057f, -6_087);
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
{
|
|
|
|
var dataLine = testData.ConvertToCSVDataLine();
|
|
|
|
Assert.That(dataLine, Is.EquivalentTo(new[] { text, "500000", "0.00057", "-6087", "False" }));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestOneLineStructure()
|
|
|
|
{
|
|
|
|
var testData = new TestDataOneLine("Bob", 120);
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
{
|
|
|
|
var numberColumns = testData.GetCSVColumnCount();
|
|
|
|
Assert.That(numberColumns, Is.EqualTo(2));
|
|
|
|
|
|
|
|
var header = testData.GetCSVHeaders();
|
|
|
|
Assert.That(header, Is.EquivalentTo(new[] { "Name", "Age" }));
|
|
|
|
|
|
|
|
var dataLine = testData.ConvertToCSVDataLine();
|
|
|
|
Assert.That(dataLine, Is.EquivalentTo(new[] { "Bob", "120" }));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-21 07:31:46 +00:00
|
|
|
[Test]
|
|
|
|
public void TestGeneric()
|
|
|
|
{
|
|
|
|
var testData = new TestGeneric<float>("Bob", 120, 4.78f);
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
{
|
|
|
|
var numberColumns = testData.GetCSVColumnCount();
|
|
|
|
Assert.That(numberColumns, Is.EqualTo(3));
|
|
|
|
|
|
|
|
var header = testData.GetCSVHeaders();
|
|
|
|
Assert.That(header, Is.EquivalentTo(new[] { "Name", "Age", "Value" }));
|
|
|
|
|
|
|
|
var dataLine = testData.ConvertToCSVDataLine();
|
|
|
|
Assert.That(dataLine, Is.EquivalentTo(new[] { "Bob", "120", "4.78" }));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-05 10:20:54 +00:00
|
|
|
[Test]
|
|
|
|
public void TestRegularStruct()
|
|
|
|
{
|
|
|
|
var testData = new TestDataRegularStruct
|
|
|
|
{
|
|
|
|
City = "New York",
|
|
|
|
Age = 35,
|
|
|
|
Size = 1.85,
|
|
|
|
};
|
|
|
|
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
{
|
|
|
|
var numberColumns = testData.GetCSVColumnCount();
|
|
|
|
Assert.That(numberColumns, Is.EqualTo(3));
|
|
|
|
|
|
|
|
var header = testData.GetCSVHeaders();
|
|
|
|
Assert.That(header, Is.EquivalentTo(new[] { "City", "Age", "Size" }));
|
|
|
|
|
|
|
|
var dataLine = testData.ConvertToCSVDataLine();
|
|
|
|
Assert.That(dataLine, Is.EquivalentTo(new[] { "New York", "35", "1.85" }));
|
|
|
|
});
|
|
|
|
}
|
2024-05-05 18:23:17 +00:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public async Task TestWritingCSV()
|
|
|
|
{
|
|
|
|
List<TestDataOneLine> testData =
|
|
|
|
[
|
|
|
|
new TestDataOneLine("Name 1", 14),
|
|
|
|
new TestDataOneLine("Name 2", 25),
|
|
|
|
new TestDataOneLine("Name 3", 36),
|
|
|
|
new TestDataOneLine("Name 4", 47),
|
|
|
|
new TestDataOneLine("Name 5", 58),
|
|
|
|
];
|
|
|
|
|
|
|
|
// Get a random file:
|
|
|
|
var fileName = Path.GetTempFileName();
|
|
|
|
await using (var storage = CSVStorage<TestDataOneLine>.Create(fileName))
|
|
|
|
{
|
|
|
|
foreach (var data in testData)
|
|
|
|
storage.Write(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
var lines = await File.ReadAllLinesAsync(fileName);
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
{
|
|
|
|
Assert.That(lines, Has.Length.EqualTo(6));
|
|
|
|
Assert.That(lines[0], Is.EqualTo("Name;Age"));
|
|
|
|
Assert.That(lines[1], Is.EqualTo("\"Name 1\";14"));
|
|
|
|
Assert.That(lines[2], Is.EqualTo("\"Name 2\";25"));
|
|
|
|
Assert.That(lines[3], Is.EqualTo("\"Name 3\";36"));
|
|
|
|
Assert.That(lines[4], Is.EqualTo("\"Name 4\";47"));
|
|
|
|
Assert.That(lines[5], Is.EqualTo("\"Name 5\";58"));
|
|
|
|
});
|
|
|
|
|
|
|
|
File.Delete(fileName);
|
|
|
|
}
|
2024-05-05 10:20:54 +00:00
|
|
|
}
|