fun_with_json_api 0.0.8.1 → 0.0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,13 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe FunWithJsonApi::CollectionManager do
4
- subject(:instance) { described_class.new(collection, deserializer_class, deserializer_options) }
5
- let(:collection) { double('collection') }
6
- let(:deserializer_class) { class_double('FunWithJsonApi::Deserializer', type: 'examples') }
4
+ subject(:instance) do
5
+ described_class.new(parent_resource, deserializer_class, deserializer_options)
6
+ end
7
+ let(:deserializer_class) { class_double('FunWithJsonApi::Deserializer') }
7
8
  let(:deserializer_options) { double('deserializer_options') }
8
9
  let(:deserializer) { instance_double('FunWithJsonApi::Deserializer', type: 'examples') }
10
+ let(:parent_resource) { double('parent_resource') }
9
11
  before do
10
12
  allow(deserializer_class).to receive(:create)
11
13
  .with(deserializer_options)
@@ -13,42 +15,35 @@ describe FunWithJsonApi::CollectionManager do
13
15
  end
14
16
 
15
17
  describe '#insert_records' do
16
- context 'when a block is provided' do
17
- context 'when the block returns true for each item' do
18
- it 'loads a collection from the document and invokes the block with each collection item' do
19
- document = double('document')
18
+ context 'when `insert_record` has been overriden' do
19
+ context 'when `insert_record` returns true for each item' do
20
+ it 'calls `#insert_item` with each item in the collection' do
20
21
  collection = [double('collection_a'), double('collection_b')]
21
- expect(
22
- FunWithJsonApi::FindCollectionFromDocument
23
- ).to receive(:find).with(document, deserializer).and_return(collection)
24
22
 
25
23
  received_items = []
26
- instance.insert_records(document) do |item|
24
+ allow(instance).to receive(:insert_record) do |item|
27
25
  received_items << item
28
26
  true
29
27
  end
28
+
29
+ instance.insert_records(collection)
30
30
  expect(received_items).to eq collection
31
31
  end
32
32
  end
33
- context 'when the block returns false for an item' do
33
+ context 'when `insert_record` returns false for an item' do
34
34
  it 'raises a FunWithJsonApi::Exceptions::InvalidResource with a payload for each item' do
35
- document = double('document')
36
35
  collection = [
37
36
  double('collection_a', success?: true),
38
37
  double('collection_b', success?: false)
39
38
  ]
40
- allow(
41
- FunWithJsonApi::FindCollectionFromDocument
42
- ).to receive(:find).with(document, deserializer).and_return(collection)
43
39
  allow(deserializer).to receive(:format_resource_id).with(collection[0]).and_return('id_a')
44
40
  allow(deserializer).to receive(:format_resource_id).with(collection[1]).and_return('id_b')
45
41
 
42
+ # Return success from the item
43
+ allow(instance).to receive(:insert_record, &:success?)
44
+
46
45
  expect do
47
- instance.insert_records(
48
- document,
49
- ->(index) { "Record '#{index}' is invalid" },
50
- &:success? # Call success on each collection items
51
- )
46
+ instance.insert_records(collection, ->(index) { "Record '#{index}' is invalid" })
52
47
  end.to raise_error(FunWithJsonApi::Exceptions::InvalidResource) do |e|
53
48
  expect(e.payload.size).to eq 1
54
49
 
@@ -63,14 +58,14 @@ describe FunWithJsonApi::CollectionManager do
63
58
  end
64
59
  end
65
60
 
66
- context 'when no block is provided' do
61
+ context 'when insert_record has not been overridden' do
67
62
  it 'raises a RelationshipNotSupported exception' do
68
- document = double('document')
63
+ collection = [double('collection_item')]
69
64
  expect do
70
- instance.insert_records(document)
65
+ instance.insert_records(collection)
71
66
  end.to raise_error(FunWithJsonApi::Exceptions::RelationshipMethodNotSupported) do |e|
72
67
  expect(e.message).to eq(
73
- 'Override FunWithJsonApi::CollectionManager#insert_records or supply a block'
68
+ 'Override FunWithJsonApi::CollectionManager#insert_record'
74
69
  )
75
70
  expect(e.payload.size).to eq 1
76
71
 
@@ -86,49 +81,42 @@ describe FunWithJsonApi::CollectionManager do
86
81
  end
87
82
 
88
83
  describe '#remove_records' do
89
- context 'when a block is provided' do
90
- context 'when the block returns true for each item' do
84
+ context 'when `remove_record` has been overriden' do
85
+ context 'when `remove_record` returns true for each item' do
91
86
  it 'loads a collection from the document and invokes the block with each collection item' do
92
- document = double('document')
93
87
  collection = [double('collection_a'), double('collection_b')]
94
- expect(
95
- FunWithJsonApi::FindCollectionFromDocument
96
- ).to receive(:find).with(document, deserializer).and_return(collection)
97
88
 
98
89
  received_items = []
99
- instance.remove_records(document) do |item|
90
+ allow(instance).to receive(:remove_record) do |item|
100
91
  received_items << item
101
92
  true
102
93
  end
94
+
95
+ instance.remove_records(collection)
103
96
  expect(received_items).to eq collection
104
97
  end
105
98
  end
106
- context 'when the block returns false for an item' do
99
+ context 'when `remove_record` returns false for an item' do
107
100
  it 'raises a FunWithJsonApi::Exceptions::InvalidResource with a payload for each item' do
108
- document = double('document')
109
101
  collection = [
110
102
  double('collection_a', success?: true),
111
103
  double('collection_b', success?: false)
112
104
  ]
113
- allow(
114
- FunWithJsonApi::FindCollectionFromDocument
115
- ).to receive(:find).with(document, deserializer).and_return(collection)
116
105
  allow(deserializer).to receive(:format_resource_id).with(collection[0]).and_return('id_a')
117
106
  allow(deserializer).to receive(:format_resource_id).with(collection[1]).and_return('id_b')
118
107
 
108
+ # Return success from the item
109
+ allow(instance).to receive(:remove_record, &:success?)
110
+
119
111
  expect do
120
- instance.remove_records(
121
- document,
122
- ->(index) { "Record '#{index}' is invalid" },
123
- &:success? # Call success on each collection items
124
- )
112
+ instance.remove_records(collection, ->(index) { "Record '#{index}' is bad!" })
125
113
  end.to raise_error(FunWithJsonApi::Exceptions::InvalidResource) do |e|
126
114
  expect(e.payload.size).to eq 1
127
115
 
128
116
  payload = e.payload.first
129
117
  expect(payload.code).to eq 'invalid_resource'
130
118
  expect(payload.title).to eq 'Unable to update the relationship with this resource'
131
- expect(payload.detail).to eq "Record 'id_b' is invalid"
119
+ expect(payload.detail).to eq "Record 'id_b' is bad!"
132
120
  expect(payload.pointer).to eq '/data/1/id'
133
121
  expect(payload.status).to eq '422'
134
122
  end
@@ -136,14 +124,15 @@ describe FunWithJsonApi::CollectionManager do
136
124
  end
137
125
  end
138
126
 
139
- context 'when no block is provided' do
127
+ context 'when `remove_record` has not been implemented' do
140
128
  it 'raises a RelationshipNotSupported exception' do
141
- document = double('document')
129
+ collection = [double('collection_item')]
130
+
142
131
  expect do
143
- instance.remove_records(document)
132
+ instance.remove_records(collection)
144
133
  end.to raise_error(FunWithJsonApi::Exceptions::RelationshipMethodNotSupported) do |e|
145
134
  expect(e.message).to eq(
146
- 'Override FunWithJsonApi::CollectionManager#remove_records or supply a block'
135
+ 'Override FunWithJsonApi::CollectionManager#remove_record'
147
136
  )
148
137
  expect(e.payload.size).to eq 1
149
138
 
@@ -161,9 +150,9 @@ describe FunWithJsonApi::CollectionManager do
161
150
  describe '#replace_all_records' do
162
151
  context 'when no block is provided' do
163
152
  it 'raises a RelationshipNotSupported exception' do
164
- document = double('document')
153
+ collection = [double('collection_item')]
165
154
  expect do
166
- instance.replace_all_records(document)
155
+ instance.replace_all_records(collection)
167
156
  end.to raise_error(FunWithJsonApi::Exceptions::RelationshipMethodNotSupported) do |e|
168
157
  expect(e.message).to eq(
169
158
  'Override FunWithJsonApi::CollectionManager#replace_all_records'\
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fun_with_json_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8.1
4
+ version: 0.0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Morrall
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-01 00:00:00.000000000 Z
11
+ date: 2016-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails