Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache file reads #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions lib/json_matchers/matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ def validation_failure_message
errors.first.to_s
end

class << self
attr_writer :document_store

def document_store
@document_store ||= begin
document_store = JsonSchema::DocumentStore.new

Dir.glob("#{JsonMatchers.schema_root}/**/*.json").
map { |path| Pathname.new(path) }.
map { |schema_path| Parser.new(schema_path).parse }.
map { |schema| document_store.add_schema(schema) }.
each { |schema| schema.expand_references!(store: document_store) }

document_store
end
end
end

private

attr_accessor :errors
Expand All @@ -29,15 +47,7 @@ def validator
end

def build_and_populate_document_store
document_store = JsonSchema::DocumentStore.new

Dir.glob("#{JsonMatchers.schema_root}/**/*.json").
map { |path| Pathname.new(path) }.
map { |schema_path| Parser.new(schema_path).parse }.
map { |schema| document_store.add_schema(schema) }.
each { |schema| schema.expand_references!(store: document_store) }

document_store
self.class.document_store
end
end
end
11 changes: 11 additions & 0 deletions spec/json_matchers/match_json_schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@
expect(json).to match_json_schema("api/v1/schema")
end

it "does not reload document store" do
create(:schema, :location, name: "api/v1/schema")

json = build(:response, :location)

expect(Dir).to receive(:glob).once.and_call_original
2.times do
expect(json).to match_json_schema("api/v1/schema")
end
end

it "supports invalidating the referenced schema when using local references" do
create(:schema, name: "post", json: {
"$schema": "https://json-schema.org/draft-04/schema#",
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

config.around do |example|
ensure_fixtures("spec", "fixtures", "schemas") do
JsonMatchers::Matcher.document_store = nil
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it necessary to throw away our cache in each of the project's test suites?

Could you elaborate more on those details in the commit message?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It clears shared state (cache) between test cases

example.run
end
end
Expand Down
1 change: 1 addition & 0 deletions test/support/json_matchers/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class TestCase < ::Minitest::Test

def setup
@original_schema_root = setup_fixtures("test", "fixtures", "schemas")
JsonMatchers::Matcher.document_store = nil
end

def teardown
Expand Down