@@ -6,40 +6,40 @@ class PathTest < ActiveSupport::TestCase
6
6
def test_one_relationship
7
7
path = JSONAPI ::Path . new ( resource_klass : Api ::V1 ::PostResource , path_string : 'comments' )
8
8
9
- assert path . parts . is_a? ( Array )
10
- assert path . parts [ 0 ] . is_a? ( JSONAPI ::PathPart ::Relationship ) , "should be a PathPart ::Relationship"
11
- assert_equal Api ::V1 ::PostResource . _relationship ( :comments ) , path . parts [ 0 ] . relationship
9
+ assert path . segments . is_a? ( Array )
10
+ assert path . segments [ 0 ] . is_a? ( JSONAPI ::PathSegment ::Relationship ) , "should be a PathSegment ::Relationship"
11
+ assert_equal Api ::V1 ::PostResource . _relationship ( :comments ) , path . segments [ 0 ] . relationship
12
12
end
13
13
14
14
def test_one_field
15
15
path = JSONAPI ::Path . new ( resource_klass : Api ::V1 ::PostResource , path_string : 'title' )
16
16
17
- assert path . parts . is_a? ( Array )
18
- assert path . parts [ 0 ] . is_a? ( JSONAPI ::PathPart ::Field ) , "should be a PathPart ::Field"
19
- assert_equal 'title' , path . parts [ 0 ] . field_name
17
+ assert path . segments . is_a? ( Array )
18
+ assert path . segments [ 0 ] . is_a? ( JSONAPI ::PathSegment ::Field ) , "should be a PathSegment ::Field"
19
+ assert_equal 'title' , path . segments [ 0 ] . field_name
20
20
end
21
21
22
22
def test_two_relationships
23
23
path = JSONAPI ::Path . new ( resource_klass : Api ::V1 ::PostResource , path_string : 'comments.author' )
24
24
25
- assert path . parts . is_a? ( Array )
26
- assert path . parts [ 0 ] . is_a? ( JSONAPI ::PathPart ::Relationship ) , "should be a PathPart ::Relationship"
27
- assert path . parts [ 1 ] . is_a? ( JSONAPI ::PathPart ::Relationship ) , "should be a PathPart ::Relationship"
28
- assert_equal Api ::V1 ::PostResource . _relationship ( :comments ) , path . parts [ 0 ] . relationship
29
- assert_equal Api ::V1 ::CommentResource . _relationship ( :author ) , path . parts [ 1 ] . relationship
25
+ assert path . segments . is_a? ( Array )
26
+ assert path . segments [ 0 ] . is_a? ( JSONAPI ::PathSegment ::Relationship ) , "should be a PathSegment ::Relationship"
27
+ assert path . segments [ 1 ] . is_a? ( JSONAPI ::PathSegment ::Relationship ) , "should be a PathSegment ::Relationship"
28
+ assert_equal Api ::V1 ::PostResource . _relationship ( :comments ) , path . segments [ 0 ] . relationship
29
+ assert_equal Api ::V1 ::CommentResource . _relationship ( :author ) , path . segments [ 1 ] . relationship
30
30
end
31
31
32
32
def test_two_relationships_and_field
33
33
path = JSONAPI ::Path . new ( resource_klass : Api ::V1 ::PostResource , path_string : 'comments.author.name' )
34
34
35
- assert path . parts . is_a? ( Array )
36
- assert path . parts [ 0 ] . is_a? ( JSONAPI ::PathPart ::Relationship ) , "should be a PathPart ::Relationship"
37
- assert path . parts [ 1 ] . is_a? ( JSONAPI ::PathPart ::Relationship ) , "should be a PathPart ::Relationship"
38
- assert path . parts [ 2 ] . is_a? ( JSONAPI ::PathPart ::Field ) , "should be a PathPart ::Field"
35
+ assert path . segments . is_a? ( Array )
36
+ assert path . segments [ 0 ] . is_a? ( JSONAPI ::PathSegment ::Relationship ) , "should be a PathSegment ::Relationship"
37
+ assert path . segments [ 1 ] . is_a? ( JSONAPI ::PathSegment ::Relationship ) , "should be a PathSegment ::Relationship"
38
+ assert path . segments [ 2 ] . is_a? ( JSONAPI ::PathSegment ::Field ) , "should be a PathSegment ::Field"
39
39
40
- assert_equal Api ::V1 ::PostResource . _relationship ( :comments ) , path . parts [ 0 ] . relationship
41
- assert_equal Api ::V1 ::CommentResource . _relationship ( :author ) , path . parts [ 1 ] . relationship
42
- assert_equal 'name' , path . parts [ 2 ] . field_name
40
+ assert_equal Api ::V1 ::PostResource . _relationship ( :comments ) , path . segments [ 0 ] . relationship
41
+ assert_equal Api ::V1 ::CommentResource . _relationship ( :author ) , path . segments [ 1 ] . relationship
42
+ assert_equal 'name' , path . segments [ 2 ] . field_name
43
43
end
44
44
45
45
def test_two_relationships_and_parse_fields_false_raises_with_field
@@ -54,32 +54,57 @@ def test_two_relationships_and_parse_fields_false_raises_with_field
54
54
def test_ensure_default_field_false
55
55
path = JSONAPI ::Path . new ( resource_klass : Api ::V1 ::PostResource , path_string : 'comments.author' , ensure_default_field : false )
56
56
57
- assert path . parts . is_a? ( Array )
58
- assert_equal 2 , path . parts . length
59
- assert path . parts [ 0 ] . is_a? ( JSONAPI ::PathPart ::Relationship ) , "should be a PathPart ::Relationship"
60
- assert path . parts [ 1 ] . is_a? ( JSONAPI ::PathPart ::Relationship ) , "should be a PathPart ::Relationship"
57
+ assert path . segments . is_a? ( Array )
58
+ assert_equal 2 , path . segments . length
59
+ assert path . segments [ 0 ] . is_a? ( JSONAPI ::PathSegment ::Relationship ) , "should be a PathSegment ::Relationship"
60
+ assert path . segments [ 1 ] . is_a? ( JSONAPI ::PathSegment ::Relationship ) , "should be a PathSegment ::Relationship"
61
61
62
- assert_equal Api ::V1 ::PostResource . _relationship ( :comments ) , path . parts [ 0 ] . relationship
63
- assert_equal Api ::V1 ::CommentResource . _relationship ( :author ) , path . parts [ 1 ] . relationship
62
+ assert_equal Api ::V1 ::PostResource . _relationship ( :comments ) , path . segments [ 0 ] . relationship
63
+ assert_equal Api ::V1 ::CommentResource . _relationship ( :author ) , path . segments [ 1 ] . relationship
64
64
end
65
65
66
66
def test_ensure_default_field_true
67
67
path = JSONAPI ::Path . new ( resource_klass : Api ::V1 ::PostResource , path_string : 'comments.author' , ensure_default_field : true )
68
68
69
- assert path . parts . is_a? ( Array )
70
- assert_equal 3 , path . parts . length
71
- assert path . parts [ 0 ] . is_a? ( JSONAPI ::PathPart ::Relationship ) , "should be a PathPart ::Relationship"
72
- assert path . parts [ 1 ] . is_a? ( JSONAPI ::PathPart ::Relationship ) , "should be a PathPart ::Relationship"
69
+ assert path . segments . is_a? ( Array )
70
+ assert_equal 3 , path . segments . length
71
+ assert path . segments [ 0 ] . is_a? ( JSONAPI ::PathSegment ::Relationship ) , "should be a PathSegment ::Relationship"
72
+ assert path . segments [ 1 ] . is_a? ( JSONAPI ::PathSegment ::Relationship ) , "should be a PathSegment ::Relationship"
73
73
74
- assert_equal Api ::V1 ::PostResource . _relationship ( :comments ) , path . parts [ 0 ] . relationship
75
- assert_equal Api ::V1 ::CommentResource . _relationship ( :author ) , path . parts [ 1 ] . relationship
74
+ assert_equal Api ::V1 ::PostResource . _relationship ( :comments ) , path . segments [ 0 ] . relationship
75
+ assert_equal Api ::V1 ::CommentResource . _relationship ( :author ) , path . segments [ 1 ] . relationship
76
76
end
77
77
78
78
def test_polymorphic_path
79
79
path = JSONAPI ::Path . new ( resource_klass : PictureResource , path_string : :imageable )
80
80
81
- assert path . parts . is_a? ( Array )
82
- assert path . parts [ 0 ] . is_a? ( JSONAPI ::PathPart ::Relationship ) , "should be a PathPart::Relationship"
83
- assert_equal PictureResource . _relationship ( :imageable ) , path . parts [ 0 ] . relationship
81
+ assert path . segments . is_a? ( Array )
82
+ assert path . segments [ 0 ] . is_a? ( JSONAPI ::PathSegment ::Relationship ) , "should be a PathSegment::Relationship"
83
+ assert_equal PictureResource . _relationship ( :imageable ) , path . segments [ 0 ] . relationship
84
+ refute path . segments [ 0 ] . path_specified_resource_klass? , "should note that the resource klass was not specified"
85
+ end
86
+
87
+ def test_polymorphic_path_with_resource_type
88
+ path = JSONAPI ::Path . new ( resource_klass : PictureResource , path_string : 'imageable#documents' )
89
+
90
+ assert path . segments . is_a? ( Array )
91
+ assert path . segments [ 0 ] . is_a? ( JSONAPI ::PathSegment ::Relationship ) , "should be a PathSegment::Relationship"
92
+ assert_equal PictureResource . _relationship ( :imageable ) , path . segments [ 0 ] . relationship
93
+ assert_equal DocumentResource , path . segments [ 0 ] . resource_klass , "should return the specified resource klass"
94
+ assert path . segments [ 0 ] . path_specified_resource_klass? , "should note that the resource klass was specified"
95
+ end
96
+
97
+ def test_polymorphic_path_with_wrong_resource_type
98
+ assert_raises JSONAPI ::Exceptions ::InvalidRelationship do
99
+ JSONAPI ::Path . new ( resource_klass : PictureResource , path_string : 'imageable#docs' )
100
+ end
101
+ end
102
+
103
+ def test_raises_when_field_is_specified_if_not_expected
104
+ assert JSONAPI ::Path . new ( resource_klass : PictureResource , path_string : 'comments.author.name' , parse_fields : true )
105
+
106
+ assert_raises JSONAPI ::Exceptions ::InvalidRelationship do
107
+ JSONAPI ::Path . new ( resource_klass : PictureResource , path_string : 'comments.author.name' , parse_fields : false )
108
+ end
84
109
end
85
110
end
0 commit comments