Skip to content

Commit daf986f

Browse files
author
Lee Richmond
committed
Use jsonapi_spec_helpers
1 parent 46708bf commit daf986f

File tree

7 files changed

+54
-203
lines changed

7 files changed

+54
-203
lines changed

.rspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--color
2+
--format documentation
3+
--require spec_helper

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
source "http://artprod.dev.bloomberg.com/artifactory/api/gems/rubygems/"
1+
source "http://artprod.dev.bloomberg.com/artifactory/api/gems/bb-ruby-repos/"
22

33
# Specify your gem's dependencies in jsonapi_compliable.gemspec
44
gemspec

bin/rspec

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
#
4+
# This file was generated by Bundler.
5+
#
6+
# The application 'rspec' is installed as part of a gem, and
7+
# this file is here to facilitate running it.
8+
#
9+
10+
require "pathname"
11+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12+
Pathname.new(__FILE__).realpath)
13+
14+
require "rubygems"
15+
require "bundler/setup"
16+
17+
load Gem.bin_path("rspec-core", "rspec")

jsonapi_compliable.gemspec

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ Gem::Specification.new do |spec|
1919

2020
spec.add_dependency "rails"
2121
spec.add_dependency "jsonapi"
22+
spec.add_development_dependency "pry"
23+
spec.add_development_dependency "pry-byebug"
2224
spec.add_development_dependency "kaminari"
2325
spec.add_development_dependency "active_model_serializers"
24-
spec.add_development_dependency "nested_attribute_reassignable"
26+
spec.add_development_dependency "nested_attribute_reassignable"
27+
spec.add_development_dependency "jsonapi_spec_helpers"
2528
spec.add_development_dependency "bundler", "~> 1.12"
2629
spec.add_development_dependency "rake", "~> 10.0"
2730
spec.add_development_dependency "rspec"

spec/jsonapi_compliable_spec.rb

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,16 @@ def create
6565
data: [
6666
{ type: 'books', attributes: { title: 'The Shining' } }
6767
]
68-
}
68+
}
6969
}
7070
}
7171
}
7272

7373
expect(json_included_types)
7474
.to match_array(%w(states books))
75-
expect(json_includes('states', :id))
76-
.to match_array([virginia.id.to_s])
77-
expect(json_includes('states', :id)[0]).to eq(virginia.id.to_s)
75+
expect(json_include('states')['id'])
76+
.to eq(virginia.id.to_s)
77+
expect(json_include('states')['id']).to eq(virginia.id.to_s)
7878
end
7979
end
8080

@@ -117,14 +117,14 @@ def update
117117
relationships: {
118118
books: {
119119
data: [
120-
{
121-
type: 'books',
122-
attributes: {
123-
title: "The Firm",
120+
{
121+
type: 'books',
122+
attributes: {
123+
title: "The Firm",
124124
genre_attributes: {
125125
name: "Thriller"
126-
}
127-
}
126+
}
127+
}
128128
}
129129
]
130130
}
@@ -133,7 +133,7 @@ def update
133133
}
134134

135135
expect(json_included_types).to match_array(%w(books))
136-
expect(json_includes('books', :id)).to match_array([Book.last.id.to_s])
136+
expect(json_include('books')['id']).to eq(Book.last.id.to_s)
137137
author.reload
138138
expect(author.book_ids).to match_array(Book.pluck(:id))
139139
end
@@ -151,7 +151,7 @@ def update
151151
Author.create!(first_name: "First", state: State.last)
152152
Author.create!(first_name: "Second", state: State.last)
153153
get :index
154-
expect(json_ids).to eq(Author.pluck(:id))
154+
expect(json_ids(true)).to eq(Author.pluck(:id))
155155
end
156156

157157
it 'should be able to override options' do
@@ -297,7 +297,7 @@ def index
297297

298298
it 'should limit by size, offset by number' do
299299
get :index, params: { page: { number: 2, size: 2 } }
300-
expect(json_ids).to eq([author3.id, author4.id])
300+
expect(json_ids(true)).to eq([author3.id, author4.id])
301301
end
302302

303303
context 'and a custom pagination function is given' do
@@ -394,14 +394,14 @@ def index
394394

395395
it 'should limit to only the requested fields' do
396396
get :index, params: { fields: { authors: 'first_name,updated_at' } }
397-
expect(json_items(0).keys).to match_array(%w(first-name updated-at))
397+
expect(json_items(0).keys).to match_array(%w(id jsonapi_type first-name updated-at))
398398
end
399399

400400
it 'should still disallow fields guarded by :if' do
401401
allow_any_instance_of(custom_serializer)
402402
.to receive(:allow_hostname?) { false }
403403
get :index, params: { fields: { authors: 'hostname,updated_at' } }
404-
expect(json_items(0).keys).to match_array(['updated-at'])
404+
expect(json_items(0).keys).to match_array(%w(id jsonapi_type updated-at))
405405
end
406406

407407
context 'when requesting extra fields' do
@@ -484,7 +484,7 @@ def include_foo!
484484

485485
it 'applies by default' do
486486
get :index
487-
expect(json_ids).to eq([author3.id])
487+
expect(json_ids(true)).to eq([author3.id])
488488
end
489489

490490
it 'is overridable if an allowed filter' do
@@ -499,7 +499,7 @@ def include_foo!
499499
end
500500

501501
get :index, params: { filter: { last_name: author4.last_name } }
502-
expect(json_ids).to eq([author4.id])
502+
expect(json_ids(true)).to eq([author4.id])
503503
end
504504

505505
it 'is overridable if an allowed filter has a corresponding alias' do
@@ -516,36 +516,36 @@ def include_foo!
516516
end
517517

518518
get :index, params: { filter: { title: author1.first_name } }
519-
expect(json_ids).to eq([author1.id])
519+
expect(json_ids(true)).to eq([author1.id])
520520
end
521521
end
522522

523523
context 'and the filter is allowed' do
524524
context 'and is customized with a block' do
525525
it 'should filter correctly via block' do
526526
get :index, params: { filter: { first_name_prefix: 'A' } }
527-
expect(json_ids).to eq([author2.id, author4.id])
527+
expect(json_ids(true)).to eq([author2.id, author4.id])
528528
end
529529
end
530530

531531
context 'with alternate param name' do
532532
it 'should filter correctly' do
533533
get :index, params: { filter: { title: author2.first_name } }
534-
expect(json_ids).to eq([author2.id])
534+
expect(json_ids(true)).to eq([author2.id])
535535
end
536536
end
537537

538538
context 'and is not customized with a block' do
539539
it 'should provide default ActiveRecord filter' do
540540
get :index, params: { filter: { first_name: author2.first_name } }
541-
expect(json_ids).to eq([author2.id])
541+
expect(json_ids(true)).to eq([author2.id])
542542
end
543543
end
544544

545545
context 'and is comma-delimited' do
546546
it 'should automatically be parsed into a ruby array' do
547547
get :index, params: { filter: { first_name: [author2.first_name, author3.first_name].join(',') } }
548-
expect(json_ids).to eq([author2.id, author3.id])
548+
expect(json_ids(true)).to eq([author2.id, author3.id])
549549
end
550550
end
551551

spec/spec_helper.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
22
require File.expand_path("../dummy/config/environment.rb", __FILE__)
33
require 'rspec/rails'
4-
require 'support/json_api_helper'
5-
64

75
RSpec.configure do |config|
86
config.include FactoryGirl::Syntax::Methods
9-
config.include JSONAPIHelper
7+
config.include JsonapiSpecHelpers
108

119
config.before(:suite) do
1210
DatabaseCleaner.strategy = :transaction
@@ -114,31 +112,31 @@ def self.extra_attributes(*names)
114112
end
115113
end
116114

117-
class AuthorSerializer < ApplicationSerializer
115+
class AuthorSerializer < ApplicationSerializer
118116
attributes :first_name, :last_name
119117
belongs_to :state
120118
has_many :books
121119
end
122120

123-
class StateSerializer < ApplicationSerializer
121+
class StateSerializer < ApplicationSerializer
124122
attributes :name
125123
end
126124

127-
class TagSerializer < ApplicationSerializer
125+
class TagSerializer < ApplicationSerializer
128126
attributes :name
129127
belongs_to :book
130128
end
131129

132-
class GenreSerializer < ApplicationSerializer
130+
class GenreSerializer < ApplicationSerializer
133131
attributes :name
134132
has_many :books
135133
end
136134

137-
class BookSerializer < ApplicationSerializer
135+
class BookSerializer < ApplicationSerializer
138136
attributes :title
139137
belongs_to :genre
140138
belongs_to :author
141139
has_many :tags
142140
end
143141

144-
ActiveModel::Serializer.config.adapter = :json_api
142+
ActiveModel::Serializer.config.adapter = :json_api

0 commit comments

Comments
 (0)