test/test_stockpile.rb in stockpile-1.0 vs test/test_stockpile.rb in stockpile-1.1
- old
+ new
@@ -1,7 +1,6 @@
require 'minitest_config'
-require 'stockpile'
require 'time'
describe Stockpile do
describe ".narrow?" do
it "is wide by default" do
@@ -16,10 +15,11 @@
end
end
end
describe ".inject!" do
+ let(:cls) { Class.new }
let(:mod) { Module.new }
let(:lrt) {
Module.new do
def last_run_time(key, value = nil)
if value
@@ -27,14 +27,28 @@
else
value = connection.hget(__method__, key)
Time.parse(value) if value
end
end
+
+ def job_ran(job, value = nil)
+ if value
+ connection.set(job, !!value)
+ else
+ !!connection.get(job)
+ end
+ end
end
}
- describe "Stockpile.inject!(Mod):" do
+ it "throws an ArgumentError unless it's a class or module" do
+ assert_raises ArgumentError do
+ ::Stockpile.inject!(Object.new)
+ end
+ end
+
+ describe "Stockpile.inject!(Module):" do
before { ::Stockpile.inject!(mod) }
it "defines Mod.cache" do
assert_respond_to mod, :cache
end
@@ -82,11 +96,13 @@
assert_respond_to mod, :stockpile_adapter!
end
end
describe "Stockpile.inject!(Mod, method: :stockpile, adaptable: false)" do
- before { ::Stockpile.inject!(mod, method: :stockpile, adaptable: false) }
+ before do
+ ::Stockpile.inject!(mod, method: :stockpile, adaptable: false)
+ end
it "defines Mod.stockpile" do
assert_respond_to mod, :stockpile
end
@@ -98,14 +114,35 @@
describe "Mod.cache_adapter" do
let(:now) { Time.now }
let(:iso) { now.utc.iso8601 }
before do
- ::Stockpile.inject!(mod, adaptable: true,
- default_manager: StockpileTestManager)
+ ::Stockpile.inject!(mod, default_manager: Stockpile::Memory)
end
+ it "queues adaptation until Stockpile has been initialized" do
+ stub Stockpile, :new do
+ mod.cache_adapter(lrt)
+ refute_called Stockpile, :new
+ refute_nil mod.instance_variable_get(:@__stockpile_triggers__)
+ refute_empty mod.instance_variable_get(:@__stockpile_triggers__)
+ end
+
+ assert_equal({ namespace: 'n' },
+ mod.cache(namespace: 'n').connection.options)
+ assert_respond_to mod.cache, :last_run_time
+ assert_respond_to mod.cache, :job_ran
+ end
+
+ it "adapts an initialized Stockpile immediately" do
+ mod.cache
+ mod.cache_adapter(lrt)
+ assert_respond_to mod.cache, :last_run_time
+ assert_respond_to mod.cache, :job_ran
+ assert_empty mod.instance_variable_get(:@__stockpile_triggers__)
+ end
+
it "adapts the cache with last_run_time" do
mod.cache_adapter(lrt)
assert_nil mod.cache.last_run_time('foo')
assert_equal iso, mod.cache.last_run_time('foo', now)
assert_equal now.to_i, mod.cache.last_run_time('foo').to_i
@@ -122,12 +159,51 @@
mod.cache_adapter!(lrt)
assert_nil lrt.last_run_time('foo')
assert_equal iso, lrt.last_run_time('foo', now)
assert_equal now.to_i, lrt.last_run_time('foo').to_i
end
+
+ it "adapts the cache with job_ran" do
+ mod.cache_adapter(lrt)
+ refute mod.cache.job_ran('foo')
+ assert mod.cache.job_ran('foo', true)
+ assert mod.cache.job_ran('foo')
+ end
+
+ it "adapts the module with last_run_time" do
+ mod.cache_adapter(lrt, mod)
+ refute mod.job_ran('foo')
+ assert mod.job_ran('foo', true)
+ assert mod.job_ran('foo')
+ end
+
+ it "adapts the lrt module with last_run_time" do
+ mod.cache_adapter!(lrt)
+ refute lrt.job_ran('foo')
+ assert lrt.job_ran('foo', true)
+ assert lrt.job_ran('foo')
+ end
end
- end
- # Testing #connection, #connect, #connection_for, #reconnect, and #disconnect
- # are testing Forwardable. Those tests are to be done in the individual
- # connectors.
+ describe "Stockpile.inject!(Class):" do
+ before { ::Stockpile.inject!(cls) }
+
+ it "defines cls.cache" do
+ assert_respond_to cls, :cache
+ end
+
+ it "defines cls.cache_adapter" do
+ assert_respond_to cls, :cache_adapter
+ end
+
+ it "defines cls.cache_adapter!" do
+ assert_respond_to cls, :cache_adapter!
+ end
+
+ it "Fails cache initialization" do
+ assert_raises ArgumentError do
+ cls.cache
+ end
+ end
+ end
+ end
end