Skip to content

Commit

Permalink
A bit more code style cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyzimarev committed Sep 20, 2022
1 parent 28a8593 commit 1c62c3d
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 124 deletions.
254 changes: 140 additions & 114 deletions test/RestSharp.Tests.Serializers.Csv/CsvHelperTests.cs
Expand Up @@ -7,150 +7,176 @@
using System.Net;
using System.Text;

namespace RestSharp.Tests.Serializers.Csv {
public class CsvHelperTests {
private static readonly Fixture Fixture = new Fixture();
namespace RestSharp.Tests.Serializers.Csv;

public class CsvHelperTests {
static readonly Fixture Fixture = new();

[Fact]
public async Task Use_CsvHelper_For_Response() {
var expected = Fixture.Create<TestObject>();

expected.DateTimeValue = new DateTime(
expected.DateTimeValue.Year,
expected.DateTimeValue.Month,
expected.DateTimeValue.Day,
expected.DateTimeValue.Hour,
expected.DateTimeValue.Minute,
expected.DateTimeValue.Second
);

using var server = HttpServerFixture.StartServer(
(_, response) => {
var serializer = new CsvHelperSerializer();
response.ContentType = "text/csv";
response.ContentEncoding = Encoding.UTF8;
response.OutputStream.WriteStringUtf8(serializer.Serialize(expected)!);
}
);

[Fact]
public async Task Use_CsvHelper_For_Response() {
var expected = Fixture.Create<TestObject>();
var client = new RestClient(server.Url).UseCsvHelper();

expected.DateTimeValue = new DateTime(expected.DateTimeValue.Year, expected.DateTimeValue.Month, expected.DateTimeValue.Day, expected.DateTimeValue.Hour, expected.DateTimeValue.Minute, expected.DateTimeValue.Second);
var actual = await client.GetAsync<TestObject>(new RestRequest());

using var server = HttpServerFixture.StartServer(
(_, response) => {
var serializer = new CsvHelperSerializer();
actual.Should().BeEquivalentTo(expected);
}

response.ContentType = "text/csv";
response.ContentEncoding = Encoding.UTF8;
response.OutputStream.WriteStringUtf8(serializer.Serialize(expected)!);
}
);
[Fact]
public async Task Use_CsvHelper_For_Collection_Response() {
var count = Fixture.Create<int>();
var expected = new List<TestObject>(count);

var client = new RestClient(server.Url).UseCsvHelper();
for (var i = 0; i < count; i++) {
var item = Fixture.Create<TestObject>();

var actual = await client.GetAsync<TestObject>(new RestRequest());
item.DateTimeValue = new DateTime(
item.DateTimeValue.Year,
item.DateTimeValue.Month,
item.DateTimeValue.Day,
item.DateTimeValue.Hour,
item.DateTimeValue.Minute,
item.DateTimeValue.Second
);

actual.Should().BeEquivalentTo(expected);
expected.Add(item);
}

[Fact]
public async Task Use_CsvHelper_For_Collection_Response() {
var count = Fixture.Create<int>();
var expected = new List<TestObject>(count);

for (int i = 0; i < count; i++) {
var item = Fixture.Create<TestObject>();
using var server = HttpServerFixture.StartServer(
(_, response) => {
var serializer = new CsvHelperSerializer();
item.DateTimeValue = new DateTime(item.DateTimeValue.Year, item.DateTimeValue.Month, item.DateTimeValue.Day, item.DateTimeValue.Hour, item.DateTimeValue.Minute, item.DateTimeValue.Second);

expected.Add(item);
response.ContentType = "text/csv";
response.ContentEncoding = Encoding.UTF8;
response.OutputStream.WriteStringUtf8(serializer.Serialize(expected));
}
);

using var server = HttpServerFixture.StartServer(
(_, response) => {
var serializer = new CsvHelperSerializer();
response.ContentType = "text/csv";
response.ContentEncoding = Encoding.UTF8;
response.OutputStream.WriteStringUtf8(serializer.Serialize(expected));
}
);
var client = new RestClient(server.Url).UseCsvHelper();

var client = new RestClient(server.Url).UseCsvHelper();
var actual = await client.GetAsync<List<TestObject>>(new RestRequest());

var actual = await client.GetAsync<List<TestObject>>(new RestRequest());
actual.Should().BeEquivalentTo(expected);
}

actual.Should().BeEquivalentTo(expected);
}
[Fact]
public async Task DeserilizationFails_IsSuccessfull_Should_BeFalse() {
using var server = HttpServerFixture.StartServer(
(_, response) => {
response.StatusCode = (int)HttpStatusCode.OK;
response.ContentType = "text/csv";
response.ContentEncoding = Encoding.UTF8;
response.OutputStream.WriteStringUtf8("invalid csv");
}
);

[Fact]
public async Task DeserilizationFails_IsSuccessfull_Should_BeFalse() {
using var server = HttpServerFixture.StartServer(
(_, response) => {
response.StatusCode = (int)HttpStatusCode.OK;
response.ContentType = "text/csv";
response.ContentEncoding = Encoding.UTF8;
response.OutputStream.WriteStringUtf8("invalid csv");
}
);
var client = new RestClient(server.Url).UseCsvHelper();

var client = new RestClient(server.Url).UseCsvHelper();
var response = await client.ExecuteAsync<TestObject>(new RestRequest());

var response = await client.ExecuteAsync<TestObject>(new RestRequest());
response.IsSuccessStatusCode.Should().BeTrue();
response.IsSuccessful.Should().BeFalse();
}

response.IsSuccessStatusCode.Should().BeTrue();
response.IsSuccessful.Should().BeFalse();
}
[Fact]
public async Task DeserilizationSucceeds_IsSuccessfull_Should_BeTrue() {
var item = Fixture.Create<TestObject>();

[Fact]
public async Task DeserilizationSucceeds_IsSuccessfull_Should_BeTrue() {
var item = Fixture.Create<TestObject>();
using var server = HttpServerFixture.StartServer(
(_, response) => {
var serializer = new SystemTextJsonSerializer();
using var server = HttpServerFixture.StartServer(
(_, response) => {
var serializer = new SystemTextJsonSerializer();
response.StatusCode = (int)HttpStatusCode.OK;
response.ContentType = "text/csv";
response.ContentEncoding = Encoding.UTF8;
response.OutputStream.WriteStringUtf8(serializer.Serialize(item)!);
}
);

response.StatusCode = (int)HttpStatusCode.OK;
response.ContentType = "text/csv";
response.ContentEncoding = Encoding.UTF8;
response.OutputStream.WriteStringUtf8(serializer.Serialize(item)!);
}
);
var client = new RestClient(server.Url).UseSystemTextJson();

var client = new RestClient(server.Url).UseSystemTextJson();
var response = await client.ExecuteAsync<TestObject>(new RestRequest());

var response = await client.ExecuteAsync<TestObject>(new RestRequest());
response.IsSuccessStatusCode.Should().BeTrue();
response.IsSuccessful.Should().BeTrue();
}

response.IsSuccessStatusCode.Should().BeTrue();
response.IsSuccessful.Should().BeTrue();
}
[Fact]
public void SerializedObject_Should_Be() {
var serializer = new CsvHelperSerializer(
new CsvConfiguration(CultureInfo.InvariantCulture) {
NewLine = ";"
}
);

var item = new TestObject {
Int32Value = 32,
SingleValue = 16.5f,
StringValue = "hello",
TimeSpanValue = TimeSpan.FromMinutes(10),
DateTimeValue = new DateTime(2024, 1, 20)
};

serializer.Serialize(item)
.Should()
.Be(
"StringValue,Int32Value,DecimalValue,DoubleValue,SingleValue,DateTimeValue,TimeSpanValue;hello,32,0,0,16.5,01/20/2024 00:00:00,00:10:00;"
);
}

[Fact]
public async Task SerializedObject_Should_Be() {
var serializer = new CsvHelperSerializer(new CsvConfiguration(CultureInfo.InvariantCulture) {
[Fact]
public void SerializedCollection_Should_Be() {
var serializer = new CsvHelperSerializer(
new CsvConfiguration(CultureInfo.InvariantCulture) {
NewLine = ";"
});
}
);

var item = new TestObject() {
Int32Value = 32,
SingleValue = 16.5f,
StringValue = "hello",
var items = new TestObject[] {
new() {
Int32Value = 32,
SingleValue = 16.5f,
StringValue = "hello",
TimeSpanValue = TimeSpan.FromMinutes(10),
DateTimeValue = new DateTime(2024, 1, 20)
};

serializer.Serialize(item).Should().Be("StringValue,Int32Value,DecimalValue,DoubleValue,SingleValue,DateTimeValue,TimeSpanValue;hello,32,0,0,16.5,01/20/2024 00:00:00,00:10:00;");
}
},
new() {
Int32Value = 65,
DecimalValue = 89.555m,
TimeSpanValue = TimeSpan.FromSeconds(61),
DateTimeValue = new DateTime(2022, 8, 19, 5, 15, 21)
},
new() {
SingleValue = 80000,
DoubleValue = 20.00001,
StringValue = "String, with comma"
}
};

[Fact]
public async Task SerializedCollection_Should_Be() {
var serializer = new CsvHelperSerializer(new CsvConfiguration(CultureInfo.InvariantCulture) {
NewLine = ";"
});

var items = new TestObject[] {
new TestObject() {
Int32Value = 32,
SingleValue = 16.5f,
StringValue = "hello",
TimeSpanValue = TimeSpan.FromMinutes(10),
DateTimeValue = new DateTime(2024, 1, 20)
},
new TestObject() {
Int32Value = 65,
DecimalValue = 89.555m,
TimeSpanValue = TimeSpan.FromSeconds(61),
DateTimeValue = new DateTime(2022, 8, 19, 5, 15, 21)
},
new TestObject() {
SingleValue = 80000,
DoubleValue = 20.00001,
StringValue = "String, with comma"
}
};

serializer.Serialize(items).Should().Be("StringValue,Int32Value,DecimalValue,DoubleValue,SingleValue,DateTimeValue,TimeSpanValue;hello,32,0,0,16.5,01/20/2024 00:00:00,00:10:00;,65,89.555,0,0,08/19/2022 05:15:21,00:01:01;\"String, with comma\",0,0,20.00001,80000,01/01/0001 00:00:00,00:00:00;");
}
serializer.Serialize(items)
.Should()
.Be(
"StringValue,Int32Value,DecimalValue,DoubleValue,SingleValue,DateTimeValue,TimeSpanValue;hello,32,0,0,16.5,01/20/2024 00:00:00,00:10:00;,65,89.555,0,0,08/19/2022 05:15:21,00:01:01;\"String, with comma\",0,0,20.00001,80000,01/01/0001 00:00:00,00:00:00;"
);
}
}
20 changes: 10 additions & 10 deletions test/RestSharp.Tests.Serializers.Csv/TestObject.cs
@@ -1,11 +1,11 @@
namespace RestSharp.Tests.Serializers.Csv {
internal class TestObject {
public string StringValue { get; set; }
public int Int32Value { get; set; }
public decimal DecimalValue { get; set; }
public double DoubleValue { get; set; }
public float SingleValue { get; set; }
public DateTime DateTimeValue { get; set; }
public TimeSpan TimeSpanValue { get; set; }
}
namespace RestSharp.Tests.Serializers.Csv;

class TestObject {
public string StringValue { get; set; }
public int Int32Value { get; set; }
public decimal DecimalValue { get; set; }
public double DoubleValue { get; set; }
public float SingleValue { get; set; }
public DateTime DateTimeValue { get; set; }
public TimeSpan TimeSpanValue { get; set; }
}

0 comments on commit 1c62c3d

Please sign in to comment.