Skip to content

Align have_relationship matcher with readme and add support for Symbol or String parameters #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require ./spec/spec_helper.rb
44 changes: 44 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,52 @@
require:
- rubocop-performance

Metrics/BlockLength:
Exclude:
- 'spec/**/*.rb'

Style/FrozenStringLiteralComment:
Enabled: false

Style/Documentation:
Enabled: false

Layout/EmptyLinesAroundAttributeAccessor:
Enabled: true

Layout/SpaceAroundMethodCallOperator:
Enabled: true

Lint/DeprecatedOpenSSLConstant:
Enabled: true

Lint/MixedRegexpCaptureTypes:
Enabled: true

Lint/RaiseException:
Enabled: true

Lint/StructNewOverride:
Enabled: true

Style/ExponentialNotation:
Enabled: true

Style/HashEachMethods:
Enabled: true

Style/HashTransformKeys:
Enabled: true

Style/HashTransformValues:
Enabled: true

Style/RedundantRegexpCharacterClass:
Enabled: true

Style/RedundantRegexpEscape:
Enabled: true

Style/SlicingWithRange:
Enabled: true

2 changes: 1 addition & 1 deletion lib/jsonapi/rspec/jsonapi_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module JsonapiObject
::RSpec::Matchers.define :have_jsonapi_object do |val|
match do |actual|
actual.key?('jsonapi') &&
(!val || actual['jsonapi'] == val)
(!val || actual['jsonapi'] == JSON.parse(JSON.generate(val)))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/jsonapi/rspec/meta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Meta
::RSpec::Matchers.define :have_meta do |val|
match do |actual|
actual.key?('meta') &&
(!val || actual['meta'] == val)
(!val || actual['meta'] == JSON.parse(JSON.generate(val)))
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/jsonapi/rspec/relationships.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Relationships
match do |actual|
return false unless (actual['relationships'] || {}).key?(rel.to_s)

!@data_set || actual['relationships'][rel.to_s]['data'] == @data_val
!@data_set || actual['relationships'][rel.to_s]['data'] == JSON.parse(JSON.generate(@data_val))
end

chain :with_data do |val|
Expand All @@ -27,7 +27,7 @@ module Relationships
match do |actual|
return false unless actual.key?('relationships')

rels.all? { |rel| actual['relationships'].key?(rel) }
rels.all? { |rel| actual['relationships'].key?(rel.to_s) }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/jsonapi/rspec/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module JSONAPI
module RSpec
module Type
::RSpec::Matchers.define :have_type do |expected|
match { |actual| actual['type'] == expected }
match { |actual| actual['type'] == expected.to_s }
end
end
end
Expand Down
10 changes: 7 additions & 3 deletions spec/jsonapi/attributes_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'spec_helper'

RSpec.describe JSONAPI::RSpec do
let(:doc) do
{
Expand All @@ -13,13 +11,19 @@

describe '#have_attribute' do
it { expect(doc).to have_attribute(:one) }
it { expect(doc).to have_attribute('one') }
it { expect(doc).not_to have_attribute(:five) }
it { expect(doc).not_to have_attribute('five') }
end

describe '#have_jsonapi_attributes' do
it { expect(doc).to have_jsonapi_attributes(:one, :two) }
it { expect(doc).to have_jsonapi_attributes('one', 'two') }
it { expect(doc).not_to have_jsonapi_attributes(:two, :five) }
it { expect(doc).not_to have_jsonapi_attributes('two', 'five') }
it { expect(doc).to have_jsonapi_attributes(:one, :two, :four).exactly }
it { expect(doc).not_to have_jsonapi_attributes(:one).exactly }
it { expect(doc).to have_jsonapi_attributes('one', 'two', 'four').exactly }
it { expect(doc).not_to have_jsonapi_attributes('one').exactly }
it { expect(doc).not_to have_jsonapi_attributes('one').exactly }
end
end
2 changes: 0 additions & 2 deletions spec/jsonapi/id_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'spec_helper'

RSpec.describe JSONAPI::RSpec, '#have_id' do
it 'succeeds when id matches' do
expect('id' => 'foo').to have_id('foo')
Expand Down
11 changes: 5 additions & 6 deletions spec/jsonapi/jsonapi_object_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'spec_helper'

RSpec.describe JSONAPI::RSpec, '#have_jsonapi_object' do
context 'when providing no value' do
it 'succeeds when jsonapi object is present' do
Expand All @@ -13,17 +11,18 @@

context 'when providing a value' do
it 'succeeds when jsonapi object matches' do
expect('jsonapi' => { 'version' => '1.0' })
.to have_jsonapi_object('version' => '1.0')
expect('jsonapi' => { 'version' => '1.0' }).to have_jsonapi_object('version' => '1.0')
expect('jsonapi' => { 'version' => '1.0' }).to have_jsonapi_object(version: '1.0')
end

it 'fails when jsonapi object mismatches' do
expect('jsonapi' => { 'version' => '2.0' })
.not_to have_jsonapi_object('version' => '1.0')
expect('jsonapi' => { 'version' => '2.0' }).not_to have_jsonapi_object('version' => '1.0')
expect('jsonapi' => { 'version' => '2.0' }).not_to have_jsonapi_object(version: '1.0')
end

it 'fails when jsonapi object is absent' do
expect({}).not_to have_jsonapi_object('version' => '1.0')
expect({}).not_to have_jsonapi_object(version: '1.0')
end
end
end
8 changes: 6 additions & 2 deletions spec/jsonapi/links_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'spec_helper'

RSpec.describe JSONAPI::RSpec do
let(:doc) do
{
Expand All @@ -13,12 +11,18 @@
context '#have_link' do
it { expect(doc).to have_link(:self) }
it { expect(doc).to have_link(:self).with_value('self_link') }
it { expect(doc).to have_link('self') }
it { expect(doc).to have_link('self').with_value('self_link') }
it { expect(doc).not_to have_link(:self).with_value('any_link') }
it { expect(doc).not_to have_link(:any) }
it { expect(doc).not_to have_link('self').with_value('any_link') }
it { expect(doc).not_to have_link('any') }
end

context '#have_links' do
it { expect(doc).to have_links(:self, :related) }
it { expect(doc).not_to have_links(:self, :other) }
it { expect(doc).to have_links('self', 'related') }
it { expect(doc).not_to have_links('self', 'other') }
end
end
9 changes: 5 additions & 4 deletions spec/jsonapi/meta_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'spec_helper'

RSpec.describe JSONAPI::RSpec, '#have_meta' do
context 'when providing no value' do
it 'succeeds when meta is present' do
Expand All @@ -13,14 +11,17 @@

context 'when providing a value' do
it 'succeeds when meta matches' do
expect('meta' => { foo: 'bar' }).to have_meta(foo: 'bar')
expect('meta' => { 'foo' => 'bar' }).to have_meta({ 'foo' => 'bar' })
expect('meta' => { 'foo' => 'bar' }).to have_meta(foo: 'bar')
end

it 'fails when meta mismatches' do
expect('meta' => { foo: 'bar' }).not_to have_meta(bar: 'baz')
expect('meta' => { 'foo' => 'bar' }).not_to have_meta({ 'foo' => 'baz' })
expect('meta' => { 'foo' => 'bar' }).not_to have_meta(bar: 'baz')
end

it 'fails when meta is absent' do
expect({}).not_to have_meta({ 'foo' => 'bar' })
expect({}).not_to have_meta(foo: 'bar')
end
end
Expand Down
61 changes: 61 additions & 0 deletions spec/jsonapi/relationships_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
RSpec.describe JSONAPI::RSpec do
let(:doc) do
{
'relationships' => {
'posts' => {
'data' => {
'id' => '1',
'type' => 'posts'
}
},
'comments' => {
'data' => [{
'id' => '1',
'type' => 'posts'
}, {
'id' => '2',
'type' => 'hides'
}]
}
}
}
end

describe '#have_jsonapi_attributes' do
it { expect(doc).to have_relationship(:posts) }
it { expect(doc).to have_relationship('posts') }
it { expect(doc).not_to have_relationship(:mails) }
it { expect(doc).not_to have_relationship('mails') }
it { expect(doc).to have_relationship(:posts).with_data({ 'id' => '1', 'type' => 'posts' }) }
it { expect(doc).to have_relationship('posts').with_data({ 'id' => '1', 'type' => 'posts' }) }
it { expect(doc).to have_relationship(:posts).with_data({ id: '1', type: 'posts' }) }
it { expect(doc).to have_relationship('posts').with_data({ id: '1', type: 'posts' }) }
it do
expect(doc).to have_relationship(:comments).with_data(
[{ 'id' => '1', 'type' => 'posts' }, { 'id' => '2', 'type' => 'hides' }]
)
end
it do
expect(doc).to have_relationship('comments').with_data(
[{ 'id' => '1', 'type' => 'posts' }, { 'id' => '2', 'type' => 'hides' }]
)
end
it do
expect(doc).to have_relationship(:comments).with_data(
[{ id: '1', type: 'posts' }, { id: '2', type: 'hides' }]
)
end
it do
expect(doc).to have_relationship('comments').with_data(
[{ id: '1', type: 'posts' }, { id: '2', type: 'hides' }]
)
end
end

describe '#have_relationships' do
it { expect(doc).to have_relationships(:posts, :comments) }
it { expect(doc).to have_relationships('posts', 'comments') }
it { expect(doc).not_to have_relationships(:posts, :comments, :mails) }
it { expect(doc).not_to have_relationships('posts', 'comments', 'mails') }
end
end
9 changes: 7 additions & 2 deletions spec/jsonapi/type_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
require 'spec_helper'

RSpec.describe JSONAPI::RSpec, '#have_type' do
it 'succeeds when type matches' do
expect('type' => 'foo').to have_type('foo')
end
it 'succeeds when type (as symbol) matches' do
expect('type' => 'foo').to have_type(:foo)
end

it 'fails when type mismatches' do
expect('type' => 'foo').not_to have_type('bar')
end

it 'fails when type (as symbol) mismatches' do
expect('type' => 'foo').not_to have_type(:bar)
end

it 'fails when type is absent' do
expect({}).not_to have_type('foo')
end
Expand Down