kasket 3.1.5 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/kasket.rb +0 -6
- data/lib/kasket/configuration_mixin.rb +0 -1
- data/lib/kasket/dirty_mixin.rb +0 -1
- data/lib/kasket/query_parser.rb +1 -14
- data/lib/kasket/read_mixin.rb +1 -2
- data/lib/kasket/reload_association_mixin.rb +1 -3
- data/lib/kasket/version.rb +2 -3
- data/lib/kasket/visitor.rb +15 -15
- data/lib/kasket/write_mixin.rb +0 -1
- metadata +49 -73
- data/test/cache_expiry_test.rb +0 -49
- data/test/cacheable_test.rb +0 -58
- data/test/configuration_mixin_test.rb +0 -44
- data/test/database.yml +0 -7
- data/test/dirty_test.rb +0 -16
- data/test/find_one_test.rb +0 -61
- data/test/find_some_test.rb +0 -55
- data/test/fixtures/authors.yml +0 -7
- data/test/fixtures/blogs.yml +0 -5
- data/test/fixtures/comments.yml +0 -16
- data/test/fixtures/posts.yml +0 -24
- data/test/helper.rb +0 -84
- data/test/parser_test.rb +0 -153
- data/test/read_mixin_test.rb +0 -89
- data/test/reload_test.rb +0 -17
- data/test/schema.rb +0 -31
- data/test/test_models.rb +0 -44
- data/test/transaction_test.rb +0 -27
- data/test/visitor_test.rb +0 -39
data/test/read_mixin_test.rb
DELETED
@@ -1,89 +0,0 @@
|
|
1
|
-
require File.expand_path("helper", File.dirname(__FILE__))
|
2
|
-
|
3
|
-
class ReadMixinTest < ActiveSupport::TestCase
|
4
|
-
fixtures :authors
|
5
|
-
|
6
|
-
context "find by sql with kasket" do
|
7
|
-
setup do
|
8
|
-
@post_database_result = { 'id' => 1, 'title' => 'Hello' }
|
9
|
-
@post_records = [Post.send(:instantiate, @post_database_result)]
|
10
|
-
Post.stubs(:find_by_sql_without_kasket).returns(@post_records)
|
11
|
-
|
12
|
-
@comment_database_result = [{ 'id' => 1, 'body' => 'Hello' }, { 'id' => 2, 'body' => 'World' }]
|
13
|
-
@comment_records = @comment_database_result.map {|r| Comment.send(:instantiate, r)}
|
14
|
-
Comment.stubs(:find_by_sql_without_kasket).returns(@comment_records)
|
15
|
-
end
|
16
|
-
|
17
|
-
should "handle unsupported sql" do
|
18
|
-
Kasket.cache.expects(:read).never
|
19
|
-
Kasket.cache.expects(:write).never
|
20
|
-
assert_equal @post_records, Post.find_by_sql_with_kasket('select unsupported sql statement')
|
21
|
-
end
|
22
|
-
|
23
|
-
should "read results" do
|
24
|
-
Kasket.cache.write("#{Post.kasket_key_prefix}id=1", @post_database_result)
|
25
|
-
assert_equal @post_records, Post.find_by_sql('SELECT * FROM `posts` WHERE (id = 1)')
|
26
|
-
end
|
27
|
-
|
28
|
-
should "support sql with ?" do
|
29
|
-
Kasket.cache.write("#{Post.kasket_key_prefix}id=1", @post_database_result)
|
30
|
-
assert_equal @post_records, Post.find_by_sql(['SELECT * FROM `posts` WHERE (id = ?)', 1])
|
31
|
-
end
|
32
|
-
|
33
|
-
should "store results in kasket" do
|
34
|
-
Post.find_by_sql('SELECT * FROM `posts` WHERE (id = 1)')
|
35
|
-
|
36
|
-
assert_equal @post_database_result, Kasket.cache.read("#{Post.kasket_key_prefix}id=1")
|
37
|
-
end
|
38
|
-
|
39
|
-
should "store multiple records in cache" do
|
40
|
-
Comment.find_by_sql('SELECT * FROM `comments` WHERE (post_id = 1)')
|
41
|
-
stored_value = Kasket.cache.read("#{Comment.kasket_key_prefix}post_id=1")
|
42
|
-
assert_equal(["#{Comment.kasket_key_prefix}id=1", "#{Comment.kasket_key_prefix}id=2"], stored_value)
|
43
|
-
assert_equal(@comment_database_result, stored_value.map {|key| Kasket.cache.read(key)})
|
44
|
-
|
45
|
-
Comment.expects(:find_by_sql_without_kasket).never
|
46
|
-
records = Comment.find_by_sql('SELECT * FROM `comments` WHERE (post_id = 1)')
|
47
|
-
assert_equal(@comment_records, records.sort {|c1, c2| c1.id <=> c2.id})
|
48
|
-
end
|
49
|
-
|
50
|
-
context "modifying results" do
|
51
|
-
setup do
|
52
|
-
Kasket.cache.write("#{Post.kasket_key_prefix}id=1", {'id' => 1, 'title' => "asd"})
|
53
|
-
@sql = 'SELECT * FROM `posts` WHERE (id = 1)'
|
54
|
-
@record = Post.find_by_sql(@sql).first
|
55
|
-
assert_equal "asd", @record.title # read from cache ?
|
56
|
-
@record.instance_variable_get(:@attributes)['id'] = 3
|
57
|
-
end
|
58
|
-
|
59
|
-
should "not impact other queries" do
|
60
|
-
same_record = Post.find_by_sql(@sql).first
|
61
|
-
|
62
|
-
assert_not_equal @record, same_record
|
63
|
-
end
|
64
|
-
|
65
|
-
end
|
66
|
-
|
67
|
-
end
|
68
|
-
|
69
|
-
should "support serialized attributes" do
|
70
|
-
author = authors(:mick)
|
71
|
-
|
72
|
-
author = Author.find(author.id)
|
73
|
-
assert_equal({'sex' => 'male'}, author.metadata)
|
74
|
-
|
75
|
-
author = Author.find(author.id)
|
76
|
-
assert_equal({'sex' => 'male'}, author.metadata)
|
77
|
-
end
|
78
|
-
|
79
|
-
should "not store time with zone" do
|
80
|
-
Time.use_zone(ActiveSupport::TimeZone.all.first) do
|
81
|
-
post = posts(:no_comments)
|
82
|
-
post = Post.find(post.id)
|
83
|
-
object = Kasket.cache.read("#{Post.kasket_key_prefix}id=#{post.id}")
|
84
|
-
|
85
|
-
assert_equal "2013-10-14 15:30:00", object["created_at"].to_s, object["created_at"].class
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
end
|
data/test/reload_test.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
require File.expand_path("helper", File.dirname(__FILE__))
|
2
|
-
|
3
|
-
class ReloadTest < ActiveSupport::TestCase
|
4
|
-
context "Reloading a model" do
|
5
|
-
setup do
|
6
|
-
@post = Post.first
|
7
|
-
assert @post
|
8
|
-
assert @post.title
|
9
|
-
end
|
10
|
-
|
11
|
-
should "clear local cache" do
|
12
|
-
Kasket.expects(:clear_local)
|
13
|
-
@post.reload
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
data/test/schema.rb
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
ActiveRecord::Schema.define(:version => 1) do
|
2
|
-
suppress_messages do
|
3
|
-
create_table 'comments', :force => true do |t|
|
4
|
-
t.text 'body'
|
5
|
-
t.integer 'post_id'
|
6
|
-
t.datetime 'created_at'
|
7
|
-
t.datetime 'updated_at'
|
8
|
-
end
|
9
|
-
|
10
|
-
create_table 'authors', :force => true do |t|
|
11
|
-
t.string 'name'
|
12
|
-
t.string 'metadata'
|
13
|
-
end
|
14
|
-
|
15
|
-
create_table 'posts', :force => true do |t|
|
16
|
-
t.string 'title'
|
17
|
-
t.integer 'author_id'
|
18
|
-
t.integer 'blog_id'
|
19
|
-
t.integer 'poly_id'
|
20
|
-
t.string 'poly_type'
|
21
|
-
t.datetime 'created_at'
|
22
|
-
t.datetime 'updated_at'
|
23
|
-
end
|
24
|
-
|
25
|
-
create_table 'blogs', :force => true do |t|
|
26
|
-
t.string 'name'
|
27
|
-
t.datetime 'created_at'
|
28
|
-
t.datetime 'updated_at'
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
data/test/test_models.rb
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
ActiveRecord::Base.configurations = YAML::load(IO.read(File.expand_path("database.yml", File.dirname(__FILE__))))
|
2
|
-
|
3
|
-
conf = ActiveRecord::Base.configurations['test']
|
4
|
-
`echo "drop DATABASE if exists #{conf['database']}" | mysql --user=#{conf['username']}`
|
5
|
-
`echo "create DATABASE #{conf['database']}" | mysql --user=#{conf['username']}`
|
6
|
-
ActiveRecord::Base.establish_connection('test')
|
7
|
-
load(File.dirname(__FILE__) + "/schema.rb")
|
8
|
-
|
9
|
-
class Comment < ActiveRecord::Base
|
10
|
-
belongs_to :post
|
11
|
-
has_one :author, :through => :post
|
12
|
-
|
13
|
-
has_kasket_on :post_id
|
14
|
-
end
|
15
|
-
|
16
|
-
class Author < ActiveRecord::Base
|
17
|
-
serialize :metadata
|
18
|
-
has_many :posts
|
19
|
-
|
20
|
-
has_kasket
|
21
|
-
end
|
22
|
-
|
23
|
-
class Post < ActiveRecord::Base
|
24
|
-
belongs_to :blog
|
25
|
-
belongs_to :author
|
26
|
-
has_many :comments
|
27
|
-
belongs_to :poly, :polymorphic => true
|
28
|
-
|
29
|
-
has_kasket
|
30
|
-
has_kasket_on :title
|
31
|
-
has_kasket_on :blog_id, :id
|
32
|
-
|
33
|
-
def make_dirty!
|
34
|
-
self.updated_at = Time.now
|
35
|
-
self.connection.execute("UPDATE posts SET updated_at = '#{updated_at.utc.to_s(:db)}' WHERE id = #{id}")
|
36
|
-
end
|
37
|
-
|
38
|
-
kasket_dirty_methods :make_dirty!
|
39
|
-
end
|
40
|
-
|
41
|
-
class Blog < ActiveRecord::Base
|
42
|
-
has_many :posts
|
43
|
-
has_many :comments, :through => :posts
|
44
|
-
end
|
data/test/transaction_test.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
require File.expand_path("helper", File.dirname(__FILE__))
|
2
|
-
|
3
|
-
class TransactionTest < ActiveSupport::TestCase
|
4
|
-
context "Transactions" do
|
5
|
-
should "have kasket disabled" do
|
6
|
-
assert_equal true, Post.use_kasket?
|
7
|
-
Post.transaction do
|
8
|
-
assert_equal false, Post.use_kasket?
|
9
|
-
end
|
10
|
-
assert_equal true, Post.use_kasket?
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
context "Nested transactions" do
|
15
|
-
setup { Comment.has_kasket }
|
16
|
-
should "disable kasket" do
|
17
|
-
Post.transaction do
|
18
|
-
assert_equal false, Comment.use_kasket?
|
19
|
-
assert_equal false, Post.use_kasket?
|
20
|
-
Comment.transaction do
|
21
|
-
assert_equal false, Post.use_kasket?
|
22
|
-
assert_equal false, Comment.use_kasket?
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
data/test/visitor_test.rb
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
require File.expand_path("helper", File.dirname(__FILE__))
|
2
|
-
|
3
|
-
module Nori
|
4
|
-
class StringWithAttributes < String
|
5
|
-
end
|
6
|
-
|
7
|
-
class Unknown
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
class VisitorTest < ActiveSupport::TestCase
|
12
|
-
if arel?
|
13
|
-
context Kasket::Visitor do
|
14
|
-
should "build select id" do
|
15
|
-
expected = {
|
16
|
-
:attributes=>[[:id, "1"]],
|
17
|
-
:from=>"posts",
|
18
|
-
:index=>[:id],
|
19
|
-
:key=>"#{Post.kasket_key_prefix}id=1"
|
20
|
-
}
|
21
|
-
assert_equal expected, Post.where(:id => 1).to_kasket_query
|
22
|
-
end
|
23
|
-
|
24
|
-
should "build from Nori::StringWithAttributes" do
|
25
|
-
expected = {
|
26
|
-
:attributes=>[[:id, "1"]],
|
27
|
-
:from=>"posts",
|
28
|
-
:index=>[:id],
|
29
|
-
:key=>"#{Post.kasket_key_prefix}id=1"
|
30
|
-
}
|
31
|
-
assert_equal expected, Post.where(:id => Nori::StringWithAttributes.new("1")).to_kasket_query
|
32
|
-
end
|
33
|
-
|
34
|
-
should "notify on missing attribute" do
|
35
|
-
assert_equal nil, Post.where(:id => Nori::Unknown.new).to_kasket_query
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|