aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/statement_cache_test.rb
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2013-04-10 16:02:26 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2013-04-10 16:02:26 -0300
commit0235cdf5ce0e92b88d49b8e135e696a89b76d097 (patch)
tree2a5fe1d457ca2ea10e95448eaf84ca6605983741 /activerecord/test/cases/statement_cache_test.rb
parent0b38c84332a6e19250e4c2e34ebe46cd618360b6 (diff)
parentaf1a4bdc564864e80b234755651303dac68ae82e (diff)
downloadrails-0235cdf5ce0e92b88d49b8e135e696a89b76d097.tar.gz
rails-0235cdf5ce0e92b88d49b8e135e696a89b76d097.tar.bz2
rails-0235cdf5ce0e92b88d49b8e135e696a89b76d097.zip
Merge pull request #10152 from Noemj/statement_cache
Statement cache Conflicts: activerecord/CHANGELOG.md
Diffstat (limited to 'activerecord/test/cases/statement_cache_test.rb')
-rw-r--r--activerecord/test/cases/statement_cache_test.rb64
1 files changed, 64 insertions, 0 deletions
diff --git a/activerecord/test/cases/statement_cache_test.rb b/activerecord/test/cases/statement_cache_test.rb
new file mode 100644
index 0000000000..a8c97cccd4
--- /dev/null
+++ b/activerecord/test/cases/statement_cache_test.rb
@@ -0,0 +1,64 @@
+require 'cases/helper'
+require 'models/book'
+require 'models/liquid'
+require 'models/molecule'
+require 'models/electron'
+
+module ActiveRecord
+ class StatementCacheTest < ActiveRecord::TestCase
+ def setup
+ @connection = ActiveRecord::Base.connection
+ end
+
+ def test_statement_cache_with_simple_statement
+ cache = ActiveRecord::StatementCache.new do
+ Book.where(name: "my book").where("author_id > 3")
+ end
+
+ Book.create(name: "my book", author_id: 4)
+
+ books = cache.execute
+ assert_equal "my book", books[0].name
+ end
+
+ def test_statement_cache_with_nil_statement_raises_error
+ assert_raise(ArgumentError) do
+ cache = ActiveRecord::StatementCache.new do
+ nil
+ end
+ end
+ end
+
+ def test_statement_cache_with_complex_statement
+ cache = ActiveRecord::StatementCache.new do
+ Liquid.joins(:molecules => :electrons).where('molecules.name' => 'dioxane', 'electrons.name' => 'lepton')
+ end
+
+ salty = Liquid.create(name: 'salty')
+ molecule = salty.molecules.create(name: 'dioxane')
+ electron = molecule.electrons.create(name: 'lepton')
+
+ liquids = cache.execute
+ assert_equal "salty", liquids[0].name
+ end
+
+ def test_statement_cache_values_differ
+ cache = ActiveRecord::StatementCache.new do
+ Book.where(name: "my book")
+ end
+
+ for i in 0..2 do
+ Book.create(name: "my book")
+ end
+
+ first_books = cache.execute
+
+ for i in 0..2 do
+ Book.create(name: "my book")
+ end
+
+ additional_books = cache.execute
+ assert first_books != additional_books
+ end
+ end
+end