#50 - advanced tests (test 11)
authorFedor Yudanov <fedwiz@academ.org>
Thu, 4 Apr 2013 13:20:55 +0000 (20:20 +0700)
committerFedor Yudanov <fedwiz@academ.org>
Thu, 4 Apr 2013 13:20:55 +0000 (20:20 +0700)
rbejdb/src/rbejdb.c
rbejdb/test/t2.rb

index 1c4f6ab..00cf9e4 100644 (file)
@@ -320,6 +320,24 @@ VALUE EJDB_update(int argc, VALUE* argv, VALUE self) {
 }
 
 
+void EJDB_remove(VALUE self, VALUE collName, VALUE rboid) {
+    SafeStringValue(collName);
+    SafeStringValue(rboid);
+
+    EJDB* ejdb = getEJDB(self);
+
+    EJCOLL *coll = ejdbgetcoll(ejdb, StringValuePtr(collName));
+    if (!coll) {
+        raise_ejdb_error(ejdb);
+    }
+
+    bson_oid_t oid = ruby_to_bson_oid(rboid);
+    if (!ejdbrmbson(coll, &oid)) {
+        raise_ejdb_error(ejdb);
+    }
+}
+
+
 void EJDB_set_index_internal(VALUE self, VALUE collName, VALUE fpath, int flags) {
     SafeStringValue(collName);
     SafeStringValue(fpath);
@@ -561,6 +579,7 @@ Init_rbejdb() {
     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, "update", RUBY_METHOD_FUNC(EJDB_update), -1);
+    rb_define_method(ejdbClass, "remove", RUBY_METHOD_FUNC(EJDB_remove), 2);
 
     rb_define_method(ejdbClass, "drop_collection", RUBY_METHOD_FUNC(EJDB_drop_collection), 2);
     rb_define_method(ejdbClass, "ensure_collection", RUBY_METHOD_FUNC(EJDB_ensure_collection), -1);
index b52ae09..f2d2379 100644 (file)
@@ -316,4 +316,22 @@ class EJDBTestUnit < Test::Unit::TestCase
     puts "test_ejdba_id_nin has passed successfull"
   end
 
+  def test_ejdbb_test_remove
+    assert_not_nil $jb
+    assert $jb.is_open?
+
+    obj = $jb.find_one("birds", {"name" => "Molly"})
+    assert_not_nil obj
+    assert_not_nil obj["_id"]
+    assert_equal("Very angry", obj["mood"])
+
+    #Bye bye Molly!
+    $jb.remove("birds", obj["_id"])
+
+    obj = $jb.find_one("birds", {"name" => "Molly"})
+    assert_nil obj
+
+    puts "test_ejdbb_test_remove has passed successfull"
+  end
+
 end