Skip to content

Commit 2cc9e61

Browse files
committed
Merge pull request #168 from cerebris/format_tests
Format Fixes
2 parents 6ebf986 + 38bfd00 commit 2cc9e61

9 files changed

Lines changed: 286 additions & 31 deletions

File tree

lib/jsonapi/request.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def parse_single_replace_operation(data, keys)
400400
end
401401

402402
type = data[:type]
403-
if type.nil? || type != @resource_klass._type.to_s
403+
if type.nil? || type != format_key(@resource_klass._type).to_s
404404
raise JSONAPI::Exceptions::ParameterMissing.new(:type)
405405
end
406406

lib/jsonapi/resource_serializer.rb

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,23 +114,17 @@ def process_primary(source, requested_associations)
114114

115115
resource = source
116116
id = resource.id
117-
# ToDo: See if this is actually needed
118-
# if already_serialized?(@primary_class_name, id)
119-
# set_primary(@primary_class_name, id)
120-
# end
121-
122117
add_included_object(@primary_class_name, id, object_hash(source, requested_associations), true)
123118
end
124119
end
125120

126-
# Returns a serialized hash for the source model, with
121+
# Returns a serialized hash for the source model
127122
def object_hash(source, requested_associations)
128123
obj_hash = attribute_hash(source)
129124
links = links_hash(source, requested_associations)
130125

131-
# ToDo: Do we format these required keys
132-
obj_hash[format_key('type')] = format_value(source.class._type.to_s, :default, source)
133-
obj_hash[format_key('id')] ||= format_value(source.id, :id, source)
126+
obj_hash['type'] = format_key(source.class._type.to_s)
127+
obj_hash['id'] ||= format_value(source.id, :id, source)
134128
obj_hash.merge!({links: links}) unless links.empty?
135129
return obj_hash
136130
end
@@ -250,7 +244,7 @@ def has_one_linkage(source, association)
250244
linkage = {}
251245
linkage_id = foreign_key_value(source, association)
252246
if linkage_id
253-
linkage[:type] = format_route(association.type)
247+
linkage[:type] = format_key(association.type)
254248
linkage[:id] = linkage_id
255249
else
256250
linkage = nil
@@ -262,7 +256,7 @@ def has_many_linkage(source, association)
262256
linkage = []
263257
linkage_ids = foreign_key_value(source, association)
264258
linkage_ids.each do |linkage_id|
265-
linkage.append({type: format_route(association.type), id: linkage_id})
259+
linkage.append({type: format_key(association.type), id: linkage_id})
266260
end
267261
linkage
268262
end

test/fixtures/active_record.rb

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,35 @@
113113
t.integer :author_id
114114
t.timestamps null: false
115115
end
116+
117+
create_table :customers, force: true do |t|
118+
t.string :name
119+
end
120+
121+
create_table :purchase_orders, force: true do |t|
122+
t.date :order_date
123+
t.date :requested_delivery_date
124+
t.date :delivery_date
125+
t.integer :customer_id
126+
t.string :delivery_name
127+
t.string :delivery_address_1
128+
t.string :delivery_address_2
129+
t.string :delivery_city
130+
t.string :delivery_state
131+
t.string :delivery_postal_code
132+
t.float :delivery_fee
133+
t.float :tax
134+
t.float :total
135+
t.timestamps null: false
136+
end
137+
138+
create_table :line_items, force: true do |t|
139+
t.integer :purchase_order_id
140+
t.string :part_number
141+
t.string :quantity
142+
t.float :item_cost
143+
t.timestamps null: false
144+
end
116145
end
117146

118147
### MODELS
@@ -239,7 +268,15 @@ def add(breed)
239268
def remove(id)
240269
@breeds.delete(id)
241270
end
271+
end
272+
273+
class CustomerOrder < ActiveRecord::Base
274+
end
275+
276+
class PurchaseOrder < ActiveRecord::Base
277+
end
242278

279+
class LineItem < ActiveRecord::Base
243280
end
244281

245282
### PORO Data - don't do this in a production app
@@ -369,6 +406,28 @@ class ExpenseEntriesController < JSONAPI::ResourceController
369406
class IsoCurrenciesController < JSONAPI::ResourceController
370407
end
371408
end
409+
410+
module V6
411+
class CustomersController < JSONAPI::ResourceController
412+
end
413+
414+
class PurchaseOrdersController < JSONAPI::ResourceController
415+
end
416+
417+
class LineItemsController < JSONAPI::ResourceController
418+
end
419+
end
420+
421+
module V7
422+
class CustomersController < JSONAPI::ResourceController
423+
end
424+
425+
class PurchaseOrdersController < JSONAPI::ResourceController
426+
end
427+
428+
class LineItemsController < JSONAPI::ResourceController
429+
end
430+
end
372431
end
373432

374433
### RESOURCES
@@ -721,6 +780,48 @@ def fetchable_fields
721780
end
722781
end
723782

783+
module Api
784+
module V6
785+
class CustomerResource < JSONAPI::Resource
786+
attribute :name
787+
788+
has_many :purchase_orders
789+
end
790+
791+
class PurchaseOrderResource < JSONAPI::Resource
792+
attribute :order_date
793+
attribute :requested_delivery_date
794+
attribute :delivery_date
795+
attribute :delivery_name
796+
attribute :delivery_address_1
797+
attribute :delivery_address_2
798+
attribute :delivery_city
799+
attribute :delivery_state
800+
attribute :delivery_postal_code
801+
attribute :delivery_fee
802+
attribute :tax
803+
attribute :total
804+
805+
has_one :customer
806+
has_many :line_items
807+
end
808+
809+
class LineItemResource < JSONAPI::Resource
810+
attribute :part_number
811+
attribute :quantity
812+
attribute :item_cost
813+
814+
has_one :purchase_order
815+
end
816+
end
817+
818+
module V7
819+
CustomerResource = V6::CustomerResource.dup
820+
PurchaseOrderResource = V6::PurchaseOrderResource.dup
821+
LineItemResource = V6::LineItemResource.dup
822+
end
823+
end
824+
724825
warn 'start testing Name Collisions'
725826
# The name collisions only emmit warnings. Exceptions would change the flow of the tests
726827

test/fixtures/customers.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
xyz_corp:
2+
id: 1
3+
name: XYZ Corporation
4+
5+
abc_corp:
6+
id: 2
7+
name: ABC Corporation

test/fixtures/line_items.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
po_1_li_1:
2+
purchase_order_id: 1
3+
part_number: 556324
4+
quantity: 1
5+
item_cost: 45.67
6+
7+
po_1_li_2:
8+
purchase_order_id: 1
9+
part_number: 79324231A
10+
quantity: 3
11+
item_cost: 19.99

test/fixtures/purchase_orders.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
po_1:
2+
id: 1
3+
requested_delivery_date:
4+
delivery_date: nil
5+
customer_id: 1
6+
7+
po_2:
8+
id: 2
9+
requested_delivery_date:
10+
delivery_date: nil
11+
customer_id: 1

test/integration/requests/request_test.rb

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class RequestTest < ActionDispatch::IntegrationTest
55

66
def setup
77
JSONAPI.configuration.json_key_format = :underscored_key
8+
JSONAPI.configuration.route_format = :underscored_route
89
end
910

1011
def after_teardown
@@ -451,4 +452,116 @@ def test_flow_link_has_many_self_link_put
451452
})
452453
end
453454

455+
def test_flow_self_formatted_route_1
456+
JSONAPI.configuration.route_format = :dasherized_route
457+
JSONAPI.configuration.json_key_format = :dasherized_key
458+
get '/api/v6/purchase-orders'
459+
assert_equal 200, status
460+
po_1 = json_response['data'][0]
461+
assert_equal 'purchase-orders', json_response['data'][0]['type']
462+
463+
get po_1['links']['self']
464+
assert_equal 200, status
465+
assert_hash_equals po_1, json_response['data']
466+
end
467+
468+
def test_flow_self_formatted_route_2
469+
JSONAPI.configuration.route_format = :underscored_route
470+
JSONAPI.configuration.json_key_format = :dasherized_key
471+
get '/api/v7/purchase_orders'
472+
assert_equal 200, status
473+
assert_equal 'purchase-orders', json_response['data'][0]['type']
474+
475+
po_1 = json_response['data'][0]
476+
477+
get po_1['links']['self']
478+
assert_equal 200, status
479+
assert_hash_equals po_1, json_response['data']
480+
end
481+
482+
def test_flow_self_formatted_route_3
483+
JSONAPI.configuration.route_format = :underscored_route
484+
JSONAPI.configuration.json_key_format = :underscored_key
485+
get '/api/v7/purchase_orders'
486+
assert_equal 200, status
487+
assert_equal 'purchase_orders', json_response['data'][0]['type']
488+
489+
po_1 = json_response['data'][0]
490+
491+
get po_1['links']['self']
492+
assert_equal 200, status
493+
assert_hash_equals po_1, json_response['data']
494+
end
495+
496+
def test_post_formatted_keys
497+
JSONAPI.configuration.route_format = :dasherized_route
498+
JSONAPI.configuration.json_key_format = :dasherized_key
499+
post '/api/v6/purchase-orders',
500+
{
501+
'data' => {
502+
'delivery-name' => 'ASDFG Corp',
503+
'type' => 'purchase-orders'
504+
}
505+
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
506+
507+
assert_equal 201, status
508+
end
509+
510+
def test_post_formatted_keys_different_route_key_1
511+
JSONAPI.configuration.route_format = :dasherized_route
512+
JSONAPI.configuration.json_key_format = :underscored_key
513+
post '/api/v6/purchase-orders',
514+
{
515+
'data' => {
516+
'delivery_name' => 'ASDFG Corp',
517+
'type' => 'purchase_orders'
518+
}
519+
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
520+
521+
assert_equal 201, status
522+
end
523+
524+
def test_post_formatted_keys_different_route_key_2
525+
JSONAPI.configuration.route_format = :underscored_route
526+
JSONAPI.configuration.json_key_format = :dasherized_key
527+
post '/api/v7/purchase_orders',
528+
{
529+
'data' => {
530+
'delivery-name' => 'ASDFG Corp',
531+
'type' => 'purchase-orders'
532+
}
533+
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
534+
535+
assert_equal 201, status
536+
end
537+
538+
def test_post_formatted_keys_wrong_format
539+
JSONAPI.configuration.route_format = :dasherized_route
540+
JSONAPI.configuration.json_key_format = :dasherized_key
541+
post '/api/v6/purchase-orders',
542+
{
543+
'data' => {
544+
'delivery_name' => 'ASDFG Corp',
545+
'type' => 'purchase-orders'
546+
}
547+
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
548+
549+
assert_equal 400, status
550+
end
551+
552+
def test_patch_formatted_dasherized
553+
JSONAPI.configuration.route_format = :dasherized_route
554+
JSONAPI.configuration.json_key_format = :dasherized_key
555+
patch '/api/v6/purchase-orders/1',
556+
{
557+
'data' => {
558+
'id' => '1',
559+
'delivery-name' => 'ASDFG Corp',
560+
'type' => 'purchase-orders'
561+
}
562+
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
563+
564+
assert_equal 200, status
565+
end
566+
454567
end

test/test_helper.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@ def as_json(options = nil)
146146

147147
end
148148
JSONAPI.configuration.route_format = :underscored_route
149+
150+
JSONAPI.configuration.route_format = :dasherized_route
151+
namespace :v6 do
152+
jsonapi_resources :customers
153+
jsonapi_resources :purchase_orders
154+
jsonapi_resources :line_items
155+
end
156+
JSONAPI.configuration.route_format = :underscored_route
157+
158+
namespace :v7 do
159+
jsonapi_resources :customers
160+
jsonapi_resources :purchase_orders
161+
jsonapi_resources :line_items
162+
end
149163
end
150164
end
151165

0 commit comments

Comments
 (0)