Add conversion for backwards links
[dueringa_WikiWalker.git] / test / JsonSerializerTests.cpp
blob1afe8258e841f9a85517875117af81aea9f44f7f
1 #include <UnitTest++/UnitTest++.h>
3 #include <memory>
4 #include <sstream>
5 #include <string>
6 #include <utility>
8 #include "Article.h"
9 #include "JsonSerializer.h"
10 #include "StringUtils.h"
12 #include "SerializerTestDefines.h"
14 SUITE(ArticleJsonSerializerTests)
16 using namespace WikiWalker;
17 using namespace WikiWalker::CollectionUtils;
19 TEST(WriteUnanalyzedArticleWithoutLinks_LinksIsNull)
21 JsonSerializer atj;
22 std::ostringstream oss;
23 ArticleCollection ac;
24 CollectionUtils::add(ac, std::make_shared<Article>("Farm"));
26 atj.serialize(ac, oss);
28 auto serString = oss.str();
29 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1) !=
30 std::string::npos);
31 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2) !=
32 std::string::npos);
33 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY
34 R"({"Farm":{"forward_links":null}})") !=
35 std::string::npos);
38 TEST(WriteAnalyzedArticleWithoutLinks_LinksIsEmptyArray)
40 JsonSerializer atj;
41 std::ostringstream oss;
42 ArticleCollection ac;
44 auto a = std::make_shared<Article>("Farm");
45 CollectionUtils::add(ac, a);
46 a->analyzed(true);
48 atj.serialize(ac, oss);
50 auto serString = oss.str();
51 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1) !=
52 std::string::npos);
53 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2) !=
54 std::string::npos);
55 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY
56 R"({"Farm":{"forward_links":[]}})") !=
57 std::string::npos);
60 TEST(WriteArticleWithOneLink)
62 JsonSerializer atj;
63 std::ostringstream oss;
64 ArticleCollection ac;
66 // yes, only a is inserted, since we want to emulate article-only
67 auto a = std::make_shared<Article>("Farm");
68 CollectionUtils::add(ac, a);
70 auto linked = std::make_shared<Article>("Animal");
71 a->addLink(linked);
73 atj.serialize(ac, oss);
75 auto serString = oss.str();
76 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1) !=
77 std::string::npos);
78 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2) !=
79 std::string::npos);
80 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY
81 R"({"Farm":{"forward_links":["Animal"]}})") !=
82 std::string::npos);
85 TEST(WriteArticleWithMultipleLinks)
87 JsonSerializer atj;
88 std::ostringstream oss;
89 ArticleCollection ac;
91 // yes, only a is inserted, since we want to emulate article-only
92 auto a = std::make_shared<Article>("Farm");
93 CollectionUtils::add(ac, a);
95 auto al1 = std::make_shared<Article>("Animal"),
96 al2 = std::make_shared<Article>("Pig"),
97 al3 = std::make_shared<Article>("Equality");
99 a->addLink(al1);
100 a->addLink(al2);
101 a->addLink(al3);
103 atj.serialize(ac, oss);
105 auto serString = oss.str();
106 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1) !=
107 std::string::npos);
108 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2) !=
109 std::string::npos);
110 CHECK(serString.find(
111 WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY
112 R"({"Farm":{"forward_links":["Animal","Pig","Equality"]}})") !=
113 std::string::npos);
116 TEST(WriteEmptyArticleCollection)
118 JsonSerializer atj;
119 ArticleCollection ac;
120 std::ostringstream oss;
122 atj.serialize(ac, oss);
124 auto serString = oss.str();
125 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1) !=
126 std::string::npos);
127 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2) !=
128 std::string::npos);
129 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY "{}") !=
130 std::string::npos);
133 TEST(WriteArticleCollection_OneUnanalyzedArticleWithoutLinks_LinksIsNull)
135 JsonSerializer atj;
136 ArticleCollection ac;
137 std::ostringstream oss;
139 auto linked = std::make_shared<Article>("Foo");
140 CollectionUtils::add(ac, linked);
142 atj.serialize(ac, oss);
144 auto serString = oss.str();
145 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1) !=
146 std::string::npos);
147 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2) !=
148 std::string::npos);
149 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY
150 R"({"Foo":{"forward_links":null}})") !=
151 std::string::npos);
154 TEST(WriteArticleCollection_OneAnalyzedArticleWithoutLinks_LinksIsEmptyArray)
156 JsonSerializer atj;
157 ArticleCollection ac;
158 std::ostringstream oss;
160 auto a = std::make_shared<Article>("Foo");
161 a->analyzed(true);
162 CollectionUtils::add(ac, a);
164 atj.serialize(ac, oss);
166 auto serString = oss.str();
167 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1) !=
168 std::string::npos);
169 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2) !=
170 std::string::npos);
171 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY
172 R"({"Foo":{"forward_links":[]}})") !=
173 std::string::npos);
176 TEST(
177 WriteArticleCollection_MultipleArticles_WithMultipleLinks_MatchesExpected)
179 JsonSerializer atj;
180 ArticleCollection ac;
181 std::ostringstream oss;
183 auto a = std::make_shared<Article>("Foo");
184 auto b = std::make_shared<Article>("Bar");
185 auto c = std::make_shared<Article>("Baz");
186 a->addLink(b);
187 b->addLink(a);
188 b->addLink(c);
189 CollectionUtils::add(ac, a);
190 CollectionUtils::add(ac, b);
191 CollectionUtils::add(ac, c);
193 atj.serialize(ac, oss);
195 auto serString = oss.str();
196 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1) !=
197 std::string::npos);
198 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2) !=
199 std::string::npos);
200 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY
202 R"("Bar":{"forward_links":["Foo","Baz"]},)"
203 R"("Baz":{"forward_links":null},)"
204 R"("Foo":{"forward_links":["Bar"]})"
205 "}") != std::string::npos);
208 TEST(SerializeArticleWithOnlyNullptr_NullptrWillBeSkipped)
210 JsonSerializer atj;
211 std::ostringstream oss;
212 ArticleCollection ac;
214 // yes, only a is inserted, since we want to emulate article-only
215 auto a = std::make_shared<Article>("Farm");
216 CollectionUtils::add(ac, a);
219 // will be skipped on serialization, since it'll become nullptr
220 auto linked = std::make_shared<Article>("Animal");
221 a->addLink(linked);
224 atj.serialize(ac, oss);
226 auto serString = oss.str();
227 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1) !=
228 std::string::npos);
229 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2) !=
230 std::string::npos);
231 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY
232 R"({"Farm":{"forward_links":[]}})") !=
233 std::string::npos);
236 TEST(SerializeArticleWithValidArticleAndANullptr_NullptrWillBeSkipped)
238 JsonSerializer atj;
239 std::ostringstream oss;
240 ArticleCollection ac;
242 // yes, only a is inserted, since we want to emulate article-only
243 auto a = std::make_shared<Article>("Farm");
244 CollectionUtils::add(ac, a);
247 // will be skipped on serialization, since it'll become nullptr
248 auto linked = std::make_shared<Article>("Animal");
249 a->addLink(linked);
252 auto linked2 = std::make_shared<Article>("Barn");
253 a->addLink(linked2);
255 atj.serialize(ac, oss);
257 auto serString = oss.str();
258 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1) !=
259 std::string::npos);
260 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2) !=
261 std::string::npos);
262 CHECK(serString.find(WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY
263 R"({"Farm":{"forward_links":["Barn"]}})") !=
264 std::string::npos);