summaryrefslogtreecommitdiff
path: root/rbejdb
diff options
context:
space:
mode:
authorFedor Yudanov <fedwiz@academ.org>2013-04-01 20:28:27 +0700
committerFedor Yudanov <fedwiz@academ.org>2013-04-01 20:28:27 +0700
commitb28242fcc17683e1af0b1c1610dbad2a7473f96e (patch)
tree606f96471e2632718aba701fe4d664054604301e /rbejdb
parent188a244d24c08a6f09c8b60097db96d24495267e (diff)
downloadejdb-b28242fcc17683e1af0b1c1610dbad2a7473f96e.tar.gz
ejdb-b28242fcc17683e1af0b1c1610dbad2a7473f96e.tar.bz2
ejdb-b28242fcc17683e1af0b1c1610dbad2a7473f96e.zip
#50 - advanced tests (test2)
Diffstat (limited to 'rbejdb')
-rw-r--r--rbejdb/src/rbejdb.c41
-rw-r--r--rbejdb/test/t2.rb115
2 files changed, 116 insertions, 40 deletions
diff --git a/rbejdb/src/rbejdb.c b/rbejdb/src/rbejdb.c
index 879b05c..022bba0 100644
--- a/rbejdb/src/rbejdb.c
+++ b/rbejdb/src/rbejdb.c
@@ -37,6 +37,15 @@ VALUE ejdbClass;
VALUE ejdbResultsClass;
+VALUE get_hash_option(VALUE hash, const char* opt) {
+ Check_Type(hash, T_HASH);
+
+ VALUE symbol = ID2SYM(rb_intern(opt));
+ VALUE res = rb_hash_aref(hash, symbol);
+ return !NIL_P(res) ? res : rb_hash_aref(hash, rb_str_new2(opt));
+}
+
+
static int raise_ejdb_error(EJDB *ejdb) {
int ecode = ejdbecode(ejdb);
const char *emsg = ejdberrmsg(ecode);
@@ -117,10 +126,10 @@ void EJDB_ensureCollection(int argc, VALUE* argv, VALUE self) {
if (!NIL_P(copts)) {
Check_Type(copts, T_HASH);
- VALUE cachedrecords = rb_hash_aref(copts, rb_str_new2("cachedrecords"));
- VALUE compressed = rb_hash_aref(copts, rb_str_new2("compressed"));
- VALUE large = rb_hash_aref(copts, rb_str_new2("large"));
- VALUE records = rb_hash_aref(copts, rb_str_new2("records"));
+ VALUE cachedrecords = get_hash_option(copts, "cachedrecords");
+ VALUE compressed = get_hash_option(copts, "compressed");
+ VALUE large = get_hash_option(copts, "large");
+ VALUE records = get_hash_option(copts, "records");
jcopts.cachedrecords = !NIL_P(cachedrecords) ? NUM2INT(cachedrecords) : 0;
jcopts.compressed = RTEST(compressed);
@@ -212,11 +221,16 @@ VALUE EJDB_load(VALUE self, VALUE collName, VALUE rboid) {
VALUE EJDB_find(int argc, VALUE* argv, VALUE self) {
VALUE collName;
VALUE q;
+ VALUE hints;
- rb_scan_args(argc, argv, "11", &collName, &q);
+ rb_scan_args(argc, argv, "12", &collName, &q, &hints);
SafeStringValue(collName);
q = !NIL_P(q) ? q :rb_hash_new();
+ hints = !NIL_P(hints) ? hints :rb_hash_new();
+
+ Check_Type(q, T_HASH);
+ Check_Type(hints, T_HASH);
EJDB* ejdb = getEJDB(self);
@@ -232,9 +246,23 @@ VALUE EJDB_find(int argc, VALUE* argv, VALUE self) {
int count;
int qflags = 0;
+ bool onlycount = RTEST(get_hash_option(hints, "onlycount"));
+ qflags |= onlycount ? EJQONLYCOUNT : 0;
TCLIST* qres = ejdbqryexecute(coll, ejq, &count, qflags, NULL);
- return create_EJDB_query_results(qres);
+ return !onlycount ? create_EJDB_query_results(qres) : INT2NUM(count);
+}
+
+static VALUE EJDB_block_true(VALUE yielded_object, VALUE context, int argc, VALUE argv[]){
+ return Qtrue;
+}
+
+VALUE EJDB_find_one(int argc, VALUE* argv, VALUE self) {
+ VALUE results = EJDB_find(argc, argv, self);
+ if (TYPE(results) == T_DATA) {
+ return rb_block_call(results, rb_intern("find"), 0, NULL, RUBY_METHOD_FUNC(EJDB_block_true), Qnil); // "find" with "always true" block gets first element
+ }
+ return results;
}
@@ -291,6 +319,7 @@ Init_rbejdb() {
rb_define_method(ejdbClass, "save", RUBY_METHOD_FUNC(EJDB_save), -1);
rb_define_method(ejdbClass, "load", RUBY_METHOD_FUNC(EJDB_load), 2);
rb_define_method(ejdbClass, "find", RUBY_METHOD_FUNC(EJDB_find), -1);
+ rb_define_method(ejdbClass, "find_one", RUBY_METHOD_FUNC(EJDB_find_one), -1);
rb_define_method(ejdbClass, "dropCollection", RUBY_METHOD_FUNC(EJDB_dropCollection), 2);
rb_define_method(ejdbClass, "ensureCollection", RUBY_METHOD_FUNC(EJDB_ensureCollection), -1);
diff --git a/rbejdb/test/t2.rb b/rbejdb/test/t2.rb
index 74bd350..f436a62 100644
--- a/rbejdb/test/t2.rb
+++ b/rbejdb/test/t2.rb
@@ -1,43 +1,90 @@
require "rbejdb"
require 'test/unit'
-
$now = Time.now
$jb = EJDB.open("tdbt2", EJDB::JBOWRITER | EJDB::JBOCREAT | EJDB::JBOTRUNC)
+
class EJDBTestUnit < Test::Unit::TestCase
- def test_save_load
- assert $jb.is_open?
-
- parrot1 = {
- "name" => "Grenny",
- "type" => "African Grey",
- "male" => true,
- "age" => 1,
- "birthdate" => $now,
- "likes" => ["green color", "night", "toys"],
- "extra1" => nil
- }
- parrot2 = {
- "name" => "Bounty",
- "type" => "Cockatoo",
- "male" => false,
- "age" => 15,
- "birthdate" => $now,
- "likes" => ["sugar cane"],
- "extra1" => nil
- }
-
- oids = $jb.save("parrots", parrot1, nil, parrot2)
- assert_not_nil oids
- assert_equal(oids.length, 3)
- assert_equal(parrot1["_id"], oids[0])
- assert_nil oids[1]
- assert_equal(parrot2["_id"], oids[2])
-
- obj = $jb.load("parrots", parrot2["_id"])
- assert_not_nil obj
- assert_equal(obj["_id"], parrot2["_id"])
- assert_equal(obj["name"], "Bounty")
+
+ def test_ejdb1_save_load
+ assert $jb.is_open?
+
+ parrot1 = {
+ "name" => "Grenny",
+ "type" => "African Grey",
+ "male" => true,
+ "age" => 1,
+ "birthdate" => $now,
+ "likes" => ["green color", "night", "toys"],
+ "extra1" => nil
+ }
+ parrot2 = {
+ "name" => "Bounty",
+ "type" => "Cockatoo",
+ "male" => false,
+ "age" => 15,
+ "birthdate" => $now,
+ "likes" => ["sugar cane"],
+ "extra1" => nil
+ }
+
+ oids = $jb.save("parrots", parrot1, nil, parrot2)
+ assert_not_nil oids
+ assert_equal(3, oids.length)
+ assert_equal(parrot1["_id"], oids[0])
+ assert_nil oids[1]
+ assert_equal(parrot2["_id"], oids[2])
+
+ obj = $jb.load("parrots", parrot2["_id"])
+ assert_not_nil obj
+ assert_equal(parrot2["_id"], obj["_id"])
+ assert_equal("Bounty", obj["name"])
+ end
+
+ def test_ejdb2_query1
+ assert_not_nil $jb
+ assert $jb.is_open?
+
+ results = $jb.find("parrots", {})
+
+ assert_not_nil results
+
+ assert_equal(2, results.to_a.length)
+ assert_equal(2, results.to_a.length) #checking second call gets same result
+
+ results.each { |rv|
+ assert rv
+ assert rv["_id"].is_a? String
+ assert rv["name"].is_a? String
+ assert rv["age"].is_a? Numeric
+ assert rv["birthdate"] != nil && rv["birthdate"].is_a?(Object)
+ assert rv["male"] == true || rv["male"] == false
+ assert_nil rv["extra1"]
+ assert rv["likes"] != nil && rv["likes"].is_a?(Array)
+ assert rv["likes"].length > 0
+ }
+
+ results = $jb.find("parrots", {})
+ assert_not_nil results
+ assert_equal(2, results.to_a.length)
+
+
+ #count mode
+ count = $jb.find("parrots", {}, :onlycount => true)
+ assert count.is_a? Numeric
+ assert_equal(2, count)
+
+ #findOne
+ obj = $jb.find_one("parrots")
+ assert_not_nil obj
+ assert obj["name"].is_a? String
+ assert obj["age"].is_a? Numeric
+
+ #empty query
+ results = $jb.find("parrots")
+ assert_not_nil results
+ assert_equal(2, results.to_a.length)
end
+
end