test/unit/serializer/serializer_test.rb in jsonapi-resources-0.3.1 vs test/unit/serializer/serializer_test.rb in jsonapi-resources-0.3.2

- old
+ new

@@ -8,10 +8,11 @@ @fred = Person.find_by(name: 'Fred Reader') @expense_entry = ExpenseEntry.find(1) JSONAPI.configuration.json_key_format = :camelized_key + JSONAPI.configuration.route_format = :camelized_route end def after_teardown JSONAPI.configuration.json_key_format = :underscored_key end @@ -128,10 +129,14 @@ fields: {posts: [:id, :title, :author]}).serialize_to_hash(PostResource.new(@post)) ) end def test_serializer_include + serialized = JSONAPI::ResourceSerializer.new( + PostResource, + include: [:author] + ).serialize_to_hash(PostResource.new(@post)) assert_hash_equals( { data: { type: 'posts', @@ -186,20 +191,30 @@ related: '/people/1/preferences', linkage: { type: 'preferences', id: '1' } + }, + hairCut: { + self: "/people/1/links/hairCut", + related: "/people/1/hairCut", + linkage: nil } } } ] }, - JSONAPI::ResourceSerializer.new(PostResource, include: [:author]).serialize_to_hash( - PostResource.new(@post))) + serialized + ) end def test_serializer_key_format + serialized = JSONAPI::ResourceSerializer.new( + PostResource, + include: [:author], + key_formatter: UnderscoredKeyFormatter + ).serialize_to_hash(PostResource.new(@post)) assert_hash_equals( { data: { type: 'posts', @@ -254,18 +269,21 @@ related: '/people/1/preferences', linkage: { type: 'preferences', id: '1' } + }, + hair_cut: { + self: '/people/1/links/hairCut', + related: '/people/1/hairCut', + linkage: nil } } } ] }, - JSONAPI::ResourceSerializer.new(PostResource, - include: [:author], - key_formatter: UnderscoredKeyFormatter).serialize_to_hash(PostResource.new(@post)) + serialized ) end def test_serializer_include_sub_objects @@ -562,10 +580,14 @@ include: ['author.comments']).serialize_to_hash(PostResource.new(@post)) ) end def test_serializer_different_foreign_key + serialized = JSONAPI::ResourceSerializer.new( + PersonResource, + include: ['comments'] + ).serialize_to_hash(PersonResource.new(@fred)) assert_hash_equals( { data: { type: 'people', @@ -589,10 +611,15 @@ }, preferences: { self: "/people/2/links/preferences", related: "/people/2/preferences", linkage: nil + }, + hairCut: { + self: "/people/2/links/hairCut", + related: "/people/2/hairCut", + linkage: nil } } }, included: [ { @@ -651,11 +678,11 @@ } } } ] }, - JSONAPI::ResourceSerializer.new(PersonResource, include: ['comments']).serialize_to_hash(PersonResource.new(@fred)) + serialized ) end def test_serializer_array_of_resources @@ -1055,46 +1082,49 @@ }).serialize_to_hash(posts) ) end def test_serializer_camelized_with_value_formatters + # JSONAPI.configuration.json_key_format = :camelized_key + # JSONAPI.configuration.route_format = :camelized_route + assert_hash_equals( { data: { - type: 'expense_entries', + type: 'expenseEntries', id: '1', transactionDate: '04/15/2014', cost: 12.05, links: { - self: '/expense_entries/1', + self: '/expenseEntries/1', isoCurrency: { - self: '/expense_entries/1/links/iso_currency', - related: '/expense_entries/1/iso_currency', + self: '/expenseEntries/1/links/isoCurrency', + related: '/expenseEntries/1/isoCurrency', linkage: { - type: 'iso_currencies', + type: 'isoCurrencies', id: 'USD' } }, employee: { - self: '/expense_entries/1/links/employee', - related: '/expense_entries/1/employee', + self: '/expenseEntries/1/links/employee', + related: '/expenseEntries/1/employee', linkage: { type: 'people', id: '3' } } } }, included: [ { - type: 'iso_currencies', + type: 'isoCurrencies', id: 'USD', countryName: 'United States', name: 'United States Dollar', minorUnit: 'cent', links: { - self: '/iso_currencies/USD' + self: '/isoCurrencies/USD' } }, { type: 'people', id: '3', @@ -1106,11 +1136,11 @@ } } ] }, JSONAPI::ResourceSerializer.new(ExpenseEntryResource, - include: ['iso_currency', 'employee'], + include: ['isoCurrency', 'employee'], fields: {people: [:id, :name, :email, :date_joined]}).serialize_to_hash( ExpenseEntryResource.new(@expense_entry)) ) end @@ -1126,12 +1156,12 @@ name: 'Beta W', description: 'Newly discovered Planet W', links: { self: '/planets/8', planetType: { - self: '/planets/8/links/planet_type', - related: '/planets/8/planet_type', + self: '/planets/8/links/planetType', + related: '/planets/8/planetType', linkage: nil }, tags: { self: '/planets/8/links/tags', related: '/planets/8/tags' @@ -1163,14 +1193,14 @@ name: 'Beta X', description: 'Newly discovered Planet Z', links: { self: '/planets/7', planetType: { - self: '/planets/7/links/planet_type', - related: '/planets/7/planet_type', + self: '/planets/7/links/planetType', + related: '/planets/7/planetType', linkage: { - type: 'planet_types', + type: 'planetTypes', id: '5' } }, tags: { self: '/planets/7/links/tags', @@ -1188,12 +1218,12 @@ name: 'Beta W', description: 'Newly discovered Planet W', links: { self: '/planets/8', planetType: { - self: '/planets/8/links/planet_type', - related: '/planets/8/planet_type', + self: '/planets/8/links/planetType', + related: '/planets/8/planetType', linkage: nil }, tags: { self: '/planets/8/links/tags', related: '/planets/8/tags' @@ -1205,14 +1235,14 @@ } } ], included: [ { - type: 'planet_types', + type: 'planetTypes', id: '5', name: 'unknown', links: { - self: '/planet_types/5' + self: '/planetTypes/5' } } ] }, planet_hash) end