ferblape-query_memcached 2.2.2

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.
Files changed (166) hide show
  1. data/.gitignore +2 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.markdown +87 -0
  4. data/RUNNING_TESTS +18 -0
  5. data/Rakefile +37 -0
  6. data/TODO +6 -0
  7. data/init.rb +3 -0
  8. data/lib/extensions/lock.rb +31 -0
  9. data/lib/query_memcached.rb +191 -0
  10. data/test/query_memcached_test.rb +26 -0
  11. data/test/testing_app/README +1 -0
  12. data/test/testing_app/Rakefile +8 -0
  13. data/test/testing_app/app/controllers/application.rb +15 -0
  14. data/test/testing_app/app/helpers/application_helper.rb +3 -0
  15. data/test/testing_app/app/models/author.rb +133 -0
  16. data/test/testing_app/app/models/auto_id.rb +4 -0
  17. data/test/testing_app/app/models/binary.rb +2 -0
  18. data/test/testing_app/app/models/book.rb +4 -0
  19. data/test/testing_app/app/models/categorization.rb +5 -0
  20. data/test/testing_app/app/models/category.rb +29 -0
  21. data/test/testing_app/app/models/citation.rb +6 -0
  22. data/test/testing_app/app/models/club.rb +7 -0
  23. data/test/testing_app/app/models/column_name.rb +3 -0
  24. data/test/testing_app/app/models/comment.rb +25 -0
  25. data/test/testing_app/app/models/company.rb +123 -0
  26. data/test/testing_app/app/models/company_in_module.rb +61 -0
  27. data/test/testing_app/app/models/computer.rb +4 -0
  28. data/test/testing_app/app/models/contact.rb +16 -0
  29. data/test/testing_app/app/models/course.rb +3 -0
  30. data/test/testing_app/app/models/customer.rb +55 -0
  31. data/test/testing_app/app/models/default.rb +2 -0
  32. data/test/testing_app/app/models/developer.rb +76 -0
  33. data/test/testing_app/app/models/edge.rb +5 -0
  34. data/test/testing_app/app/models/entrant.rb +3 -0
  35. data/test/testing_app/app/models/guid.rb +2 -0
  36. data/test/testing_app/app/models/item.rb +7 -0
  37. data/test/testing_app/app/models/job.rb +5 -0
  38. data/test/testing_app/app/models/joke.rb +3 -0
  39. data/test/testing_app/app/models/keyboard.rb +3 -0
  40. data/test/testing_app/app/models/legacy_thing.rb +3 -0
  41. data/test/testing_app/app/models/matey.rb +4 -0
  42. data/test/testing_app/app/models/member.rb +9 -0
  43. data/test/testing_app/app/models/membership.rb +9 -0
  44. data/test/testing_app/app/models/minimalistic.rb +2 -0
  45. data/test/testing_app/app/models/mixed_case_monkey.rb +3 -0
  46. data/test/testing_app/app/models/movie.rb +5 -0
  47. data/test/testing_app/app/models/order.rb +4 -0
  48. data/test/testing_app/app/models/owner.rb +4 -0
  49. data/test/testing_app/app/models/parrot.rb +13 -0
  50. data/test/testing_app/app/models/person.rb +10 -0
  51. data/test/testing_app/app/models/pet.rb +4 -0
  52. data/test/testing_app/app/models/pirate.rb +9 -0
  53. data/test/testing_app/app/models/post.rb +80 -0
  54. data/test/testing_app/app/models/price_estimate.rb +3 -0
  55. data/test/testing_app/app/models/project.rb +29 -0
  56. data/test/testing_app/app/models/reader.rb +4 -0
  57. data/test/testing_app/app/models/reference.rb +4 -0
  58. data/test/testing_app/app/models/reply.rb +39 -0
  59. data/test/testing_app/app/models/ship.rb +3 -0
  60. data/test/testing_app/app/models/sponsor.rb +4 -0
  61. data/test/testing_app/app/models/subject.rb +4 -0
  62. data/test/testing_app/app/models/subscriber.rb +8 -0
  63. data/test/testing_app/app/models/subscription.rb +4 -0
  64. data/test/testing_app/app/models/tag.rb +7 -0
  65. data/test/testing_app/app/models/tagging.rb +10 -0
  66. data/test/testing_app/app/models/task.rb +2 -0
  67. data/test/testing_app/app/models/topic.rb +65 -0
  68. data/test/testing_app/app/models/treasure.rb +6 -0
  69. data/test/testing_app/app/models/vertex.rb +9 -0
  70. data/test/testing_app/app/models/warehouse_thing.rb +5 -0
  71. data/test/testing_app/config/boot.rb +109 -0
  72. data/test/testing_app/config/database.yml +11 -0
  73. data/test/testing_app/config/environment.rb +19 -0
  74. data/test/testing_app/config/environments/development.rb +0 -0
  75. data/test/testing_app/config/environments/production.rb +0 -0
  76. data/test/testing_app/config/environments/test.rb +0 -0
  77. data/test/testing_app/config/initializers/inflections.rb +10 -0
  78. data/test/testing_app/config/initializers/mime_types.rb +5 -0
  79. data/test/testing_app/config/initializers/new_rails_defaults.rb +15 -0
  80. data/test/testing_app/config/routes.rb +41 -0
  81. data/test/testing_app/db/schema.rb +443 -0
  82. data/test/testing_app/script/about +3 -0
  83. data/test/testing_app/script/console +3 -0
  84. data/test/testing_app/script/dbconsole +3 -0
  85. data/test/testing_app/script/destroy +3 -0
  86. data/test/testing_app/script/generate +3 -0
  87. data/test/testing_app/script/performance/benchmarker +3 -0
  88. data/test/testing_app/script/performance/profiler +3 -0
  89. data/test/testing_app/script/performance/request +3 -0
  90. data/test/testing_app/script/plugin +3 -0
  91. data/test/testing_app/script/process/inspector +3 -0
  92. data/test/testing_app/script/process/reaper +3 -0
  93. data/test/testing_app/script/process/spawner +3 -0
  94. data/test/testing_app/script/runner +3 -0
  95. data/test/testing_app/script/server +3 -0
  96. data/test/testing_app/test/fixtures/accounts.yml +28 -0
  97. data/test/testing_app/test/fixtures/all/developers.yml +0 -0
  98. data/test/testing_app/test/fixtures/all/people.csv +0 -0
  99. data/test/testing_app/test/fixtures/all/tasks.yml +0 -0
  100. data/test/testing_app/test/fixtures/author_addresses.yml +5 -0
  101. data/test/testing_app/test/fixtures/author_favorites.yml +4 -0
  102. data/test/testing_app/test/fixtures/authors.yml +9 -0
  103. data/test/testing_app/test/fixtures/binaries.yml +132 -0
  104. data/test/testing_app/test/fixtures/books.yml +7 -0
  105. data/test/testing_app/test/fixtures/categories.yml +14 -0
  106. data/test/testing_app/test/fixtures/categories/special_categories.yml +9 -0
  107. data/test/testing_app/test/fixtures/categories/subsubdir/arbitrary_filename.yml +4 -0
  108. data/test/testing_app/test/fixtures/categories_posts.yml +23 -0
  109. data/test/testing_app/test/fixtures/categorizations.yml +17 -0
  110. data/test/testing_app/test/fixtures/clubs.yml +6 -0
  111. data/test/testing_app/test/fixtures/comments.yml +59 -0
  112. data/test/testing_app/test/fixtures/companies.yml +55 -0
  113. data/test/testing_app/test/fixtures/computers.yml +4 -0
  114. data/test/testing_app/test/fixtures/courses.yml +7 -0
  115. data/test/testing_app/test/fixtures/customers.yml +17 -0
  116. data/test/testing_app/test/fixtures/developers.yml +21 -0
  117. data/test/testing_app/test/fixtures/developers_projects.yml +17 -0
  118. data/test/testing_app/test/fixtures/edges.yml +6 -0
  119. data/test/testing_app/test/fixtures/entrants.yml +14 -0
  120. data/test/testing_app/test/fixtures/fk_test_has_fk.yml +3 -0
  121. data/test/testing_app/test/fixtures/fk_test_has_pk.yml +2 -0
  122. data/test/testing_app/test/fixtures/funny_jokes.yml +10 -0
  123. data/test/testing_app/test/fixtures/items.yml +4 -0
  124. data/test/testing_app/test/fixtures/jobs.yml +7 -0
  125. data/test/testing_app/test/fixtures/legacy_things.yml +3 -0
  126. data/test/testing_app/test/fixtures/mateys.yml +4 -0
  127. data/test/testing_app/test/fixtures/members.yml +4 -0
  128. data/test/testing_app/test/fixtures/memberships.yml +20 -0
  129. data/test/testing_app/test/fixtures/minimalistics.yml +2 -0
  130. data/test/testing_app/test/fixtures/mixed_case_monkeys.yml +6 -0
  131. data/test/testing_app/test/fixtures/mixins.yml +29 -0
  132. data/test/testing_app/test/fixtures/movies.yml +7 -0
  133. data/test/testing_app/test/fixtures/naked/csv/accounts.csv +1 -0
  134. data/test/testing_app/test/fixtures/naked/yml/accounts.yml +1 -0
  135. data/test/testing_app/test/fixtures/naked/yml/companies.yml +1 -0
  136. data/test/testing_app/test/fixtures/naked/yml/courses.yml +1 -0
  137. data/test/testing_app/test/fixtures/owners.yml +7 -0
  138. data/test/testing_app/test/fixtures/parrots.yml +27 -0
  139. data/test/testing_app/test/fixtures/parrots_pirates.yml +7 -0
  140. data/test/testing_app/test/fixtures/people.yml +6 -0
  141. data/test/testing_app/test/fixtures/pets.yml +14 -0
  142. data/test/testing_app/test/fixtures/pirates.yml +9 -0
  143. data/test/testing_app/test/fixtures/posts.yml +49 -0
  144. data/test/testing_app/test/fixtures/price_estimates.yml +7 -0
  145. data/test/testing_app/test/fixtures/projects.yml +7 -0
  146. data/test/testing_app/test/fixtures/readers.yml +9 -0
  147. data/test/testing_app/test/fixtures/references.yml +17 -0
  148. data/test/testing_app/test/fixtures/reserved_words/distinct.yml +5 -0
  149. data/test/testing_app/test/fixtures/reserved_words/distincts_selects.yml +11 -0
  150. data/test/testing_app/test/fixtures/reserved_words/group.yml +14 -0
  151. data/test/testing_app/test/fixtures/reserved_words/select.yml +8 -0
  152. data/test/testing_app/test/fixtures/reserved_words/values.yml +7 -0
  153. data/test/testing_app/test/fixtures/ships.yml +5 -0
  154. data/test/testing_app/test/fixtures/sponsors.yml +9 -0
  155. data/test/testing_app/test/fixtures/subscribers.yml +7 -0
  156. data/test/testing_app/test/fixtures/subscriptions.yml +12 -0
  157. data/test/testing_app/test/fixtures/taggings.yml +28 -0
  158. data/test/testing_app/test/fixtures/tags.yml +7 -0
  159. data/test/testing_app/test/fixtures/tasks.yml +7 -0
  160. data/test/testing_app/test/fixtures/topics.yml +42 -0
  161. data/test/testing_app/test/fixtures/treasures.yml +10 -0
  162. data/test/testing_app/test/fixtures/vertices.yml +4 -0
  163. data/test/testing_app/test/fixtures/warehouse-things.yml +3 -0
  164. data/test/testing_app/test/test_helper.rb +131 -0
  165. data/test/testing_app/test/unit/query_cache_test.rb +180 -0
  166. metadata +289 -0
@@ -0,0 +1,8 @@
1
+ select1:
2
+ id: 1
3
+
4
+ select2:
5
+ id: 2
6
+
7
+ select3:
8
+ id: 3
@@ -0,0 +1,7 @@
1
+ values1:
2
+ id: 1
3
+ group_id: 2
4
+
5
+ values2:
6
+ id: 2
7
+ group_id: 1
@@ -0,0 +1,5 @@
1
+ black_pearl:
2
+ name: "Black Pearl"
3
+ interceptor:
4
+ id: 2
5
+ name: "Interceptor"
@@ -0,0 +1,9 @@
1
+ moustache_club_sponsor_for_groucho:
2
+ sponsor_club: moustache_club
3
+ sponsorable: groucho (Member)
4
+ boring_club_sponsor_for_groucho:
5
+ sponsor_club: boring_club
6
+ sponsorable: some_other_guy (Member)
7
+ crazy_club_sponsor_for_groucho:
8
+ sponsor_club: crazy_club
9
+ sponsorable: some_other_guy (Member)
@@ -0,0 +1,7 @@
1
+ first:
2
+ nick: alterself
3
+ name: Luke Holden
4
+
5
+ second:
6
+ nick: webster132
7
+ name: David Heinemeier Hansson
@@ -0,0 +1,12 @@
1
+ webster_awdr:
2
+ id: 1
3
+ subscriber_id: webster132
4
+ book_id: 1
5
+ webster_rfr:
6
+ id: 2
7
+ subscriber_id: webster132
8
+ book_id: 2
9
+ alterself_awdr:
10
+ id: 3
11
+ subscriber_id: alterself
12
+ book_id: 3
@@ -0,0 +1,28 @@
1
+ welcome_general:
2
+ id: 1
3
+ tag_id: 1
4
+ super_tag_id: 2
5
+ taggable_id: 1
6
+ taggable_type: Post
7
+
8
+ thinking_general:
9
+ id: 2
10
+ tag_id: 1
11
+ taggable_id: 2
12
+ taggable_type: Post
13
+
14
+ fake:
15
+ id: 3
16
+ tag_id: 1
17
+ taggable_id: 1
18
+ taggable_type: FakeModel
19
+
20
+ godfather:
21
+ id: 4
22
+ tag_id: 1
23
+ taggable_id: 1
24
+ taggable_type: Item
25
+
26
+ orphaned:
27
+ id: 5
28
+ tag_id: 1
@@ -0,0 +1,7 @@
1
+ general:
2
+ id: 1
3
+ name: General
4
+
5
+ misc:
6
+ id: 2
7
+ name: Misc
@@ -0,0 +1,7 @@
1
+ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2
+ first_task:
3
+ id: 1
4
+ starting: 2005-03-30t06:30:00.00+01:00
5
+ ending: 2005-03-30t08:30:00.00+01:00
6
+ another_task:
7
+ id: 2
@@ -0,0 +1,42 @@
1
+ first:
2
+ id: 1
3
+ title: The First Topic
4
+ author_name: David
5
+ author_email_address: david@loudthinking.com
6
+ written_on: 2003-07-16t15:28:11.2233+01:00
7
+ last_read: 2004-04-15
8
+ bonus_time: 2005-01-30t15:28:00.00+01:00
9
+ content: Have a nice day
10
+ approved: false
11
+ replies_count: 1
12
+
13
+ second:
14
+ id: 2
15
+ title: The Second Topic of the day
16
+ author_name: Mary
17
+ written_on: 2004-07-15t15:28:00.0099+01:00
18
+ content: Have a nice day
19
+ approved: true
20
+ replies_count: 0
21
+ parent_id: 1
22
+ type: Reply
23
+
24
+ third:
25
+ id: 3
26
+ title: The Third Topic of the day
27
+ author_name: Nick
28
+ written_on: 2005-07-15t15:28:00.0099+01:00
29
+ content: I'm a troll
30
+ approved: true
31
+ replies_count: 1
32
+
33
+ fourth:
34
+ id: 4
35
+ title: The Fourth Topic of the day
36
+ author_name: Carl
37
+ written_on: 2006-07-15t15:28:00.0099+01:00
38
+ content: Why not?
39
+ approved: true
40
+ type: Reply
41
+ parent_id: 3
42
+
@@ -0,0 +1,10 @@
1
+ diamond:
2
+ name: $LABEL
3
+
4
+ sapphire:
5
+ name: $LABEL
6
+ looter: redbeard (Pirate)
7
+
8
+ ruby:
9
+ name: $LABEL
10
+ looter: louis (Parrot)
@@ -0,0 +1,4 @@
1
+ <% (1..5).each do |id| %>
2
+ vertex_<%= id %>:
3
+ id: <%= id %>
4
+ <% end %>
@@ -0,0 +1,3 @@
1
+ one:
2
+ id: 1
3
+ value: 1000
@@ -0,0 +1,131 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
4
+ require 'test_help'
5
+ require 'test/unit'
6
+ require 'rubygems'
7
+ require 'active_record'
8
+
9
+ TEST_ROOT = File.expand_path(File.dirname(__FILE__)) + '/test'
10
+ FIXTURES_ROOT = TEST_ROOT + "/fixtures"
11
+ SCHEMA_ROOT = TEST_ROOT + "/../db"
12
+
13
+ # Show backtraces for deprecated behavior for quicker cleanup.
14
+ ActiveSupport::Deprecation.debug = true
15
+
16
+ # Quote "type" if it's a reserved word for the current connection.
17
+ QUOTED_TYPE = ActiveRecord::Base.connection.quote_column_name('type')
18
+
19
+ def current_adapter?(*types)
20
+ types.any? do |type|
21
+ ActiveRecord::ConnectionAdapters.const_defined?(type) &&
22
+ ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters.const_get(type))
23
+ end
24
+ end
25
+
26
+ def uses_mocha(description)
27
+ require 'rubygems'
28
+ require 'mocha'
29
+ yield
30
+ rescue LoadError
31
+ $stderr.puts "Skipping #{description} tests. `gem install mocha` and try again."
32
+ end
33
+
34
+ ActiveRecord::Base.connection.class.class_eval do
35
+ IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/]
36
+
37
+ def execute_with_counting(sql, name = nil, &block)
38
+ $query_count ||= 0
39
+ $query_count += 1 unless IGNORED_SQL.any? { |r| sql =~ r }
40
+ execute_without_counting(sql, name, &block)
41
+ end
42
+
43
+ alias_method_chain :execute, :counting
44
+ end
45
+
46
+ # Make with_scope public for tests
47
+ class << ActiveRecord::Base
48
+ public :with_scope, :with_exclusive_scope
49
+ end
50
+
51
+ # module Test
52
+ # module Unit
53
+ # class TestCase
54
+ #
55
+ #
56
+ # def run_with_query_memcached(*args, &block)
57
+ # Rails.cache.clear
58
+ # ActiveRecord::Base.cache do
59
+ # run_without_query_memcached(*args, &block)
60
+ # end
61
+ # end
62
+ #
63
+ # alias_method_chain :run, :query_memcached
64
+ #
65
+ # end
66
+ # end
67
+ # end
68
+
69
+ class Test::Unit::TestCase
70
+ # Transactional fixtures accelerate your tests by wrapping each test method
71
+ # in a transaction that's rolled back on completion. This ensures that the
72
+ # test database remains unchanged so your fixtures don't have to be reloaded
73
+ # between every test method. Fewer database queries means faster tests.
74
+ #
75
+ # Read Mike Clark's excellent walkthrough at
76
+ # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
77
+ #
78
+ # Every Active Record database supports transactions except MyISAM tables
79
+ # in MySQL. Turn off transactional fixtures in this case; however, if you
80
+ # don't care one way or the other, switching from MyISAM to InnoDB tables
81
+ # is recommended.
82
+ #
83
+ # The only drawback to using transactional fixtures is when you actually
84
+ # need to test transactions. Since your test is bracketed by a transaction,
85
+ # any transactions started in your code will be automatically rolled back.
86
+ self.use_transactional_fixtures = true
87
+
88
+ # Instantiated fixtures are slow, but give you @david where otherwise you
89
+ # would need people(:david). If you don't want to migrate your existing
90
+ # test cases which use the @david style and don't mind the speed hit (each
91
+ # instantiated fixtures translates to a database query per test method),
92
+ # then set this back to true.
93
+ self.use_instantiated_fixtures = false
94
+
95
+ # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
96
+ #
97
+ # Note: You'll currently still have to declare fixtures explicitly in integration tests
98
+ # -- they do not yet inherit this setting
99
+ fixtures :all
100
+
101
+ # Add more helper methods to be used by all tests here...
102
+
103
+ def assert_queries(num = 1)
104
+ $query_count = 0
105
+ yield
106
+ ensure
107
+ assert_equal num, $query_count, "#{$query_count} instead of #{num} queries were executed."
108
+ end
109
+
110
+ def assert_no_queries(&block)
111
+ assert_queries(0, &block)
112
+ end
113
+
114
+ def create_fixtures(*table_names, &block)
115
+ Fixtures.create_fixtures(FIXTURES_ROOT, table_names, {}, &block)
116
+ end
117
+
118
+ def assert_date_from_db(expected, actual, message = nil)
119
+ # SQL Server doesn't have a separate column type just for dates,
120
+ # so the time is in the string and incorrectly formatted
121
+ if current_adapter?(:SQLServerAdapter)
122
+ assert_equal expected.strftime("%Y/%m/%d 00:00:00"), actual.strftime("%Y/%m/%d 00:00:00")
123
+ elsif current_adapter?(:SybaseAdapter)
124
+ assert_equal expected.to_s, actual.to_date.to_s, message
125
+ else
126
+ assert_equal expected.to_s, actual.to_s, message
127
+ end
128
+ end
129
+
130
+
131
+ end
@@ -0,0 +1,180 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class QueryCacheTest < Test::Unit::TestCase
4
+
5
+ fixtures :tasks, :topics, :categories, :posts, :categories_posts
6
+
7
+ def test_find_queries
8
+ assert_queries(2) { Task.find(1); Task.find(1) }
9
+ end
10
+
11
+ def test_find_queries_with_query_memcache_enabled
12
+ Computer.cache do
13
+ assert_queries(1) { Computer.find(1); Computer.find(1) }
14
+ end
15
+ end
16
+
17
+ def test_find_queries_with_cache
18
+ Task.cache do
19
+ assert_queries(1) { Task.find(1); Task.find(1) }
20
+ end
21
+ end
22
+
23
+ def test_count_queries_with_cache
24
+ Task.cache do
25
+ assert_queries(1) { Task.count; Task.count }
26
+ end
27
+ end
28
+
29
+ def test_query_cache_dups_results_correctly
30
+ Task.cache do
31
+ now = Time.now.utc
32
+ task = Task.find 1
33
+ assert_not_equal now, task.starting
34
+ task.starting = now
35
+ task.reload
36
+ assert_not_equal now, task.starting
37
+ end
38
+ end
39
+
40
+ def test_cache_is_flat
41
+ Task.cache do
42
+ Topic.columns # don't count this query
43
+ assert_queries(1) { Topic.find(1); Topic.find(1); }
44
+ end
45
+
46
+ ActiveRecord::Base.cache do
47
+ assert_queries(1) { Task.find(1); Task.find(1) }
48
+ end
49
+ end
50
+
51
+ def test_cache_does_not_wrap_string_results_in_arrays
52
+ Task.cache do
53
+ assert_instance_of String, Task.connection.select_value("SELECT count(*) AS count_all FROM tasks")
54
+ end
55
+ end
56
+ end
57
+
58
+ uses_mocha 'QueryCacheExpiryTest' do
59
+
60
+ class QueryCacheExpiryTest < Test::Unit::TestCase
61
+ fixtures :tasks
62
+
63
+ def setup
64
+ ::Rails.cache.clear
65
+ end
66
+
67
+ def test_find
68
+ Task.connection.expects(:clear_query_cache).times(1)
69
+
70
+ assert !Task.connection.query_cache_enabled
71
+ Task.cache do
72
+ assert Task.connection.query_cache_enabled
73
+ Task.find(1)
74
+
75
+ Task.uncached do
76
+ assert !Task.connection.query_cache_enabled
77
+ Task.find(1)
78
+ end
79
+
80
+ assert Task.connection.query_cache_enabled
81
+ end
82
+ assert !Task.connection.query_cache_enabled
83
+ end
84
+
85
+ def test_find_without_query_memcached_activated
86
+ ::Rails.cache.expects(:write).times(0)
87
+ ::Rails.cache.expects(:read).times(0)
88
+ Task.cache do
89
+ Task.find(1)
90
+ Task.find(1)
91
+ end
92
+ end
93
+
94
+ def test_find_with_query_memcached_activated
95
+ # 3 writes:
96
+ # - version
97
+ # - version/computers
98
+ # - version/computers/1
99
+ ::Rails.cache.expects(:write).times(3)
100
+ # The same reads
101
+ ::Rails.cache.expects(:read).times(3)
102
+ Computer.cache do
103
+ Computer.find(1)
104
+ Computer.find(1)
105
+ end
106
+ end
107
+
108
+ def test_update
109
+ Task.connection.expects(:clear_query_cache).times(2)
110
+
111
+ Task.cache do
112
+ task = Task.find(1)
113
+ task.starting = Time.now.utc
114
+ task.save!
115
+ end
116
+ end
117
+
118
+ def test_update_model_with_query_memcached_should_update_key
119
+ version = ::Rails.cache.read('version/computers') || 0
120
+ Computer.cache do
121
+ computer = Computer.find(1)
122
+ computer.developer = Developer.find(2)
123
+ computer.save!
124
+ end
125
+ assert_equal version + 1, ::Rails.cache.read('version/computers')
126
+ end
127
+
128
+
129
+ def test_destroy
130
+ Task.connection.expects(:clear_query_cache).times(2)
131
+
132
+ Task.cache do
133
+ Task.find(1).destroy
134
+ end
135
+ end
136
+
137
+ def test_destroy_model_with_query_memcached_should_update_key
138
+ version = ::Rails.cache.read('version/computers') || 0
139
+ Computer.cache do
140
+ Computer.find(1).destroy
141
+ end
142
+ assert_equal version + 1, ::Rails.cache.read('version/computers')
143
+ end
144
+
145
+ def test_insert
146
+ ActiveRecord::Base.connection.expects(:clear_query_cache).times(2)
147
+
148
+ Task.cache do
149
+ Task.create!
150
+ end
151
+ end
152
+
153
+ def test_insert_model_with_query_memcached_should_update_key
154
+ version = ::Rails.cache.read('version/computers') || 0
155
+ Computer.cache do
156
+ Computer.create!(:developer => Developer.find(1), :extendedWarranty => 1)
157
+ end
158
+ assert_equal version + 1, ::Rails.cache.read('version/computers')
159
+ end
160
+
161
+ def test_cache_is_expired_by_habtm_update
162
+ ActiveRecord::Base.connection.expects(:clear_query_cache).times(2)
163
+ ActiveRecord::Base.cache do
164
+ c = Category.find(:first)
165
+ p = Post.find(:first)
166
+ p.categories << c
167
+ end
168
+ end
169
+
170
+ def test_cache_is_expired_by_habtm_delete
171
+ ActiveRecord::Base.connection.expects(:clear_query_cache).times(2)
172
+ ActiveRecord::Base.cache do
173
+ c = Category.find(:first)
174
+ p = Post.find(:first)
175
+ p.categories.delete_all
176
+ end
177
+ end
178
+ end
179
+
180
+ end