summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/count_request_test.cpp
diff options
context:
space:
mode:
authorKaloian Manassiev <kaloian.manassiev@mongodb.com>2015-06-03 07:19:36 -0400
committerKaloian Manassiev <kaloian.manassiev@mongodb.com>2015-06-11 10:34:56 -0400
commit2cfb90ff4107da243309664dced5e3c43665a018 (patch)
treeaa5b18f598bf537bbbb01122549d8849cd7c82fe /src/mongo/db/query/count_request_test.cpp
parent37f58ed85dc2ad27bac876ec4f2802d8f248b91d (diff)
downloadmongo-2cfb90ff4107da243309664dced5e3c43665a018.tar.gz
SERVER-18791 Pull count command parsing into a separate library
Diffstat (limited to 'src/mongo/db/query/count_request_test.cpp')
-rw-r--r--src/mongo/db/query/count_request_test.cpp133
1 files changed, 133 insertions, 0 deletions
diff --git a/src/mongo/db/query/count_request_test.cpp b/src/mongo/db/query/count_request_test.cpp
new file mode 100644
index 00000000000..4f0546b4180
--- /dev/null
+++ b/src/mongo/db/query/count_request_test.cpp
@@ -0,0 +1,133 @@
+/**
+ * Copyright (C) 2015 MongoDB Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the GNU Affero General Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#include "mongo/platform/basic.h"
+
+#include "mongo/bson/json.h"
+#include "mongo/db/query/count_request.h"
+#include "mongo/util/mongoutils/str.h"
+#include "mongo/unittest/unittest.h"
+
+namespace mongo {
+namespace {
+
+ TEST(CountRequest, ParseDefaults) {
+ const auto countRequestStatus =
+ CountRequest::parseFromBSON("TestDB",
+ BSON("count" << "TestColl" <<
+ "query" << BSON("a" << BSON("$lte" << 10))));
+
+ ASSERT_OK(countRequestStatus.getStatus());
+
+ const CountRequest& countRequest = countRequestStatus.getValue();
+
+ ASSERT_EQUALS(countRequest.getNs(), "TestDB.TestColl");
+ ASSERT_EQUALS(countRequest.getQuery(), fromjson("{ a : { '$lte' : 10 } }"));
+
+ // Defaults
+ ASSERT_EQUALS(countRequest.getLimit(), 0);
+ ASSERT_EQUALS(countRequest.getSkip(), 0);
+ ASSERT(countRequest.getHint().isEmpty());
+ }
+
+ TEST(CountRequest, ParseComplete) {
+ const auto countRequestStatus =
+ CountRequest::parseFromBSON("TestDB",
+ BSON("count" << "TestColl" <<
+ "query" << BSON("a" << BSON("$gte" << 11)) <<
+ "limit" << 100 <<
+ "skip" << 1000 <<
+ "hint" << BSON("b" << 5)));
+
+ ASSERT_OK(countRequestStatus.getStatus());
+
+ const CountRequest& countRequest = countRequestStatus.getValue();
+
+ ASSERT_EQUALS(countRequest.getNs(), "TestDB.TestColl");
+ ASSERT_EQUALS(countRequest.getQuery(), fromjson("{ a : { '$gte' : 11 } }"));
+ ASSERT_EQUALS(countRequest.getLimit(), 100);
+ ASSERT_EQUALS(countRequest.getSkip(), 1000);
+ ASSERT_EQUALS(countRequest.getHint(), fromjson("{ b : 5 }"));
+ }
+
+ TEST(CountRequest, ParseNegativeLimit) {
+ const auto countRequestStatus =
+ CountRequest::parseFromBSON("TestDB",
+ BSON("count" << "TestColl" <<
+ "query" << BSON("a" << BSON("$gte" << 11)) <<
+ "limit" << -100 <<
+ "skip" << 1000 <<
+ "hint" << BSON("b" << 5)));
+
+ ASSERT_OK(countRequestStatus.getStatus());
+
+ const CountRequest& countRequest = countRequestStatus.getValue();
+
+ ASSERT_EQUALS(countRequest.getNs(), "TestDB.TestColl");
+ ASSERT_EQUALS(countRequest.getQuery(), fromjson("{ a : { '$gte' : 11 } }"));
+ ASSERT_EQUALS(countRequest.getLimit(), 100);
+ ASSERT_EQUALS(countRequest.getSkip(), 1000);
+ ASSERT_EQUALS(countRequest.getHint(), fromjson("{ b : 5 }"));
+ }
+
+ TEST(CountRequest, FailParseMissingNS) {
+ const auto countRequestStatus =
+ CountRequest::parseFromBSON("TestDB",
+ BSON("query" << BSON("a" << BSON("$gte" << 11))));
+
+ ASSERT_EQUALS(countRequestStatus.getStatus(), ErrorCodes::BadValue);
+ }
+
+ TEST(CountRequest, FailParseBadSkipValue) {
+ const auto countRequestStatus =
+ CountRequest::parseFromBSON("TestDB",
+ BSON("count" << "TestColl" <<
+ "query" << BSON("a" << BSON("$gte" << 11)) <<
+ "skip" << -1000));
+
+ ASSERT_EQUALS(countRequestStatus.getStatus(), ErrorCodes::BadValue);
+ }
+
+ TEST(CountRequest, ToBSON) {
+ CountRequest countRequest("TestDB.TestColl", BSON("a" << BSON("$gte" << 11)));
+ countRequest.setLimit(100);
+ countRequest.setSkip(1000);
+ countRequest.setHint(BSON("b" << 5));
+
+ BSONObj actualObj = countRequest.toBSON();
+ BSONObj expectedObj(fromjson("{ count : 'TestDB.TestColl',"
+ " query : { a : { '$gte' : 11 } },"
+ " limit : 100,"
+ " skip : 1000,"
+ " hint : { b : 5 } }"));
+
+ ASSERT_EQUALS(actualObj, expectedObj);
+ }
+
+} // namespace
+} // namespace mongo