|
| 1 | +# Execute this test in isolation |
| 2 | +require 'support/isolated_unit' |
| 3 | + |
| 4 | +class CachingConfigurationTest < ActiveSupport::TestCase |
| 5 | + include ActiveSupport::Testing::Isolation |
| 6 | + |
| 7 | + setup do |
| 8 | + require 'rails' |
| 9 | + # AMS needs to be required before Rails.application is initialized for |
| 10 | + # Railtie's to fire in Rails.application.initialize! |
| 11 | + # (and make_basic_app initializes the app) |
| 12 | + require 'active_model_serializers' |
| 13 | + # Create serializers before Rails.application.initialize! |
| 14 | + # To ensure we're testing that the cache settings depend on |
| 15 | + # the Railtie firing, not on the ActionController being loaded. |
| 16 | + create_serializers |
| 17 | + end |
| 18 | + |
| 19 | + def create_serializers |
| 20 | + @cached_serializer = Class.new(ActiveModel::Serializer) do |
| 21 | + cache skip_digest: true |
| 22 | + attributes :id, :name, :title |
| 23 | + end |
| 24 | + @fragment_cached_serializer = Class.new(ActiveModel::Serializer) do |
| 25 | + cache only: :id |
| 26 | + attributes :id, :name, :title |
| 27 | + end |
| 28 | + @non_cached_serializer = Class.new(ActiveModel::Serializer) do |
| 29 | + attributes :id, :name, :title |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + class PerformCachingTrue < CachingConfigurationTest |
| 34 | + setup do |
| 35 | + # Let's make that Rails app and initialize it! |
| 36 | + make_basic_app do |app| |
| 37 | + app.config.action_controller.perform_caching = true |
| 38 | + app.config.action_controller.cache_store = ActiveSupport::Cache.lookup_store(:memory_store) |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + test 'it sets perform_caching to true on AMS.config and serializers' do |
| 43 | + assert Rails.configuration.action_controller.perform_caching |
| 44 | + assert ActiveModelSerializers.config.perform_caching |
| 45 | + assert ActiveModel::Serializer.perform_caching? |
| 46 | + assert @cached_serializer.perform_caching? |
| 47 | + assert @non_cached_serializer.perform_caching? |
| 48 | + assert @fragment_cached_serializer.perform_caching? |
| 49 | + end |
| 50 | + |
| 51 | + test 'it sets the AMS.config.cache_store to the controller cache_store' do |
| 52 | + assert_equal controller_cache_store, ActiveSupport::Cache::MemoryStore |
| 53 | + assert_equal controller_cache_store, ActiveModelSerializers.config.cache_store.class |
| 54 | + end |
| 55 | + |
| 56 | + test 'it sets the cached serializer cache_store to the ActionController::Base.cache_store' do |
| 57 | + assert_equal ActiveSupport::Cache::NullStore, @cached_serializer._cache.class |
| 58 | + assert_equal controller_cache_store, @cached_serializer.cache_store.class |
| 59 | + assert_equal ActiveSupport::Cache::MemoryStore, @cached_serializer._cache.class |
| 60 | + end |
| 61 | + |
| 62 | + test 'the cached serializer has cache_enabled?' do |
| 63 | + assert @cached_serializer.cache_enabled? |
| 64 | + end |
| 65 | + |
| 66 | + test 'the cached serializer does not have fragment_cache_enabled?' do |
| 67 | + refute @cached_serializer.fragment_cache_enabled? |
| 68 | + end |
| 69 | + |
| 70 | + test 'the non-cached serializer cache_store is nil' do |
| 71 | + assert_equal nil, @non_cached_serializer._cache |
| 72 | + assert_equal nil, @non_cached_serializer.cache_store |
| 73 | + assert_equal nil, @non_cached_serializer._cache |
| 74 | + end |
| 75 | + |
| 76 | + test 'the non-cached serializer does not have cache_enabled?' do |
| 77 | + refute @non_cached_serializer.cache_enabled? |
| 78 | + end |
| 79 | + |
| 80 | + test 'the non-cached serializer does not have fragment_cache_enabled?' do |
| 81 | + refute @non_cached_serializer.fragment_cache_enabled? |
| 82 | + end |
| 83 | + |
| 84 | + test 'it sets the fragment cached serializer cache_store to the ActionController::Base.cache_store' do |
| 85 | + assert_equal ActiveSupport::Cache::NullStore, @fragment_cached_serializer._cache.class |
| 86 | + assert_equal controller_cache_store, @fragment_cached_serializer.cache_store.class |
| 87 | + assert_equal ActiveSupport::Cache::MemoryStore, @fragment_cached_serializer._cache.class |
| 88 | + end |
| 89 | + |
| 90 | + test 'the fragment cached serializer does not have cache_enabled?' do |
| 91 | + refute @fragment_cached_serializer.cache_enabled? |
| 92 | + end |
| 93 | + |
| 94 | + test 'the fragment cached serializer has fragment_cache_enabled?' do |
| 95 | + assert @fragment_cached_serializer.fragment_cache_enabled? |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + class PerformCachingFalse < CachingConfigurationTest |
| 100 | + setup do |
| 101 | + # Let's make that Rails app and initialize it! |
| 102 | + make_basic_app do |app| |
| 103 | + app.config.action_controller.perform_caching = false |
| 104 | + app.config.action_controller.cache_store = ActiveSupport::Cache.lookup_store(:memory_store) |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + test 'it sets perform_caching to false on AMS.config and serializers' do |
| 109 | + refute Rails.configuration.action_controller.perform_caching |
| 110 | + refute ActiveModelSerializers.config.perform_caching |
| 111 | + refute ActiveModel::Serializer.perform_caching? |
| 112 | + refute @cached_serializer.perform_caching? |
| 113 | + refute @non_cached_serializer.perform_caching? |
| 114 | + refute @fragment_cached_serializer.perform_caching? |
| 115 | + end |
| 116 | + |
| 117 | + test 'it sets the AMS.config.cache_store to the controller cache_store' do |
| 118 | + assert_equal controller_cache_store, ActiveSupport::Cache::MemoryStore |
| 119 | + assert_equal controller_cache_store, ActiveModelSerializers.config.cache_store.class |
| 120 | + end |
| 121 | + |
| 122 | + test 'it sets the cached serializer cache_store to the ActionController::Base.cache_store' do |
| 123 | + assert_equal ActiveSupport::Cache::NullStore, @cached_serializer._cache.class |
| 124 | + assert_equal controller_cache_store, @cached_serializer.cache_store.class |
| 125 | + assert_equal ActiveSupport::Cache::MemoryStore, @cached_serializer._cache.class |
| 126 | + end |
| 127 | + |
| 128 | + test 'the cached serializer does not have cache_enabled?' do |
| 129 | + refute @cached_serializer.cache_enabled? |
| 130 | + end |
| 131 | + |
| 132 | + test 'the cached serializer does not have fragment_cache_enabled?' do |
| 133 | + refute @cached_serializer.fragment_cache_enabled? |
| 134 | + end |
| 135 | + |
| 136 | + test 'the non-cached serializer cache_store is nil' do |
| 137 | + assert_equal nil, @non_cached_serializer._cache |
| 138 | + assert_equal nil, @non_cached_serializer.cache_store |
| 139 | + assert_equal nil, @non_cached_serializer._cache |
| 140 | + end |
| 141 | + |
| 142 | + test 'the non-cached serializer does not have cache_enabled?' do |
| 143 | + refute @non_cached_serializer.cache_enabled? |
| 144 | + end |
| 145 | + |
| 146 | + test 'the non-cached serializer does not have fragment_cache_enabled?' do |
| 147 | + refute @non_cached_serializer.fragment_cache_enabled? |
| 148 | + end |
| 149 | + |
| 150 | + test 'it sets the fragment cached serializer cache_store to the ActionController::Base.cache_store' do |
| 151 | + assert_equal ActiveSupport::Cache::NullStore, @fragment_cached_serializer._cache.class |
| 152 | + assert_equal controller_cache_store, @fragment_cached_serializer.cache_store.class |
| 153 | + assert_equal ActiveSupport::Cache::MemoryStore, @fragment_cached_serializer._cache.class |
| 154 | + end |
| 155 | + |
| 156 | + test 'the fragment cached serializer does not have cache_enabled?' do |
| 157 | + refute @fragment_cached_serializer.cache_enabled? |
| 158 | + end |
| 159 | + |
| 160 | + test 'the fragment cached serializer does not have fragment_cache_enabled?' do |
| 161 | + refute @fragment_cached_serializer.fragment_cache_enabled? |
| 162 | + end |
| 163 | + end |
| 164 | + |
| 165 | + def controller_cache_store |
| 166 | + ActionController::Base.cache_store.class |
| 167 | + end |
| 168 | +end |
0 commit comments