Skip to content

Commit

Permalink
Cache file reads
Browse files Browse the repository at this point in the history
  • Loading branch information
dizer committed Jun 12, 2019
1 parent 1e84c2e commit bcbfb5a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
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
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

0 comments on commit bcbfb5a

Please sign in to comment.