Skip to content

Commit

Permalink
Use jsonapi_spec_helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee Richmond committed Sep 2, 2016
1 parent 46708bf commit daf986f
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 203 deletions.
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--color
--format documentation
--require spec_helper
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source "http://artprod.dev.bloomberg.com/artifactory/api/gems/rubygems/"
source "http://artprod.dev.bloomberg.com/artifactory/api/gems/bb-ruby-repos/"

# Specify your gem's dependencies in jsonapi_compliable.gemspec
gemspec
Expand Down
17 changes: 17 additions & 0 deletions bin/rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
#
# This file was generated by Bundler.
#
# The application 'rspec' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require "pathname"
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
Pathname.new(__FILE__).realpath)

require "rubygems"
require "bundler/setup"

load Gem.bin_path("rspec-core", "rspec")
5 changes: 4 additions & 1 deletion jsonapi_compliable.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ Gem::Specification.new do |spec|

spec.add_dependency "rails"
spec.add_dependency "jsonapi"
spec.add_development_dependency "pry"
spec.add_development_dependency "pry-byebug"
spec.add_development_dependency "kaminari"
spec.add_development_dependency "active_model_serializers"
spec.add_development_dependency "nested_attribute_reassignable"
spec.add_development_dependency "nested_attribute_reassignable"
spec.add_development_dependency "jsonapi_spec_helpers"
spec.add_development_dependency "bundler", "~> 1.12"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec"
Expand Down
44 changes: 22 additions & 22 deletions spec/jsonapi_compliable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@ def create
data: [
{ type: 'books', attributes: { title: 'The Shining' } }
]
}
}
}
}
}

expect(json_included_types)
.to match_array(%w(states books))
expect(json_includes('states', :id))
.to match_array([virginia.id.to_s])
expect(json_includes('states', :id)[0]).to eq(virginia.id.to_s)
expect(json_include('states')['id'])
.to eq(virginia.id.to_s)
expect(json_include('states')['id']).to eq(virginia.id.to_s)
end
end

Expand Down Expand Up @@ -117,14 +117,14 @@ def update
relationships: {
books: {
data: [
{
type: 'books',
attributes: {
title: "The Firm",
{
type: 'books',
attributes: {
title: "The Firm",
genre_attributes: {
name: "Thriller"
}
}
}
}
}
]
}
Expand All @@ -133,7 +133,7 @@ def update
}

expect(json_included_types).to match_array(%w(books))
expect(json_includes('books', :id)).to match_array([Book.last.id.to_s])
expect(json_include('books')['id']).to eq(Book.last.id.to_s)
author.reload
expect(author.book_ids).to match_array(Book.pluck(:id))
end
Expand All @@ -151,7 +151,7 @@ def update
Author.create!(first_name: "First", state: State.last)
Author.create!(first_name: "Second", state: State.last)
get :index
expect(json_ids).to eq(Author.pluck(:id))
expect(json_ids(true)).to eq(Author.pluck(:id))
end

it 'should be able to override options' do
Expand Down Expand Up @@ -297,7 +297,7 @@ def index

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

context 'and a custom pagination function is given' do
Expand Down Expand Up @@ -394,14 +394,14 @@ def index

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

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

context 'when requesting extra fields' do
Expand Down Expand Up @@ -484,7 +484,7 @@ def include_foo!

it 'applies by default' do
get :index
expect(json_ids).to eq([author3.id])
expect(json_ids(true)).to eq([author3.id])
end

it 'is overridable if an allowed filter' do
Expand All @@ -499,7 +499,7 @@ def include_foo!
end

get :index, params: { filter: { last_name: author4.last_name } }
expect(json_ids).to eq([author4.id])
expect(json_ids(true)).to eq([author4.id])
end

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

get :index, params: { filter: { title: author1.first_name } }
expect(json_ids).to eq([author1.id])
expect(json_ids(true)).to eq([author1.id])
end
end

context 'and the filter is allowed' do
context 'and is customized with a block' do
it 'should filter correctly via block' do
get :index, params: { filter: { first_name_prefix: 'A' } }
expect(json_ids).to eq([author2.id, author4.id])
expect(json_ids(true)).to eq([author2.id, author4.id])
end
end

context 'with alternate param name' do
it 'should filter correctly' do
get :index, params: { filter: { title: author2.first_name } }
expect(json_ids).to eq([author2.id])
expect(json_ids(true)).to eq([author2.id])
end
end

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

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

Expand Down
16 changes: 7 additions & 9 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require File.expand_path("../dummy/config/environment.rb", __FILE__)
require 'rspec/rails'
require 'support/json_api_helper'


RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.include JSONAPIHelper
config.include JsonapiSpecHelpers

config.before(:suite) do
DatabaseCleaner.strategy = :transaction
Expand Down Expand Up @@ -114,31 +112,31 @@ def self.extra_attributes(*names)
end
end

class AuthorSerializer < ApplicationSerializer
class AuthorSerializer < ApplicationSerializer
attributes :first_name, :last_name
belongs_to :state
has_many :books
end

class StateSerializer < ApplicationSerializer
class StateSerializer < ApplicationSerializer
attributes :name
end

class TagSerializer < ApplicationSerializer
class TagSerializer < ApplicationSerializer
attributes :name
belongs_to :book
end

class GenreSerializer < ApplicationSerializer
class GenreSerializer < ApplicationSerializer
attributes :name
has_many :books
end

class BookSerializer < ApplicationSerializer
class BookSerializer < ApplicationSerializer
attributes :title
belongs_to :genre
belongs_to :author
has_many :tags
end

ActiveModel::Serializer.config.adapter = :json_api
ActiveModel::Serializer.config.adapter = :json_api
Loading

0 comments on commit daf986f

Please sign in to comment.