|
|
@@ -0,0 +1,75 @@
|
|
|
+#include <gtest/gtest.h>
|
|
|
+
|
|
|
+#include <nlohmann/json.hpp>
|
|
|
+
|
|
|
+#include "functions/known_keys.hpp"
|
|
|
+
|
|
|
+using svapi::functions::unknownKeyMessage;
|
|
|
+using svapi::functions::unknownKeys;
|
|
|
+
|
|
|
+namespace {
|
|
|
+constexpr std::initializer_list<std::string_view> kSearch = {
|
|
|
+ "query_text", "query_vector", "top_k", "min_score", "filters", "embedTimeoutMs"};
|
|
|
+}
|
|
|
+
|
|
|
+TEST(KnownKeys, AValidBodyHasNoUnknownKeys) {
|
|
|
+ const nlohmann::json body = {{"query_text", "x"}, {"top_k", 3}, {"min_score", 0.5}};
|
|
|
+ EXPECT_TRUE(unknownKeys(body, kSearch).empty());
|
|
|
+}
|
|
|
+
|
|
|
+TEST(KnownKeys, THEThreeMisspellingsMEASUREDOnProd) {
|
|
|
+ // Each of these answered 200 before the fix, and each was wrong in its own
|
|
|
+ // way: topK gave the DEFAULT five rows for a caller asking three, minScore
|
|
|
+ // dropped a quality threshold, and `filter` returned the rows the caller
|
|
|
+ // meant to exclude.
|
|
|
+ EXPECT_EQ(unknownKeys({{"query_text", "x"}, {"topK", 3}}, kSearch),
|
|
|
+ std::vector<std::string>{"topK"});
|
|
|
+ EXPECT_EQ(unknownKeys({{"query_text", "x"}, {"minScore", 0.99}}, kSearch),
|
|
|
+ std::vector<std::string>{"minScore"});
|
|
|
+ EXPECT_EQ(unknownKeys({{"query_text", "x"}, {"filter", nlohmann::json::array()}}, kSearch),
|
|
|
+ std::vector<std::string>{"filter"});
|
|
|
+}
|
|
|
+
|
|
|
+TEST(KnownKeys, EVERYUnknownKeyIsNamedNotJustTheFirst) {
|
|
|
+ // A caller who fixes one typo and gets the same error again learns nothing
|
|
|
+ // about how many are left.
|
|
|
+ const nlohmann::json body = {{"query_text", "x"}, {"topK", 1}, {"minScore", 2}, {"rerank", true}};
|
|
|
+ const auto unknown = unknownKeys(body, kSearch);
|
|
|
+ EXPECT_EQ(unknown.size(), 3u);
|
|
|
+ const std::string msg = unknownKeyMessage(unknown, kSearch);
|
|
|
+ for (const char* k : {"topK", "minScore", "rerank"})
|
|
|
+ EXPECT_NE(msg.find(k), std::string::npos) << msg;
|
|
|
+ // And the accepted set is listed, because the usual cause is a plausible
|
|
|
+ // spelling of a real option rather than an invented one.
|
|
|
+ EXPECT_NE(msg.find("top_k"), std::string::npos) << msg;
|
|
|
+ EXPECT_NE(msg.find("min_score"), std::string::npos) << msg;
|
|
|
+}
|
|
|
+
|
|
|
+TEST(KnownKeys, TheMessageReadsCorrectlyForOneAndForMany) {
|
|
|
+ EXPECT_NE(unknownKeyMessage({"topK"}, kSearch).find("unknown option 'topK'"),
|
|
|
+ std::string::npos);
|
|
|
+ const std::string two = unknownKeyMessage({"a", "b"}, kSearch);
|
|
|
+ EXPECT_NE(two.find("unknown options 'a' and 'b'"), std::string::npos) << two;
|
|
|
+ const std::string three = unknownKeyMessage({"a", "b", "c"}, kSearch);
|
|
|
+ EXPECT_NE(three.find("'a', 'b' and 'c'"), std::string::npos) << three;
|
|
|
+}
|
|
|
+
|
|
|
+TEST(KnownKeys, ANonObjectBodyIsNotAnUnknownKeyProblem) {
|
|
|
+ // It has its own error elsewhere; this must not turn an array body into a
|
|
|
+ // confusing list of "unknown keys".
|
|
|
+ EXPECT_TRUE(unknownKeys(nlohmann::json::array({1, 2}), kSearch).empty());
|
|
|
+ EXPECT_TRUE(unknownKeys(nlohmann::json(nullptr), kSearch).empty());
|
|
|
+ EXPECT_TRUE(unknownKeys(nlohmann::json("a string"), kSearch).empty());
|
|
|
+}
|
|
|
+
|
|
|
+TEST(KnownKeys, MatchingIsExactAndCaseSensitive) {
|
|
|
+ // `Top_K` is not `top_k`. Accepting case variants would mean the service
|
|
|
+ // has two spellings for one option, which is how the next drift starts.
|
|
|
+ EXPECT_FALSE(unknownKeys({{"Top_K", 1}}, kSearch).empty());
|
|
|
+ EXPECT_FALSE(unknownKeys({{"TOP_K", 1}}, kSearch).empty());
|
|
|
+ EXPECT_TRUE(unknownKeys({{"top_k", 1}}, kSearch).empty());
|
|
|
+}
|
|
|
+
|
|
|
+TEST(KnownKeys, AnEmptyBodyIsFine) {
|
|
|
+ EXPECT_TRUE(unknownKeys(nlohmann::json::object(), kSearch).empty());
|
|
|
+}
|