#50 - advanced tests (tests 9)
authorFedor Yudanov <fedwiz@academ.org>
Thu, 4 Apr 2013 12:30:39 +0000 (19:30 +0700)
committerFedor Yudanov <fedwiz@academ.org>
Thu, 4 Apr 2013 12:30:39 +0000 (19:30 +0700)
rbejdb/src/rbejdb.c
rbejdb/test/t2.rb

index ac8aaa7..1c4f6ab 100644 (file)
@@ -163,6 +163,8 @@ VALUE EJDB_save(int argc, VALUE *argv, VALUE self) {
     VALUE collName = argv[0];
     Check_Type(collName, T_STRING);
 
+    bool merge = TYPE(argv[argc - 1]) == T_TRUE;
+
     EJDB* ejdb = getEJDB(self);
 
     EJCOLL *coll = ejdbcreatecoll(ejdb, StringValuePtr(collName), NULL);
@@ -174,6 +176,9 @@ VALUE EJDB_save(int argc, VALUE *argv, VALUE self) {
     int i;
     for (i = 1; i < argc; i++) {
         VALUE rbobj = argv[i];
+
+        if (merge && i == argc - 1) break;
+
         if (NIL_P(rbobj)) {
             rb_ary_push(oids, Qnil);
             continue;
@@ -183,7 +188,7 @@ VALUE EJDB_save(int argc, VALUE *argv, VALUE self) {
         ruby_to_bson(rbobj, &bsonval, 0);
 
         bson_oid_t oid;
-        if (!ejdbsavebson2(coll, bsonval, &oid, true /*TODO read this param*/)) {
+        if (!ejdbsavebson2(coll, bsonval, &oid, merge)) {
             bson_destroy(bsonval);
             raise_ejdb_error(ejdb);
         }
@@ -310,6 +315,10 @@ VALUE EJDB_find_one(int argc, VALUE* argv, VALUE self) {
     return results;
 }
 
+VALUE EJDB_update(int argc, VALUE* argv, VALUE self) {
+    return EJDB_find(argc, argv, self);
+}
+
 
 void EJDB_set_index_internal(VALUE self, VALUE collName, VALUE fpath, int flags) {
     SafeStringValue(collName);
@@ -551,6 +560,7 @@ Init_rbejdb() {
     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, "update", RUBY_METHOD_FUNC(EJDB_update), -1);
 
     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 597923a..d0dc958 100644 (file)
@@ -263,4 +263,33 @@ class EJDBTestUnit < Test::Unit::TestCase
     puts "test_ejdb8_cmeta has passed successfull"
   end
 
+  def test_ejdb9_test_update1
+    assert_not_nil $jb
+    assert $jb.is_open?
+
+    result = $jb.update("parrots", {"name" => {"$icase" => "GRENNY"}, "$inc" => {"age" => 10}}, {:explain => true})
+    assert_equal(1, result.count)
+
+    log = result.log
+    assert_not_nil log
+    assert log.include?("UPDATING MODE: YES")
+
+    obj = $jb.find_one("parrots", {"age" => 11})
+    assert_not_nil obj
+    assert_equal("Grenny", obj["name"])
+
+    id = $jb.save("parrots", {"_id" => obj["_id"], "extra1" => 1}, true)
+    assert_not_nil id
+
+    obj = $jb.load("parrots", id)
+    assert_not_nil obj
+    assert_equal("Grenny", obj["name"])
+    assert_equal(1, obj["extra1"])
+
+    q = {"_id" => {"$in" => [id]}, "$set" => {"stime" => Time.now}}
+    result = $jb.update("parrots", q)
+    assert_equal(1, result.count)
+
+    puts "test_ejdb9_test_update1 has passed successfull"
+  end
 end