Skip to content

Commit 496f143

Browse files
committed
CDRIVER-2691 crud tests for count
1 parent fb9f568 commit 496f143

File tree

7 files changed

+247
-9
lines changed

7 files changed

+247
-9
lines changed

src/libmongoc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,7 @@ set (test-libmongoc-sources
712712
${PROJECT_SOURCE_DIR}/tests/test-mongoc-connection-uri.c
713713
${PROJECT_SOURCE_DIR}/tests/test-mongoc-command-monitoring.c
714714
${PROJECT_SOURCE_DIR}/tests/test-mongoc-counters.c
715+
${PROJECT_SOURCE_DIR}/tests/test-mongoc-crud.c
715716
${PROJECT_SOURCE_DIR}/tests/test-mongoc-cursor.c
716717
${PROJECT_SOURCE_DIR}/tests/test-mongoc-database.c
717718
${PROJECT_SOURCE_DIR}/tests/test-mongoc-error.c

src/libmongoc/tests/json-test-operations.c

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -884,33 +884,67 @@ count (mongoc_collection_t *collection,
884884
const mongoc_read_prefs_t *read_prefs)
885885
{
886886
bson_t filter;
887-
bson_t reply = BSON_INITIALIZER;
887+
bson_t reply;
888888
bson_t opts = BSON_INITIALIZER;
889889
bson_error_t error;
890-
int64_t r;
890+
int64_t r = -1;
891891
bson_value_t value;
892+
const char *name;
892893

893-
bson_lookup_doc (operation, "arguments.filter", &filter);
894+
if (bson_has_field (operation, "arguments.filter")) {
895+
bson_lookup_doc (operation, "arguments.filter", &filter);
896+
}
897+
if (bson_has_field (operation, "arguments.skip")) {
898+
BSON_APPEND_INT64 (
899+
&opts, "skip", bson_lookup_int32 (operation, "arguments.skip"));
900+
}
901+
if (bson_has_field (operation, "arguments.limit")) {
902+
BSON_APPEND_INT64 (
903+
&opts, "limit", bson_lookup_int32 (operation, "arguments.limit"));
904+
}
905+
if (bson_has_field (operation, "arguments.collation")) {
906+
bson_t collation;
907+
bson_lookup_doc (operation, "arguments.collation", &collation);
908+
BSON_APPEND_DOCUMENT (&opts, "collation", &collation);
909+
}
894910
append_session (session, &opts);
895-
r = mongoc_collection_count_with_opts (
896-
collection, MONGOC_QUERY_NONE, &filter, 0, 0, &opts, read_prefs, &error);
911+
912+
name = bson_lookup_utf8 (operation, "name");
913+
if (!strcmp (name, "countDocuments")) {
914+
r = mongoc_collection_count_documents (
915+
collection, &filter, &opts, read_prefs, &reply, &error);
916+
} else if (!strcmp (name, "estimatedDocumentCount")) {
917+
r = mongoc_collection_estimated_document_count (
918+
collection, &opts, read_prefs, &reply, &error);
919+
} else if (!strcmp (name, "count")) {
920+
/* deprecated old count function */
921+
r = mongoc_collection_count_with_opts (collection,
922+
MONGOC_QUERY_NONE,
923+
&filter,
924+
0,
925+
0,
926+
&opts,
927+
read_prefs,
928+
&error);
929+
/* fake a reply for the test framework's sake */
930+
bson_init (&reply);
931+
} else {
932+
test_error ("count() called with unrecognized operation name %s", name);
933+
}
897934

898935
if (r >= 0) {
899936
value.value_type = BSON_TYPE_INT64;
900937
value.value.v_int64 = r;
901938
} else {
902-
/* fake a reply for the test framework's sake */
903939
value_init_from_doc (&value, &reply);
904940
}
905-
906941
check_result (test, operation, r > -1, &value, &error);
907942

908943
bson_value_destroy (&value);
909944
bson_destroy (&reply);
910945
bson_destroy (&opts);
911946
}
912947

913-
914948
static void
915949
distinct (mongoc_collection_t *collection,
916950
const bson_t *test,
@@ -1228,6 +1262,10 @@ json_test_operation (json_test_ctx_t *ctx,
12281262
insert_many (collection, test, operation, session, wc);
12291263
} else if (!strcmp (op_name, "count")) {
12301264
count (collection, test, operation, session, read_prefs);
1265+
} else if (!strcmp (op_name, "estimatedDocumentCount")) {
1266+
count (collection, test, operation, session, read_prefs);
1267+
} else if (!strcmp (op_name, "countDocuments")) {
1268+
count (collection, test, operation, session, read_prefs);
12311269
} else if (!strcmp (op_name, "distinct")) {
12321270
distinct (collection, test, operation, session, read_prefs);
12331271
} else if (!strcmp (op_name, "find")) {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"data": [
3+
{
4+
"_id": 1,
5+
"x": "PING"
6+
}
7+
],
8+
"minServerVersion": "3.4",
9+
"tests": [
10+
{
11+
"description": "Count documents with collation",
12+
"operation": {
13+
"name": "countDocuments",
14+
"arguments": {
15+
"filter": {
16+
"x": "ping"
17+
},
18+
"collation": {
19+
"locale": "en_US",
20+
"strength": 2
21+
}
22+
}
23+
},
24+
"outcome": {
25+
"result": 1
26+
}
27+
},
28+
{
29+
"description": "Deprecated count with collation",
30+
"operation": {
31+
"name": "count",
32+
"arguments": {
33+
"filter": {
34+
"x": "ping"
35+
},
36+
"collation": {
37+
"locale": "en_US",
38+
"strength": 2
39+
}
40+
}
41+
},
42+
"outcome": {
43+
"result": 1
44+
}
45+
}
46+
]
47+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
{
2+
"data": [
3+
{
4+
"_id": 1,
5+
"x": 11
6+
},
7+
{
8+
"_id": 2,
9+
"x": 22
10+
},
11+
{
12+
"_id": 3,
13+
"x": 33
14+
}
15+
],
16+
"tests": [
17+
{
18+
"description": "Estimated document count",
19+
"operation": {
20+
"name": "estimatedDocumentCount",
21+
"arguments": {}
22+
},
23+
"outcome": {
24+
"result": 3
25+
}
26+
},
27+
{
28+
"description": "Count documents without a filter",
29+
"operation": {
30+
"name": "countDocuments",
31+
"arguments": {
32+
"filter": {}
33+
}
34+
},
35+
"outcome": {
36+
"result": 3
37+
}
38+
},
39+
{
40+
"description": "Count documents with a filter",
41+
"operation": {
42+
"name": "countDocuments",
43+
"arguments": {
44+
"filter": {
45+
"_id": {
46+
"$gt": 1
47+
}
48+
}
49+
}
50+
},
51+
"outcome": {
52+
"result": 2
53+
}
54+
},
55+
{
56+
"description": "Count documents with skip and limit",
57+
"operation": {
58+
"name": "countDocuments",
59+
"arguments": {
60+
"filter": {},
61+
"skip": 1,
62+
"limit": 3
63+
}
64+
},
65+
"outcome": {
66+
"result": 2
67+
}
68+
},
69+
{
70+
"description": "Deprecated count without a filter",
71+
"operation": {
72+
"name": "count",
73+
"arguments": {
74+
"filter": {}
75+
}
76+
},
77+
"outcome": {
78+
"result": 3
79+
}
80+
},
81+
{
82+
"description": "Deprecated count with a filter",
83+
"operation": {
84+
"name": "count",
85+
"arguments": {
86+
"filter": {
87+
"_id": {
88+
"$gt": 1
89+
}
90+
}
91+
}
92+
},
93+
"outcome": {
94+
"result": 2
95+
}
96+
},
97+
{
98+
"description": "Deprecated count with skip and limit",
99+
"operation": {
100+
"name": "count",
101+
"arguments": {
102+
"filter": {},
103+
"skip": 1,
104+
"limit": 3
105+
}
106+
},
107+
"outcome": {
108+
"result": 2
109+
}
110+
}
111+
]
112+
}

src/libmongoc/tests/test-conveniences.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ bson_lookup_value (const bson_t *b, const char *key, bson_value_t *value)
177177

178178
bson_iter_init (&iter, b);
179179
BSON_ASSERT (bson_iter_find_descendant (&iter, key, &descendent));
180-
bson_value_copy (bson_iter_value (&iter), value);
180+
bson_value_copy (bson_iter_value (&descendent), value);
181181
}
182182

183183

src/libmongoc/tests/test-libmongoc.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ extern void
205205
test_happy_eyeballs_install (TestSuite *suite);
206206
extern void
207207
test_counters_install (TestSuite *suite);
208+
extern void
209+
test_crud_install (TestSuite *suite);
208210

209211
typedef struct {
210212
mongoc_log_level_t level;
@@ -2365,6 +2367,7 @@ main (int argc, char *argv[])
23652367
#endif
23662368
test_happy_eyeballs_install (&suite);
23672369
test_counters_install (&suite);
2370+
test_crud_install (&suite);
23682371

23692372
ret = TestSuite_Run (&suite);
23702373

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <mongoc.h>
2+
3+
#include "json-test.h"
4+
#include "json-test-operations.h"
5+
#include "test-libmongoc.h"
6+
7+
static void
8+
crud_test_operation_cb (json_test_ctx_t *ctx,
9+
const bson_t *test,
10+
const bson_t *operation)
11+
{
12+
json_test_operation (ctx, test, operation, NULL);
13+
}
14+
15+
static void
16+
test_crud_cb (bson_t *scenario)
17+
{
18+
json_test_config_t config = JSON_TEST_CONFIG_INIT;
19+
config.run_operation_cb = crud_test_operation_cb;
20+
config.scenario = scenario;
21+
run_json_general_test (&config);
22+
}
23+
24+
static void
25+
test_all_spec_tests (TestSuite *suite)
26+
{
27+
char resolved[PATH_MAX];
28+
29+
test_framework_resolve_path (JSON_DIR "/crud", resolved);
30+
install_json_test_suite (suite, resolved, &test_crud_cb);
31+
}
32+
33+
void
34+
test_crud_install (TestSuite *suite)
35+
{
36+
test_all_spec_tests (suite);
37+
}

0 commit comments

Comments
 (0)