From 7956e6d289f3dbb60b175a099b1f62346523a299 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Tue, 9 Feb 2016 11:20:27 +0800 Subject: [PATCH] check map#contains matches json schema in spec --- Gemfile | 1 + Gemfile.lock | 4 +++ spec/controllers/maps_controller_spec.rb | 8 +++++ spec/schemas/map.json | 0 spec/schemas/map_contains.json | 42 ++++++++++++++++++++++++ spec/support/schema_matcher.rb | 7 ++++ 6 files changed, 62 insertions(+) create mode 100644 spec/schemas/map.json create mode 100644 spec/schemas/map_contains.json create mode 100644 spec/support/schema_matcher.rb diff --git a/Gemfile b/Gemfile index b8872752..8379a7db 100644 --- a/Gemfile +++ b/Gemfile @@ -43,6 +43,7 @@ group :test do gem 'factory_girl_rails' gem 'shoulda-matchers' gem 'simplecov', require: false + gem 'json-schema' end group :production do #this is used on heroku diff --git a/Gemfile.lock b/Gemfile.lock index 710f5f10..3a5f1b41 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -36,6 +36,7 @@ GEM minitest (~> 5.1) thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) + addressable (2.3.8) arel (6.0.3) aws-sdk (1.66.0) aws-sdk-v1 (= 1.66.0) @@ -105,6 +106,8 @@ GEM jquery-ui-rails (5.0.5) railties (>= 3.2.16) json (1.8.3) + json-schema (2.6.0) + addressable (~> 2.3.8) kaminari (0.16.3) actionpack (>= 3.0.0) activesupport (>= 3.0.0) @@ -251,6 +254,7 @@ DEPENDENCIES jquery-rails jquery-ui-rails json + json-schema kaminari paperclip pg diff --git a/spec/controllers/maps_controller_spec.rb b/spec/controllers/maps_controller_spec.rb index 7c7a0499..74a03b7d 100644 --- a/spec/controllers/maps_controller_spec.rb +++ b/spec/controllers/maps_controller_spec.rb @@ -15,6 +15,14 @@ RSpec.describe MapsController, type: :controller do end end + describe 'GET #contains' do + it 'returns json matching schema' do + get :contains, { id: map.to_param, format: :json } + # get "maps/#{map.id}/contains" + expect(response.body).to match_json_schema(:map_contains) + end + end + describe 'GET #show' do it 'assigns the requested map as @map' do get :show, { id: map.to_param } diff --git a/spec/schemas/map.json b/spec/schemas/map.json new file mode 100644 index 00000000..e69de29b diff --git a/spec/schemas/map_contains.json b/spec/schemas/map_contains.json new file mode 100644 index 00000000..0b4faebf --- /dev/null +++ b/spec/schemas/map_contains.json @@ -0,0 +1,42 @@ +{ + "name": "Map Contents", + "type": "object", + "properties": { + "map": { + "type": "object" + }, + "topics": { + "type": "array", + "items": { + "type": "object" + } + }, + "synapses": { + "type": "array", + "items": { + "type": "object" + } + }, + "mappings": { + "type": "array", + "items": { + "type": "object" + } + }, + "mappers": { + "type": "array", + "items": { + "type": "object" + } + } + }, + "required": [ + "map", + "topics", + "synapses", + "mappings", + "mappers" + ] +} + + diff --git a/spec/support/schema_matcher.rb b/spec/support/schema_matcher.rb new file mode 100644 index 00000000..b2a89352 --- /dev/null +++ b/spec/support/schema_matcher.rb @@ -0,0 +1,7 @@ +RSpec::Matchers.define :match_json_schema do |schema| + match do |json| + schema_directory = Rails.root.join('spec', 'schemas').to_s + schema_path = "#{schema_directory}/#{schema}.json" + JSON::Validator.validate!(schema_path, json) + end +end