Skip to content

Commit cfd8c3c

Browse files
author
Chris Santero
committed
refactor JsonApiFormatter tests
1 parent d915fcf commit cfd8c3c

4 files changed

Lines changed: 128 additions & 24 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"samples": [
3+
{
4+
"id": "1",
5+
"booleanField": false,
6+
"nullableBooleanField": false,
7+
"sByteField": 0,
8+
"nullableSByteField": null,
9+
"byteField": 0,
10+
"nullableByteField": null,
11+
"int16Field": 0,
12+
"nullableInt16Field": null,
13+
"uInt16Field": 0,
14+
"nullableUInt16Field": null,
15+
"int32Field": 0,
16+
"nullableInt32Field": null,
17+
"uInt32Field": 0,
18+
"nullableUInt32Field": null,
19+
"int64Field": 0,
20+
"nullableInt64Field": null,
21+
"uInt64Field": 0,
22+
"nullableUInt64Field": null,
23+
"doubleField": 0.0,
24+
"nullableDoubleField": null,
25+
"singleField": 0.0,
26+
"nullableSingleField": null,
27+
"decimalField": "0",
28+
"nullableDecimalField": null,
29+
"dateTimeField": "0001-01-01T00:00:00",
30+
"nullableDateTimeField": null,
31+
"dateTimeOffsetField": "0001-01-01T00:00:00+00:00",
32+
"nullableDateTimeOffsetField": null,
33+
"guidField": "00000000-0000-0000-0000-000000000000",
34+
"nullableGuidField": null,
35+
"stringField": null,
36+
"enumField": 0,
37+
"nullableEnumField": null
38+
}, {
39+
"id": "2",
40+
"booleanField": true,
41+
"nullableBooleanField": true,
42+
"sByteField": 123,
43+
"nullableSByteField": 123,
44+
"byteField": 253,
45+
"nullableByteField": 253,
46+
"int16Field": 32000,
47+
"nullableInt16Field": 32000,
48+
"uInt16Field": 64000,
49+
"nullableUInt16Field": 64000,
50+
"int32Field": 2000000000,
51+
"nullableInt32Field": 2000000000,
52+
"uInt32Field": 3000000000,
53+
"nullableUInt32Field": 3000000000,
54+
"int64Field": 9223372036854775807,
55+
"nullableInt64Field": 9223372036854775807,
56+
"uInt64Field": 9223372036854775808,
57+
"nullableUInt64Field": 9223372036854775808,
58+
"doubleField": 1056789.123,
59+
"nullableDoubleField": 1056789.123,
60+
"singleField": 1056789.13,
61+
"nullableSingleField": 1056789.13,
62+
"decimalField": "1056789.123",
63+
"nullableDecimalField": "1056789.123",
64+
"dateTimeField": "1776-07-04T00:00:00",
65+
"nullableDateTimeField": "1776-07-04T00:00:00",
66+
"dateTimeOffsetField": "1776-07-04T00:00:00-05:00",
67+
"nullableDateTimeOffsetField": "1776-07-04T00:00:00-05:00",
68+
"guidField": "6566f9b4-5245-40de-890d-98b40a4ad656",
69+
"nullableGuidField": "3d1fb81e-43ee-4d04-af91-c8a326341293",
70+
"stringField": "Some string 156",
71+
"enumField": 1,
72+
"nullableEnumField": 2
73+
}
74+
]
75+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"posts": [
3+
{
4+
"id": "1",
5+
"title": "Linkbait!",
6+
"links": {
7+
"author": "1"
8+
}
9+
},
10+
{
11+
"id": "2",
12+
"title": "Rant #1023",
13+
"links": {
14+
"author": "1"
15+
}
16+
}
17+
]
18+
}

JSONAPI.Tests/JSONAPI.Tests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@
107107
<None Include="Data\ByteIdSerializationTest.json">
108108
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
109109
</None>
110+
<None Include="Data\DeserializeAttributeRequest.json">
111+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
112+
</None>
113+
<None Include="Data\DeserializeCollectionRequest.json">
114+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
115+
</None>
110116
<None Include="Data\ReformatsRawJsonStringWithUnquotedKeys.json">
111117
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
112118
</None>

JSONAPI.Tests/Json/JsonApiMediaFormaterTests.cs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -378,40 +378,45 @@ public void SerializeErrorIntegrationTest()
378378
}
379379

380380
[TestMethod]
381-
public void DeserializeCollectionIntegrationTest()
381+
[DeploymentItem(@"Data\DeserializeCollectionRequest.json")]
382+
public void Deserializes_collections_properly()
382383
{
383-
// Arrange
384-
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
385-
MemoryStream stream = new MemoryStream();
386-
387-
formatter.WriteToStreamAsync(typeof(Post), new List<Post> {p, p2}, stream, (System.Net.Http.HttpContent)null, (System.Net.TransportContext)null);
388-
stream.Seek(0, SeekOrigin.Begin);
384+
using (var inputStream = File.OpenRead("DeserializeCollectionRequest.json"))
385+
{
386+
// Arrange
387+
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
389388

390-
// Act
391-
IList<Post> posts;
392-
posts = (IList<Post>)formatter.ReadFromStreamAsync(typeof(Post), stream, (System.Net.Http.HttpContent)null, (System.Net.Http.Formatting.IFormatterLogger)null).Result;
389+
// Act
390+
var posts = (IList<Post>)formatter.ReadFromStreamAsync(typeof(Post), inputStream, null, null).Result;
393391

394-
// Assert
395-
Assert.AreEqual(2, posts.Count);
396-
Assert.AreEqual(p.Id, posts[0].Id); // Order matters, right?
392+
// Assert
393+
posts.Count.Should().Be(2);
394+
posts[0].Id.Should().Be(p.Id);
395+
posts[0].Title.Should().Be(p.Title);
396+
posts[0].Author.Id.Should().Be(a.Id);
397+
posts[1].Id.Should().Be(p2.Id);
398+
posts[1].Title.Should().Be(p2.Title);
399+
posts[1].Author.Id.Should().Be(a.Id);
400+
}
397401
}
398402

399403
[TestMethod]
404+
[DeploymentItem(@"Data\DeserializeAttributeRequest.json")]
400405
public async Task Deserializes_attributes_properly()
401406
{
402-
// Arrange
403-
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
404-
MemoryStream stream = new MemoryStream();
405-
406-
await formatter.WriteToStreamAsync(typeof(Sample), new List<Sample> { s1, s2 }, stream, null, null);
407-
stream.Seek(0, SeekOrigin.Begin);
407+
using (var inputStream = File.OpenRead("DeserializeAttributeRequest.json"))
408+
{
409+
// Arrange
410+
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
408411

409-
var deserialized = (IList<Sample>)await formatter.ReadFromStreamAsync(typeof(Sample), stream, null, null);
412+
// Act
413+
var deserialized = (IList<Sample>)await formatter.ReadFromStreamAsync(typeof(Sample), inputStream, null, null);
410414

411-
// Assert
412-
deserialized.Count.Should().Be(2);
413-
deserialized[0].ShouldBeEquivalentTo(s1);
414-
deserialized[1].ShouldBeEquivalentTo(s2);
415+
// Assert
416+
deserialized.Count.Should().Be(2);
417+
deserialized[0].ShouldBeEquivalentTo(s1);
418+
deserialized[1].ShouldBeEquivalentTo(s2);
419+
}
415420
}
416421

417422
[TestMethod]

0 commit comments

Comments
 (0)