jsonapi-resources 0.7.1.beta1 → 0.7.1.beta2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +248 -74
- data/lib/jsonapi-resources.rb +5 -3
- data/lib/jsonapi/acts_as_resource_controller.rb +77 -14
- data/lib/jsonapi/configuration.rb +77 -16
- data/lib/jsonapi/error.rb +12 -0
- data/lib/jsonapi/error_codes.rb +2 -0
- data/lib/jsonapi/exceptions.rb +29 -9
- data/lib/jsonapi/formatter.rb +29 -4
- data/lib/jsonapi/link_builder.rb +18 -18
- data/lib/jsonapi/mime_types.rb +25 -6
- data/lib/jsonapi/naive_cache.rb +30 -0
- data/lib/jsonapi/operation.rb +10 -342
- data/lib/jsonapi/operation_dispatcher.rb +87 -0
- data/lib/jsonapi/operation_result.rb +2 -1
- data/lib/jsonapi/paginator.rb +6 -2
- data/lib/jsonapi/processor.rb +283 -0
- data/lib/jsonapi/relationship.rb +6 -4
- data/lib/jsonapi/{request.rb → request_parser.rb} +46 -35
- data/lib/jsonapi/resource.rb +88 -13
- data/lib/jsonapi/resource_controller.rb +2 -14
- data/lib/jsonapi/resource_controller_metal.rb +17 -0
- data/lib/jsonapi/resource_serializer.rb +62 -47
- data/lib/jsonapi/resources/version.rb +1 -1
- data/lib/jsonapi/response_document.rb +13 -2
- data/lib/jsonapi/routing_ext.rb +49 -11
- metadata +37 -129
- data/.gitignore +0 -22
- data/.travis.yml +0 -9
- data/Gemfile +0 -23
- data/Rakefile +0 -20
- data/jsonapi-resources.gemspec +0 -29
- data/lib/jsonapi/active_record_operations_processor.rb +0 -35
- data/lib/jsonapi/operations_processor.rb +0 -120
- data/locales/en.yml +0 -80
- data/test/config/database.yml +0 -5
- data/test/controllers/controller_test.rb +0 -3312
- data/test/fixtures/active_record.rb +0 -1486
- data/test/fixtures/author_details.yml +0 -9
- data/test/fixtures/book_authors.yml +0 -3
- data/test/fixtures/book_comments.yml +0 -12
- data/test/fixtures/books.yml +0 -7
- data/test/fixtures/categories.yml +0 -35
- data/test/fixtures/comments.yml +0 -21
- data/test/fixtures/comments_tags.yml +0 -20
- data/test/fixtures/companies.yml +0 -4
- data/test/fixtures/craters.yml +0 -9
- data/test/fixtures/customers.yml +0 -11
- data/test/fixtures/documents.yml +0 -3
- data/test/fixtures/expense_entries.yml +0 -13
- data/test/fixtures/facts.yml +0 -11
- data/test/fixtures/hair_cuts.yml +0 -3
- data/test/fixtures/iso_currencies.yml +0 -17
- data/test/fixtures/line_items.yml +0 -37
- data/test/fixtures/makes.yml +0 -2
- data/test/fixtures/moons.yml +0 -6
- data/test/fixtures/numeros_telefone.yml +0 -3
- data/test/fixtures/order_flags.yml +0 -7
- data/test/fixtures/people.yml +0 -31
- data/test/fixtures/pictures.yml +0 -15
- data/test/fixtures/planet_types.yml +0 -19
- data/test/fixtures/planets.yml +0 -47
- data/test/fixtures/posts.yml +0 -102
- data/test/fixtures/posts_tags.yml +0 -59
- data/test/fixtures/preferences.yml +0 -14
- data/test/fixtures/products.yml +0 -3
- data/test/fixtures/purchase_orders.yml +0 -23
- data/test/fixtures/sections.yml +0 -8
- data/test/fixtures/tags.yml +0 -39
- data/test/fixtures/vehicles.yml +0 -17
- data/test/fixtures/web_pages.yml +0 -3
- data/test/helpers/assertions.rb +0 -13
- data/test/helpers/functional_helpers.rb +0 -59
- data/test/helpers/value_matchers.rb +0 -60
- data/test/helpers/value_matchers_test.rb +0 -40
- data/test/integration/requests/namespaced_model_test.rb +0 -13
- data/test/integration/requests/request_test.rb +0 -932
- data/test/integration/routes/routes_test.rb +0 -218
- data/test/integration/sti_fields_test.rb +0 -18
- data/test/lib/generators/jsonapi/controller_generator_test.rb +0 -25
- data/test/lib/generators/jsonapi/resource_generator_test.rb +0 -30
- data/test/test_helper.rb +0 -342
- data/test/unit/formatters/dasherized_key_formatter_test.rb +0 -8
- data/test/unit/jsonapi_request/jsonapi_request_test.rb +0 -199
- data/test/unit/operation/operations_processor_test.rb +0 -528
- data/test/unit/pagination/offset_paginator_test.rb +0 -245
- data/test/unit/pagination/paged_paginator_test.rb +0 -242
- data/test/unit/resource/resource_test.rb +0 -560
- data/test/unit/serializer/include_directives_test.rb +0 -113
- data/test/unit/serializer/link_builder_test.rb +0 -244
- data/test/unit/serializer/polymorphic_serializer_test.rb +0 -383
- data/test/unit/serializer/response_document_test.rb +0 -61
- data/test/unit/serializer/serializer_test.rb +0 -1939
@@ -1,199 +0,0 @@
|
|
1
|
-
require File.expand_path('../../../test_helper', __FILE__)
|
2
|
-
|
3
|
-
class CatResource < JSONAPI::Resource
|
4
|
-
attribute :name
|
5
|
-
attribute :breed
|
6
|
-
|
7
|
-
has_one :mother, class_name: 'Cat'
|
8
|
-
has_one :father, class_name: 'Cat'
|
9
|
-
|
10
|
-
filters :name
|
11
|
-
end
|
12
|
-
|
13
|
-
class JSONAPIRequestTest < ActiveSupport::TestCase
|
14
|
-
def test_parse_includes_underscored
|
15
|
-
params = ActionController::Parameters.new(
|
16
|
-
{
|
17
|
-
controller: 'expense_entries',
|
18
|
-
action: 'index',
|
19
|
-
include: 'iso_currency'
|
20
|
-
}
|
21
|
-
)
|
22
|
-
|
23
|
-
request = JSONAPI::Request.new(
|
24
|
-
params,
|
25
|
-
{
|
26
|
-
context: nil,
|
27
|
-
key_formatter: JSONAPI::Formatter.formatter_for(:underscored_key)
|
28
|
-
}
|
29
|
-
)
|
30
|
-
|
31
|
-
assert request.errors.empty?
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_parse_dasherized_with_dasherized_include
|
35
|
-
params = ActionController::Parameters.new(
|
36
|
-
{
|
37
|
-
controller: 'expense_entries',
|
38
|
-
action: 'index',
|
39
|
-
include: 'iso-currency'
|
40
|
-
}
|
41
|
-
)
|
42
|
-
|
43
|
-
request = JSONAPI::Request.new(
|
44
|
-
params,
|
45
|
-
{
|
46
|
-
context: nil,
|
47
|
-
key_formatter: JSONAPI::Formatter.formatter_for(:dasherized_key)
|
48
|
-
}
|
49
|
-
)
|
50
|
-
|
51
|
-
assert request.errors.empty?
|
52
|
-
end
|
53
|
-
|
54
|
-
def test_parse_dasherized_with_underscored_include
|
55
|
-
params = ActionController::Parameters.new(
|
56
|
-
{
|
57
|
-
controller: 'expense_entries',
|
58
|
-
action: 'index',
|
59
|
-
include: 'iso_currency'
|
60
|
-
}
|
61
|
-
)
|
62
|
-
|
63
|
-
request = JSONAPI::Request.new(
|
64
|
-
params,
|
65
|
-
{
|
66
|
-
context: nil,
|
67
|
-
key_formatter: JSONAPI::Formatter.formatter_for(:dasherized_key)
|
68
|
-
}
|
69
|
-
)
|
70
|
-
|
71
|
-
refute request.errors.empty?
|
72
|
-
assert_equal 'iso_currency is not a valid relationship of expense-entries', request.errors[0].detail
|
73
|
-
end
|
74
|
-
|
75
|
-
def test_parse_fields_underscored
|
76
|
-
params = ActionController::Parameters.new(
|
77
|
-
{
|
78
|
-
controller: 'expense_entries',
|
79
|
-
action: 'index',
|
80
|
-
fields: {expense_entries: 'iso_currency'}
|
81
|
-
}
|
82
|
-
)
|
83
|
-
|
84
|
-
request = JSONAPI::Request.new(
|
85
|
-
params,
|
86
|
-
{
|
87
|
-
context: nil,
|
88
|
-
key_formatter: JSONAPI::Formatter.formatter_for(:underscored_key)
|
89
|
-
}
|
90
|
-
)
|
91
|
-
|
92
|
-
assert request.errors.empty?
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_parse_dasherized_with_dasherized_fields
|
96
|
-
params = ActionController::Parameters.new(
|
97
|
-
{
|
98
|
-
controller: 'expense_entries',
|
99
|
-
action: 'index',
|
100
|
-
fields: {
|
101
|
-
'expense-entries' => 'iso-currency'
|
102
|
-
}
|
103
|
-
}
|
104
|
-
)
|
105
|
-
|
106
|
-
request = JSONAPI::Request.new(
|
107
|
-
params,
|
108
|
-
{
|
109
|
-
context: nil,
|
110
|
-
key_formatter: JSONAPI::Formatter.formatter_for(:dasherized_key)
|
111
|
-
}
|
112
|
-
)
|
113
|
-
|
114
|
-
assert request.errors.empty?
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_parse_dasherized_with_underscored_fields
|
118
|
-
params = ActionController::Parameters.new(
|
119
|
-
{
|
120
|
-
controller: 'expense_entries',
|
121
|
-
action: 'index',
|
122
|
-
fields: {
|
123
|
-
'expense-entries' => 'iso_currency'
|
124
|
-
}
|
125
|
-
}
|
126
|
-
)
|
127
|
-
|
128
|
-
request = JSONAPI::Request.new(
|
129
|
-
params,
|
130
|
-
{
|
131
|
-
context: nil,
|
132
|
-
key_formatter: JSONAPI::Formatter.formatter_for(:dasherized_key)
|
133
|
-
}
|
134
|
-
)
|
135
|
-
|
136
|
-
refute request.errors.empty?
|
137
|
-
assert_equal 'iso_currency is not a valid field for expense-entries.', request.errors[0].detail
|
138
|
-
end
|
139
|
-
|
140
|
-
def test_parse_dasherized_with_underscored_resource
|
141
|
-
params = ActionController::Parameters.new(
|
142
|
-
{
|
143
|
-
controller: 'expense_entries',
|
144
|
-
action: 'index',
|
145
|
-
fields: {
|
146
|
-
'expense_entries' => 'iso-currency'
|
147
|
-
}
|
148
|
-
}
|
149
|
-
)
|
150
|
-
|
151
|
-
request = JSONAPI::Request.new(
|
152
|
-
params,
|
153
|
-
{
|
154
|
-
context: nil,
|
155
|
-
key_formatter: JSONAPI::Formatter.formatter_for(:dasherized_key)
|
156
|
-
}
|
157
|
-
)
|
158
|
-
|
159
|
-
refute request.errors.empty?
|
160
|
-
assert_equal 'expense_entries is not a valid resource.', request.errors[0].detail
|
161
|
-
end
|
162
|
-
|
163
|
-
def test_parse_filters_with_valid_filters
|
164
|
-
setup_request
|
165
|
-
@request.parse_filters({name: 'Whiskers'})
|
166
|
-
assert_equal(@request.filters[:name], 'Whiskers')
|
167
|
-
assert_equal(@request.errors, [])
|
168
|
-
end
|
169
|
-
|
170
|
-
def test_parse_filters_with_non_valid_filter
|
171
|
-
setup_request
|
172
|
-
@request.parse_filters({breed: 'Whiskers'}) # breed is not a set filter
|
173
|
-
assert_equal(@request.filters, {})
|
174
|
-
assert_equal(@request.errors.count, 1)
|
175
|
-
assert_equal(@request.errors.first.title, "Filter not allowed")
|
176
|
-
end
|
177
|
-
|
178
|
-
def test_parse_filters_with_no_filters
|
179
|
-
setup_request
|
180
|
-
@request.parse_filters(nil)
|
181
|
-
assert_equal(@request.filters, {})
|
182
|
-
assert_equal(@request.errors, [])
|
183
|
-
end
|
184
|
-
|
185
|
-
def test_parse_filters_with_invalid_filters_param
|
186
|
-
setup_request
|
187
|
-
@request.parse_filters('noeach') # String does not implement #each
|
188
|
-
assert_equal(@request.filters, {})
|
189
|
-
assert_equal(@request.errors.count, 1)
|
190
|
-
assert_equal(@request.errors.first.title, "Invalid filters syntax")
|
191
|
-
end
|
192
|
-
|
193
|
-
private
|
194
|
-
|
195
|
-
def setup_request
|
196
|
-
@request = JSONAPI::Request.new
|
197
|
-
@request.resource_klass = CatResource
|
198
|
-
end
|
199
|
-
end
|
@@ -1,528 +0,0 @@
|
|
1
|
-
require File.expand_path('../../../test_helper', __FILE__)
|
2
|
-
|
3
|
-
class TestOperationsProcessor < JSONAPI::OperationsProcessor
|
4
|
-
before_operation :log_before_operation
|
5
|
-
|
6
|
-
after_operation :log_after_operation
|
7
|
-
|
8
|
-
around_operation :log_around_operation
|
9
|
-
|
10
|
-
def log_before_operation
|
11
|
-
msg = "Before Operation"
|
12
|
-
# puts msg
|
13
|
-
end
|
14
|
-
|
15
|
-
def log_around_operation
|
16
|
-
msg = "Starting... #{@operation.class.name}"
|
17
|
-
# puts msg
|
18
|
-
yield
|
19
|
-
msg = "... Finishing #{@operation.class.name}"
|
20
|
-
# puts msg
|
21
|
-
end
|
22
|
-
|
23
|
-
def log_after_operation
|
24
|
-
msg = "After Operation"
|
25
|
-
# puts msg
|
26
|
-
end
|
27
|
-
|
28
|
-
before_operations :log_before_operations
|
29
|
-
|
30
|
-
after_operations :log_after_operations
|
31
|
-
|
32
|
-
around_operations :log_around_operations
|
33
|
-
|
34
|
-
def log_before_operations
|
35
|
-
msg = "Before #{@operations.count} Operation(s)"
|
36
|
-
# puts msg
|
37
|
-
end
|
38
|
-
|
39
|
-
def log_around_operations
|
40
|
-
msg = "Starting #{@operations.count} Operation(s)..."
|
41
|
-
# puts msg
|
42
|
-
yield
|
43
|
-
msg = "...Finishing Up Operations"
|
44
|
-
# puts msg
|
45
|
-
end
|
46
|
-
|
47
|
-
def log_after_operations
|
48
|
-
msg = "After Operations"
|
49
|
-
# puts msg
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
class OperationsProcessorTest < Minitest::Test
|
54
|
-
def setup
|
55
|
-
betax = Planet.find(5)
|
56
|
-
betay = Planet.find(6)
|
57
|
-
betaz = Planet.find(7)
|
58
|
-
unknown = PlanetType.find(5)
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_create_single_resource
|
62
|
-
op = TestOperationsProcessor.new()
|
63
|
-
|
64
|
-
count = Planet.count
|
65
|
-
|
66
|
-
operations = [
|
67
|
-
JSONAPI::CreateResourceOperation.new(PlanetResource, data: {attributes: {'name' => 'earth', 'description' => 'The best planet ever.'}})
|
68
|
-
]
|
69
|
-
|
70
|
-
request = JSONAPI::Request.new
|
71
|
-
request.operations = operations
|
72
|
-
|
73
|
-
operation_results = op.process(request)
|
74
|
-
|
75
|
-
assert_kind_of(JSONAPI::OperationResults, operation_results)
|
76
|
-
assert_equal(:created, operation_results.results[0].code)
|
77
|
-
assert_equal(operation_results.results.size, 1)
|
78
|
-
assert_equal(Planet.count, count + 1)
|
79
|
-
end
|
80
|
-
|
81
|
-
def test_create_multiple_resources
|
82
|
-
op = JSONAPI::OperationsProcessor.new()
|
83
|
-
|
84
|
-
count = Planet.count
|
85
|
-
|
86
|
-
operations = [
|
87
|
-
JSONAPI::CreateResourceOperation.new(PlanetResource, data: {attributes: {'name' => 'earth', 'description' => 'The best planet for life.'}}),
|
88
|
-
JSONAPI::CreateResourceOperation.new(PlanetResource, data: {attributes: {'name' => 'mars', 'description' => 'The red planet.'}}),
|
89
|
-
JSONAPI::CreateResourceOperation.new(PlanetResource, data: {attributes: {'name' => 'venus', 'description' => 'A very hot planet.'}})
|
90
|
-
]
|
91
|
-
|
92
|
-
request = JSONAPI::Request.new
|
93
|
-
request.operations = operations
|
94
|
-
|
95
|
-
operation_results = op.process(request)
|
96
|
-
|
97
|
-
assert_kind_of(JSONAPI::OperationResults, operation_results)
|
98
|
-
assert_equal(operation_results.results.size, 3)
|
99
|
-
assert_equal(Planet.count, count + 3)
|
100
|
-
end
|
101
|
-
|
102
|
-
def test_replace_to_one_relationship
|
103
|
-
op = JSONAPI::OperationsProcessor.new()
|
104
|
-
|
105
|
-
saturn = Planet.find(1)
|
106
|
-
gas_giant = PlanetType.find(1)
|
107
|
-
planetoid = PlanetType.find(2)
|
108
|
-
assert_equal(saturn.planet_type_id, planetoid.id)
|
109
|
-
|
110
|
-
operations = [
|
111
|
-
JSONAPI::ReplaceToOneRelationshipOperation.new(
|
112
|
-
PlanetResource,
|
113
|
-
{
|
114
|
-
resource_id: saturn.id,
|
115
|
-
relationship_type: :planet_type,
|
116
|
-
key_value: gas_giant.id
|
117
|
-
}
|
118
|
-
)
|
119
|
-
]
|
120
|
-
|
121
|
-
request = JSONAPI::Request.new
|
122
|
-
request.operations = operations
|
123
|
-
|
124
|
-
operation_results = op.process(request)
|
125
|
-
|
126
|
-
assert_kind_of(JSONAPI::OperationResults, operation_results)
|
127
|
-
assert_kind_of(JSONAPI::OperationResult, operation_results.results[0])
|
128
|
-
assert_equal(:no_content, operation_results.results[0].code)
|
129
|
-
|
130
|
-
saturn.reload
|
131
|
-
assert_equal(saturn.planet_type_id, gas_giant.id)
|
132
|
-
|
133
|
-
# Remove link
|
134
|
-
operations = [
|
135
|
-
JSONAPI::ReplaceToOneRelationshipOperation.new(
|
136
|
-
PlanetResource,
|
137
|
-
{
|
138
|
-
resource_id: saturn.id,
|
139
|
-
relationship_type: :planet_type,
|
140
|
-
key_value: nil
|
141
|
-
}
|
142
|
-
)
|
143
|
-
]
|
144
|
-
|
145
|
-
request = JSONAPI::Request.new
|
146
|
-
request.operations = operations
|
147
|
-
|
148
|
-
op.process(request)
|
149
|
-
saturn.reload
|
150
|
-
assert_equal(saturn.planet_type_id, nil)
|
151
|
-
|
152
|
-
# Reset
|
153
|
-
operations = [
|
154
|
-
JSONAPI::ReplaceToOneRelationshipOperation.new(
|
155
|
-
PlanetResource,
|
156
|
-
{
|
157
|
-
resource_id: saturn.id,
|
158
|
-
relationship_type: :planet_type,
|
159
|
-
key_value: 5
|
160
|
-
}
|
161
|
-
)
|
162
|
-
]
|
163
|
-
|
164
|
-
request = JSONAPI::Request.new
|
165
|
-
request.operations = operations
|
166
|
-
|
167
|
-
op.process(request)
|
168
|
-
saturn.reload
|
169
|
-
assert_equal(saturn.planet_type_id, 5)
|
170
|
-
end
|
171
|
-
|
172
|
-
def test_create_to_many_relationship
|
173
|
-
op = JSONAPI::OperationsProcessor.new()
|
174
|
-
|
175
|
-
betax = Planet.find(5)
|
176
|
-
betay = Planet.find(6)
|
177
|
-
betaz = Planet.find(7)
|
178
|
-
gas_giant = PlanetType.find(1)
|
179
|
-
unknown = PlanetType.find(5)
|
180
|
-
betax.planet_type_id = unknown.id
|
181
|
-
betay.planet_type_id = unknown.id
|
182
|
-
betaz.planet_type_id = unknown.id
|
183
|
-
betax.save!
|
184
|
-
betay.save!
|
185
|
-
betaz.save!
|
186
|
-
|
187
|
-
operations = [
|
188
|
-
JSONAPI::CreateToManyRelationshipOperation.new(
|
189
|
-
PlanetTypeResource,
|
190
|
-
{
|
191
|
-
resource_id: gas_giant.id,
|
192
|
-
relationship_type: :planets,
|
193
|
-
data: [betax.id, betay.id, betaz.id]
|
194
|
-
}
|
195
|
-
)
|
196
|
-
]
|
197
|
-
|
198
|
-
request = JSONAPI::Request.new
|
199
|
-
request.operations = operations
|
200
|
-
|
201
|
-
op.process(request)
|
202
|
-
|
203
|
-
betax.reload
|
204
|
-
betay.reload
|
205
|
-
betaz.reload
|
206
|
-
|
207
|
-
assert_equal(betax.planet_type_id, gas_giant.id)
|
208
|
-
assert_equal(betay.planet_type_id, gas_giant.id)
|
209
|
-
assert_equal(betaz.planet_type_id, gas_giant.id)
|
210
|
-
|
211
|
-
# Reset
|
212
|
-
betax.planet_type_id = unknown.id
|
213
|
-
betay.planet_type_id = unknown.id
|
214
|
-
betaz.planet_type_id = unknown.id
|
215
|
-
betax.save!
|
216
|
-
betay.save!
|
217
|
-
betaz.save!
|
218
|
-
end
|
219
|
-
|
220
|
-
def test_replace_to_many_relationship
|
221
|
-
op = JSONAPI::OperationsProcessor.new()
|
222
|
-
|
223
|
-
betax = Planet.find(5)
|
224
|
-
betay = Planet.find(6)
|
225
|
-
betaz = Planet.find(7)
|
226
|
-
gas_giant = PlanetType.find(1)
|
227
|
-
unknown = PlanetType.find(5)
|
228
|
-
betax.planet_type_id = unknown.id
|
229
|
-
betay.planet_type_id = unknown.id
|
230
|
-
betaz.planet_type_id = unknown.id
|
231
|
-
betax.save!
|
232
|
-
betay.save!
|
233
|
-
betaz.save!
|
234
|
-
|
235
|
-
operations = [
|
236
|
-
JSONAPI::ReplaceToManyRelationshipOperation.new(
|
237
|
-
PlanetTypeResource,
|
238
|
-
{
|
239
|
-
resource_id: gas_giant.id,
|
240
|
-
relationship_type: :planets,
|
241
|
-
data: [betax.id, betay.id, betaz.id]
|
242
|
-
}
|
243
|
-
)
|
244
|
-
]
|
245
|
-
|
246
|
-
request = JSONAPI::Request.new
|
247
|
-
request.operations = operations
|
248
|
-
|
249
|
-
op.process(request)
|
250
|
-
|
251
|
-
betax.reload
|
252
|
-
betay.reload
|
253
|
-
betaz.reload
|
254
|
-
|
255
|
-
assert_equal(betax.planet_type_id, gas_giant.id)
|
256
|
-
assert_equal(betay.planet_type_id, gas_giant.id)
|
257
|
-
assert_equal(betaz.planet_type_id, gas_giant.id)
|
258
|
-
|
259
|
-
# Reset
|
260
|
-
betax.planet_type_id = unknown.id
|
261
|
-
betay.planet_type_id = unknown.id
|
262
|
-
betaz.planet_type_id = unknown.id
|
263
|
-
betax.save!
|
264
|
-
betay.save!
|
265
|
-
betaz.save!
|
266
|
-
end
|
267
|
-
|
268
|
-
def test_replace_attributes
|
269
|
-
op = JSONAPI::OperationsProcessor.new()
|
270
|
-
|
271
|
-
count = Planet.count
|
272
|
-
saturn = Planet.find(1)
|
273
|
-
assert_equal(saturn.name, 'Satern')
|
274
|
-
|
275
|
-
operations = [
|
276
|
-
JSONAPI::ReplaceFieldsOperation.new(
|
277
|
-
PlanetResource,
|
278
|
-
{
|
279
|
-
resource_id: 1,
|
280
|
-
data: {attributes: {'name' => 'saturn'}}
|
281
|
-
}
|
282
|
-
)
|
283
|
-
]
|
284
|
-
|
285
|
-
request = JSONAPI::Request.new
|
286
|
-
request.operations = operations
|
287
|
-
|
288
|
-
operation_results = op.process(request)
|
289
|
-
|
290
|
-
assert_kind_of(JSONAPI::OperationResults, operation_results)
|
291
|
-
assert_equal(operation_results.results.size, 1)
|
292
|
-
|
293
|
-
assert_kind_of(JSONAPI::ResourceOperationResult, operation_results.results[0])
|
294
|
-
assert_equal(:ok, operation_results.results[0].code)
|
295
|
-
|
296
|
-
saturn = Planet.find(1)
|
297
|
-
|
298
|
-
assert_equal(saturn.name, 'saturn')
|
299
|
-
|
300
|
-
assert_equal(Planet.count, count)
|
301
|
-
end
|
302
|
-
|
303
|
-
def test_remove_resource
|
304
|
-
op = JSONAPI::OperationsProcessor.new
|
305
|
-
|
306
|
-
count = Planet.count
|
307
|
-
makemake = Planet.find(2)
|
308
|
-
assert_equal(makemake.name, 'Makemake')
|
309
|
-
|
310
|
-
operations = [
|
311
|
-
JSONAPI::RemoveResourceOperation.new(PlanetResource, resource_id: 2),
|
312
|
-
]
|
313
|
-
|
314
|
-
request = JSONAPI::Request.new
|
315
|
-
request.operations = operations
|
316
|
-
|
317
|
-
operation_results = op.process(request)
|
318
|
-
|
319
|
-
assert_kind_of(JSONAPI::OperationResults, operation_results)
|
320
|
-
assert_equal(operation_results.results.size, 1)
|
321
|
-
|
322
|
-
assert_kind_of(JSONAPI::OperationResult, operation_results.results[0])
|
323
|
-
assert_equal(:no_content, operation_results.results[0].code)
|
324
|
-
assert_equal(Planet.count, count - 1)
|
325
|
-
end
|
326
|
-
|
327
|
-
def test_rollback_from_error
|
328
|
-
op = ActiveRecordOperationsProcessor.new
|
329
|
-
|
330
|
-
count = Planet.count
|
331
|
-
|
332
|
-
operations = [
|
333
|
-
JSONAPI::RemoveResourceOperation.new(PlanetResource, resource_id: 3),
|
334
|
-
JSONAPI::RemoveResourceOperation.new(PlanetResource, resource_id: 4),
|
335
|
-
JSONAPI::RemoveResourceOperation.new(PlanetResource, resource_id: 4)
|
336
|
-
]
|
337
|
-
|
338
|
-
request = JSONAPI::Request.new
|
339
|
-
request.operations = operations
|
340
|
-
|
341
|
-
operation_results = op.process(request)
|
342
|
-
|
343
|
-
assert_kind_of(JSONAPI::OperationResults, operation_results)
|
344
|
-
|
345
|
-
assert_equal(Planet.count, count)
|
346
|
-
|
347
|
-
assert_equal(operation_results.results.size, 3)
|
348
|
-
|
349
|
-
assert_kind_of(JSONAPI::OperationResult, operation_results.results[0])
|
350
|
-
assert_equal(:no_content, operation_results.results[0].code)
|
351
|
-
assert_equal(:no_content, operation_results.results[1].code)
|
352
|
-
assert_equal('404', operation_results.results[2].code)
|
353
|
-
end
|
354
|
-
|
355
|
-
def test_show_operation
|
356
|
-
op = JSONAPI::OperationsProcessor.new
|
357
|
-
|
358
|
-
operations = [
|
359
|
-
JSONAPI::ShowOperation.new(PlanetResource, {id: '1'})
|
360
|
-
]
|
361
|
-
|
362
|
-
request = JSONAPI::Request.new
|
363
|
-
request.operations = operations
|
364
|
-
|
365
|
-
operation_results = op.process(request)
|
366
|
-
|
367
|
-
assert_kind_of(JSONAPI::OperationResults, operation_results)
|
368
|
-
assert_equal(operation_results.results.size, 1)
|
369
|
-
refute operation_results.has_errors?
|
370
|
-
end
|
371
|
-
|
372
|
-
def test_show_operation_error
|
373
|
-
op = JSONAPI::OperationsProcessor.new
|
374
|
-
|
375
|
-
operations = [
|
376
|
-
JSONAPI::ShowOperation.new(PlanetResource, {id: '145'})
|
377
|
-
]
|
378
|
-
|
379
|
-
request = JSONAPI::Request.new
|
380
|
-
request.operations = operations
|
381
|
-
|
382
|
-
operation_results = op.process(request)
|
383
|
-
|
384
|
-
assert_kind_of(JSONAPI::OperationResults, operation_results)
|
385
|
-
assert_equal(operation_results.results.size, 1)
|
386
|
-
assert operation_results.has_errors?
|
387
|
-
end
|
388
|
-
|
389
|
-
def test_show_relationship_operation
|
390
|
-
op = JSONAPI::OperationsProcessor.new
|
391
|
-
|
392
|
-
operations = [
|
393
|
-
JSONAPI::ShowRelationshipOperation.new(PlanetResource, {parent_key: '1', relationship_type: :planet_type})
|
394
|
-
]
|
395
|
-
|
396
|
-
request = JSONAPI::Request.new
|
397
|
-
request.operations = operations
|
398
|
-
|
399
|
-
operation_results = op.process(request)
|
400
|
-
|
401
|
-
assert_kind_of(JSONAPI::OperationResults, operation_results)
|
402
|
-
assert_equal(operation_results.results.size, 1)
|
403
|
-
refute operation_results.has_errors?
|
404
|
-
end
|
405
|
-
|
406
|
-
def test_show_relationship_operation_error
|
407
|
-
op = JSONAPI::OperationsProcessor.new
|
408
|
-
|
409
|
-
operations = [
|
410
|
-
JSONAPI::ShowRelationshipOperation.new(PlanetResource, {parent_key: '145', relationship_type: :planet_type})
|
411
|
-
]
|
412
|
-
|
413
|
-
request = JSONAPI::Request.new
|
414
|
-
request.operations = operations
|
415
|
-
|
416
|
-
operation_results = op.process(request)
|
417
|
-
|
418
|
-
assert_kind_of(JSONAPI::OperationResults, operation_results)
|
419
|
-
assert_equal(operation_results.results.size, 1)
|
420
|
-
assert operation_results.has_errors?
|
421
|
-
end
|
422
|
-
|
423
|
-
def test_show_related_resource_operation
|
424
|
-
op = JSONAPI::OperationsProcessor.new
|
425
|
-
|
426
|
-
operations = [
|
427
|
-
JSONAPI::ShowRelatedResourceOperation.new(PlanetResource,
|
428
|
-
{
|
429
|
-
source_klass: PlanetResource,
|
430
|
-
source_id: '1',
|
431
|
-
relationship_type: :planet_type})
|
432
|
-
]
|
433
|
-
|
434
|
-
request = JSONAPI::Request.new
|
435
|
-
request.operations = operations
|
436
|
-
|
437
|
-
operation_results = op.process(request)
|
438
|
-
|
439
|
-
assert_kind_of(JSONAPI::OperationResults, operation_results)
|
440
|
-
assert_equal(operation_results.results.size, 1)
|
441
|
-
refute operation_results.has_errors?
|
442
|
-
end
|
443
|
-
|
444
|
-
def test_show_related_resource_operation_error
|
445
|
-
op = JSONAPI::OperationsProcessor.new
|
446
|
-
|
447
|
-
operations = [
|
448
|
-
JSONAPI::ShowRelatedResourceOperation.new(PlanetResource,
|
449
|
-
{
|
450
|
-
source_klass: PlanetResource,
|
451
|
-
source_id: '145',
|
452
|
-
relationship_type: :planet_type})
|
453
|
-
]
|
454
|
-
|
455
|
-
request = JSONAPI::Request.new
|
456
|
-
request.operations = operations
|
457
|
-
|
458
|
-
operation_results = op.process(request)
|
459
|
-
|
460
|
-
assert_kind_of(JSONAPI::OperationResults, operation_results)
|
461
|
-
assert_equal(operation_results.results.size, 1)
|
462
|
-
assert operation_results.has_errors?
|
463
|
-
end
|
464
|
-
|
465
|
-
def test_show_related_resources_operation
|
466
|
-
op = JSONAPI::OperationsProcessor.new
|
467
|
-
|
468
|
-
operations = [
|
469
|
-
JSONAPI::ShowRelatedResourcesOperation.new(PlanetResource,
|
470
|
-
{
|
471
|
-
source_klass: PlanetResource,
|
472
|
-
source_id: '1',
|
473
|
-
relationship_type: :moons})
|
474
|
-
]
|
475
|
-
|
476
|
-
request = JSONAPI::Request.new
|
477
|
-
request.operations = operations
|
478
|
-
|
479
|
-
operation_results = op.process(request)
|
480
|
-
|
481
|
-
assert_kind_of(JSONAPI::OperationResults, operation_results)
|
482
|
-
assert_equal(operation_results.results.size, 1)
|
483
|
-
refute operation_results.has_errors?
|
484
|
-
end
|
485
|
-
|
486
|
-
def test_show_related_resources_operation_error
|
487
|
-
op = JSONAPI::OperationsProcessor.new
|
488
|
-
|
489
|
-
operations = [
|
490
|
-
JSONAPI::ShowRelatedResourcesOperation.new(PlanetResource,
|
491
|
-
{
|
492
|
-
source_klass: PlanetResource,
|
493
|
-
source_id: '145',
|
494
|
-
relationship_type: :moons})
|
495
|
-
]
|
496
|
-
|
497
|
-
request = JSONAPI::Request.new
|
498
|
-
request.operations = operations
|
499
|
-
|
500
|
-
operation_results = op.process(request)
|
501
|
-
|
502
|
-
assert_kind_of(JSONAPI::OperationResults, operation_results)
|
503
|
-
assert_equal(operation_results.results.size, 1)
|
504
|
-
assert operation_results.has_errors?
|
505
|
-
end
|
506
|
-
|
507
|
-
def test_safe_run_callback_pass
|
508
|
-
op = JSONAPI::OperationsProcessor.new
|
509
|
-
error = StandardError.new
|
510
|
-
|
511
|
-
check = false
|
512
|
-
callback = ->(error) { check = true}
|
513
|
-
|
514
|
-
op.send(:safe_run_callback, callback, error)
|
515
|
-
assert check
|
516
|
-
end
|
517
|
-
|
518
|
-
def test_safe_run_callback_catch_fail
|
519
|
-
op = JSONAPI::OperationsProcessor.new
|
520
|
-
error = StandardError.new
|
521
|
-
|
522
|
-
callback = ->(error) { nil.explosions}
|
523
|
-
result = op.send(:safe_run_callback, callback, error)
|
524
|
-
|
525
|
-
assert_instance_of(JSONAPI::ErrorsOperationResult, result)
|
526
|
-
assert_equal(result.code, '500')
|
527
|
-
end
|
528
|
-
end
|