summaryrefslogtreecommitdiff
path: root/rbejdb
diff options
context:
space:
mode:
authorFedor Yudanov <fedwiz@academ.org>2013-04-09 19:54:55 +0700
committerFedor Yudanov <fedwiz@academ.org>2013-04-09 19:54:55 +0700
commit53c77cf4606a68d5602aea3d90de42930a1859c4 (patch)
treefb0a6e30ef7638c4c566647959ab82f96f7d617f /rbejdb
parent3814ffa3fbbecc71a0440f7d7754b3d09f0da290 (diff)
downloadejdb-53c77cf4606a68d5602aea3d90de42930a1859c4.tar.gz
ejdb-53c77cf4606a68d5602aea3d90de42930a1859c4.tar.bz2
ejdb-53c77cf4606a68d5602aea3d90de42930a1859c4.zip
#50 - advanced ruby types test
Diffstat (limited to 'rbejdb')
-rw-r--r--rbejdb/src/rbbson.c36
-rw-r--r--rbejdb/test/t2.rb54
2 files changed, 85 insertions, 5 deletions
diff --git a/rbejdb/src/rbbson.c b/rbejdb/src/rbbson.c
index c41f9c9..22be25b 100644
--- a/rbejdb/src/rbbson.c
+++ b/rbejdb/src/rbbson.c
@@ -33,6 +33,8 @@ void ruby_to_bson_internal(VALUE rbobj, bson** bsonresp, VALUE traverse, int fla
VALUE bson_date_to_ruby(bson_date_t date);
+VALUE bson_regex_to_ruby(const char* regex, const char* opts);
+
VALUE bsonContextClass = Qnil;
VALUE bsonWrapClass = Qnil;
@@ -142,12 +144,18 @@ int iterate_key_values_callback(VALUE key, VALUE val, VALUE bsonContextWrap) {
}
break;
case T_SYMBOL: {
- VALUE sname = rb_funcall(val, rb_intern("inspect"), 0);
+ VALUE sname = rb_funcall(val, rb_intern("to_s"), 0);
bson_append_symbol(b, attrName, StringValuePtr(sname));
}
break;
case T_FIXNUM:
- bson_append_int(b, attrName, FIX2INT(val));
+ bson_append_long(b, attrName, NUM2LONG(val));
+ break;
+ case T_BIGNUM:
+ bson_append_long(b, attrName, rb_big2ll(val));
+ break;
+ case T_FLOAT:
+ bson_append_double(b, attrName, NUM2DBL(val));
break;
case T_DATA:
if (0 == strcmp(rb_obj_classname(val), "Time")) {
@@ -171,8 +179,10 @@ int iterate_key_values_callback(VALUE key, VALUE val, VALUE bsonContextWrap) {
case T_NIL:
bson_append_null(b, attrName);
break;
- default:
- rb_raise(rb_eRuntimeError, "Cannot convert value type to bson: %d", TYPE(val));
+ default: {
+ VALUE objStr = rb_funcall(val, rb_intern("inspect"), 0);
+ rb_raise(rb_eRuntimeError, "Cannot convert ruby value to bson: %s: %s", rb_obj_classname(val), StringValuePtr(objStr));
+ }
}
return 0;
}
@@ -272,9 +282,18 @@ VALUE bson_iterator_to_ruby(bson_iterator* it, bson_type t) {
case BSON_INT:
val = INT2NUM(bson_iterator_int(it));
break;
+ case BSON_LONG:
+ val = LONG2NUM(bson_iterator_long(it));
+ break;
+ case BSON_DOUBLE:
+ val = rb_float_new(bson_iterator_double(it));
+ break;
case BSON_DATE:
val = bson_date_to_ruby(bson_iterator_date(it));
break;
+ case BSON_REGEX:
+ val = bson_regex_to_ruby(bson_iterator_regex(it), bson_iterator_regex_opts(it));
+ break;
case BSON_OBJECT: {
bson subbson;
bson_iterator_subobject(it, &subbson);
@@ -326,6 +345,15 @@ VALUE bson_date_to_ruby(bson_date_t date) {
INT2NUM((date % 1000) * 1000)); // microseconds
}
+VALUE bson_regex_to_ruby(const char* regex, const char* opts) {
+ char* regexbuf = malloc(sizeof(char) * (strlen(regex) + strlen(opts) + 3)); // 3 for // and 0
+ sprintf(regexbuf, "/%s/%s", regex, opts);
+ VALUE res = rb_eval_string(regexbuf);
+
+ free(regexbuf);
+ return res;
+}
+
VALUE bson_to_ruby(const bson* bsonval) {
VALUE res = rb_hash_new();
diff --git a/rbejdb/test/t2.rb b/rbejdb/test/t2.rb
index e360451..39a5841 100644
--- a/rbejdb/test/t2.rb
+++ b/rbejdb/test/t2.rb
@@ -457,6 +457,7 @@ class EJDBTestUnit < Test::Unit::TestCase
assert_nil $jb.load("parrots", "111")
assert_nil $jb.load("parrots", "")
+ #testing load
assert_raise(ArgumentError) {
$jb.load
}
@@ -471,6 +472,7 @@ class EJDBTestUnit < Test::Unit::TestCase
}
+ #testing save
assert_raise(ArgumentError) {
$jb.save
}
@@ -504,15 +506,65 @@ class EJDBTestUnit < Test::Unit::TestCase
$jb.save("monsters", self)
$jb.save("monsters", nil, false)
+ # what we can save
+ maxfixnum = 2 ** (0.size * 8 -2) - 1
+ minfixnum = -(2**(0.size * 8 -2))
+ oid = $jb.save("monsters", {
+ :nil => nil,
+ :object => Object.new,
+ :float => 0.2323232,
+ :string => "string",
+ :regexp => /regexp.*\\\n/imx,
+ :array => [1, 2, 3, 4, 5],
+ :maxfixnum => maxfixnum,
+ :minfixnum => minfixnum,
+ :hash => {:key => "value"},
+ :bignum => maxfixnum + 1, #but not too big )
+ :true => true,
+ :false => false,
+ :symbol => :symbol,
+ :binary => EJDBBinary.new([1, 1, 1]),
+ :time => Time.now
+ })
+
+ obj = $jb.load("monsters", oid)
+ assert_nil obj["nil"]
+ assert obj["object"].is_a? Object
+ assert_equal(0.2323232, obj["float"])
+ assert_equal("string", obj["string"])
+ assert_equal(/regexp.*\\\n/imx, obj["regexp"])
+ assert_equal([1, 2, 3, 4, 5], obj["array"])
+ assert obj["maxfixnum"].is_a? Fixnum
+ assert obj["minfixnum"].is_a? Fixnum
+ assert obj["hash"].is_a? Hash
+ assert_equal("value", obj["hash"]["key"])
+ assert_equal(maxfixnum + 1, obj["bignum"])
+ assert obj["bignum"].is_a? Bignum
+ assert obj["true"]
+ assert !obj["false"]
+ assert obj["symbol"].is_a? Symbol
+ assert_equal(:symbol, obj["symbol"])
+ assert_equal([1, 1, 1], obj["binary"].to_a)
+ assert obj["time"].is_a? Time
+
#puts $jb.find("monsters").to_a.to_s
- assert_equal(6, $jb.find("monsters").count)
+ assert_equal(7, $jb.find("monsters").count)
+
+ #testing find
assert_raise(ArgumentError) {
$jb.find
}
assert_equal(0, $jb.find("dsldajsdlkjasl").count)
+ assert_equal(0, $jb.find("monsters", {:name => "abracadabra"}).count)
+ assert_equal(0, $jb.find("monsters", {:name => ":abracadabra"}).count)
+ assert_equal(7, $jb.find("monsters", {:name => :abracadabra}).count) #todo: symbol search seems to be not supported yet
+ assert_equal(7, $jb.find("monsters", {:name => :what_is_this?}).count)
+
+ assert_equal(0, $jb.find("monsters", {:name => {"$in" => ["what_is_this?"]}}).count)
+
puts __method__.inspect + " has passed successfull"
end