File: caching_test.rb

package info (click to toggle)
ruby-json-schema 2.8.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 896 kB
  • sloc: ruby: 5,806; makefile: 6
file content (57 lines) | stat: -rw-r--r-- 1,171 bytes parent folder | download | duplicates (4)
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
require File.expand_path('../support/test_helper', __FILE__)

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