-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUnitTest1.cs
165 lines (155 loc) · 4.1 KB
/
UnitTest1.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using DiscourseApi;
using System.IO;
using System.Threading.Tasks;
using System;
using Newtonsoft.Json.Linq;
namespace Tests {
public class Settings : DiscourseApi.Settings {
public bool LoginTests = false;
public bool ModifyTests = true;
public bool DestructiveTests = false;
public string TestUser = "nikki";
public string TestUser2 = "nikkilocke";
public int TestCategory = 6;
public int TestTopic = 8;
public int TestPost = 11;
public string TestGroup = "Test";
public override List<string> Validate() {
List<string> errors = base.Validate();
#if false
if (string.IsNullOrEmpty(TestUser))
errors.Add("TestUser missing");
if (TestCategory <= 0)
errors.Add("TestCategory missing");
if (TestTopic <= 0)
errors.Add("TestTopic missing");
#endif
return errors;
}
}
public class TestBase {
static Settings _settings;
static Api _api;
public static Api Api {
get {
if (_api == null) {
_api = new Api(Settings);
}
return _api;
}
}
public static Settings Settings {
get {
if (_settings == null) {
string dataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DiscourseApi");
Directory.CreateDirectory(dataPath);
string filename = Path.Combine(dataPath, "TestSettings.json");
_settings = new Settings();
_settings.Load(filename);
List<string> errors = _settings.Validate();
if (errors.Count > 0)
throw new ApplicationException(string.Join("\r\n", errors));
}
return _settings;
}
}
public static T RunTest<T>(Task<T> task) {
T t = task.Result;
Console.WriteLine(t);
return t;
}
public static void RunTest(Task task) {
task.Wait();
}
public static void ShowList<T,L>(Task<L> task) where T:new() where L : ApiList<T> {
ApiList<T> result = RunTest(task);
foreach (T o in result.All(Api))
Console.WriteLine(o);
}
public static void ShowList<T>(Task<List<T>> task) {
List<T> result = RunTest(task);
foreach (T o in result)
Console.WriteLine(o);
}
}
[TestClass]
public class UserTests : TestBase {
[TestMethod]
public void List() {
foreach (var u in User.ListAll(Api, "active").Result.All(Api))
Console.WriteLine(u);
}
[TestMethod]
public void GetByName() {
RunTest(User.GetByName(Api, Settings.TestUser));
}
}
[TestClass]
public class CategoryTests : TestBase {
[TestMethod]
public void List() {
RunTest(Category.ListAll(Api));
}
[TestMethod]
public void Get() {
RunTest(Category.Get(Api, Settings.TestCategory));
}
[TestMethod]
public void Create() {
if (!Settings.ModifyTests)
return;
var c = RunTest(Category.Create(Api, new UpdateCategoryParams() {
name = "Test Sub Category",
slug = "test-sub",
parent_category_id = Settings.TestCategory,
permissions = new Permissions(
new GroupPermission("Test", PermissionLevel.Create),
new GroupPermission("trust_level_0", PermissionLevel.See)
)
}));
RunTest(Category.Get(Api, c.id));
RunTest(Category.Delete(Api, c.id));
}
}
[TestClass]
public class TopicTests : TestBase {
[TestMethod]
public void List() {
ShowList<Topic, TopicListReturn>(Topic.ListAll(Api, Settings.TestCategory, true));
}
[TestMethod]
public void Get() {
RunTest(Topic.Get(Api, Settings.TestTopic));
}
}
[TestClass]
public class PostTests : TestBase {
[TestMethod]
public void GetAll() {
RunTest(Post.GetAll(Api, Settings.TestTopic));
}
[TestMethod]
public void Get() {
RunTest(Post.Get(Api, Settings.TestPost));
}
[TestMethod]
public void ChangeOwner() {
Post p = Post.Get(Api, Settings.TestPost).Result;
string user = p.username == Settings.TestUser ? Settings.TestUser2 : Settings.TestUser;
RunTest(Topic.ChangePostOwners(Api, p.topic_id, user, p.id));
}
}
[TestClass]
public class GroupTests : TestBase {
[TestMethod]
public void List() {
ShowList<Group, GroupList>(Group.ListAll(Api));
}
[TestMethod]
public void Get() {
RunTest(Group.Get(Api, Settings.TestGroup));
}
}
}