about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-11-13 01:01:58 -0800
committerEric Wong <normalperson@yhbt.net>2009-11-13 01:01:58 -0800
commiteead1a80dd5fdca57cc8aa0333d249415ba2d85a (patch)
tree34578a70f39dc6627831c1fa73031fedfacba15d
parentf4b44763fa6802543f7d1590719e5aed21e120cd (diff)
downloadupr-eead1a80dd5fdca57cc8aa0333d249415ba2d85a.tar.gz
example/rails_app-2.3.4: UprStatus unit tests
UprStatus.incr now returns the value incremented to
be compatible with Upr::Monitor.incr
-rw-r--r--examples/rails_app-2.3.4/app/models/upr_status.rb10
-rw-r--r--examples/rails_app-2.3.4/test/fixtures/upr_statuses.yml22
-rw-r--r--examples/rails_app-2.3.4/test/unit/upr_status_test.rb41
3 files changed, 71 insertions, 2 deletions
diff --git a/examples/rails_app-2.3.4/app/models/upr_status.rb b/examples/rails_app-2.3.4/app/models/upr_status.rb
index a8afc30..871b49f 100644
--- a/examples/rails_app-2.3.4/app/models/upr_status.rb
+++ b/examples/rails_app-2.3.4/app/models/upr_status.rb
@@ -16,8 +16,14 @@ class UprStatus < ActiveRecord::Base
     end
 
     def incr(upid, nr)
-      update_all("seen = seen + #{nr.to_i}, time = #{Time.now.to_i}",
-                 [ "upid = ? AND seen >= 0", upid ])
+      transaction do
+        if rv = find_by_upid(upid)
+          rv.time = Time.now.to_i
+          rv.seen += nr if rv.seen >= 0
+          rv.save
+          rv
+        end
+      end
     end
 
     def error!(upid)
diff --git a/examples/rails_app-2.3.4/test/fixtures/upr_statuses.yml b/examples/rails_app-2.3.4/test/fixtures/upr_statuses.yml
new file mode 100644
index 0000000..2859c5f
--- /dev/null
+++ b/examples/rails_app-2.3.4/test/fixtures/upr_statuses.yml
@@ -0,0 +1,22 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+chunky:
+  upid: one
+  length: nil
+  seen: 0
+  time: 0
+length:
+  upid: two
+  length: 5
+  seen: 0
+  time: 0
+done:
+  upid: done
+  length: 5
+  seen: 5
+  time: 0
+errored:
+  upid: errored
+  length: 5
+  seen: -1
+  time: 0
diff --git a/examples/rails_app-2.3.4/test/unit/upr_status_test.rb b/examples/rails_app-2.3.4/test/unit/upr_status_test.rb
new file mode 100644
index 0000000..d62d869
--- /dev/null
+++ b/examples/rails_app-2.3.4/test/unit/upr_status_test.rb
@@ -0,0 +1,41 @@
+require 'test_helper'
+
+class UprStatusTest < ActiveSupport::TestCase
+  # Replace this with your real tests.
+  test "start with length" do
+    assert_kind_of UprStatus, UprStatus.start('abcde', 5)
+  end
+
+  test "start without length" do
+    assert_kind_of UprStatus, UprStatus.start('abcde', nil)
+  end
+
+  test "incr from start" do
+    assert_nothing_raised { UprStatus.incr('two', 2) }
+    assert_equal 2, UprStatus.read('two').seen
+  end
+
+  test "errored" do
+    assert UprStatus.read('errored').error?
+  end
+
+  test "errored incr no-op" do
+    assert UprStatus.incr('errored', 6)
+    assert UprStatus.read('errored').error?
+  end
+
+  test "finish" do
+    assert_kind_of UprStatus, UprStatus.finish('two')
+    assert UprStatus.read('two').done?
+  end
+
+  test "done?" do
+    assert UprStatus.read('done').done?
+  end
+
+  test "error!" do
+    assert_kind_of UprStatus, UprStatus.error!('two')
+    assert UprStatus.read('two').error?
+  end
+
+end