|
|
@@ -0,0 +1,131 @@
|
|
|
+#include "db_errors.hpp"
|
|
|
+#include <gtest/gtest.h>
|
|
|
+
|
|
|
+// The database-failure mapping. It had no tests at all until a third reason
|
|
|
+// branch went in, which is one branch too many for "it looked right".
|
|
|
+//
|
|
|
+// Driven through the FailureDetail overload because that struct is plain data:
|
|
|
+// the whole mapping is assertable with no database, no network and no clock,
|
|
|
+// and every one of these decisions is behaviour a caller depends on.
|
|
|
+namespace {
|
|
|
+
|
|
|
+using Detail = smartbotic::database::Client::FailureDetail;
|
|
|
+using Code = smartbotic::database::Client::ErrorCode;
|
|
|
+
|
|
|
+Detail detail(Code code, std::string reason = "", std::string message = "boom") {
|
|
|
+ Detail d;
|
|
|
+ d.code = code;
|
|
|
+ d.reason = std::move(reason);
|
|
|
+ d.message = std::move(message);
|
|
|
+ return d;
|
|
|
+}
|
|
|
+
|
|
|
+// --- AlreadyExists covers TWO different collisions --------------------------
|
|
|
+
|
|
|
+TEST(DbErrors, ADuplicateFieldValueIsDuplicateValues) {
|
|
|
+ const svapi::ApiError e = svapi::apiErrorFor(detail(Code::AlreadyExists));
|
|
|
+ EXPECT_EQ(svapi::httpStatus(e.code), 409);
|
|
|
+ EXPECT_EQ(e.slug, "duplicate_values");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(DbErrors, ATakenDocumentIdIsDuplicateIdNotDuplicateValues) {
|
|
|
+ // Different remedies: a unique FIELD collision means change the value, a
|
|
|
+ // taken ID means choose another id or upsert with PUT. Reporting both as
|
|
|
+ // duplicate_values sent a caller looking for a field that was never the
|
|
|
+ // problem. The reason arrived with database 2.12.1.
|
|
|
+ const svapi::ApiError e = svapi::apiErrorFor(detail(Code::AlreadyExists, "duplicate_id"));
|
|
|
+ EXPECT_EQ(svapi::httpStatus(e.code), 409);
|
|
|
+ EXPECT_EQ(e.slug, "duplicate_id");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(DbErrors, AnUnknownAlreadyExistsReasonFallsBackToTheBroaderAnswer) {
|
|
|
+ // An older server sets no reason, and a future one may set a reason we do
|
|
|
+ // not know. Both must take the general answer rather than a guess at a
|
|
|
+ // specific one.
|
|
|
+ EXPECT_EQ(svapi::apiErrorFor(detail(Code::AlreadyExists, "")).slug, "duplicate_values");
|
|
|
+ EXPECT_EQ(svapi::apiErrorFor(detail(Code::AlreadyExists, "something_new")).slug,
|
|
|
+ "duplicate_values");
|
|
|
+}
|
|
|
+
|
|
|
+// --- FailedPrecondition means three unrelated things ------------------------
|
|
|
+
|
|
|
+TEST(DbErrors, MissingParentAndRestrictedDeleteAreUnprocessable) {
|
|
|
+ for (const char* reason : {"missing_parent", "delete_restricted"}) {
|
|
|
+ const svapi::ApiError e = svapi::apiErrorFor(detail(Code::FailedPrecondition, reason));
|
|
|
+ EXPECT_EQ(svapi::httpStatus(e.code), 422) << reason;
|
|
|
+ EXPECT_EQ(e.slug, "relation_violation") << reason;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+TEST(DbErrors, AReadOnlyDatabaseIs503NotTheCallersMistake) {
|
|
|
+ // The one that mattered: mapping the bare code to 422 told a caller that a
|
|
|
+ // read-only database was its own validation error, so it would fix its
|
|
|
+ // payload and retry forever instead of waiting.
|
|
|
+ const svapi::ApiError e = svapi::apiErrorFor(detail(Code::FailedPrecondition, "read_only"));
|
|
|
+ EXPECT_EQ(svapi::httpStatus(e.code), 503);
|
|
|
+ EXPECT_EQ(e.slug, "read_only");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(DbErrors, AnUnknownFailedPreconditionReasonIs500NotAGuessedFourTwentyTwo) {
|
|
|
+ // Three unrelated refusals share the code, so with no reason we genuinely
|
|
|
+ // do not know - and 500 says "we do not know" where 422 would assert the
|
|
|
+ // caller was wrong.
|
|
|
+ for (const char* reason : {"", "something_new"}) {
|
|
|
+ const svapi::ApiError e = svapi::apiErrorFor(detail(Code::FailedPrecondition, reason));
|
|
|
+ EXPECT_EQ(svapi::httpStatus(e.code), 500) << "reason='" << reason << "'";
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// --- the rest of the table --------------------------------------------------
|
|
|
+
|
|
|
+TEST(DbErrors, AVersionConflictIs409AndSaysSo) {
|
|
|
+ // Arrived as Unknown before 2.12.0 and folded into 503 "retry as-is",
|
|
|
+ // which is the opposite of the required instruction: re-read first.
|
|
|
+ const svapi::ApiError e = svapi::apiErrorFor(detail(Code::Aborted, "version_conflict"));
|
|
|
+ EXPECT_EQ(svapi::httpStatus(e.code), 409);
|
|
|
+ EXPECT_EQ(e.slug, "version_conflict");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(DbErrors, NotFoundIs404) {
|
|
|
+ EXPECT_EQ(svapi::httpStatus(svapi::apiErrorFor(detail(Code::NotFound)).code), 404);
|
|
|
+}
|
|
|
+
|
|
|
+TEST(DbErrors, TransportFailuresAre503) {
|
|
|
+ for (const Code c : {Code::Unavailable, Code::DeadlineExceeded, Code::Unknown})
|
|
|
+ EXPECT_EQ(svapi::httpStatus(svapi::apiErrorFor(detail(c)).code), 503)
|
|
|
+ << static_cast<int>(c);
|
|
|
+}
|
|
|
+
|
|
|
+TEST(DbErrors, OkMeansDidNotApplyWhichIsA404) {
|
|
|
+ // A call that returned false while the database raised nothing: the
|
|
|
+ // document was simply not there. Mapping this to 500 turned a plain
|
|
|
+ // missing document into a server fault with an empty message.
|
|
|
+ const svapi::ApiError e = svapi::apiErrorFor(detail(Code::Ok, "", ""), "no such document");
|
|
|
+ EXPECT_EQ(svapi::httpStatus(e.code), 404);
|
|
|
+ EXPECT_EQ(e.what(), std::string("no such document"))
|
|
|
+ << "the empty database message must not be surfaced in place of ours";
|
|
|
+}
|
|
|
+
|
|
|
+TEST(DbErrors, NotFoundUsesOurMessageNotTheDatabasesWording) {
|
|
|
+ // Deliberate: the caller gets the route's own phrasing for a missing
|
|
|
+ // document, so the answer does not change if the database rewords itself.
|
|
|
+ const svapi::ApiError e = svapi::apiErrorFor(detail(Code::NotFound, "", "no row 7"),
|
|
|
+ "no such document");
|
|
|
+ EXPECT_EQ(e.what(), std::string("no such document"));
|
|
|
+}
|
|
|
+
|
|
|
+TEST(DbErrors, TheDatabaseMessageIsCarriedThroughWhereItIsTheUsefulOne) {
|
|
|
+ // For a validation refusal the database knows WHY and we do not, so its
|
|
|
+ // sentence is the one worth showing.
|
|
|
+ const svapi::ApiError bad =
|
|
|
+ svapi::apiErrorFor(detail(Code::InvalidArgument, "", "field 'x' is not a number"));
|
|
|
+ EXPECT_EQ(svapi::httpStatus(bad.code), 422);
|
|
|
+ EXPECT_EQ(bad.what(), std::string("field 'x' is not a number"));
|
|
|
+
|
|
|
+ const svapi::ApiError denied =
|
|
|
+ svapi::apiErrorFor(detail(Code::PermissionDenied, "", "collection is read-protected"));
|
|
|
+ EXPECT_EQ(svapi::httpStatus(denied.code), 403);
|
|
|
+ EXPECT_EQ(denied.what(), std::string("collection is read-protected"));
|
|
|
+}
|
|
|
+
|
|
|
+} // namespace
|