-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathLinkTemplateTests.cs
More file actions
93 lines (80 loc) · 2.74 KB
/
LinkTemplateTests.cs
File metadata and controls
93 lines (80 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using JSONAPI.Attributes;
using JSONAPI.Json;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace JSONAPI.Tests.Json
{
[TestClass]
public class LinkTemplateTests
{
private class Post
{
public string Id { get; set; }
public string Title { get; set; }
[SerializeAs(SerializeAsOptions.Link)]
[LinkTemplate("/users/{0}")]
public virtual User Author { get; set; }
}
private class User
{
public string Id { get; set; }
public string Name { get; set; }
}
private Post ThePost { get; set; }
[TestInitialize]
public void SetupModels()
{
ThePost = new Post
{
Id = "2",
Title = "How to fry an egg",
Author = new User
{
Id = "5",
Name = "Bob"
}
};
}
[TestMethod]
[DeploymentItem(@"Data\LinkTemplateTest.json")]
public void GetResourceWithLinkTemplateRelationship()
{
var formatter = new JsonApiFormatter
(
new JSONAPI.Core.PluralizationService()
);
var stream = new MemoryStream();
formatter.WriteToStreamAsync(typeof(Post), ThePost, stream, null, null);
// Assert
var expected = JsonHelpers.MinifyJson(File.ReadAllText("LinkTemplateTest.json"));
var output = Encoding.ASCII.GetString(stream.ToArray());
Trace.WriteLine(output);
Assert.AreEqual(expected,output.Trim());
}
[TestMethod]
[DeploymentItem(@"Data\OverrideSerializationAttributesTest.json")]
public void OverrideSerializationAttributesTest()
{
// Arrange
var formatter = new JsonApiFormatter
(
new JSONAPI.Core.PluralizationService()
);
var stream = new MemoryStream();
// Act
JSONAPI.Core.MetadataManager.Instance.SetPropertyAttributeOverrides(
ThePost, typeof(Post).GetProperty("Author"),
new SerializeAs(SerializeAsOptions.Ids),
new IncludeInPayload(true)
);
formatter.WriteToStreamAsync(typeof(Post), ThePost, stream, null, null);
// Assert
var expected = JsonHelpers.MinifyJson(File.ReadAllText("OverrideSerializationAttributesTest.json"));
var output = Encoding.ASCII.GetString(stream.ToArray());
Trace.WriteLine(output);
Assert.AreEqual(expected, output.Trim());
}
}
}