File: cache_test.rb

package info (click to toggle)
ruby-i18n 1.14.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 764 kB
  • sloc: ruby: 6,560; makefile: 5
file content (119 lines) | stat: -rw-r--r-- 3,627 bytes parent folder | download | duplicates (2)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
require 'test_helper'
require 'openssl'

begin
  require 'active_support'
rescue LoadError
  $stderr.puts "Skipping cache tests using ActiveSupport"
else

class I18nBackendCacheTest < I18n::TestCase
  class Backend < I18n::Backend::Simple
    include I18n::Backend::Cache
  end

  def setup
    I18n.backend = Backend.new
    super
    I18n.cache_store = ActiveSupport::Cache.lookup_store(:memory_store)
    I18n.cache_store.clear
    I18n.cache_key_digest = nil
  end

  def teardown
    super
    I18n.cache_store.clear
    I18n.cache_store = nil
  end

  test "it uses the cache" do
    assert I18n.cache_store.is_a?(ActiveSupport::Cache::MemoryStore)
  end

  test "translate hits the backend and caches the response" do
    I18n.backend.expects(:lookup).returns('Foo')
    assert_equal 'Foo', I18n.t(:foo)

    I18n.backend.expects(:lookup).never
    assert_equal 'Foo', I18n.t(:foo)

    I18n.backend.expects(:lookup).returns('Bar')
    assert_equal 'Bar', I18n.t(:bar)
  end

  test "translate returns a cached false response" do
    I18n.backend.expects(:lookup).never
    I18n.cache_store.expects(:read).returns(false)
    assert_equal false, I18n.t(:foo)
  end

  test "still raises MissingTranslationData but also caches it" do
    assert_raises(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) }
    assert_raises(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) }
    assert_equal 1, I18n.cache_store.instance_variable_get(:@data).size

    # I18n.backend.expects(:lookup).returns(nil)
    # assert_raises(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) }
    # I18n.backend.expects(:lookup).never
    # assert_raises(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) }
  end

  test "MissingTranslationData does not cache custom options" do
    I18n.t(:missing, :scope => :foo, :extra => true)
    assert_equal 1, I18n.cache_store.instance_variable_get(:@data).size

    value = I18n.cache_store.read(I18n.cache_store.instance_variable_get(:@data).keys.first)

    assert_equal({ scope: :foo }, value.options)
  end

  test "uses 'i18n' as a cache key namespace by default" do
    assert_equal 0, I18n.backend.send(:cache_key, :en, :foo, {}).index('i18n')
  end

  test "adds a custom cache key namespace" do
    with_cache_namespace('bar') do
      assert_equal 0, I18n.backend.send(:cache_key, :en, :foo, {}).index('i18n/bar/')
    end
  end

  test "adds locale and hash of key and hash of options" do
    options = { :bar => 1 }
    assert_equal "i18n//en/#{:foo.to_s.hash}/#{options.to_s.hash}", I18n.backend.send(:cache_key, :en, :foo, options)
  end

  test "cache_key uses configured digest method" do
    digest = OpenSSL::Digest::SHA256.new
    options = { :bar => 1 }
    options_hash = options.inspect
    with_cache_key_digest(digest) do
      assert_equal "i18n//en/#{digest.hexdigest(:foo.to_s)}/#{digest.hexdigest(options_hash)}", I18n.backend.send(:cache_key, :en, :foo, options)
    end
  end

  test "keys should not be equal" do
    interpolation_values1 = { :foo => 1, :bar => 2 }
    interpolation_values2 = { :foo => 2, :bar => 1 }

    key1 = I18n.backend.send(:cache_key, :en, :some_key, interpolation_values1)
    key2 = I18n.backend.send(:cache_key, :en, :some_key, interpolation_values2)

    assert key1 != key2
  end

  protected

    def with_cache_namespace(namespace)
      I18n.cache_namespace = namespace
      yield
      I18n.cache_namespace = nil
    end

    def with_cache_key_digest(digest)
      I18n.cache_key_digest = digest
      yield
      I18n.cache_key_digest = nil
    end
end

end # AS cache check