diff options
author | Anton Adamansky <adamansky@gmail.com> | 2015-04-20 12:04:10 +0600 |
---|---|---|
committer | Anton Adamansky <adamansky@gmail.com> | 2015-04-20 12:04:10 +0600 |
commit | e5428ff3d1e364192e436b4fe24cafd0e1333a35 (patch) | |
tree | e0dbc122856467db0bb927909bcd7f3fda7e6230 | |
parent | 8fbadcfd83a457fdf18226c2e59b38736d7a7cce (diff) | |
parent | c65659713f7fd5648dd327a64aff791e831c7bc4 (diff) | |
download | ejdb-e5428ff3d1e364192e436b4fe24cafd0e1333a35.tar.gz ejdb-e5428ff3d1e364192e436b4fe24cafd0e1333a35.tar.bz2 ejdb-e5428ff3d1e364192e436b4fe24cafd0e1333a35.zip |
Merge branch 'symbols' of https://github.com/achoy/ejdb into achoy-symbols
-rw-r--r-- | src/bson/bson.h | 2 | ||||
-rw-r--r-- | src/ejdb/ejdb.c | 5 | ||||
-rw-r--r-- | src/ejdb/tests/ejdbtest2.c | 112 |
3 files changed, 116 insertions, 3 deletions
diff --git a/src/bson/bson.h b/src/bson/bson.h index de5aab7..a741336 100644 --- a/src/bson/bson.h +++ b/src/bson/bson.h @@ -32,7 +32,7 @@ #include "tcutil.h" #define BSON_IS_NUM_TYPE(atype) (atype == BSON_INT || atype == BSON_LONG || atype == BSON_DOUBLE) -#define BSON_IS_STRING_TYPE(atype) (atype == BSON_STRING || atype == BSON_SYMBOL) +#define BSON_IS_STRING_TYPE(atype) ((atype) == BSON_STRING || (atype) == BSON_SYMBOL) EJDB_EXTERN_C_START diff --git a/src/ejdb/ejdb.c b/src/ejdb/ejdb.c index 5eabbfc..d568a57 100644 --- a/src/ejdb/ejdb.c +++ b/src/ejdb/ejdb.c @@ -1794,10 +1794,11 @@ static bool _qrybsvalmatch(const EJQF *qf, bson_iterator *it, bool expandarrays, int sp; const char *fval; + // Feature #129: Handle BSON_SYMBOL like BSON_STRING #define _FETCHSTRFVAL() \ do { \ - fvalsz = (bt == BSON_STRING) ? bson_iterator_string_len(it) : 1; \ - fval = (bt == BSON_STRING) ? bson_iterator_string(it) : ""; \ + fvalsz = (BSON_IS_STRING_TYPE(bt)) ? bson_iterator_string_len(it) : 1; \ + fval = (BSON_IS_STRING_TYPE(bt)) ? bson_iterator_string(it) : ""; \ if (bt == BSON_OID) { \ bson_oid_to_string(bson_iterator_oid(it), oidbuf); \ fvalsz = 25; \ diff --git a/src/ejdb/tests/ejdbtest2.c b/src/ejdb/tests/ejdbtest2.c index ffaed5f..beea331 100644 --- a/src/ejdb/tests/ejdbtest2.c +++ b/src/ejdb/tests/ejdbtest2.c @@ -63,6 +63,7 @@ void testAddData(void) { bson_append_finish_object(&a1); bson_append_finish_array(&a1); //EOF complexarr CU_ASSERT_FALSE_FATAL(a1.err); + bson_append_symbol(&a1, "symbol", "apple"); bson_finish(&a1); ejdbsavebson(ccoll, &a1, &oid); bson_destroy(&a1); @@ -100,6 +101,7 @@ void testAddData(void) { bson_append_long(&a1, "1", 556667); bson_append_double(&a1, "2", 77676.22); bson_append_finish_array(&a1); + bson_append_symbol(&a1, "symbol", "application"); bson_finish(&a1); CU_ASSERT_FALSE_FATAL(a1.err); @@ -122,6 +124,7 @@ void testAddData(void) { bson_append_long(&a1, "1", 222334); bson_append_double(&a1, "2", 77676.22); bson_append_finish_array(&a1); + bson_append_symbol(&a1, "symbol", "bison"); bson_finish(&a1); CU_ASSERT_FALSE_FATAL(a1.err); @@ -2573,6 +2576,7 @@ void testQuery28(void) { // $gte: 64 bit number bson_init_as_query(&bsq1); bson_append_start_object(&bsq1, "longscore"); bson_append_long(&bsq1, "$gte", int64value); + bson_append_finish_object(&bsq1); bson_finish(&bsq1); CU_ASSERT_FALSE_FATAL(bsq1.err); @@ -2602,6 +2606,111 @@ void testQuery28(void) { // $gte: 64 bit number ejdbquerydel(q1); } +void testQuery29(void) { + // #129: Test $begin Query with Symbols + EJCOLL *contacts = ejdbcreatecoll(jb, "contacts", NULL); + CU_ASSERT_PTR_NOT_NULL_FATAL(contacts); + + bson bsq1; + bson_init_as_query(&bsq1); + bson_append_start_object(&bsq1, "symbol"); + bson_append_string(&bsq1, "$begin", "app"); + bson_append_finish_object(&bsq1); + bson_finish(&bsq1); + CU_ASSERT_FALSE_FATAL(bsq1.err); + + EJQ *q1 = ejdbcreatequery(jb, &bsq1, NULL, 0, NULL); + CU_ASSERT_PTR_NOT_NULL_FATAL(q1); + + uint32_t count = 0; + TCXSTR *log = tcxstrnew(); + TCLIST *q1res = ejdbqryexecute(contacts, q1, &count, 0, log); + //fprintf(stderr, "%s", TCXSTRPTR(log)); + + CU_ASSERT_EQUAL(count, 2); // should match symbol_info: apple, application + CU_ASSERT_TRUE(TCLISTNUM(q1res) == 2); + + for (int i = 0; i < TCLISTNUM(q1res); ++i) { + if (i == 0) CU_ASSERT_FALSE(bson_compare_string("apple", TCLISTVALPTR(q1res, i), "symbol")); + if (i == 1) CU_ASSERT_FALSE(bson_compare_string("application", TCLISTVALPTR(q1res, i), "symbol")); + } + + bson_destroy(&bsq1); + tclistdel(q1res); + tcxstrdel(log); + ejdbquerydel(q1); +} + +void testQuery30(void) { + // #129: Test equal with Symbols + EJCOLL *contacts = ejdbcreatecoll(jb, "contacts", NULL); + CU_ASSERT_PTR_NOT_NULL_FATAL(contacts); + + bson bsq1; + bson_init_as_query(&bsq1); + bson_append_string(&bsq1, "symbol", "bison"); + bson_finish(&bsq1); + CU_ASSERT_FALSE_FATAL(bsq1.err); + + EJQ *q1 = ejdbcreatequery(jb, &bsq1, NULL, 0, NULL); + CU_ASSERT_PTR_NOT_NULL_FATAL(q1); + + uint32_t count = 0; + TCXSTR *log = tcxstrnew(); + TCLIST *q1res = ejdbqryexecute(contacts, q1, &count, 0, log); + //fprintf(stderr, "%s", TCXSTRPTR(log)); + + CU_ASSERT_EQUAL(count, 1); // should match symbol_info: bison + CU_ASSERT_TRUE(TCLISTNUM(q1res) == 1); + + for (int i = 0; i < TCLISTNUM(q1res); ++i) { + if (i == 0) CU_ASSERT_FALSE(bson_compare_string("bison", TCLISTVALPTR(q1res, i), "symbol")); + } + + bson_destroy(&bsq1); + tclistdel(q1res); + tcxstrdel(log); + ejdbquerydel(q1); +} + +void testQuery31(void) { + // #129: Test $in array Query with Symbols + EJCOLL *contacts = ejdbcreatecoll(jb, "contacts", NULL); + CU_ASSERT_PTR_NOT_NULL_FATAL(contacts); + + bson bsq1; + bson_init_as_query(&bsq1); + bson_append_start_object(&bsq1, "symbol"); + bson_append_start_array(&bsq1, "$in"); + bson_append_string(&bsq1, "0", "apple"); + bson_append_string(&bsq1, "1", "bison"); + bson_append_finish_array(&bsq1); + bson_append_finish_object(&bsq1); + bson_finish(&bsq1); + CU_ASSERT_FALSE_FATAL(bsq1.err); + + EJQ *q1 = ejdbcreatequery(jb, &bsq1, NULL, 0, NULL); + CU_ASSERT_PTR_NOT_NULL_FATAL(q1); + + uint32_t count = 0; + TCXSTR *log = tcxstrnew(); + TCLIST *q1res = ejdbqryexecute(contacts, q1, &count, 0, log); + //fprintf(stderr, "%s", TCXSTRPTR(log)); + + CU_ASSERT_EQUAL(count, 2); // should match symbol_info: apple, bison + CU_ASSERT_TRUE(TCLISTNUM(q1res) == 2); + + for (int i = 0; i < TCLISTNUM(q1res); ++i) { + if (i == 0) CU_ASSERT_FALSE(bson_compare_string("apple", TCLISTVALPTR(q1res, i), "symbol")); + if (i == 1) CU_ASSERT_FALSE(bson_compare_string("bison", TCLISTVALPTR(q1res, i), "symbol")); + } + + bson_destroy(&bsq1); + tclistdel(q1res); + tcxstrdel(log); + ejdbquerydel(q1); +} + void testOIDSMatching(void) { //OID matching EJCOLL *contacts = ejdbcreatecoll(jb, "contacts", NULL); CU_ASSERT_PTR_NOT_NULL_FATAL(contacts); @@ -5772,6 +5881,9 @@ int main() { (NULL == CU_add_test(pSuite, "testQuery26", testQuery26)) || (NULL == CU_add_test(pSuite, "testQuery27", testQuery27)) || (NULL == CU_add_test(pSuite, "testQuery28", testQuery28)) || + (NULL == CU_add_test(pSuite, "testQuery29", testQuery29)) || + (NULL == CU_add_test(pSuite, "testQuery30", testQuery30)) || + (NULL == CU_add_test(pSuite, "testQuery31", testQuery31)) || (NULL == CU_add_test(pSuite, "testOIDSMatching", testOIDSMatching)) || (NULL == CU_add_test(pSuite, "testEmptyFieldIndex", testEmptyFieldIndex)) || (NULL == CU_add_test(pSuite, "testICaseIndex", testICaseIndex)) || |