Support conversion of linkshere
[dueringa_WikiWalker.git] / test / JsonDeserializerTests.cpp
blobfc7c45ff91254fd943630c4fdd0643c61f1ab29d
1 #include <memory>
2 #include <sstream>
4 #include <UnitTest++/UnitTest++.h>
6 #include "Article.h"
7 #include "JsonSerializer.h"
8 #include "WalkerException.h"
10 #include "SerializerTestDefines.h"
12 #define WW_TEST_JSONSERIALIZER_PROTOCOL_COMPLETE \
13 "{" WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1 \
14 "," WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2 \
15 "," WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY
17 SUITE(JsonDeserializerTests)
19 using namespace WikiWalker;
20 using namespace WikiWalker::CollectionUtils;
22 TEST(Deserialize_ArticleCollection_OneArticleWithoutLinks_Unanalyzed)
24 std::string jsonString = WW_TEST_JSONSERIALIZER_PROTOCOL_COMPLETE
25 R"({"Farm":{"forward_links":null}})"
26 "}";
27 std::istringstream json(jsonString);
29 ArticleCollection ac;
30 JsonSerializer deser;
31 deser.deserialize(ac, json);
33 CHECK_EQUAL(1, ac.size());
35 auto a = CollectionUtils::get(ac, "Farm");
36 CHECK(a != nullptr);
37 CHECK_EQUAL(false, a->analyzed());
40 TEST(Deserialize_ArticleCollection_OneArticleWithoutLinks_Analyzed)
42 std::string jsonString = WW_TEST_JSONSERIALIZER_PROTOCOL_COMPLETE
43 R"({"Farm":{"forward_links":[]}})"
44 "}";
45 std::istringstream json(jsonString);
47 ArticleCollection ac;
48 JsonSerializer deser;
49 deser.deserialize(ac, json);
51 CHECK_EQUAL(1, ac.size());
53 auto a = CollectionUtils::get(ac, "Farm");
54 CHECK(a != nullptr);
56 CHECK_EQUAL(true, a->analyzed());
57 CHECK_EQUAL(0, a->countLinks());
60 TEST(Deserialize_ArticleCollection_OneArticleWithOneLink)
62 std::string jsonString = WW_TEST_JSONSERIALIZER_PROTOCOL_COMPLETE
63 R"({"Farm":{"forward_links":["Animal"]}})"
64 "}";
65 std::istringstream json(jsonString);
67 ArticleCollection ac;
68 JsonSerializer deser;
69 deser.deserialize(ac, json);
71 /* Deserialized state doesn't exactly match serialized one.
72 * This is intended. Only-link articles become new articles in the
73 * collection.
75 CHECK_EQUAL(2, ac.size());
77 auto a = CollectionUtils::get(ac, "Farm");
78 CHECK(a != nullptr);
80 CHECK_EQUAL(1, a->countLinks());
83 TEST(Deserialize_ArticleCollection_OneArticleWithMultipleLinks)
85 std::string jsonString = WW_TEST_JSONSERIALIZER_PROTOCOL_COMPLETE
86 R"({"Farm":{"forward_links":["Animal","Pig","Equality"]}})"
87 "}";
88 std::istringstream json(jsonString);
90 ArticleCollection ac;
91 JsonSerializer deser;
92 deser.deserialize(ac, json);
94 // see comment in Deserialize_ArticleCollection_OneArticleWithOneLink
95 CHECK_EQUAL(4, ac.size());
97 auto a = CollectionUtils::get(ac, "Farm");
98 CHECK(a != nullptr);
100 CHECK_EQUAL(3, a->countLinks());
103 TEST(Deserialize_ArticleCollection_Empty)
105 std::string jsonString = WW_TEST_JSONSERIALIZER_PROTOCOL_COMPLETE
106 "{}"
107 "}";
108 std::istringstream json(jsonString);
110 ArticleCollection ac;
111 JsonSerializer deser;
112 deser.deserialize(ac, json);
113 CHECK_EQUAL(0, ac.size());
116 TEST(Deserialize_ArticleCollection_MultipleArticlesWithMultipleLinks)
118 std::string jsonString = WW_TEST_JSONSERIALIZER_PROTOCOL_COMPLETE
119 R"({)"
120 R"("Farm":)"
121 R"({"forward_links":["Animal","Pig","Equality"]},)"
122 R"("Animal":)"
123 R"({"forward_links":["Cat","Pig","Dog"]})"
124 R"(})"
125 "}";
126 std::istringstream json(jsonString);
128 ArticleCollection ac;
129 JsonSerializer deser;
130 deser.deserialize(ac, json);
132 // see comment in Deserialize_ArticleCollection_OneArticleWithOneLink
133 CHECK_EQUAL(6, ac.size());
135 for(std::string a : {"Farm", "Animal", "Pig", "Equality", "Cat", "Dog"}) {
136 auto x = CollectionUtils::get(ac, a);
137 CHECK(x != nullptr);
140 auto a = CollectionUtils::get(ac, "Farm");
141 CHECK(a != nullptr);
142 CHECK_EQUAL(3, a->countLinks());
145 TEST(Deserialize_ArticleCollection_WrongProgramName)
147 std::string jsonString =
148 R"({"program":"wikwalker","scheme-version":2,)"
149 R"("ArticleCollection":)"
150 R"({"Farm":{"forward_links":null}})"
151 "}";
153 std::istringstream json(jsonString);
155 ArticleCollection ac;
156 JsonSerializer deser;
157 CHECK_THROW(deser.deserialize(ac, json), WalkerException);
160 TEST(Deserialize_ArticleCollection_WrongVersion)
162 std::string jsonString =
163 R"({"program":"wikiwalker","scheme-version":3,)"
164 R"("ArticleCollection":)"
165 R"({"Farm":{"forward_links":null}})"
166 "}";
168 std::istringstream json(jsonString);
170 ArticleCollection ac;
171 JsonSerializer deser;
172 CHECK_THROW(deser.deserialize(ac, json), WalkerException);