File: caching_test.rb

package info (click to toggle)
ruby-json-schema 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 960 kB
  • sloc: ruby: 6,373; makefile: 4
file content (63 lines) | stat: -rw-r--r-- 1,164 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
require File.expand_path('support/test_helper', __dir__)

class CachingTestTest < Minitest::Test
  def setup
    @schema = Tempfile.new(['schema', '.json'])
  end

  def teardown
    @schema.close
    @schema.unlink

    JSON::Validator.clear_cache
  end

  def test_caching
    set_schema('type' => 'string')

    assert_valid(schema_path, 'foo', clear_cache: false)

    set_schema('type' => 'number')

    refute_valid(schema_path, 123)
  end

  def test_clear_cache
    set_schema('type' => 'string')

    assert_valid(schema_path, 'foo', clear_cache: true)

    set_schema('type' => 'number')

    assert_valid(schema_path, 123)
  end

  def test_cache_schemas
    suppress_warnings do
      JSON::Validator.cache_schemas = false
    end

    set_schema('type' => 'string')

    assert_valid(schema_path, 'foo', clear_cache: false)

    set_schema('type' => 'number')

    assert_valid(schema_path, 123)
  ensure
    suppress_warnings do
      JSON::Validator.cache_schemas = true
    end
  end

  private

  def schema_path
    @schema.path
  end

  def set_schema(schema_definition)
    @schema.write(schema_definition.to_json)
    @schema.rewind
  end
end