ejdbquerydel(q1);
}
+// Ticket #14
+void testSlice(void) {
+ EJCOLL *coll = ejdbgetcoll(jb, "f_projection");
+ CU_ASSERT_PTR_NOT_NULL_FATAL(coll);
+
+ bson_iterator it;
+
+// { z : 44, $do : { arr : {$slice : 1} } }
+ bson bsq;
+ bson_init_as_query(&bsq);
+ bson_append_int(&bsq, "z", 44);
+ bson_append_start_object(&bsq, "$do");
+ bson_append_start_object(&bsq, "arr");
+ bson_append_int(&bsq, "$slice", 1);
+ bson_append_finish_object(&bsq);
+ bson_append_finish_object(&bsq);
+ bson_finish(&bsq);
+ CU_ASSERT_FALSE_FATAL(bsq.err);
+
+ EJQ *q1 = ejdbcreatequery(jb, &bsq, NULL, 0, NULL);
+ CU_ASSERT_PTR_NOT_NULL_FATAL(q1);
+
+ uint32_t count = 0;
+ TCLIST *q1res = ejdbqryexecute(coll, q1, &count, 0, NULL);
+ //fprintf(stderr, "\n%s", TCXSTRPTR(log));
+ CU_ASSERT_EQUAL(TCLISTNUM(q1res), 1);
+ for (int i = 0; i < TCLISTNUM(q1res); ++i) {
+ void *bsdata = TCLISTVALPTR(q1res, i);
+ CU_ASSERT_PTR_NOT_NULL_FATAL(bsdata);
+ CU_ASSERT_FALSE(bson_compare_long(1, bsdata, "arr.0.h"));
+ bson_iterator_from_buffer(&it, bsdata);
+ CU_ASSERT_TRUE(bson_find_fieldpath_value("arr.1.h", &it) == BSON_EOO);
+ }
+ tclistdel(q1res);
+ bson_destroy(&bsq);
+ ejdbquerydel(q1);
+
+ // { z : 44, $do : { arr : {$slice : 2} } }
+ bson_init_as_query(&bsq);
+ bson_append_int(&bsq, "z", 44);
+ bson_append_start_object(&bsq, "$do");
+ bson_append_start_object(&bsq, "arr");
+ bson_append_int(&bsq, "$slice", 2);
+ bson_append_finish_object(&bsq);
+ bson_append_finish_object(&bsq);
+ bson_finish(&bsq);
+ CU_ASSERT_FALSE_FATAL(bsq.err);
+
+ q1 = ejdbcreatequery(jb, &bsq, NULL, 0, NULL);
+ CU_ASSERT_PTR_NOT_NULL_FATAL(q1);
+
+ count = 0;
+ q1res = ejdbqryexecute(coll, q1, &count, 0, NULL);
+ CU_ASSERT_EQUAL(TCLISTNUM(q1res), 1);
+ CU_ASSERT_EQUAL(count, 1);
+ for (int i = 0; i < TCLISTNUM(q1res); ++i) {
+ void *bsdata = TCLISTVALPTR(q1res, i);
+ CU_ASSERT_PTR_NOT_NULL_FATAL(bsdata);
+ CU_ASSERT_FALSE(bson_compare_long(1, bsdata, "arr.0.h"));
+ CU_ASSERT_FALSE(bson_compare_long(2, bsdata, "arr.1.h"));
+ bson_iterator_from_buffer(&it, bsdata);
+ CU_ASSERT_TRUE(bson_find_fieldpath_value("arr.2.h", &it) == BSON_EOO);
+ }
+ tclistdel(q1res);
+ bson_destroy(&bsq);
+ ejdbquerydel(q1);
+
+ // { $do : { arr : {$slice : [1, 2]} } }
+ bson_init_as_query(&bsq);
+ bson_append_start_object(&bsq, "$do");
+ bson_append_start_object(&bsq, "arr");
+ bson_append_start_array(&bsq, "$slice");
+ bson_append_int(&bsq, "0", 1);
+ bson_append_int(&bsq, "1", 2);
+ bson_append_finish_array(&bsq);
+ bson_append_finish_object(&bsq);
+ bson_append_finish_object(&bsq);
+ bson_finish(&bsq);
+ CU_ASSERT_FALSE_FATAL(bsq.err);
+
+ q1 = ejdbcreatequery(jb, &bsq, NULL, 0, NULL);
+ CU_ASSERT_PTR_NOT_NULL_FATAL(q1);
+
+ count = 0;
+ q1res = ejdbqryexecute(coll, q1, &count, 0, NULL);
+ CU_ASSERT_EQUAL(TCLISTNUM(q1res), 3);
+ CU_ASSERT_EQUAL(count, 3);
+ for (int i = 0; i < TCLISTNUM(q1res); ++i) {
+ void *bsdata = TCLISTVALPTR(q1res, i);
+ CU_ASSERT_PTR_NOT_NULL_FATAL(bsdata);
+ if (i == 0) {
+ CU_ASSERT_FALSE(bson_compare_long(1, bsdata, "arr.0"));
+ CU_ASSERT_FALSE(bson_compare_long(2, bsdata, "arr.1"));
+ bson_iterator_from_buffer(&it, bsdata);
+ CU_ASSERT_TRUE(bson_find_fieldpath_value("arr.2", &it) == BSON_EOO);
+ } else if (i == 1) {
+ CU_ASSERT_FALSE(bson_compare_long(2, bsdata, "arr.0"));
+ CU_ASSERT_FALSE(bson_compare_long(1, bsdata, "arr.1"));
+ bson_iterator_from_buffer(&it, bsdata);
+ CU_ASSERT_TRUE(bson_find_fieldpath_value("arr.2", &it) == BSON_EOO);
+ } else if (i == 2) {
+ CU_ASSERT_FALSE(bson_compare_long(2, bsdata, "arr.0.h"));
+ bson_iterator_from_buffer(&it, bsdata);
+ CU_ASSERT_TRUE(bson_find_fieldpath_value("arr.1", &it) == BSON_EOO);
+ }
+ }
+ tclistdel(q1res);
+ bson_destroy(&bsq);
+ ejdbquerydel(q1);
+
+
+
+//{ _id: '54d7a2f07e671e140000001f',
+// z: 33,
+// arr: [ 0, 1, 2, 3 ] }
+//{ _id: '54d7a2f07e671e1400000020',
+// z: 33,
+// arr: [ 3, 2, 1, 0 ] }
+//{ _id: '54d7a2f07e671e1400000021',
+// z: 44,
+// arr: [ { h: 1 }, { h: 2 } ] }
+
+ // { $do : { arr : {$slice : [-3, 1]} } }
+ bson_init_as_query(&bsq);
+ bson_append_start_object(&bsq, "$do");
+ bson_append_start_object(&bsq, "arr");
+ bson_append_start_array(&bsq, "$slice");
+ bson_append_int(&bsq, "0", -3);
+ bson_append_int(&bsq, "1", 1);
+ bson_append_finish_array(&bsq);
+ bson_append_finish_object(&bsq);
+ bson_append_finish_object(&bsq);
+ bson_finish(&bsq);
+ CU_ASSERT_FALSE_FATAL(bsq.err);
+
+ q1 = ejdbcreatequery(jb, &bsq, NULL, 0, NULL);
+ CU_ASSERT_PTR_NOT_NULL_FATAL(q1);
+
+ count = 0;
+ q1res = ejdbqryexecute(coll, q1, &count, 0, NULL);
+ CU_ASSERT_EQUAL(TCLISTNUM(q1res), 3);
+ CU_ASSERT_EQUAL(count, 3);
+ for (int i = 0; i < TCLISTNUM(q1res); ++i) {
+ void *bsdata = TCLISTVALPTR(q1res, i);
+ CU_ASSERT_PTR_NOT_NULL_FATAL(bsdata);
+ if (i == 0) {
+ CU_ASSERT_FALSE(bson_compare_long(1, bsdata, "arr.0"));
+ bson_iterator_from_buffer(&it, bsdata);
+ CU_ASSERT_TRUE(bson_find_fieldpath_value("arr.1", &it) == BSON_EOO);
+ } else if (i == 1) {
+ CU_ASSERT_FALSE(bson_compare_long(2, bsdata, "arr.0"));
+ bson_iterator_from_buffer(&it, bsdata);
+ CU_ASSERT_TRUE(bson_find_fieldpath_value("arr.1", &it) == BSON_EOO);
+ } else if (i == 2) {
+ CU_ASSERT_FALSE(bson_compare_long(2, bsdata, "arr.0.h"));
+ bson_iterator_from_buffer(&it, bsdata);
+ CU_ASSERT_TRUE(bson_find_fieldpath_value("arr.1", &it) == BSON_EOO);
+ }
+ }
+ tclistdel(q1res);
+ bson_destroy(&bsq);
+ ejdbquerydel(q1);
+}
+
+
+ void testDistinct() {
+ EJCOLL *contacts = ejdbcreatecoll(jb, "contacts", NULL);
+ CU_ASSERT_PTR_NOT_NULL_FATAL(contacts);
+
+ int count;
+ TCXSTR *log;
+ bson *q1res;
+
+ log = tcxstrnew();
+ q1res = ejdbqrydistinct(contacts, "address", NULL, NULL, 0, &count, log);
+
+ CU_ASSERT_PTR_NOT_NULL_FATAL(q1res);
+ CU_ASSERT_EQUAL(count, 4);
+
+ bson_del(q1res);
+
+ bson bsq1;
+ bson_init_as_query(&bsq1);
+ bson_append_string(&bsq1, "address.street", "Pirogova");
+ bson_finish(&bsq1);
+ CU_ASSERT_FALSE_FATAL(bsq1.err);
+
+ q1res = ejdbqrydistinct(contacts, "address.room", &bsq1, NULL, 0, &count, log);
+
+ CU_ASSERT_PTR_NOT_NULL_FATAL(q1res);
+ CU_ASSERT_EQUAL(count, 1);
+
+ bson_del(q1res);
+
+ q1res = ejdbqrydistinct(contacts, "nonexisted", NULL, NULL, 0, &count, log);
+
+ CU_ASSERT_PTR_NOT_NULL_FATAL(q1res);
+ CU_ASSERT_EQUAL(count, 0);
+
+ bson_del(q1res);
+
+ bson_destroy(&bsq1);
+ tcxstrdel(log);
+ }
+
int main() {
setlocale(LC_ALL, "en_US.UTF-8");
(NULL == CU_add_test(pSuite, "testTicket99", testTicket99)) ||
(NULL == CU_add_test(pSuite, "testTicket101", testTicket101)) ||
(NULL == CU_add_test(pSuite, "testTicket110", testTicket110)) ||
- (NULL == CU_add_test(pSuite, "testMetaInfo", testMetaInfo)) ||
+ (NULL == CU_add_test(pSuite, "testDistinct", testDistinct)) ||
- (false)
+ (NULL == CU_add_test(pSuite, "testSlice", testSlice)) ||
+ (NULL == CU_add_test(pSuite, "testMetaInfo", testMetaInfo))
+
) {
CU_cleanup_registry();
return CU_get_error();