Skip to content

Commit 45a629d

Browse files
committed
Adds tests for empty has_one associations
1 parent cacfd9a commit 45a629d

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

test/controllers/controller_test.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,19 @@ def test_show_has_many_relationship_invalid_id
13351335
assert_response :bad_request
13361336
assert_match /2,1 is not a valid value for id/, response.body
13371337
end
1338+
1339+
def test_show_has_one_relationship_nil
1340+
get :show_association, {post_id: '17', association: 'author'}
1341+
assert_response :success
1342+
assert_hash_equals json_response,
1343+
{
1344+
data: nil,
1345+
links: {
1346+
self: 'http://test.host/posts/17/links/author',
1347+
related: 'http://test.host/posts/17/author'
1348+
}
1349+
}
1350+
end
13381351
end
13391352

13401353
class TagsControllerTest < ActionController::TestCase
@@ -1678,6 +1691,50 @@ def test_valid_filter_value
16781691
assert_equal json_response['data'][0]['id'], '1'
16791692
assert_equal json_response['data'][0]['name'], 'Joe Author'
16801693
end
1694+
1695+
def test_get_related_resource
1696+
get :get_related_resource, {post_id: '2', association: 'author', :source=>'posts'}
1697+
assert_response :success
1698+
assert_hash_equals json_response,
1699+
{
1700+
data: {
1701+
id: '1',
1702+
type: 'people',
1703+
name: 'Joe Author',
1704+
email: 'joe@xyz.fake',
1705+
dateJoined: '2013-08-07 16:25:00 -0400',
1706+
links: {
1707+
self: 'http://test.host/people/1',
1708+
comments: {
1709+
self: 'http://test.host/people/1/links/comments',
1710+
related: 'http://test.host/people/1/comments'
1711+
},
1712+
posts: {
1713+
self: 'http://test.host/people/1/links/posts',
1714+
related: 'http://test.host/people/1/posts'
1715+
},
1716+
preferences: {
1717+
self: 'http://test.host/people/1/links/preferences',
1718+
related: 'http://test.host/people/1/preferences',
1719+
linkage: {
1720+
type: 'preferences',
1721+
id: '1'
1722+
}
1723+
}
1724+
}
1725+
}
1726+
}
1727+
end
1728+
1729+
def test_get_related_resource_nil
1730+
get :get_related_resource, {post_id: '17', association: 'author', :source=>'posts'}
1731+
assert_response :success
1732+
assert_hash_equals json_response,
1733+
{
1734+
data: nil
1735+
}
1736+
1737+
end
16811738
end
16821739

16831740
class Api::V5::AuthorsControllerTest < ActionController::TestCase

test/fixtures/posts.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,10 @@ post_16:
9393
id: 16
9494
title: SDFGH
9595
body: Not First!!!!
96-
author_id: 3
96+
author_id: 3
97+
98+
post_17:
99+
id: 17
100+
title: No Author!!!!!!
101+
body: This post has no Author
102+
author_id:

0 commit comments

Comments
 (0)