|
| 1 | +require 'test_helper' |
| 2 | +module ActiveModel |
| 3 | + class Serializer |
| 4 | + module Adapter |
| 5 | + class CachedSerializerTest < ActiveSupport::TestCase |
| 6 | + def test_cached_false_without_cache_store |
| 7 | + cached_serializer = build do |serializer| |
| 8 | + serializer._cache = nil |
| 9 | + end |
| 10 | + refute cached_serializer.cached? |
| 11 | + end |
| 12 | + |
| 13 | + def test_cached_true_with_cache_store_and_without_cache_only_and_cache_except |
| 14 | + cached_serializer = build do |serializer| |
| 15 | + serializer._cache = Object |
| 16 | + end |
| 17 | + assert cached_serializer.cached? |
| 18 | + end |
| 19 | + |
| 20 | + def test_cached_false_with_cache_store_and_with_cache_only |
| 21 | + cached_serializer = build do |serializer| |
| 22 | + serializer._cache = Object |
| 23 | + serializer._cache_only = [:name] |
| 24 | + end |
| 25 | + refute cached_serializer.cached? |
| 26 | + end |
| 27 | + |
| 28 | + def test_cached_false_with_cache_store_and_with_cache_except |
| 29 | + cached_serializer = build do |serializer| |
| 30 | + serializer._cache = Object |
| 31 | + serializer._cache_except = [:content] |
| 32 | + end |
| 33 | + refute cached_serializer.cached? |
| 34 | + end |
| 35 | + |
| 36 | + def test_fragment_cached_false_without_cache_store |
| 37 | + cached_serializer = build do |serializer| |
| 38 | + serializer._cache = nil |
| 39 | + serializer._cache_only = [:name] |
| 40 | + end |
| 41 | + refute cached_serializer.fragment_cached? |
| 42 | + end |
| 43 | + |
| 44 | + def test_fragment_cached_true_with_cache_store_and_cache_only |
| 45 | + cached_serializer = build do |serializer| |
| 46 | + serializer._cache = Object |
| 47 | + serializer._cache_only = [:name] |
| 48 | + end |
| 49 | + assert cached_serializer.fragment_cached? |
| 50 | + end |
| 51 | + |
| 52 | + def test_fragment_cached_true_with_cache_store_and_cache_except |
| 53 | + cached_serializer = build do |serializer| |
| 54 | + serializer._cache = Object |
| 55 | + serializer._cache_except = [:content] |
| 56 | + end |
| 57 | + assert cached_serializer.fragment_cached? |
| 58 | + end |
| 59 | + |
| 60 | + def test_fragment_cached_false_with_cache_store_and_cache_except_and_cache_only |
| 61 | + cached_serializer = build do |serializer| |
| 62 | + serializer._cache = Object |
| 63 | + serializer._cache_except = [:content] |
| 64 | + serializer._cache_only = [:name] |
| 65 | + end |
| 66 | + refute cached_serializer.fragment_cached? |
| 67 | + end |
| 68 | + |
| 69 | + private |
| 70 | + |
| 71 | + def build |
| 72 | + serializer = Class.new(ActiveModel::Serializer) |
| 73 | + serializer._cache_key = nil |
| 74 | + serializer._cache_options = nil |
| 75 | + yield serializer if block_given? |
| 76 | + serializer_instance = serializer.new(Object) |
| 77 | + ActiveModel::Serializer::Adapter::CachedSerializer.new(serializer_instance) |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | + end |
| 82 | +end |
0 commit comments