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

index b70c15d..16f27f1 100644 (file)
@@ -233,7 +233,13 @@ VALUE EJDB_load(VALUE self, VALUE collName, VALUE rboid) {
 
     bson *bs = ejdbloadbson(coll, &oid);
     if (!bs) {
-        raise_ejdb_error(ejdb);
+        int ecode = ejdbecode(ejdb);
+        if (ecode != TCESUCCESS && ecode != TCENOREC) {
+            raise_ejdb_error(ejdb);
+        } else {
+            return Qnil;
+        }
+
     }
 
     return bson_to_ruby(bs);
@@ -416,7 +422,6 @@ void EJDB_drop_array_index(VALUE self, VALUE collName, VALUE fpath) {
 }
 
 VALUE EJDB_get_db_meta(VALUE self) {
-
     EJDB* ejdb = getEJDB(self);
 
     TCLIST *cols = ejdbgetcolls(ejdb);
@@ -479,6 +484,65 @@ void EJDB_sync(VALUE self) {
     }
 }
 
+VALUE EJDB_get_transaction_status(VALUE self, VALUE collName) {
+    SafeStringValue(collName);
+
+    EJDB* ejdb = getEJDB(self);
+    EJCOLL *coll = ejdbcreatecoll(ejdb, StringValuePtr(collName), NULL);
+    if (!coll) {
+        raise_ejdb_error(ejdb);
+    }
+
+    bool status;
+    if (!ejdbtranstatus(coll, &status)) {
+        raise_ejdb_error(ejdb);
+    }
+
+    return status ? Qtrue : Qfalse;
+}
+
+void EJDB_begin_transaction(VALUE self, VALUE collName) {
+    SafeStringValue(collName);
+
+    EJDB* ejdb = getEJDB(self);
+    EJCOLL *coll = ejdbcreatecoll(ejdb, StringValuePtr(collName), NULL);
+    if (!coll) {
+        raise_ejdb_error(ejdb);
+    }
+
+    if (!ejdbtranbegin(coll)) {
+        raise_ejdb_error(ejdb);
+    }
+}
+
+void EJDB_commit_transaction(VALUE self, VALUE collName) {
+    SafeStringValue(collName);
+
+    EJDB* ejdb = getEJDB(self);
+    EJCOLL *coll = ejdbcreatecoll(ejdb, StringValuePtr(collName), NULL);
+    if (!coll) {
+        raise_ejdb_error(ejdb);
+    }
+
+    if (!ejdbtrancommit(coll)) {
+        raise_ejdb_error(ejdb);
+    }
+}
+
+void EJDB_rollback_transaction(VALUE self, VALUE collName) {
+    SafeStringValue(collName);
+
+    EJDB* ejdb = getEJDB(self);
+    EJCOLL *coll = ejdbcreatecoll(ejdb, StringValuePtr(collName), NULL);
+    if (!coll) {
+        raise_ejdb_error(ejdb);
+    }
+
+    if (!ejdbtranabort(coll)) {
+        raise_ejdb_error(ejdb);
+    }
+}
+
 
 void close_ejdb_results_internal(RBEJDB_RESULTS* rbres) {
     tclistdel(rbres->results);
@@ -614,6 +678,11 @@ Init_rbejdb() {
     rb_define_method(ejdbClass, "get_db_meta", RUBY_METHOD_FUNC(EJDB_get_db_meta), 0);
     rb_define_method(ejdbClass, "sync", RUBY_METHOD_FUNC(EJDB_sync), 0);
 
+    rb_define_method(ejdbClass, "get_transaction_status", RUBY_METHOD_FUNC(EJDB_get_transaction_status), 1);
+    rb_define_method(ejdbClass, "begin_transaction", RUBY_METHOD_FUNC(EJDB_begin_transaction), 1);
+    rb_define_method(ejdbClass, "commit_transaction", RUBY_METHOD_FUNC(EJDB_commit_transaction), 1);
+    rb_define_method(ejdbClass, "rollback_transaction", RUBY_METHOD_FUNC(EJDB_rollback_transaction), 1);
+
 
     ejdbResultsClass = rb_define_class("EJDBResults", rb_cObject);
     rb_include_module(ejdbResultsClass, rb_mEnumerable);
index 42cefd7..5f8ec26 100644 (file)
@@ -354,4 +354,48 @@ class EJDBTestUnit < Test::Unit::TestCase
     puts "test_ejdbd_remove_colls has passed successfull"
   end
 
+  def test_ejdbd_tx1
+    assert_not_nil $jb
+    assert $jb.is_open?
+
+    obj = {:foo => "bar"}
+
+    assert_equal(false, $jb.get_transaction_status("bars"))
+
+    $jb.begin_transaction("bars")
+
+    $jb.save("bars", obj)
+    id = obj["_id"]
+    assert_not_nil id
+
+    obj = $jb.load("bars", obj["_id"])
+    assert_not_nil obj
+    assert $jb.get_transaction_status("bars")
+
+    $jb.rollback_transaction("bars")
+    assert_equal(false, $jb.get_transaction_status("bars"))
+
+    obj = $jb.load("bars", obj["_id"])
+    assert_nil obj
+
+    $jb.begin_transaction("bars")
+
+    assert $jb.get_transaction_status("bars")
+    assert_nil $jb.load("bars", id)
+    obj = {:foo => "bar"}
+
+    $jb.save("bars", obj)
+    id = obj["_id"]
+    assert_not_nil id
+
+    assert_not_nil $jb.load("bars", id)
+
+    $jb.commit_transaction("bars")
+
+    assert_equal(false, $jb.get_transaction_status("bars"))
+
+    assert_not_nil $jb.load("bars", id)
+
+    puts "test_ejdbd_tx1 has passed successfull"
+  end
 end