Support conversion of linkshere
[dueringa_WikiWalker.git] / test / JsonSerializeDeserializeTests.cpp
blobd46bac1c5e8c1c6096c3d06b48af1de2aa0161bb
1 #include <UnitTest++/UnitTest++.h>
3 #include <memory>
4 #include <sstream>
5 #include <utility>
7 #include "Article.h"
8 #include "JsonSerializer.h"
10 SUITE(JsonSerializeDeserializeTests)
12 using namespace WikiWalker;
13 using namespace WikiWalker::CollectionUtils;
15 TEST(SerializeUnanalyzedArticleWithoutLinks_DeserializesInSameState)
17 JsonSerializer atj;
18 std::stringstream ss;
21 ArticleCollection ac;
22 CollectionUtils::add(ac, std::make_shared<Article>("Farm"));
23 atj.serialize(ac, ss);
26 ArticleCollection ac2;
27 atj.deserialize(ac2, ss);
28 CHECK_EQUAL(1, ac2.size());
29 CHECK(CollectionUtils::get(ac2, "Farm") != nullptr);
30 CHECK(!CollectionUtils::get(ac2, "Farm")->analyzed());
33 TEST(SerializeAnalyzedArticleWithoutLinks_DeserializesInSameState)
35 JsonSerializer atj;
36 std::stringstream ss;
39 ArticleCollection ac;
40 auto a = std::make_shared<Article>("Farm");
41 CollectionUtils::add(ac, a);
42 a->analyzed(true);
43 atj.serialize(ac, ss);
46 ArticleCollection ac2;
47 atj.deserialize(ac2, ss);
48 CHECK_EQUAL(1, ac2.size());
49 CHECK(CollectionUtils::get(ac2, "Farm") != nullptr);
50 CHECK(CollectionUtils::get(ac2, "Farm")->analyzed());
51 CHECK_EQUAL(0, CollectionUtils::get(ac2, "Farm")->countLinks());
54 TEST(SerializeArticleWithOneLink_DeserializesTwoArticles)
56 JsonSerializer atj;
57 std::stringstream ss;
60 ArticleCollection ac;
61 // yes, only a is inserted, since we want to emulate article-only
62 auto a = std::make_shared<Article>("Farm");
63 CollectionUtils::add(ac, a);
65 auto linked = std::make_shared<Article>("Animal");
66 a->addLink(linked);
67 atj.serialize(ac, ss);
70 ArticleCollection ac2;
71 atj.deserialize(ac2, ss);
73 // Per design, linked-only articles are restored as "new" ones
74 CHECK_EQUAL(2, ac2.size());
76 CHECK(CollectionUtils::get(ac2, "Farm") != nullptr);
77 CHECK(CollectionUtils::get(ac2, "Farm")->analyzed());
78 CHECK_EQUAL(1, CollectionUtils::get(ac2, "Farm")->countLinks());
80 CHECK(CollectionUtils::get(ac2, "Animal") != nullptr);
81 CHECK(!CollectionUtils::get(ac2, "Animal")->analyzed());
84 TEST(SerializeArticleWithMultipleLinks_DeserializesNewArticles)
86 JsonSerializer atj;
87 std::stringstream ss;
90 ArticleCollection ac;
92 // yes, only a is inserted, since we want to emulate article-only
93 auto a = std::make_shared<Article>("Farm");
94 CollectionUtils::add(ac, a);
95 CHECK_EQUAL(1, ac.size());
97 auto al1 = std::make_shared<Article>("Animal"),
98 al2 = std::make_shared<Article>("Pig"),
99 al3 = std::make_shared<Article>("Equality");
100 a->addLink(al1);
101 a->addLink(al2);
102 a->addLink(al3);
103 CHECK_EQUAL(1, ac.size());
104 atj.serialize(ac, ss);
107 ArticleCollection ac2;
108 atj.deserialize(ac2, ss);
110 // Per design, linked-only articles are restored as "new" ones
111 CHECK_EQUAL(4, ac2.size());
113 CHECK(CollectionUtils::get(ac2, "Farm") != nullptr);
114 CHECK(CollectionUtils::get(ac2, "Farm")->analyzed());
115 CHECK_EQUAL(3, CollectionUtils::get(ac2, "Farm")->countLinks());
117 for(std::string title : {"Animal", "Pig", "Equality"}) {
118 CHECK(CollectionUtils::get(ac2, title) != nullptr);
119 CHECK(!CollectionUtils::get(ac2, title)->analyzed());
123 TEST(SerializeEmptyArticleCollection_DeserializesInSameState)
125 JsonSerializer atj;
126 std::stringstream ss;
129 ArticleCollection ac;
130 atj.serialize(ac, ss);
133 ArticleCollection ac2;
134 atj.deserialize(ac2, ss);
135 CHECK_EQUAL(0, ac2.size());