Skip to content

Commit 1c62c3d

Browse files
committed
A bit more code style cleanup
1 parent 28a8593 commit 1c62c3d

File tree

2 files changed

+150
-124
lines changed

2 files changed

+150
-124
lines changed

test/RestSharp.Tests.Serializers.Csv/CsvHelperTests.cs

+140-114
Original file line numberDiff line numberDiff line change
@@ -7,150 +7,176 @@
77
using System.Net;
88
using System.Text;
99

10-
namespace RestSharp.Tests.Serializers.Csv {
11-
public class CsvHelperTests {
12-
private static readonly Fixture Fixture = new Fixture();
10+
namespace RestSharp.Tests.Serializers.Csv;
11+
12+
public class CsvHelperTests {
13+
static readonly Fixture Fixture = new();
14+
15+
[Fact]
16+
public async Task Use_CsvHelper_For_Response() {
17+
var expected = Fixture.Create<TestObject>();
18+
19+
expected.DateTimeValue = new DateTime(
20+
expected.DateTimeValue.Year,
21+
expected.DateTimeValue.Month,
22+
expected.DateTimeValue.Day,
23+
expected.DateTimeValue.Hour,
24+
expected.DateTimeValue.Minute,
25+
expected.DateTimeValue.Second
26+
);
27+
28+
using var server = HttpServerFixture.StartServer(
29+
(_, response) => {
30+
var serializer = new CsvHelperSerializer();
31+
32+
response.ContentType = "text/csv";
33+
response.ContentEncoding = Encoding.UTF8;
34+
response.OutputStream.WriteStringUtf8(serializer.Serialize(expected)!);
35+
}
36+
);
1337

14-
[Fact]
15-
public async Task Use_CsvHelper_For_Response() {
16-
var expected = Fixture.Create<TestObject>();
38+
var client = new RestClient(server.Url).UseCsvHelper();
1739

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

20-
using var server = HttpServerFixture.StartServer(
21-
(_, response) => {
22-
var serializer = new CsvHelperSerializer();
42+
actual.Should().BeEquivalentTo(expected);
43+
}
2344

24-
response.ContentType = "text/csv";
25-
response.ContentEncoding = Encoding.UTF8;
26-
response.OutputStream.WriteStringUtf8(serializer.Serialize(expected)!);
27-
}
28-
);
45+
[Fact]
46+
public async Task Use_CsvHelper_For_Collection_Response() {
47+
var count = Fixture.Create<int>();
48+
var expected = new List<TestObject>(count);
2949

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

32-
var actual = await client.GetAsync<TestObject>(new RestRequest());
53+
item.DateTimeValue = new DateTime(
54+
item.DateTimeValue.Year,
55+
item.DateTimeValue.Month,
56+
item.DateTimeValue.Day,
57+
item.DateTimeValue.Hour,
58+
item.DateTimeValue.Minute,
59+
item.DateTimeValue.Second
60+
);
3361

34-
actual.Should().BeEquivalentTo(expected);
62+
expected.Add(item);
3563
}
3664

37-
[Fact]
38-
public async Task Use_CsvHelper_For_Collection_Response() {
39-
var count = Fixture.Create<int>();
40-
var expected = new List<TestObject>(count);
41-
42-
for (int i = 0; i < count; i++) {
43-
var item = Fixture.Create<TestObject>();
65+
using var server = HttpServerFixture.StartServer(
66+
(_, response) => {
67+
var serializer = new CsvHelperSerializer();
4468

45-
item.DateTimeValue = new DateTime(item.DateTimeValue.Year, item.DateTimeValue.Month, item.DateTimeValue.Day, item.DateTimeValue.Hour, item.DateTimeValue.Minute, item.DateTimeValue.Second);
46-
47-
expected.Add(item);
69+
response.ContentType = "text/csv";
70+
response.ContentEncoding = Encoding.UTF8;
71+
response.OutputStream.WriteStringUtf8(serializer.Serialize(expected));
4872
}
73+
);
4974

50-
using var server = HttpServerFixture.StartServer(
51-
(_, response) => {
52-
var serializer = new CsvHelperSerializer();
53-
54-
response.ContentType = "text/csv";
55-
response.ContentEncoding = Encoding.UTF8;
56-
response.OutputStream.WriteStringUtf8(serializer.Serialize(expected));
57-
}
58-
);
75+
var client = new RestClient(server.Url).UseCsvHelper();
5976

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

62-
var actual = await client.GetAsync<List<TestObject>>(new RestRequest());
79+
actual.Should().BeEquivalentTo(expected);
80+
}
6381

64-
actual.Should().BeEquivalentTo(expected);
65-
}
82+
[Fact]
83+
public async Task DeserilizationFails_IsSuccessfull_Should_BeFalse() {
84+
using var server = HttpServerFixture.StartServer(
85+
(_, response) => {
86+
response.StatusCode = (int)HttpStatusCode.OK;
87+
response.ContentType = "text/csv";
88+
response.ContentEncoding = Encoding.UTF8;
89+
response.OutputStream.WriteStringUtf8("invalid csv");
90+
}
91+
);
6692

67-
[Fact]
68-
public async Task DeserilizationFails_IsSuccessfull_Should_BeFalse() {
69-
using var server = HttpServerFixture.StartServer(
70-
(_, response) => {
71-
response.StatusCode = (int)HttpStatusCode.OK;
72-
response.ContentType = "text/csv";
73-
response.ContentEncoding = Encoding.UTF8;
74-
response.OutputStream.WriteStringUtf8("invalid csv");
75-
}
76-
);
93+
var client = new RestClient(server.Url).UseCsvHelper();
7794

78-
var client = new RestClient(server.Url).UseCsvHelper();
95+
var response = await client.ExecuteAsync<TestObject>(new RestRequest());
7996

80-
var response = await client.ExecuteAsync<TestObject>(new RestRequest());
97+
response.IsSuccessStatusCode.Should().BeTrue();
98+
response.IsSuccessful.Should().BeFalse();
99+
}
81100

82-
response.IsSuccessStatusCode.Should().BeTrue();
83-
response.IsSuccessful.Should().BeFalse();
84-
}
101+
[Fact]
102+
public async Task DeserilizationSucceeds_IsSuccessfull_Should_BeTrue() {
103+
var item = Fixture.Create<TestObject>();
85104

86-
[Fact]
87-
public async Task DeserilizationSucceeds_IsSuccessfull_Should_BeTrue() {
88-
var item = Fixture.Create<TestObject>();
105+
using var server = HttpServerFixture.StartServer(
106+
(_, response) => {
107+
var serializer = new SystemTextJsonSerializer();
89108

90-
using var server = HttpServerFixture.StartServer(
91-
(_, response) => {
92-
var serializer = new SystemTextJsonSerializer();
109+
response.StatusCode = (int)HttpStatusCode.OK;
110+
response.ContentType = "text/csv";
111+
response.ContentEncoding = Encoding.UTF8;
112+
response.OutputStream.WriteStringUtf8(serializer.Serialize(item)!);
113+
}
114+
);
93115

94-
response.StatusCode = (int)HttpStatusCode.OK;
95-
response.ContentType = "text/csv";
96-
response.ContentEncoding = Encoding.UTF8;
97-
response.OutputStream.WriteStringUtf8(serializer.Serialize(item)!);
98-
}
99-
);
116+
var client = new RestClient(server.Url).UseSystemTextJson();
100117

101-
var client = new RestClient(server.Url).UseSystemTextJson();
118+
var response = await client.ExecuteAsync<TestObject>(new RestRequest());
102119

103-
var response = await client.ExecuteAsync<TestObject>(new RestRequest());
120+
response.IsSuccessStatusCode.Should().BeTrue();
121+
response.IsSuccessful.Should().BeTrue();
122+
}
104123

105-
response.IsSuccessStatusCode.Should().BeTrue();
106-
response.IsSuccessful.Should().BeTrue();
107-
}
124+
[Fact]
125+
public void SerializedObject_Should_Be() {
126+
var serializer = new CsvHelperSerializer(
127+
new CsvConfiguration(CultureInfo.InvariantCulture) {
128+
NewLine = ";"
129+
}
130+
);
131+
132+
var item = new TestObject {
133+
Int32Value = 32,
134+
SingleValue = 16.5f,
135+
StringValue = "hello",
136+
TimeSpanValue = TimeSpan.FromMinutes(10),
137+
DateTimeValue = new DateTime(2024, 1, 20)
138+
};
139+
140+
serializer.Serialize(item)
141+
.Should()
142+
.Be(
143+
"StringValue,Int32Value,DecimalValue,DoubleValue,SingleValue,DateTimeValue,TimeSpanValue;hello,32,0,0,16.5,01/20/2024 00:00:00,00:10:00;"
144+
);
145+
}
108146

109-
[Fact]
110-
public async Task SerializedObject_Should_Be() {
111-
var serializer = new CsvHelperSerializer(new CsvConfiguration(CultureInfo.InvariantCulture) {
147+
[Fact]
148+
public void SerializedCollection_Should_Be() {
149+
var serializer = new CsvHelperSerializer(
150+
new CsvConfiguration(CultureInfo.InvariantCulture) {
112151
NewLine = ";"
113-
});
152+
}
153+
);
114154

115-
var item = new TestObject() {
116-
Int32Value = 32,
117-
SingleValue = 16.5f,
118-
StringValue = "hello",
155+
var items = new TestObject[] {
156+
new() {
157+
Int32Value = 32,
158+
SingleValue = 16.5f,
159+
StringValue = "hello",
119160
TimeSpanValue = TimeSpan.FromMinutes(10),
120161
DateTimeValue = new DateTime(2024, 1, 20)
121-
};
122-
123-
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;");
124-
}
162+
},
163+
new() {
164+
Int32Value = 65,
165+
DecimalValue = 89.555m,
166+
TimeSpanValue = TimeSpan.FromSeconds(61),
167+
DateTimeValue = new DateTime(2022, 8, 19, 5, 15, 21)
168+
},
169+
new() {
170+
SingleValue = 80000,
171+
DoubleValue = 20.00001,
172+
StringValue = "String, with comma"
173+
}
174+
};
125175

126-
[Fact]
127-
public async Task SerializedCollection_Should_Be() {
128-
var serializer = new CsvHelperSerializer(new CsvConfiguration(CultureInfo.InvariantCulture) {
129-
NewLine = ";"
130-
});
131-
132-
var items = new TestObject[] {
133-
new TestObject() {
134-
Int32Value = 32,
135-
SingleValue = 16.5f,
136-
StringValue = "hello",
137-
TimeSpanValue = TimeSpan.FromMinutes(10),
138-
DateTimeValue = new DateTime(2024, 1, 20)
139-
},
140-
new TestObject() {
141-
Int32Value = 65,
142-
DecimalValue = 89.555m,
143-
TimeSpanValue = TimeSpan.FromSeconds(61),
144-
DateTimeValue = new DateTime(2022, 8, 19, 5, 15, 21)
145-
},
146-
new TestObject() {
147-
SingleValue = 80000,
148-
DoubleValue = 20.00001,
149-
StringValue = "String, with comma"
150-
}
151-
};
152-
153-
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;");
154-
}
176+
serializer.Serialize(items)
177+
.Should()
178+
.Be(
179+
"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;"
180+
);
155181
}
156182
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
namespace RestSharp.Tests.Serializers.Csv {
2-
internal class TestObject {
3-
public string StringValue { get; set; }
4-
public int Int32Value { get; set; }
5-
public decimal DecimalValue { get; set; }
6-
public double DoubleValue { get; set; }
7-
public float SingleValue { get; set; }
8-
public DateTime DateTimeValue { get; set; }
9-
public TimeSpan TimeSpanValue { get; set; }
10-
}
1+
namespace RestSharp.Tests.Serializers.Csv;
2+
3+
class TestObject {
4+
public string StringValue { get; set; }
5+
public int Int32Value { get; set; }
6+
public decimal DecimalValue { get; set; }
7+
public double DoubleValue { get; set; }
8+
public float SingleValue { get; set; }
9+
public DateTime DateTimeValue { get; set; }
10+
public TimeSpan TimeSpanValue { get; set; }
1111
}

0 commit comments

Comments
 (0)