test/unit/serializer/serializer_test.rb in jsonapi-resources-0.0.14 vs test/unit/serializer/serializer_test.rb in jsonapi-resources-0.0.15

- old
+ new

@@ -20,35 +20,54 @@ def test_serializer assert_hash_equals( { posts: { - id: 1, + id: '1', title: 'New post', body: 'A body!!!', subject: 'New post', links: { section: nil, - author: 1, - tags: [1, 2, 3], - comments: [1, 2] + author: '1', + tags: ['1', '2', '3'], + comments: ['1', '2'] } } }, JSONAPI::ResourceSerializer.new(PostResource).serialize_to_hash( PostResource.new(@post))) end + def test_serializer_namespaced_resource + assert_hash_equals( + { + posts: { + id: '1', + title: 'New post', + body: 'A body!!!', + subject: 'New post', + links: { + section: nil, + writer: '1', + comments: ['1', '2'] + } + } + }, + JSONAPI::ResourceSerializer.new(Api::V1::PostResource).serialize_to_hash( + Api::V1::PostResource.new(@post))) + end + def test_serializer_limited_fieldset assert_hash_equals( { posts: { - id: 1, + id: '1', title: 'New post', links: { - author: 1 + author: '1' } } }, JSONAPI::ResourceSerializer.new(PostResource).serialize_to_hash( PostResource.new(@post), @@ -58,30 +77,30 @@ def test_serializer_include assert_hash_equals( { posts: { - id: 1, + id: '1', title: 'New post', body: 'A body!!!', subject: 'New post', links: { - author: 1, - tags: [1, 2, 3], - comments: [1, 2], + author: '1', + tags: ['1', '2', '3'], + comments: ['1', '2'], section: nil } }, linked: { people: [{ - id: 1, + id: '1', name: 'Joe Author', email: 'joe@xyz.fake', dateJoined: '2013-08-07 16:25:00 -0400', links: { - comments: [1], - posts: [1, 2, 11] + comments: ['1'], + posts: ['1', '2', '11'] } }] } }, JSONAPI::ResourceSerializer.new(PostResource).serialize_to_hash( @@ -91,30 +110,30 @@ def test_serializer_key_format assert_hash_equals( { posts: { - id: 1, + id: '1', title: 'New post', body: 'A body!!!', subject: 'New post', links: { - author: 1, - tags: [1, 2, 3], - comments: [1, 2], + author: '1', + tags: ['1', '2', '3'], + comments: ['1', '2'], section: nil } }, linked: { people: [{ - id: 1, + id: '1', name: 'Joe Author', email: 'joe@xyz.fake', date_joined: '2013-08-07 16:25:00 -0400', links: { - comments: [1], - posts: [1, 2, 11] + comments: ['1'], + posts: ['1', '2', '11'] } }] } }, JSONAPI::ResourceSerializer.new(PostResource).serialize_to_hash( @@ -126,62 +145,62 @@ def test_serializer_include_sub_objects assert_hash_equals( { posts: { - id: 1, + id: '1', title: 'New post', body: 'A body!!!', subject: 'New post', links: { - author: 1, - tags: [1, 2, 3], - comments: [1, 2], + author: '1', + tags: ['1', '2', '3'], + comments: ['1', '2'], section: nil } }, linked: { tags: [ { - id: 1, + id: '1', name: 'short', links: { posts: :not_nil } }, { - id: 2, + id: '2', name: 'whiny', links: { posts: :not_nil } }, { - id: 4, + id: '4', name: 'happy', links: { posts: :not_nil } } ], comments: [ { - id: 1, + id: '1', body: 'what a dumb post', links: { - author: 1, - post: 1, - tags: [2, 1] + author: '1', + post: '1', + tags: ['2', '1'] } }, { - id: 2, + id: '2', body: 'i liked it', links: { - author: 2, - post: 1, - tags: [4, 1] + author: '2', + post: '1', + tags: ['4', '1'] } } ] } }, @@ -192,39 +211,39 @@ def test_serializer_include_has_many_sub_objects_only assert_hash_equals( { posts: { - id: 1, + id: '1', title: 'New post', body: 'A body!!!', subject: 'New post', links: { - author: 1, - tags: [1, 2, 3], - comments: [1, 2], + author: '1', + tags: ['1', '2', '3'], + comments: ['1', '2'], section: nil } }, linked: { tags: [ { - id: 1, + id: '1', name: 'short', links: { posts: :not_nil } }, { - id: 2, + id: '2', name: 'whiny', links: { posts: :not_nil } }, { - id: 4, + id: '4', name: 'happy', links: { posts: :not_nil } } @@ -238,30 +257,30 @@ def test_serializer_include_has_one_sub_objects_only assert_hash_equals( { posts: { - id: 1, + id: '1', title: 'New post', body: 'A body!!!', subject: 'New post', links: { - author: 1, - tags: [1, 2, 3], - comments: [1, 2], + author: '1', + tags: ['1', '2', '3'], + comments: ['1', '2'], section: nil } }, linked: { comments: [ { - id: 1, + id: '1', body: 'what a dumb post', links: { - author: 1, - post: 1, - tags: [2, 1] + author: '1', + post: '1', + tags: ['2', '1'] } } ] } }, @@ -272,36 +291,36 @@ def test_serializer_different_foreign_key assert_hash_equals( { people: { - id: 2, + id: '2', name: 'Fred Reader', email: 'fred@xyz.fake', dateJoined: '2013-10-31 16:25:00 -0400', links: { posts: [], - comments: [2, 3] + comments: ['2', '3'] } }, linked: { comments: [{ - id: 2, + id: '2', body: 'i liked it', links: { - author: 2, - post: 1, - tags: [4, 1] + author: '2', + post: '1', + tags: ['4', '1'] } }, { - id: 3, + id: '3', body: 'Thanks man. Great post. But what is JR?', links: { - author: 2, - post: 2, - tags: [5] + author: '2', + post: '2', + tags: ['5'] } } ] } }, @@ -317,90 +336,90 @@ end assert_hash_equals( { posts: [{ - id: 1, + id: '1', title: 'New post', body: 'A body!!!', subject: 'New post', links: { - author: 1, - tags: [1, 2, 3], - comments: [1, 2], + author: '1', + tags: ['1', '2', '3'], + comments: ['1', '2'], section: nil } }, { - id: 2, + id: '2', title: 'JR Solves your serialization woes!', body: 'Use JR', subject: 'JR Solves your serialization woes!', links: { - author: 1, - tags: [5], - comments: [3], - section: 3 + author: '1', + tags: ['5'], + comments: ['3'], + section: '3' } }], linked: { tags: [ { - id: 1, + id: '1', name: 'short', links: { posts: :not_nil } }, { - id: 2, + id: '2', name: 'whiny', links: { posts: :not_nil } }, { - id: 4, + id: '4', name: 'happy', links: { posts: :not_nil } }, { - id: 5, + id: '5', name: 'JR', links: { - posts: [2, 11] + posts: ['2', '11'] } } ], comments: [ { - id: 1, + id: '1', body: 'what a dumb post', links: { - author: 1, - post: 1, - tags: [2, 1] + author: '1', + post: '1', + tags: ['2', '1'] } }, { - id: 2, + id: '2', body: 'i liked it', links: { - author: 2, - post: 1, - tags: [4, 1] + author: '2', + post: '1', + tags: ['4', '1'] } }, { - id: 3, + id: '3', body: 'Thanks man. Great post. But what is JR?', links: { - author: 2, - post: 2, - tags: [5] + author: '2', + post: '2', + tags: ['5'] } } ] } }, @@ -416,21 +435,21 @@ end assert_hash_equals( { posts: [{ - id: 1, + id: '1', title: 'New post', links: { - author: 1 + author: '1' } }, { - id: 2, + id: '2', title: 'JR Solves your serialization woes!', links: { - author: 1 + author: '1' } }], linked: { tags: [ { @@ -446,46 +465,46 @@ name: 'JR' } ], comments: [ { - id: 1, + id: '1', body: 'what a dumb post', links: { - post: 1 + post: '1' } }, { - id: 2, + id: '2', body: 'i liked it', links: { - post: 1 + post: '1' } }, { - id: 3, + id: '3', body: 'Thanks man. Great post. But what is JR?', links: { - post: 2 + post: '2' } } ], posts: [ { - id: 11, + id: '11', title: 'JR How To', links: { - author: 1 + author: '1' } } ], people: [ { - id: 1, + id: '1', email: 'joe@xyz.fake', links: { - comments: [1] + comments: ['1'] } }] } }, JSONAPI::ResourceSerializer.new(PostResource).serialize_to_hash( @@ -501,27 +520,27 @@ def test_serializer_camelized_with_value_formatters assert_hash_equals( { expenseEntries: { - id: 1, + id: '1', transactionDate: '04/15/2014', cost: 12.05, links: { isoCurrency: 'USD', - employee: 3 + employee: '3' } }, linked: { isoCurrencies: [{ id: 'USD', countryName: 'United States', name: 'United States Dollar', minorUnit: 'cent' }], people: [{ - id: 3, + id: '3', name: 'Lazy Author', email: 'lazy@xyz.fake', dateJoined: '2013-10-31 17:25:00 -0400', }] } @@ -539,11 +558,11 @@ PlanetResource.new(Planet.find(8))) assert_hash_equals( { planets: { - id: 8, + id: '8', name: 'Beta W', description: 'Newly discovered Planet W', links: { planetType: nil, tags: [], @@ -570,32 +589,32 @@ ) assert_hash_equals( { planets: [{ - id: 7, + id: '7', name: 'Beta X', description: 'Newly discovered Planet Z', links: { - planetType: 1, + planetType: '1', tags: [], moons: [] } }, { - id: 8, + id: '8', name: 'Beta W', description: 'Newly discovered Planet W', links: { planetType: nil, tags: [], moons: [] } }], linked: { planetTypes: [ - { id: 1, name: "Gas Giant" } + { id: '1', name: "Gas Giant" } ] } }, planet_hash) json = planet_hash.to_json @@ -609,11 +628,11 @@ preferences = PreferencesResource.new(Preferences.find(1)) assert_hash_equals( { preferences: { - id: 1, + id: '1', advanced_mode: false, links: { author: nil, friends: [] } @@ -628,10 +647,10 @@ facts = FactResource.new(Fact.find(1)) assert_hash_equals( { facts: { - id: 1, + id: '1', spouse_name: 'Jane Author', bio: 'First man to run across Antartica.', quality_rating: 23.89/45.6, salary: BigDecimal('47000.56', 30), date_time_joined: DateTime.parse('2013-08-07 20:25:00 UTC +00:00'),