From 4d6868d9bb84a5a0628def6bd5650ecf36bb3abe Mon Sep 17 00:00:00 2001 From: Hernan Schmidt Date: Thu, 18 Aug 2016 16:21:09 +0200 Subject: [PATCH] Fix conference serializer spec to work with MySQL --- Gemfile | 2 + Gemfile.lock | 3 + .../serializers/conference_serializer_spec.rb | 83 ++------------ spec/support/api_schema_matcher.rb | 9 ++ spec/support/schemas/conference.json | 101 ++++++++++++++++++ 5 files changed, 125 insertions(+), 73 deletions(-) create mode 100644 spec/support/api_schema_matcher.rb create mode 100644 spec/support/schemas/conference.json diff --git a/Gemfile b/Gemfile index 5a1cae6e..c4564a2b 100644 --- a/Gemfile +++ b/Gemfile @@ -227,6 +227,8 @@ group :test do gem 'webmock' # for mocking Stripe responses in tests gem 'stripe-ruby-mock' + # For validating JSON schemas + gem 'json-schema' end group :development, :test do diff --git a/Gemfile.lock b/Gemfile.lock index 98196af8..2f84aa81 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -230,6 +230,8 @@ GEM jquery-ui-rails (4.2.1) railties (>= 3.2.16) json (1.8.3) + json-schema (2.5.0) + addressable (~> 2.3) jwt (1.0.0) launchy (2.4.2) addressable (~> 2.3) @@ -577,6 +579,7 @@ DEPENDENCIES jquery-datatables-rails (~> 2.2.1) jquery-rails jquery-ui-rails (~> 4.2.1) + json-schema leaflet-rails letter_opener letter_opener_web diff --git a/spec/serializers/conference_serializer_spec.rb b/spec/serializers/conference_serializer_spec.rb index 20c28f7f..851c6518 100644 --- a/spec/serializers/conference_serializer_spec.rb +++ b/spec/serializers/conference_serializer_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' + describe ConferenceSerializer, type: :serializer do let(:conference) do create(:conference, short_title: 'goto', @@ -8,84 +9,20 @@ describe ConferenceSerializer, type: :serializer do end let(:serializer) { ConferenceSerializer.new(conference) } - let(:expected_hash) do - { - conference: { - short_title: 'goto', - title: conference.title, - description: 'Lorem ipsum dolor sit', - start_date: '2014-03-04', - end_date: '2014-03-10', - picture_url: nil, - difficulty_levels: - [{id: 1, - title: 'Easy', - description: 'Events are understandable for everyone without knowledge of the topic.' - }, - {id: 2, - title: 'Medium', - description: 'Events require a basic understanding of the topic.' - }, - {id: 3, - title: 'Hard', - description: 'Events require expert knowledge of the topic.' - } - ], - event_types: - [{id: 1, - title: 'Talk', - length: 30, - description: 'Presentation in lecture format' - }, - {id: 2, - title: 'Workshop', - length: 60, - description: 'Interactive hands-on practice' - } - ], - rooms: [], - tracks: [], - date_range: 'March 04 - 10', - revision: 1 - } - } - end - context 'conference does not have rooms and tracks' do - it 'sets conference attributes with empty room and tracks' do - expect(serializer.to_json).to eq expected_hash.to_json + context 'when the conference does not have rooms and tracks' do + it 'correctly serializes the conference' do + expect(serializer.to_json).to match_response_schema('conference') end end - context 'conference has rooms and tracks' do - before do - venue = create(:venue, conference: conference) - _room = create(:room, venue: venue) - track = create(:track, program: conference.program) + context 'when the conference has rooms and tracks' do + let(:venue) { create(:venue, conference: conference) } + let!(:room) { create(:room, venue: venue) } + let!(:track) { create(:track, program: conference.program) } - room_hash = { - rooms: [{ - id: 1, - size: 4, - events: [] - } - ] - } - track_hash = { - tracks: [{ - id: 1, - name: track.name, - description: track.description - } - ] - } - - expected_hash[:conference].merge! room_hash - expected_hash[:conference].merge! track_hash - end - - it 'sets conference attributes with rooms and tracks' do - expect(serializer.to_json).to eq expected_hash.to_json + it 'correctly serializes the conference' do + expect(serializer.to_json).to match_response_schema('conference') end end end diff --git a/spec/support/api_schema_matcher.rb b/spec/support/api_schema_matcher.rb new file mode 100644 index 00000000..4da6bfe6 --- /dev/null +++ b/spec/support/api_schema_matcher.rb @@ -0,0 +1,9 @@ +# Source: https://robots.thoughtbot.com/validating-json-schemas-with-an-rspec-matcher + +RSpec::Matchers.define :match_response_schema do |schema| + match do |json| + schema_directory = "#{Dir.pwd}/spec/support/schemas" + schema_path = "#{schema_directory}/#{schema}.json" + JSON::Validator.validate!(schema_path, json, strict: true) + end +end diff --git a/spec/support/schemas/conference.json b/spec/support/schemas/conference.json new file mode 100644 index 00000000..3117d98f --- /dev/null +++ b/spec/support/schemas/conference.json @@ -0,0 +1,101 @@ +{ + "type": "object", + "required": ["conference"], + "properties": { + "conference" : { + "type" : "object", + "required" : [ + "short_title", + "title", + "description", + "start_date", + "end_date", + "picture_url", + "difficulty_levels", + "event_types", + "rooms", + "tracks", + "date_range", + "revision" + ], + "properties" : { + "short_title": { + "type": "string" + }, + "title": { + "type": "string" + }, + "description": { + "type": "string" + }, + "start_date": { + "type": "string", "format": "date" + }, + "end_date": { + "type": "string", "format": "date" + }, + "picture_url": { + "anyOf": [ + { "type": "string" }, + { "type": "null" } + ] + }, + "difficulty_levels": { + "type": "array", + "items": { + "type": "object", + "required": ["id", "title", "description"], + "properties": { + "id": { "type": "integer" }, + "title": { "type": "string" }, + "description": { "type": "string" } + } + } + }, + "event_types": { + "type": "array", + "items": { + "type": "object", + "required": ["id", "title", "description", "length"], + "properties": { + "id": { "type": "integer" }, + "title": { "type": "string" }, + "description": { "type": "string" }, + "length": { "type": "integer" } + } + } + }, + "rooms": { + "type": "array", + "items": { + "type": "object", + "required": ["id", "size", "events"], + "properties": { + "id": { "type": "integer" }, + "size": { "type": "integer" }, + "events": { "type": "array" } + } + } + }, + "tracks": { + "type": "array", + "items": { + "type": "object", + "required": ["id", "name", "description"], + "properties": { + "id": { "type": "integer" }, + "name": { "type": "string" }, + "description": { "type": "string" } + } + } + }, + "date_range": { + "type": "string" + }, + "revision": { + "type": "integer" + } + } + } + } +}