mirror of
https://github.com/ditkrg/jsonapi-deserializable.git
synced 2026-01-25 15:23:05 +00:00
Proper tests. (#1)
* Add tests for Deserializable::Resource. * Add codecov integration. * Add tests for Deserializable::Relationship + fix bad arity.
This commit is contained in:
118
spec/resource/DSL/has_many_spec.rb
Normal file
118
spec/resource/DSL/has_many_spec.rb
Normal file
@@ -0,0 +1,118 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe JSONAPI::Deserializable::Resource, '.has_many' do
|
||||
let(:deserializable_foo) do
|
||||
Class.new(JSONAPI::Deserializable::Resource) do
|
||||
has_many :foo do |rel, ids, types|
|
||||
field foo_ids: ids
|
||||
field foo_types: types
|
||||
field foo_rel: rel
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'relationship is not empty' do
|
||||
let(:payload) do
|
||||
{
|
||||
'data' => {
|
||||
'type' => 'foo',
|
||||
'relationships' => {
|
||||
'foo' => {
|
||||
'data' => [
|
||||
{ 'type' => 'foo', 'id' => 'bar' },
|
||||
{ 'type' => 'foo', 'id' => 'baz' }
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
it 'creates corresponding fields' do
|
||||
actual = deserializable_foo.call(payload)
|
||||
expected = { foo_ids: %w(bar baz), foo_types: %w(foo foo),
|
||||
foo_rel: payload['data']['relationships']['foo'] }
|
||||
|
||||
expect(actual).to eq(expected)
|
||||
end
|
||||
|
||||
it 'defaults to creating a field of the same name' do
|
||||
klass = Class.new(JSONAPI::Deserializable::Resource) do
|
||||
has_many :foo
|
||||
end
|
||||
actual = klass.call(payload)
|
||||
expected = { foo: payload['data']['relationships']['foo'] }
|
||||
|
||||
expect(actual).to eq(expected)
|
||||
end
|
||||
end
|
||||
|
||||
context 'relationship is empty' do
|
||||
it 'creates corresponding fields' do
|
||||
payload = {
|
||||
'data' => {
|
||||
'type' => 'foo',
|
||||
'relationships' => {
|
||||
'foo' => {
|
||||
'data' => []
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
actual = deserializable_foo.call(payload)
|
||||
expected = { foo_ids: [], foo_types: [],
|
||||
foo_rel: payload['data']['relationships']['foo'] }
|
||||
|
||||
expect(actual).to eq(expected)
|
||||
end
|
||||
end
|
||||
|
||||
context 'relationship is absent' do
|
||||
it 'does not create corresponding fields' do
|
||||
payload = {
|
||||
'data' => {
|
||||
'type' => 'foo',
|
||||
'relationships' => {}
|
||||
}
|
||||
}
|
||||
actual = deserializable_foo.call(payload)
|
||||
expected = {}
|
||||
|
||||
expect(actual).to eq(expected)
|
||||
end
|
||||
end
|
||||
|
||||
context 'there is no relationships member' do
|
||||
it 'does not create corresponding fields' do
|
||||
payload = {
|
||||
'data' => {
|
||||
'type' => 'foo'
|
||||
}
|
||||
}
|
||||
actual = deserializable_foo.call(payload)
|
||||
expected = {}
|
||||
|
||||
expect(actual).to eq(expected)
|
||||
end
|
||||
end
|
||||
|
||||
context 'relationship is not to-many' do
|
||||
it 'does not create corresponding fields' do
|
||||
payload = {
|
||||
'data' => {
|
||||
'type' => 'foo',
|
||||
'relationships' => {
|
||||
'foo' => {
|
||||
'data' => { 'type' => 'foo', 'id' => 'bar' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
actual = deserializable_foo.call(payload)
|
||||
expected = {}
|
||||
|
||||
expect(actual).to eq(expected)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user