2006-08-03 19:59:38 -04:00
|
|
|
require 'fileutils'
|
2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2015-02-15 16:39:04 -05:00
|
|
|
require 'lib/controller/fake_models'
|
2006-08-03 19:59:38 -04:00
|
|
|
|
|
|
|
CACHE_DIR = 'test_cache'
|
2007-09-28 10:18:47 -04:00
|
|
|
# Don't change '/../temp/' cavalierly or you might hose something you don't want hosed
|
2006-08-03 19:59:38 -04:00
|
|
|
FILE_STORE_PATH = File.join(File.dirname(__FILE__), '/../temp/', CACHE_DIR)
|
2012-09-28 00:10:53 -04:00
|
|
|
|
2012-10-03 12:14:28 -04:00
|
|
|
class FragmentCachingMetalTestController < ActionController::Metal
|
2012-09-28 00:10:53 -04:00
|
|
|
abstract!
|
|
|
|
|
|
|
|
include ActionController::Caching
|
|
|
|
|
2012-10-03 12:14:28 -04:00
|
|
|
def some_action; end
|
2012-09-28 00:10:53 -04:00
|
|
|
end
|
|
|
|
|
2012-10-03 12:14:28 -04:00
|
|
|
class FragmentCachingMetalTest < ActionController::TestCase
|
2012-09-28 00:10:53 -04:00
|
|
|
def setup
|
2012-10-03 12:14:28 -04:00
|
|
|
super
|
|
|
|
@store = ActiveSupport::Cache::MemoryStore.new
|
|
|
|
@controller = FragmentCachingMetalTestController.new
|
|
|
|
@controller.perform_caching = true
|
|
|
|
@controller.cache_store = @store
|
2013-09-12 15:52:53 -04:00
|
|
|
@params = { controller: 'posts', action: 'index' }
|
2012-10-03 12:14:28 -04:00
|
|
|
@controller.params = @params
|
|
|
|
@controller.request = @request
|
|
|
|
@controller.response = @response
|
2012-09-28 00:10:53 -04:00
|
|
|
end
|
|
|
|
|
2012-10-03 12:14:28 -04:00
|
|
|
def test_fragment_cache_key
|
|
|
|
assert_equal 'views/what a key', @controller.fragment_cache_key('what a key')
|
2012-09-28 00:10:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-04 19:50:57 -05:00
|
|
|
class CachingController < ActionController::Base
|
|
|
|
abstract!
|
|
|
|
|
|
|
|
self.cache_store = :file_store, FILE_STORE_PATH
|
|
|
|
end
|
|
|
|
|
|
|
|
class FragmentCachingTestController < CachingController
|
2013-09-12 15:52:53 -04:00
|
|
|
def some_action; end
|
2008-01-03 10:35:10 -05:00
|
|
|
end
|
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
class FragmentCachingTest < ActionController::TestCase
|
2008-01-03 10:35:10 -05:00
|
|
|
def setup
|
2009-04-08 20:33:06 -04:00
|
|
|
super
|
2008-01-03 16:05:12 -05:00
|
|
|
@store = ActiveSupport::Cache::MemoryStore.new
|
2008-01-03 10:35:10 -05:00
|
|
|
@controller = FragmentCachingTestController.new
|
2010-04-15 15:58:54 -04:00
|
|
|
@controller.perform_caching = true
|
2010-03-04 19:50:57 -05:00
|
|
|
@controller.cache_store = @store
|
2008-01-03 10:35:10 -05:00
|
|
|
@params = {:controller => 'posts', :action => 'index'}
|
|
|
|
@controller.params = @params
|
|
|
|
@controller.request = @request
|
|
|
|
@controller.response = @response
|
|
|
|
end
|
|
|
|
|
2008-01-11 20:19:46 -05:00
|
|
|
def test_fragment_cache_key
|
2008-01-03 16:05:12 -05:00
|
|
|
assert_equal 'views/what a key', @controller.fragment_cache_key('what a key')
|
2008-07-15 21:52:29 -04:00
|
|
|
assert_equal "views/test.host/fragment_caching_test/some_action",
|
|
|
|
@controller.fragment_cache_key(:controller => 'fragment_caching_test',:action => 'some_action')
|
2008-01-03 10:35:10 -05:00
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def test_read_fragment_with_caching_enabled
|
2008-01-03 16:05:12 -05:00
|
|
|
@store.write('views/name', 'value')
|
2008-01-03 10:35:10 -05:00
|
|
|
assert_equal 'value', @controller.read_fragment('name')
|
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def test_read_fragment_with_caching_disabled
|
2010-04-15 15:58:54 -04:00
|
|
|
@controller.perform_caching = false
|
2008-01-03 16:05:12 -05:00
|
|
|
@store.write('views/name', 'value')
|
2008-01-03 10:35:10 -05:00
|
|
|
assert_nil @controller.read_fragment('name')
|
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def test_fragment_exist_with_caching_enabled
|
2008-05-16 13:10:30 -04:00
|
|
|
@store.write('views/name', 'value')
|
|
|
|
assert @controller.fragment_exist?('name')
|
|
|
|
assert !@controller.fragment_exist?('other_name')
|
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def test_fragment_exist_with_caching_disabled
|
2010-04-15 15:58:54 -04:00
|
|
|
@controller.perform_caching = false
|
2008-05-16 13:10:30 -04:00
|
|
|
@store.write('views/name', 'value')
|
|
|
|
assert !@controller.fragment_exist?('name')
|
|
|
|
assert !@controller.fragment_exist?('other_name')
|
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def test_write_fragment_with_caching_enabled
|
2008-01-03 16:05:12 -05:00
|
|
|
assert_nil @store.read('views/name')
|
2008-01-03 10:35:10 -05:00
|
|
|
assert_equal 'value', @controller.write_fragment('name', 'value')
|
2008-01-03 16:05:12 -05:00
|
|
|
assert_equal 'value', @store.read('views/name')
|
2008-01-03 10:35:10 -05:00
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def test_write_fragment_with_caching_disabled
|
2008-01-03 16:05:12 -05:00
|
|
|
assert_nil @store.read('views/name')
|
2010-04-15 15:58:54 -04:00
|
|
|
@controller.perform_caching = false
|
2008-11-22 14:19:11 -05:00
|
|
|
assert_equal 'value', @controller.write_fragment('name', 'value')
|
2008-01-03 16:05:12 -05:00
|
|
|
assert_nil @store.read('views/name')
|
2008-01-03 10:35:10 -05:00
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def test_expire_fragment_with_simple_key
|
2008-01-03 16:05:12 -05:00
|
|
|
@store.write('views/name', 'value')
|
2008-01-03 10:35:10 -05:00
|
|
|
@controller.expire_fragment 'name'
|
2008-01-03 16:05:12 -05:00
|
|
|
assert_nil @store.read('views/name')
|
2008-01-03 10:35:10 -05:00
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def test_expire_fragment_with_regexp
|
2008-01-03 16:05:12 -05:00
|
|
|
@store.write('views/name', 'value')
|
|
|
|
@store.write('views/another_name', 'another_value')
|
|
|
|
@store.write('views/primalgrasp', 'will not expire ;-)')
|
2008-01-03 10:35:10 -05:00
|
|
|
|
2010-09-18 17:14:46 -04:00
|
|
|
@controller.expire_fragment(/name/)
|
2008-01-03 10:35:10 -05:00
|
|
|
|
2008-01-03 16:05:12 -05:00
|
|
|
assert_nil @store.read('views/name')
|
|
|
|
assert_nil @store.read('views/another_name')
|
|
|
|
assert_equal 'will not expire ;-)', @store.read('views/primalgrasp')
|
2008-01-03 10:35:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_fragment_for
|
2008-01-03 16:05:12 -05:00
|
|
|
@store.write('views/expensive', 'fragment content')
|
2008-01-03 10:35:10 -05:00
|
|
|
fragment_computed = false
|
|
|
|
|
2010-03-18 18:52:43 -04:00
|
|
|
view_context = @controller.view_context
|
|
|
|
|
For performance reasons, you can no longer call html_safe! on Strings. Instead, all Strings are always not html_safe?. Instead, you can get a SafeBuffer from a String by calling #html_safe, which will SafeBuffer.new(self).
* Additionally, instead of doing concat("</form>".html_safe), you can do
safe_concat("</form>"), which will skip both the flag set, and the flag
check.
* For the first pass, I converted virtually all #html_safe!s to #html_safe,
and the tests pass. A further optimization would be to try to use
#safe_concat as much as possible, reducing the performance impact if
we know up front that a String is safe.
2010-01-31 22:17:42 -05:00
|
|
|
buffer = 'generated till now -> '.html_safe
|
2010-03-18 18:52:43 -04:00
|
|
|
buffer << view_context.send(:fragment_for, 'expensive') { fragment_computed = true }
|
2008-01-03 10:35:10 -05:00
|
|
|
|
|
|
|
assert !fragment_computed
|
|
|
|
assert_equal 'generated till now -> fragment content', buffer
|
|
|
|
end
|
2009-08-08 11:46:44 -04:00
|
|
|
|
2010-03-14 21:55:13 -04:00
|
|
|
def test_html_safety
|
|
|
|
assert_nil @store.read('views/name')
|
|
|
|
content = 'value'.html_safe
|
|
|
|
assert_equal content, @controller.write_fragment('name', content)
|
|
|
|
|
|
|
|
cached = @store.read('views/name')
|
|
|
|
assert_equal content, cached
|
|
|
|
assert_equal String, cached.class
|
|
|
|
|
|
|
|
html_safe = @controller.read_fragment('name')
|
|
|
|
assert_equal content, html_safe
|
|
|
|
assert html_safe.html_safe?
|
|
|
|
end
|
2008-01-03 10:35:10 -05:00
|
|
|
end
|
2008-01-11 20:19:46 -05:00
|
|
|
|
2010-03-04 19:50:57 -05:00
|
|
|
class FunctionalCachingController < CachingController
|
2008-01-11 20:19:46 -05:00
|
|
|
def fragment_cached
|
|
|
|
end
|
|
|
|
|
|
|
|
def html_fragment_cached_with_partial
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def formatted_fragment_cached
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.xml
|
|
|
|
end
|
|
|
|
end
|
2012-11-25 23:10:44 -05:00
|
|
|
|
2014-03-08 17:09:54 -05:00
|
|
|
def formatted_fragment_cached_with_variant
|
2015-07-08 19:15:50 -04:00
|
|
|
request.variant = :phone if params[:v] == "phone"
|
|
|
|
|
2014-03-08 17:09:54 -05:00
|
|
|
respond_to do |format|
|
|
|
|
format.html.phone
|
|
|
|
format.html
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-25 23:10:44 -05:00
|
|
|
def fragment_cached_without_digest
|
|
|
|
end
|
2008-01-11 20:19:46 -05:00
|
|
|
end
|
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
class FunctionalFragmentCachingTest < ActionController::TestCase
|
2008-01-11 20:19:46 -05:00
|
|
|
def setup
|
2009-04-08 20:33:06 -04:00
|
|
|
super
|
2008-01-11 20:19:46 -05:00
|
|
|
@store = ActiveSupport::Cache::MemoryStore.new
|
|
|
|
@controller = FunctionalCachingController.new
|
2010-04-15 15:58:54 -04:00
|
|
|
@controller.perform_caching = true
|
2010-03-04 19:50:57 -05:00
|
|
|
@controller.cache_store = @store
|
2008-01-11 20:19:46 -05:00
|
|
|
end
|
2008-06-25 16:24:12 -04:00
|
|
|
|
2008-01-11 20:19:46 -05:00
|
|
|
def test_fragment_caching
|
|
|
|
get :fragment_cached
|
|
|
|
assert_response :success
|
|
|
|
expected_body = <<-CACHED
|
|
|
|
Hello
|
|
|
|
This bit's fragment cached
|
2011-07-23 16:50:34 -04:00
|
|
|
Ciao
|
2008-01-11 20:19:46 -05:00
|
|
|
CACHED
|
|
|
|
assert_equal expected_body, @response.body
|
2008-05-14 13:33:54 -04:00
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
assert_equal "This bit's fragment cached",
|
2014-03-21 12:37:23 -04:00
|
|
|
@store.read("views/test.host/functional_caching/fragment_cached/#{template_digest("functional_caching/fragment_cached")}")
|
2008-01-11 20:19:46 -05:00
|
|
|
end
|
2008-05-14 13:33:54 -04:00
|
|
|
|
2008-01-11 20:19:46 -05:00
|
|
|
def test_fragment_caching_in_partials
|
|
|
|
get :html_fragment_cached_with_partial
|
|
|
|
assert_response :success
|
2010-09-18 17:14:46 -04:00
|
|
|
assert_match(/Old fragment caching in a partial/, @response.body)
|
2012-09-28 00:10:53 -04:00
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
assert_match("Old fragment caching in a partial",
|
2014-03-21 12:37:23 -04:00
|
|
|
@store.read("views/test.host/functional_caching/html_fragment_cached_with_partial/#{template_digest("functional_caching/_partial")}"))
|
2008-01-11 20:19:46 -05:00
|
|
|
end
|
2008-05-14 13:33:54 -04:00
|
|
|
|
2012-11-25 23:10:44 -05:00
|
|
|
def test_skipping_fragment_cache_digesting
|
2015-01-04 04:35:06 -05:00
|
|
|
get :fragment_cached_without_digest, format: "html"
|
2012-11-25 23:10:44 -05:00
|
|
|
assert_response :success
|
|
|
|
expected_body = "<body>\n<p>ERB</p>\n</body>\n"
|
|
|
|
|
|
|
|
assert_equal expected_body, @response.body
|
|
|
|
assert_equal "<p>ERB</p>", @store.read("views/nodigest")
|
|
|
|
end
|
|
|
|
|
2008-07-13 14:26:48 -04:00
|
|
|
def test_render_inline_before_fragment_caching
|
|
|
|
get :inline_fragment_cached
|
|
|
|
assert_response :success
|
2010-09-18 17:14:46 -04:00
|
|
|
assert_match(/Some inline content/, @response.body)
|
|
|
|
assert_match(/Some cached content/, @response.body)
|
2012-09-28 00:10:53 -04:00
|
|
|
assert_match("Some cached content",
|
2014-03-21 12:37:23 -04:00
|
|
|
@store.read("views/test.host/functional_caching/inline_fragment_cached/#{template_digest("functional_caching/inline_fragment_cached")}"))
|
2008-07-13 14:26:48 -04:00
|
|
|
end
|
|
|
|
|
2014-02-21 05:28:51 -05:00
|
|
|
def test_fragment_cache_instrumentation
|
|
|
|
payload = nil
|
|
|
|
|
|
|
|
subscriber = proc do |*args|
|
|
|
|
event = ActiveSupport::Notifications::Event.new(*args)
|
|
|
|
payload = event.payload
|
|
|
|
end
|
|
|
|
|
|
|
|
ActiveSupport::Notifications.subscribed(subscriber, "read_fragment.action_controller") do
|
|
|
|
get :inline_fragment_cached
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "functional_caching", payload[:controller]
|
|
|
|
assert_equal "inline_fragment_cached", payload[:action]
|
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def test_html_formatted_fragment_caching
|
2015-01-04 04:35:06 -05:00
|
|
|
get :formatted_fragment_cached, format: "html"
|
2008-07-15 21:52:29 -04:00
|
|
|
assert_response :success
|
2010-03-16 02:48:46 -04:00
|
|
|
expected_body = "<body>\n<p>ERB</p>\n</body>\n"
|
2008-07-15 21:52:29 -04:00
|
|
|
|
|
|
|
assert_equal expected_body, @response.body
|
|
|
|
|
2012-09-28 00:10:53 -04:00
|
|
|
assert_equal "<p>ERB</p>",
|
2014-03-21 12:37:23 -04:00
|
|
|
@store.read("views/test.host/functional_caching/formatted_fragment_cached/#{template_digest("functional_caching/formatted_fragment_cached")}")
|
2008-07-15 21:52:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_xml_formatted_fragment_caching
|
2015-01-04 04:35:06 -05:00
|
|
|
get :formatted_fragment_cached, format: "xml"
|
2008-07-15 21:52:29 -04:00
|
|
|
assert_response :success
|
|
|
|
expected_body = "<body>\n <p>Builder</p>\n</body>\n"
|
|
|
|
|
|
|
|
assert_equal expected_body, @response.body
|
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
assert_equal " <p>Builder</p>\n",
|
2014-03-21 12:37:23 -04:00
|
|
|
@store.read("views/test.host/functional_caching/formatted_fragment_cached/#{template_digest("functional_caching/formatted_fragment_cached")}")
|
2008-07-15 21:52:29 -04:00
|
|
|
end
|
2012-09-28 00:10:53 -04:00
|
|
|
|
2014-03-08 17:09:54 -05:00
|
|
|
|
|
|
|
def test_fragment_caching_with_variant
|
2015-07-08 19:15:50 -04:00
|
|
|
get :formatted_fragment_cached_with_variant, format: "html", params: { v: :phone }
|
2014-03-08 17:09:54 -05:00
|
|
|
assert_response :success
|
|
|
|
expected_body = "<body>\n<p>PHONE</p>\n</body>\n"
|
|
|
|
|
|
|
|
assert_equal expected_body, @response.body
|
|
|
|
|
|
|
|
assert_equal "<p>PHONE</p>",
|
2014-03-21 12:37:23 -04:00
|
|
|
@store.read("views/test.host/functional_caching/formatted_fragment_cached_with_variant/#{template_digest("functional_caching/formatted_fragment_cached_with_variant")}")
|
2014-03-08 17:09:54 -05:00
|
|
|
end
|
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
private
|
2014-03-21 12:37:23 -04:00
|
|
|
def template_digest(name)
|
|
|
|
ActionView::Digestor.digest(name: name, finder: @controller.lookup_context)
|
2012-08-29 15:23:15 -04:00
|
|
|
end
|
2008-01-11 20:19:46 -05:00
|
|
|
end
|
2011-07-23 16:50:34 -04:00
|
|
|
|
|
|
|
class CacheHelperOutputBufferTest < ActionController::TestCase
|
|
|
|
|
|
|
|
class MockController
|
|
|
|
def read_fragment(name, options)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_fragment(name, fragment, options)
|
|
|
|
fragment
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_output_buffer
|
|
|
|
output_buffer = ActionView::OutputBuffer.new
|
|
|
|
controller = MockController.new
|
2015-09-05 10:58:21 -04:00
|
|
|
cache_helper = Class.new do
|
|
|
|
def self.controller; end;
|
|
|
|
def self.output_buffer; end;
|
|
|
|
def self.output_buffer=; end;
|
|
|
|
end
|
2011-07-23 16:50:34 -04:00
|
|
|
cache_helper.extend(ActionView::Helpers::CacheHelper)
|
|
|
|
|
2015-09-05 10:58:21 -04:00
|
|
|
cache_helper.stub :controller, controller do
|
|
|
|
cache_helper.stub :output_buffer, output_buffer do
|
|
|
|
assert_called_with cache_helper, :output_buffer=, [output_buffer.class.new(output_buffer)] do
|
|
|
|
assert_nothing_raised do
|
|
|
|
cache_helper.send :fragment_for, 'Test fragment name', 'Test fragment', &Proc.new{ nil }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-07-23 16:50:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_safe_buffer
|
|
|
|
output_buffer = ActiveSupport::SafeBuffer.new
|
|
|
|
controller = MockController.new
|
2015-09-05 10:58:21 -04:00
|
|
|
cache_helper = Class.new do
|
|
|
|
def self.controller; end;
|
|
|
|
def self.output_buffer; end;
|
|
|
|
def self.output_buffer=; end;
|
|
|
|
end
|
2011-07-23 16:50:34 -04:00
|
|
|
cache_helper.extend(ActionView::Helpers::CacheHelper)
|
|
|
|
|
2015-09-05 10:58:21 -04:00
|
|
|
cache_helper.stub :controller, controller do
|
|
|
|
cache_helper.stub :output_buffer, output_buffer do
|
|
|
|
assert_called_with cache_helper, :output_buffer=, [output_buffer.class.new(output_buffer)] do
|
|
|
|
assert_nothing_raised do
|
|
|
|
cache_helper.send :fragment_for, 'Test fragment name', 'Test fragment', &Proc.new{ nil }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-07-23 16:50:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-10-03 16:35:19 -04:00
|
|
|
|
2013-01-08 13:20:47 -05:00
|
|
|
class ViewCacheDependencyTest < ActionController::TestCase
|
|
|
|
class NoDependenciesController < ActionController::Base
|
|
|
|
end
|
|
|
|
|
|
|
|
class HasDependenciesController < ActionController::Base
|
|
|
|
view_cache_dependency { "trombone" }
|
|
|
|
view_cache_dependency { "flute" }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_view_cache_dependencies_are_empty_by_default
|
2013-01-08 14:02:11 -05:00
|
|
|
assert NoDependenciesController.new.view_cache_dependencies.empty?
|
2013-01-08 13:20:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_view_cache_dependencies_are_listed_in_declaration_order
|
2013-01-08 14:02:11 -05:00
|
|
|
assert_equal %w(trombone flute), HasDependenciesController.new.view_cache_dependencies
|
2013-01-08 13:20:47 -05:00
|
|
|
end
|
|
|
|
end
|
2015-02-15 16:39:04 -05:00
|
|
|
|
|
|
|
class CollectionCacheController < ActionController::Base
|
2015-05-24 16:02:22 -04:00
|
|
|
attr_accessor :partial_rendered_times
|
|
|
|
|
2015-02-15 16:39:04 -05:00
|
|
|
def index
|
|
|
|
@customers = [Customer.new('david', params[:id] || 1)]
|
|
|
|
end
|
|
|
|
|
|
|
|
def index_ordered
|
|
|
|
@customers = [Customer.new('david', 1), Customer.new('david', 2), Customer.new('david', 3)]
|
|
|
|
render 'index'
|
|
|
|
end
|
|
|
|
|
|
|
|
def index_explicit_render
|
|
|
|
@customers = [Customer.new('david', 1)]
|
|
|
|
render partial: 'customers/customer', collection: @customers
|
|
|
|
end
|
|
|
|
|
|
|
|
def index_with_comment
|
|
|
|
@customers = [Customer.new('david', 1)]
|
|
|
|
render partial: 'customers/commented_customer', collection: @customers, as: :customer
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class AutomaticCollectionCacheTest < ActionController::TestCase
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
@controller = CollectionCacheController.new
|
|
|
|
@controller.perform_caching = true
|
2015-05-24 16:02:22 -04:00
|
|
|
@controller.partial_rendered_times = 0
|
2015-06-15 16:04:43 -04:00
|
|
|
@controller.cache_store = ActiveSupport::Cache::MemoryStore.new
|
2015-07-05 07:53:20 -04:00
|
|
|
ActionView::PartialRenderer.collection_cache = @controller.cache_store
|
2015-02-15 16:39:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_collection_fetches_cached_views
|
|
|
|
get :index
|
2015-05-24 16:02:22 -04:00
|
|
|
assert_equal 1, @controller.partial_rendered_times
|
2015-02-15 16:39:04 -05:00
|
|
|
|
|
|
|
get :index
|
2015-05-24 16:02:22 -04:00
|
|
|
assert_equal 1, @controller.partial_rendered_times
|
2015-02-15 16:39:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_preserves_order_when_reading_from_cache_plus_rendering
|
|
|
|
get :index, params: { id: 2 }
|
|
|
|
get :index_ordered
|
|
|
|
|
|
|
|
assert_select ':root', "david, 1\n david, 2\n david, 3"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_explicit_render_call_with_options
|
|
|
|
get :index_explicit_render
|
|
|
|
|
|
|
|
assert_select ':root', "david, 1"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_caching_works_with_beginning_comment
|
|
|
|
get :index_with_comment
|
2015-05-24 16:02:22 -04:00
|
|
|
assert_equal 1, @controller.partial_rendered_times
|
2015-02-15 16:39:04 -05:00
|
|
|
|
|
|
|
get :index_with_comment
|
2015-05-24 16:02:22 -04:00
|
|
|
assert_equal 1, @controller.partial_rendered_times
|
2015-02-15 16:39:04 -05:00
|
|
|
end
|
|
|
|
end
|
2015-12-14 20:53:43 -05:00
|
|
|
|
|
|
|
class FragmentCacheKeyTestController < CachingController
|
|
|
|
attr_accessor :account_id
|
|
|
|
|
|
|
|
fragment_cache_key "v1"
|
|
|
|
fragment_cache_key { account_id }
|
|
|
|
end
|
|
|
|
|
|
|
|
class FragmentCacheKeyTest < ActionController::TestCase
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
@store = ActiveSupport::Cache::MemoryStore.new
|
|
|
|
@controller = FragmentCacheKeyTestController.new
|
|
|
|
@controller.perform_caching = true
|
|
|
|
@controller.cache_store = @store
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fragment_cache_key
|
|
|
|
@controller.account_id = "123"
|
|
|
|
assert_equal 'views/v1/123/what a key', @controller.fragment_cache_key('what a key')
|
|
|
|
|
|
|
|
@controller.account_id = nil
|
|
|
|
assert_equal 'views/v1//what a key', @controller.fragment_cache_key('what a key')
|
|
|
|
end
|
|
|
|
end
|