Skip to content

Commit 548e94f

Browse files
author
Doug Smith
committed
test: show relationships w/o included error (issue #352)
1 parent 2b2033a commit 548e94f

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
require 'test_helper'
2+
3+
class CompoundNonIncludedDocumentTest < MiniTest::Test
4+
5+
TEST_DATA = %{
6+
{
7+
"links": {
8+
"self": "http://example.com/posts",
9+
"next": "http://example.com/posts?page[offset]=2",
10+
"last": "http://example.com/posts?page[offset]=10"
11+
},
12+
"data": [{
13+
"type": "posts",
14+
"id": "1",
15+
"attributes": {
16+
"title": "JSON API paints my bikeshed!"
17+
},
18+
"relationships": {
19+
"author": {
20+
"links": {
21+
"self": "http://example.com/posts/1/relationships/author",
22+
"related": "http://example.com/posts/1/author"
23+
},
24+
"data": { "type": "people", "id": "9" }
25+
},
26+
"comments": {
27+
"links": {
28+
"self": "http://example.com/posts/1/relationships/comments",
29+
"related": "http://example.com/posts/1/comments"
30+
},
31+
"data": [
32+
{ "type": "comments", "id": "5" },
33+
{ "type": "comments", "id": "12" }
34+
]
35+
}
36+
},
37+
"links": {
38+
"self": "http://example.com/posts/1"
39+
}
40+
}]
41+
}
42+
}
43+
44+
def test_can_handle_related_data_without_included
45+
stub_request(:get, "http://example.com/articles")
46+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: TEST_DATA)
47+
48+
articles = Article.all
49+
50+
assert articles.is_a?(JsonApiClient::ResultSet)
51+
assert_equal 1, articles.length
52+
53+
article = articles.first
54+
assert_equal "1", article.id
55+
assert_equal "JSON API paints my bikeshed!", article.title
56+
57+
# has_one is nil if not included
58+
assert_nil article.author
59+
60+
# has_many is empty if not included
61+
assert_equal 0, article.comments.size
62+
end
63+
end

0 commit comments

Comments
 (0)