thermos 0.5.2 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +4 -0
- data/lib/thermos/beverage.rb +40 -28
- data/lib/thermos/notifier.rb +1 -3
- data/lib/thermos/refill_job.rb +10 -4
- data/lib/thermos/version.rb +1 -1
- data/lib/thermos.rb +25 -7
- data/test/dependencies_test.rb +307 -0
- data/test/dummy/app/models/category.rb +5 -7
- data/test/dummy/config/application.rb +1 -4
- data/test/dummy/config/environments/development.rb +1 -1
- data/test/dummy/config/environments/production.rb +2 -1
- data/test/dummy/config/environments/test.rb +2 -2
- data/test/dummy/config/routes.rb +0 -9
- data/test/dummy/db/schema.rb +19 -21
- data/test/filter_test.rb +52 -0
- data/test/queue_test.rb +36 -0
- data/test/test_helper.rb +16 -6
- data/test/thermos_test.rb +45 -395
- metadata +56 -36
data/test/dummy/db/schema.rb
CHANGED
@@ -11,32 +11,30 @@
|
|
11
11
|
# It's strongly recommended that you check this file into your version control system.
|
12
12
|
|
13
13
|
ActiveRecord::Schema.define(version: 2016_03_26_174530) do
|
14
|
-
|
15
|
-
|
16
|
-
t.
|
17
|
-
t.
|
18
|
-
t.datetime
|
19
|
-
t.datetime "updated_at", null: false
|
14
|
+
create_table 'categories', force: :cascade do |t|
|
15
|
+
t.string 'name'
|
16
|
+
t.integer 'store_id'
|
17
|
+
t.datetime 'created_at', null: false
|
18
|
+
t.datetime 'updated_at', null: false
|
20
19
|
end
|
21
20
|
|
22
|
-
create_table
|
23
|
-
t.string
|
24
|
-
t.integer
|
25
|
-
t.integer
|
26
|
-
t.datetime
|
27
|
-
t.datetime
|
21
|
+
create_table 'category_items', force: :cascade do |t|
|
22
|
+
t.string 'name'
|
23
|
+
t.integer 'category_id'
|
24
|
+
t.integer 'product_id'
|
25
|
+
t.datetime 'created_at', null: false
|
26
|
+
t.datetime 'updated_at', null: false
|
28
27
|
end
|
29
28
|
|
30
|
-
create_table
|
31
|
-
t.string
|
32
|
-
t.datetime
|
33
|
-
t.datetime
|
29
|
+
create_table 'products', force: :cascade do |t|
|
30
|
+
t.string 'name'
|
31
|
+
t.datetime 'created_at', null: false
|
32
|
+
t.datetime 'updated_at', null: false
|
34
33
|
end
|
35
34
|
|
36
|
-
create_table
|
37
|
-
t.string
|
38
|
-
t.datetime
|
39
|
-
t.datetime
|
35
|
+
create_table 'stores', force: :cascade do |t|
|
36
|
+
t.string 'name'
|
37
|
+
t.datetime 'created_at', null: false
|
38
|
+
t.datetime 'updated_at', null: false
|
40
39
|
end
|
41
|
-
|
42
40
|
end
|
data/test/filter_test.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class FilterTest < ActiveSupport::TestCase
|
4
|
+
self.use_transactional_tests = true
|
5
|
+
teardown :clear_cache
|
6
|
+
|
7
|
+
test 'allows filtering for which records should be rebuilt' do
|
8
|
+
mock = Minitest::Mock.new
|
9
|
+
category = categories(:baseball)
|
10
|
+
filter = ->(model) { model.name.match('ball') }
|
11
|
+
Thermos.fill(
|
12
|
+
key: 'key',
|
13
|
+
model: Category,
|
14
|
+
lookup_key: 'name',
|
15
|
+
filter: filter,
|
16
|
+
) { |name| mock.call(name) }
|
17
|
+
|
18
|
+
mock.expect(:call, 1, ['basketball'])
|
19
|
+
category.update!(name: 'basketball')
|
20
|
+
mock.verify
|
21
|
+
|
22
|
+
mock.expect(:call, 1, ['hockey'])
|
23
|
+
category.update!(name: 'hockey')
|
24
|
+
assert_raises(MockExpectationError) { mock.verify }
|
25
|
+
end
|
26
|
+
|
27
|
+
test 'allows filtering based on the beverage when multiple beverages are configured and only one of them has a filter' do
|
28
|
+
mock = Minitest::Mock.new
|
29
|
+
store = stores(:supermarket)
|
30
|
+
category = categories(:baseball)
|
31
|
+
|
32
|
+
# filter method specific to one model
|
33
|
+
# store.ball? doesn't exist
|
34
|
+
filter = ->(model) { model.ball? }
|
35
|
+
|
36
|
+
Thermos.fill(
|
37
|
+
key: 'key',
|
38
|
+
model: Category,
|
39
|
+
lookup_key: 'name',
|
40
|
+
filter: filter,
|
41
|
+
) { |name| mock.call(name) }
|
42
|
+
|
43
|
+
Thermos.fill(key: 'key_2', model: Store, lookup_key: 'name') do |name|
|
44
|
+
mock.call(name)
|
45
|
+
end
|
46
|
+
|
47
|
+
mock.expect(:call, 1, ['groceries'])
|
48
|
+
store.update!(name: 'groceries')
|
49
|
+
assert_equal 1, Thermos.drink(key: 'key_2', id: 'groceries')
|
50
|
+
mock.verify
|
51
|
+
end
|
52
|
+
end
|
data/test/queue_test.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class QueueTest < ActiveSupport::TestCase
|
4
|
+
include ActiveJob::TestHelper
|
5
|
+
self.use_transactional_tests = true
|
6
|
+
teardown :clear_cache
|
7
|
+
|
8
|
+
test 'uses the default background queue by default' do
|
9
|
+
mock = Minitest::Mock.new
|
10
|
+
category = categories(:baseball)
|
11
|
+
|
12
|
+
Thermos.fill(key: 'key', model: Category) { |id| mock.call(id) }
|
13
|
+
|
14
|
+
mock.expect(:call, 1, [category.id])
|
15
|
+
assert_performed_with(job: Thermos::RebuildCacheJob, queue: 'default') do
|
16
|
+
category.update!(name: 'foo')
|
17
|
+
end
|
18
|
+
mock.verify
|
19
|
+
end
|
20
|
+
|
21
|
+
test 'can specify a preferred queue name for the cache filling' do
|
22
|
+
mock = Minitest::Mock.new
|
23
|
+
category = categories(:baseball)
|
24
|
+
|
25
|
+
Thermos.fill(key: 'key', model: Category, queue: 'low_priority') do |id|
|
26
|
+
mock.call(id)
|
27
|
+
end
|
28
|
+
|
29
|
+
mock.expect(:call, 1, [category.id])
|
30
|
+
assert_performed_with(
|
31
|
+
job: Thermos::RebuildCacheJob,
|
32
|
+
queue: 'low_priority',
|
33
|
+
) { category.update!(name: 'foo') }
|
34
|
+
mock.verify
|
35
|
+
end
|
36
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
# Configure Rails Environment
|
2
|
-
ENV[
|
2
|
+
ENV['RAILS_ENV'] = 'test'
|
3
3
|
|
4
|
-
require File.expand_path(
|
5
|
-
ActiveRecord::Migrator.migrations_paths = [
|
6
|
-
|
4
|
+
require File.expand_path('../../test/dummy/config/environment.rb', __FILE__)
|
5
|
+
ActiveRecord::Migrator.migrations_paths = [
|
6
|
+
File.expand_path('../../test/dummy/db/migrate', __FILE__),
|
7
|
+
]
|
8
|
+
require 'rails/test_help'
|
7
9
|
|
8
10
|
# Filter out Minitest backtrace while allowing backtrace from other libraries
|
9
11
|
# to be shown.
|
@@ -15,9 +17,17 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
|
15
17
|
|
16
18
|
# Load fixtures from the engine
|
17
19
|
if ActiveSupport::TestCase.respond_to?(:fixture_path=)
|
18
|
-
ActiveSupport::TestCase.fixture_path =
|
19
|
-
|
20
|
+
ActiveSupport::TestCase.fixture_path =
|
21
|
+
File.expand_path('../fixtures', __FILE__)
|
22
|
+
ActionDispatch::IntegrationTest.fixture_path =
|
23
|
+
ActiveSupport::TestCase.fixture_path
|
20
24
|
ActiveSupport::TestCase.fixtures :all
|
21
25
|
end
|
22
26
|
|
23
27
|
ActiveJob::Base.queue_adapter = :inline
|
28
|
+
ActiveSupport.test_order = :random
|
29
|
+
|
30
|
+
def clear_cache
|
31
|
+
Thermos::BeverageStorage.instance.empty
|
32
|
+
Rails.cache.clear
|
33
|
+
end
|