|
@@ -5078,3 +5078,85 @@ TEST_F(ApiFixture, PatchReportsAnAbsentDocumentWithoutAnExistenceProbe) {
|
|
|
EXPECT_EQ(body["v"], 1) << "the empty patch or the merge dropped an existing field";
|
|
EXPECT_EQ(body["v"], 1) << "the empty patch or the merge dropped an existing field";
|
|
|
EXPECT_EQ(body["w"], 2);
|
|
EXPECT_EQ(body["w"], 2);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+// --- the documents listing's paging metadata ---------------------------------
|
|
|
|
|
+//
|
|
|
|
|
+// `total` and `has_more` are computed by the database BEFORE pagination and
|
|
|
|
|
+// were being discarded by our handler, which sent only `count` - the PAGE size.
|
|
|
|
|
+// A consumer therefore had to page an entire matching set to learn how big it
|
|
|
|
|
+// was, and one measured instance of that was a full scan plus 1750 documents of
|
|
|
|
|
+// encoding to obtain a single integer.
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, TheDocumentsListingReportsTheFILTEREDTotalNotThePageOrTheCollection) {
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","pagemeta"},{"kind","json"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+ const std::string docs = base + "/pagemeta/documents";
|
|
|
|
|
+ // SEVEN rows, FOUR of which match. Three distinct numbers - page 2, match
|
|
|
|
|
+ // total 4, collection 7 - so confusing any pair of them fails this test.
|
|
|
|
|
+ // With one shared number the assertions would pass under every mix-up.
|
|
|
|
|
+ for (const char* k : {"yes", "yes", "yes", "yes", "no", "no", "no"})
|
|
|
|
|
+ ASSERT_EQ(c.Post(docs.c_str(), nlohmann::json{{"flag", k}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+
|
|
|
|
|
+ auto first = c.Get((docs + "?filter=flag:eq:yes&limit=2").c_str());
|
|
|
|
|
+ ASSERT_TRUE(first); ASSERT_EQ(first->status, 200);
|
|
|
|
|
+ const nlohmann::json a = nlohmann::json::parse(first->body);
|
|
|
|
|
+ // PRESENCE BEFORE VALUE, in every arm. Indexing a missing key with
|
|
|
|
|
+ // `operator[]` on a const json ASSERTS inside nlohmann and aborts the whole
|
|
|
|
|
+ // binary - which in a suite that must run serially would take every later
|
|
|
|
|
+ // test with it and report the regression as a crash rather than as a named
|
|
|
|
|
+ // failing assertion. Found by deliberately falsifying this test.
|
|
|
|
|
+ ASSERT_TRUE(a.contains("total")) << "the listing stopped reporting `total`";
|
|
|
|
|
+ ASSERT_TRUE(a.contains("has_more")) << "the listing stopped reporting `has_more`";
|
|
|
|
|
+ EXPECT_EQ(a["documents"].size(), 2u);
|
|
|
|
|
+ EXPECT_EQ(a["count"], 2) << "`count` must stay the PAGE size; callers parse it already";
|
|
|
|
|
+ EXPECT_EQ(a["total"], 4) << "the filtered match total, not the page (2) and not the "
|
|
|
|
|
+ "collection (7)";
|
|
|
|
|
+ EXPECT_TRUE(a["has_more"].get<bool>());
|
|
|
|
|
+
|
|
|
|
|
+ // The PAIRING, and the half that makes `has_more` worth sending: the last
|
|
|
|
|
+ // page must say false while `total` is unchanged. Without this arm a
|
|
|
|
|
+ // handler hardcoding `true` would pass the assertion above.
|
|
|
|
|
+ auto last = c.Get((docs + "?filter=flag:eq:yes&limit=2&offset=2").c_str());
|
|
|
|
|
+ ASSERT_TRUE(last); ASSERT_EQ(last->status, 200);
|
|
|
|
|
+ const nlohmann::json b = nlohmann::json::parse(last->body);
|
|
|
|
|
+ ASSERT_TRUE(b.contains("total")); ASSERT_TRUE(b.contains("has_more"));
|
|
|
|
|
+ EXPECT_EQ(b["documents"].size(), 2u);
|
|
|
|
|
+ EXPECT_EQ(b["total"], 4);
|
|
|
|
|
+ EXPECT_FALSE(b["has_more"].get<bool>()) << "a full page and the LAST page must not look "
|
|
|
|
|
+ "identical - that was the original defect";
|
|
|
|
|
+
|
|
|
|
|
+ // Unfiltered, the total is the whole collection. Pins that the filter is
|
|
|
|
|
+ // what narrows `total`, rather than `total` happening to equal some
|
|
|
|
|
+ // constant this collection has.
|
|
|
|
|
+ auto all = c.Get((docs + "?limit=1").c_str());
|
|
|
|
|
+ ASSERT_TRUE(all); ASSERT_EQ(all->status, 200);
|
|
|
|
|
+ const nlohmann::json u = nlohmann::json::parse(all->body);
|
|
|
|
|
+ ASSERT_TRUE(u.contains("total"));
|
|
|
|
|
+ EXPECT_EQ(u["total"], 7);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, ThePagingFieldsArePRESENTEvenWhenNothingMatches) {
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","pagempty"},{"kind","json"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+ const std::string docs = base + "/pagempty/documents";
|
|
|
|
|
+ ASSERT_EQ(c.Post(docs.c_str(), nlohmann::json{{"flag","no"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+
|
|
|
|
|
+ auto r = c.Get((docs + "?filter=flag:eq:nothingmatchesthis").c_str());
|
|
|
|
|
+ ASSERT_TRUE(r); ASSERT_EQ(r->status, 200);
|
|
|
|
|
+ const nlohmann::json body = nlohmann::json::parse(r->body);
|
|
|
|
|
+ // KEY PRESENCE is the assertion, not the value. It is what makes "the field
|
|
|
|
|
+ // is absent" mean exactly one thing - a server older than this release -
|
|
|
|
|
+ // rather than also meaning "there was nothing interesting to report". A
|
|
|
|
|
+ // field emitted only when true is this codebase's most repeated trap.
|
|
|
|
|
+ ASSERT_TRUE(body.contains("total"));
|
|
|
|
|
+ ASSERT_TRUE(body.contains("has_more"));
|
|
|
|
|
+ EXPECT_EQ(body["total"], 0);
|
|
|
|
|
+ EXPECT_FALSE(body["has_more"].get<bool>());
|
|
|
|
|
+ EXPECT_TRUE(body["documents"].empty());
|
|
|
|
|
+}
|