File: serialization_testing.rb

package info (click to toggle)
ruby-active-model-serializers 0.10.12-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,752 kB
  • sloc: ruby: 13,138; sh: 53; makefile: 6
file content (81 lines) | stat: -rw-r--r-- 2,500 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true

module SerializationTesting
  def config
    ActiveModelSerializers.config
  end

  private

  def generate_cached_serializer(obj)
    ActiveModelSerializers::SerializableResource.new(obj).to_json
  end

  def with_namespace_separator(separator)
    original_separator = ActiveModelSerializers.config.jsonapi_namespace_separator
    ActiveModelSerializers.config.jsonapi_namespace_separator = separator
    yield
  ensure
    ActiveModelSerializers.config.jsonapi_namespace_separator = original_separator
  end

  def with_prepended_lookup(lookup_proc)
    original_lookup = ActiveModelSerializers.config.serializer_lookup_chain
    ActiveModelSerializers.config.serializer_lookup_chain.unshift lookup_proc
    yield
  ensure
    ActiveModelSerializers.config.serializer_lookup_chain = original_lookup
  end

  # Aliased as :with_configured_adapter to clarify that
  # this method tests the configured adapter.
  # When not testing configuration, it may be preferable
  # to pass in the +adapter+ option to <tt>ActiveModelSerializers::SerializableResource</tt>.
  # e.g ActiveModelSerializers::SerializableResource.new(resource, adapter: :json_api)
  def with_adapter(adapter)
    old_adapter = ActiveModelSerializers.config.adapter
    ActiveModelSerializers.config.adapter = adapter
    yield
  ensure
    ActiveModelSerializers.config.adapter = old_adapter
  end
  alias with_configured_adapter with_adapter

  def with_config(hash)
    old_config = config.dup
    ActiveModelSerializers.config.update(hash)
    yield
  ensure
    ActiveModelSerializers.config.replace(old_config)
  end

  def with_jsonapi_inflection(inflection)
    original_inflection = ActiveModelSerializers.config.jsonapi_resource_type
    ActiveModelSerializers.config.jsonapi_resource_type = inflection
    yield
  ensure
    ActiveModelSerializers.config.jsonapi_resource_type = original_inflection
  end

  def with_serializer_lookup_disabled
    original_serializer_lookup = ActiveModelSerializers.config.serializer_lookup_enabled
    ActiveModelSerializers.config.serializer_lookup_enabled = false
    yield
  ensure
    ActiveModelSerializers.config.serializer_lookup_enabled = original_serializer_lookup
  end

  def serializable(resource, options = {})
    ActiveModelSerializers::SerializableResource.new(resource, options)
  end
end

module Minitest
  class Test
    def before_setup
      ActionController::Base.cache_store.clear
    end

    include SerializationTesting
  end
end