test/patella_test.rb in patella-0.0.3 vs test/patella_test.rb in patella-0.9.0
- old
+ new
@@ -1,144 +1,97 @@
require 'test_helper'
class Dummy
- include Patella::Patella
include Patella::SendLater
attr_accessor :id
def initialize id
self.id = id
end
- def self.find_by_id id
+ def self.find id
new id
end
+end
- def foo
- 5
+class PatellaTest < ActiveSupport::TestCase
+ setup do
+ @dummy = Dummy.new(5)
end
- patella_reflex :foo
-
- def bar(a,b)
- a+b
+
+ teardown do
+ Resque.redis.flushall
end
- patella_reflex :bar
-
- def baz(add)
- self.id + add
+
+ def test_send_now_to_instance
+ assert_equal 5, @dummy.send_later(:id)
end
- patella_reflex :baz
-
- def self.bing
- 3
+
+ def test_send_now_to_class
+ assert_equal 6, Dummy.send_later(:find, 6).id
end
- patella_reflex :bing, :class_method => true
+
+ def test_send_later_to_instance
+ sending_later do
+ @dummy.send_later(:id)
+ end
- def no_background_add(a,b)
- a + b
+ assert job = Resque.peek(:send_later)
+ assert_equal ["Dummy", 5, "id"], job["args"]
end
- patella_reflex :no_background_add, :no_backgrounding => true
-end
+
+ def test_send_later_on_queue_to_instance
+ sending_later do
+ @dummy.send_later_on_queue(:unimportant, :id)
+ end
-class PatellaTest < ActiveSupport::TestCase
-
- def test_patella_basics
- # foreground
- f = Dummy.new 6
- f.stubs :caching_foo => 5
- assert_equal 5, f.foo
- assert_received(f, :caching_foo)
-
- #background
- with_caching do
- Patella::SendLaterWorker.stubs :perform_later => 'loading'
- f1 = Dummy.new 5
- assert f1.foo.loading?
- assert_received(Patella::SendLaterWorker, :perform_later) do |ex|
- ex.once
- ex.with 'Dummy', f1.id, :caching_foo, []
- end
+ assert job = Resque.peek(:unimportant)
+ assert_equal ["Dummy", 5, "id"], job["args"]
+ end
+
+ def test_send_later_to_class
+ sending_later do
+ Dummy.send_later(:find, 6)
end
+
+ assert job = Resque.peek(:send_later)
+ assert_equal ["Dummy", nil, "find", 6], job["args"]
end
-
- def test_turning_off_background
- #background
- with_caching do
- Patella::SendLaterWorker.expects(:perform_later).never
- Dummy.any_instance.expects('caching_no_background_add').once.returns(9)
- d = Dummy.new(1)
- result = d.no_background_add(4,5)
- assert result.loaded?
- assert_equal 9, result
+
+ def test_send_later_on_queue_to_class
+ sending_later do
+ Dummy.send_later_on_queue(:object_finding, :find, 6)
end
+
+ assert job = Resque.peek(:object_finding)
+ assert_equal ["Dummy", nil, "find", 6], job["args"]
end
-
- def cache_clearing
- d = Dummy.new(1)
- result = d.bar(4,5) #load it by turning caching off
- assert result.loaded?
- assert_equal 9, result
-
- with_caching do
- result = d.bar(4,5) #still here
- assert result.loaded?
- assert_equal 9, result
-
- d.clear_bar(4,5)
- result = d.bar(4,5) #cleared
- assert result.loading?
-
+
+ def test_changing_default_queue
+ default_queue = Patella::SendLaterWorker.default_queue
+ begin
+ Patella::SendLaterWorker.default_queue = :new_default
+
+ sending_later do
+ @dummy.send_later(:id)
+ Dummy.send_later(:find, 6)
+ end
+
+ job1, job2 = Resque.pop(:new_default), Resque.pop(:new_default)
+ assert_equal ["Dummy", 5, "id"], job1["args"]
+ assert_equal ["Dummy", nil, "find", 6], job2["args"]
+ ensure
+ Patella::SendLaterWorker.default_queue = default_queue
end
end
- def test_patella_for_instance_objs
- four = Dummy.new 4
- assert_equal(8, four.baz(4))
- assert_equal(13, four.baz(9))
- end
-
- def test_patella_for_class_methods
- assert Dummy.bing.loaded?
- assert_equal(3, Dummy.bing)
- end
-
- def test_keys
- d = Dummy.new 2
- assert_equal "patella/Dummy/2/foo/#{md5 [].to_json}", d.patella_key(:foo,[])
- assert_equal "patella/Dummy/2/foo/#{md5 [1].to_json}", d.patella_key(:foo,[1])
- assert_equal "patella/Dummy/2/foo/#{md5 [1,3].to_json}", d.patella_key(:foo,[1,3])
- assert_equal "patella/Dummy/2/foo/#{md5 [1,"asdf"].to_json}", d.patella_key(:foo,[1,"asdf"])
- assert_equal "patella/Dummy/2/foo/#{md5 [1,"asdf"*1000].to_json}", d.patella_key(:foo,[1, ("asdf"*1000)])
-
- d3 = Dummy.new 3
- assert_equal "patella/Dummy/3/foo/#{md5 [].to_json}", d3.patella_key(:foo,[])
- assert_equal "patella/Dummy/3/foo/#{md5 [1].to_json}", d3.patella_key(:foo,[1])
- assert_equal "patella/Dummy/3/foo/#{md5 [1,3].to_json}", d3.patella_key(:foo,[1,3])
- assert_equal "patella/Dummy/3/foo/#{md5 [1,"asdf" * 1000].to_json}", d3.patella_key(:foo,[1,"asdf" * 1000])
- end
-
- def test_soft_expiration
- dummy = Dummy.new 1
- ::Rails.cache.stubs( :fetch => { 'result' => "loads of data", 'soft_expiration' => Time.now-1.minute }.to_json )
- ::Rails.cache.stubs( :write => true )
- dummy.stubs :send_later => 'loading'
- dummy.foo
- assert_received(Rails.cache, :write) { |ex| ex.once }
- assert_received(dummy, :send_later) { |ex| ex.once }
- end
-
private
- def md5(str)
- Digest::MD5.hexdigest(str)
- end
-
- def with_caching(&block)
- #previous_caching = ActionController::Base.perform_caching
+ def sending_later(&block)
+ previous_send_now = Patella::SendLater.send_now
begin
- Rails.stubs(:caching? => true)
- #ActionController::Base.perform_caching = true
+ Patella::SendLater.send_now = false
yield
ensure
- #ActionController::Base.perform_caching = previous_caching
+ Patella::SendLater.send_now = previous_send_now
end
end
end