Module: SchemaTest::Minitest

Defined in:
lib/schema_test/minitest.rb

Instance Method Summary collapse

Instance Method Details

#assert_json_schema_validates_against(json, schema) ⇒ Object



42
43
44
45
# File 'lib/schema_test/minitest.rb', line 42

def assert_json_schema_validates_against(json, schema)
  errors = SchemaTest.validate_json(json, schema)
  assert errors.empty?, "JSON did not pass schema:\n#{errors.join("\n")}"
end

#assert_valid_json_for_schema(json, name, arguments) ⇒ Object



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
# File 'lib/schema_test/minitest.rb', line 5

def assert_valid_json_for_schema(json, name, arguments)
  version = arguments[:version]

  if SchemaTest.configuration.compiled
    schema = SchemaTest.load_compiled_schema(name, version: version)
    actual_fingerprint = SchemaTest.schema_fingerprint(schema)

    if arguments[:fingerprint] != actual_fingerprint
      if ENV['CI']
        flunk "Schema fingerprint mismatch for #{name.inspect} (version: #{version.inspect}) at #{caller[0]}. The compiled schema has changed; run the tests locally to update the fingerprint."
      else
        install_fingerprint_rewrite_hook
        queue_write_schema_fingerprint(caller[0], actual_fingerprint)
      end
    end

    assert_json_schema_validates_against(json, schema)
  else
    install_assert_api_expansion_hook

    schema = arguments[:schema]

    definition = SchemaTest::Definition.find(name, version)
    raise "Unknown definition #{name}, version: #{version}" unless definition.present?

    expected_schema = definition.as_json_schema

    if schema != expected_schema && ENV['CI']
      flunk "Outdated API schema assertion at #{caller[0]}"
    end

    queue_write_expanded_assert_api_call(caller[0], __method__, name, version, definition.location, expected_schema)

    assert_json_schema_validates_against(json, expected_schema)
  end
end