This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 633
/
GitHubRestTests.cs
112 lines (102 loc) · 4.08 KB
/
GitHubRestTests.cs
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using NUnit.Framework;
namespace ServiceStack.Text.Tests.UseCases
{
public class GitHubRestTests
{
private const string JsonPullRequest = @"{
""pulls"": [
{
""state"": ""open"",
""base"": {
""label"": ""technoweenie:master"",
""ref"": ""master"",
""sha"": ""53397635da83a2f4b5e862b5e59cc66f6c39f9c6"",
},
""head"": {
""label"": ""smparkes:synchrony"",
""ref"": ""synchrony"",
""sha"": ""83306eef49667549efebb880096cb539bd436560"",
},
""discussion"": [
{
""type"": ""IssueComment"",
""gravatar_id"": ""821395fe70906c8290df7f18ac4ac6cf"",
""created_at"": ""2010/10/07 07:38:35 -0700"",
""body"": ""Did you intend to remove net/http? Otherwise, this looks good. Have you tried running the LIVE tests with it?\r\n\r\n ruby test/live_server.rb # start the demo server\r\n LIVE=1 rake"",
""updated_at"": ""2010/10/07 07:38:35 -0700"",
""id"": 453980,
},
{
""type"": ""Commit"",
""created_at"": ""2010-11-04T16:27:45-07:00"",
""sha"": ""83306eef49667549efebb880096cb539bd436560"",
""author"": ""Steven Parkes"",
""subject"": ""add em_synchrony support"",
""email"": ""smparkes@smparkes.net""
}
],
""title"": ""Synchrony"",
""body"": ""Here's the pull request.\r\n\r\nThis isn't generic EM: require's Ilya's synchrony and needs to be run on its own fiber, e.g., via synchrony or rack-fiberpool.\r\n\r\nI thought about a \""first class\"" em adapter, but I think the faraday api is sync right now, right? Interesting idea to add something like rack's async support to faraday, but that's an itch I don't have right now."",
""position"": 4.0,
""number"": 15,
""votes"": 0,
""comments"": 4,
""diff_url"": ""https://github.com/technoweenie/faraday/pull/15.diff"",
""patch_url"": ""https://github.com/technoweenie/faraday/pull/15.patch"",
""labels"": [],
""html_url"": ""https://github.com/technoweenie/faraday/pull/15"",
""issue_created_at"": ""2010-10-04T12:39:18-07:00"",
""issue_updated_at"": ""2010-11-04T16:35:04-07:00"",
""created_at"": ""2010-10-04T12:39:18-07:00"",
""updated_at"": ""2010-11-04T16:30:14-07:00""
}
]
}";
public class Discussion
{
public string Type { get; set; }
public string GravatarId { get; set; }
public string Body { get; set; }
public string CreatedAt { get; set; }
public string UpdatedAt { get; set; }
public int? Id { get; set; }
public string Sha { get; set; }
public string Author { get; set; }
public string Subject { get; set; }
public string Email { get; set; }
}
T Get<T>(Dictionary<string, string> map, string key)
{
string strVal;
return map.TryGetValue(key, out strVal) ? TypeSerializer.DeserializeFromString<T>(strVal) : default(T);
}
string GetString(Dictionary<string, string> map, string key)
{
string strVal;
return map.TryGetValue(key, out strVal) ? strVal : null;
}
[Test]
public void Can_parse_GitHub_discussion()
{
var jsonObj = JsonSerializer.DeserializeFromString<List<Dictionary<string, string>>>(JsonPullRequest);
var jsonPulls = JsonSerializer.DeserializeFromString<List<Dictionary<string, string>>>(jsonObj[0]["pulls"]);
var discussions = JsonSerializer.DeserializeFromString<List<Dictionary<string, string>>>(jsonPulls[0]["discussion"])
.ConvertAll(x => new Discussion {
Type = GetString(x, "type"),
GravatarId = GetString(x, "gravatar_id"),
CreatedAt = GetString(x, "created_at"),
Body = GetString(x, "body"),
UpdatedAt = GetString(x, "updated_at"),
Id = Get<int?>(x, "id"),
Sha = GetString(x, "sha"),
Author = GetString(x, "author"),
Subject = GetString(x, "subject"),
Email = GetString(x, "email"),
});
Console.WriteLine(discussions.Dump()); //See what's been parsed
Assert.That(discussions.ConvertAll(x => x.Type), Is.EquivalentTo(new[] { "IssueComment", "Commit" }));
}
}
}