@@ -23,7 +23,7 @@ def test_no_added_joins
23
23
records = join_manager . join ( records , { } )
24
24
assert_equal 'SELECT "posts".* FROM "posts"' , records . to_sql
25
25
26
- assert_hash_equals ( { alias : 'posts' , join_type : :root } , join_manager . source_join_details )
26
+ assert_hash_equals ( { alias : 'posts' , join_type : :root } , join_manager . source_join_details . except! ( :join_options ) )
27
27
end
28
28
29
29
def test_add_single_join
@@ -32,8 +32,22 @@ def test_add_single_join
32
32
records = PostResource . records ( { } )
33
33
records = join_manager . join ( records , { } )
34
34
assert_equal 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "posts_tags" ON "posts_tags"."post_id" = "posts"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "posts_tags"."tag_id"' , records . to_sql
35
- assert_hash_equals ( { alias : 'posts' , join_type : :root } , join_manager . source_join_details )
36
- assert_hash_equals ( { alias : 'tags' , join_type : :left } , join_manager . join_details_by_relationship ( PostResource . _relationship ( :tags ) ) )
35
+ assert_hash_equals ( { alias : 'posts' , join_type : :root } , join_manager . source_join_details . except! ( :join_options ) )
36
+ assert_hash_equals ( { alias : 'tags' , join_type : :left } , join_manager . join_details_by_relationship ( PostResource . _relationship ( :tags ) ) . except! ( :join_options ) )
37
+ end
38
+
39
+ def test_joins_have_join_options
40
+ filters = { 'tags' => [ '1' ] }
41
+ join_manager = JSONAPI ::ActiveRelation ::JoinManager . new ( resource_klass : PostResource , filters : filters )
42
+ records = PostResource . records ( { } )
43
+ records = join_manager . join ( records , { } )
44
+ assert_equal 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "posts_tags" ON "posts_tags"."post_id" = "posts"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "posts_tags"."tag_id"' , records . to_sql
45
+
46
+ source_join_options = join_manager . source_join_details [ :join_options ]
47
+ assert_array_equals [ :relationship , :relationship_details , :related_resource_klass ] , source_join_options . keys
48
+
49
+ relationship_join_options = join_manager . join_details_by_relationship ( PostResource . _relationship ( :tags ) ) [ :join_options ]
50
+ assert_array_equals [ :relationship , :relationship_details , :related_resource_klass ] , relationship_join_options . keys
37
51
end
38
52
39
53
def test_add_single_sort_join
@@ -43,8 +57,8 @@ def test_add_single_sort_join
43
57
records = join_manager . join ( records , { } )
44
58
45
59
assert_equal 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "posts_tags" ON "posts_tags"."post_id" = "posts"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "posts_tags"."tag_id"' , records . to_sql
46
- assert_hash_equals ( { alias : 'posts' , join_type : :root } , join_manager . source_join_details )
47
- assert_hash_equals ( { alias : 'tags' , join_type : :left } , join_manager . join_details_by_relationship ( PostResource . _relationship ( :tags ) ) )
60
+ assert_hash_equals ( { alias : 'posts' , join_type : :root } , join_manager . source_join_details . except! ( :join_options ) )
61
+ assert_hash_equals ( { alias : 'tags' , join_type : :left } , join_manager . join_details_by_relationship ( PostResource . _relationship ( :tags ) ) . except! ( :join_options ) )
48
62
end
49
63
50
64
def test_add_single_sort_and_filter_join
@@ -54,8 +68,8 @@ def test_add_single_sort_and_filter_join
54
68
records = PostResource . records ( { } )
55
69
records = join_manager . join ( records , { } )
56
70
assert_equal 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "posts_tags" ON "posts_tags"."post_id" = "posts"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "posts_tags"."tag_id"' , records . to_sql
57
- assert_hash_equals ( { alias : 'posts' , join_type : :root } , join_manager . source_join_details )
58
- assert_hash_equals ( { alias : 'tags' , join_type : :left } , join_manager . join_details_by_relationship ( PostResource . _relationship ( :tags ) ) )
71
+ assert_hash_equals ( { alias : 'posts' , join_type : :root } , join_manager . source_join_details . except! ( :join_options ) )
72
+ assert_hash_equals ( { alias : 'tags' , join_type : :left } , join_manager . join_details_by_relationship ( PostResource . _relationship ( :tags ) ) . except! ( :join_options ) )
59
73
end
60
74
61
75
def test_add_sibling_joins
@@ -69,9 +83,9 @@ def test_add_sibling_joins
69
83
records = join_manager . join ( records , { } )
70
84
71
85
assert_equal 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "posts_tags" ON "posts_tags"."post_id" = "posts"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "posts_tags"."tag_id" LEFT OUTER JOIN "people" ON "people"."id" = "posts"."author_id"' , records . to_sql
72
- assert_hash_equals ( { alias : 'posts' , join_type : :root } , join_manager . source_join_details )
73
- assert_hash_equals ( { alias : 'tags' , join_type : :left } , join_manager . join_details_by_relationship ( PostResource . _relationship ( :tags ) ) )
74
- assert_hash_equals ( { alias : 'people' , join_type : :left } , join_manager . join_details_by_relationship ( PostResource . _relationship ( :author ) ) )
86
+ assert_hash_equals ( { alias : 'posts' , join_type : :root } , join_manager . source_join_details . except! ( :join_options ) )
87
+ assert_hash_equals ( { alias : 'tags' , join_type : :left } , join_manager . join_details_by_relationship ( PostResource . _relationship ( :tags ) ) . except! ( :join_options ) )
88
+ assert_hash_equals ( { alias : 'people' , join_type : :left } , join_manager . join_details_by_relationship ( PostResource . _relationship ( :author ) ) . except! ( :join_options ) )
75
89
end
76
90
77
91
@@ -82,7 +96,7 @@ def test_add_joins_source_relationship
82
96
records = join_manager . join ( records , { } )
83
97
84
98
assert_equal 'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id"' , records . to_sql
85
- assert_hash_equals ( { alias : 'comments' , join_type : :inner } , join_manager . source_join_details )
99
+ assert_hash_equals ( { alias : 'comments' , join_type : :inner } , join_manager . source_join_details . except! ( :join_options ) )
86
100
end
87
101
88
102
@@ -96,7 +110,7 @@ def test_add_joins_source_relationship_with_custom_apply
96
110
97
111
assert_equal sql , records . to_sql
98
112
99
- assert_hash_equals ( { alias : 'comments' , join_type : :inner } , join_manager . source_join_details )
113
+ assert_hash_equals ( { alias : 'comments' , join_type : :inner } , join_manager . source_join_details . except! ( :join_options ) )
100
114
end
101
115
102
116
def test_add_nested_scoped_joins
@@ -110,11 +124,11 @@ def test_add_nested_scoped_joins
110
124
records = Api ::V10 ::PostResource . records ( { } )
111
125
records = join_manager . join ( records , { } )
112
126
113
- assert_hash_equals ( { alias : 'posts' , join_type : :root } , join_manager . source_join_details )
114
- assert_hash_equals ( { alias : 'comments' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::PostResource . _relationship ( :comments ) ) )
115
- assert_hash_equals ( { alias : 'authors_comments' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::CommentResource . _relationship ( :author ) ) )
116
- assert_hash_equals ( { alias : 'tags' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::CommentResource . _relationship ( :tags ) ) )
117
- assert_hash_equals ( { alias : 'people' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::PostResource . _relationship ( :author ) ) )
127
+ assert_hash_equals ( { alias : 'posts' , join_type : :root } , join_manager . source_join_details . except! ( :join_options ) )
128
+ assert_hash_equals ( { alias : 'comments' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::PostResource . _relationship ( :comments ) ) . except! ( :join_options ) )
129
+ assert_hash_equals ( { alias : 'authors_comments' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::CommentResource . _relationship ( :author ) ) . except! ( :join_options ) )
130
+ assert_hash_equals ( { alias : 'tags' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::CommentResource . _relationship ( :tags ) ) . except! ( :join_options ) )
131
+ assert_hash_equals ( { alias : 'people' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::PostResource . _relationship ( :author ) ) . except! ( :join_options ) )
118
132
119
133
# Now test with different order for the filters
120
134
filters = {
@@ -127,11 +141,11 @@ def test_add_nested_scoped_joins
127
141
records = Api ::V10 ::PostResource . records ( { } )
128
142
records = join_manager . join ( records , { } )
129
143
130
- assert_hash_equals ( { alias : 'posts' , join_type : :root } , join_manager . source_join_details )
131
- assert_hash_equals ( { alias : 'comments' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::PostResource . _relationship ( :comments ) ) )
132
- assert_hash_equals ( { alias : 'authors_comments' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::CommentResource . _relationship ( :author ) ) )
133
- assert_hash_equals ( { alias : 'tags' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::CommentResource . _relationship ( :tags ) ) )
134
- assert_hash_equals ( { alias : 'people' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::PostResource . _relationship ( :author ) ) )
144
+ assert_hash_equals ( { alias : 'posts' , join_type : :root } , join_manager . source_join_details . except! ( :join_options ) )
145
+ assert_hash_equals ( { alias : 'comments' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::PostResource . _relationship ( :comments ) ) . except! ( :join_options ) )
146
+ assert_hash_equals ( { alias : 'authors_comments' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::CommentResource . _relationship ( :author ) ) . except! ( :join_options ) )
147
+ assert_hash_equals ( { alias : 'tags' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::CommentResource . _relationship ( :tags ) ) . except! ( :join_options ) )
148
+ assert_hash_equals ( { alias : 'people' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::PostResource . _relationship ( :author ) ) . except! ( :join_options ) )
135
149
end
136
150
137
151
def test_add_nested_joins_with_fields
@@ -145,11 +159,11 @@ def test_add_nested_joins_with_fields
145
159
records = Api ::V10 ::PostResource . records ( { } )
146
160
records = join_manager . join ( records , { } )
147
161
148
- assert_hash_equals ( { alias : 'posts' , join_type : :root } , join_manager . source_join_details )
149
- assert_hash_equals ( { alias : 'comments' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::PostResource . _relationship ( :comments ) ) )
150
- assert_hash_equals ( { alias : 'authors_comments' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::CommentResource . _relationship ( :author ) ) )
151
- assert_hash_equals ( { alias : 'tags' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::CommentResource . _relationship ( :tags ) ) )
152
- assert_hash_equals ( { alias : 'people' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::PostResource . _relationship ( :author ) ) )
162
+ assert_hash_equals ( { alias : 'posts' , join_type : :root } , join_manager . source_join_details . except! ( :join_options ) )
163
+ assert_hash_equals ( { alias : 'comments' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::PostResource . _relationship ( :comments ) ) . except! ( :join_options ) )
164
+ assert_hash_equals ( { alias : 'authors_comments' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::CommentResource . _relationship ( :author ) ) . except! ( :join_options ) )
165
+ assert_hash_equals ( { alias : 'tags' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::CommentResource . _relationship ( :tags ) ) . except! ( :join_options ) )
166
+ assert_hash_equals ( { alias : 'people' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::PostResource . _relationship ( :author ) ) . except! ( :join_options ) )
153
167
end
154
168
155
169
def test_add_joins_with_sub_relationship
@@ -160,10 +174,10 @@ def test_add_joins_with_sub_relationship
160
174
records = Api ::V10 ::PostResource . records ( { } )
161
175
records = join_manager . join ( records , { } )
162
176
163
- assert_hash_equals ( { alias : 'comments' , join_type : :inner } , join_manager . source_join_details )
164
- assert_hash_equals ( { alias : 'comments' , join_type : :inner } , join_manager . join_details_by_relationship ( Api ::V10 ::PostResource . _relationship ( :comments ) ) )
165
- assert_hash_equals ( { alias : 'tags' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::CommentResource . _relationship ( :tags ) ) )
166
- assert_hash_equals ( { alias : 'comments_people' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::PersonResource . _relationship ( :comments ) ) )
177
+ assert_hash_equals ( { alias : 'comments' , join_type : :inner } , join_manager . source_join_details . except! ( :join_options ) )
178
+ assert_hash_equals ( { alias : 'comments' , join_type : :inner } , join_manager . join_details_by_relationship ( Api ::V10 ::PostResource . _relationship ( :comments ) ) . except! ( :join_options ) )
179
+ assert_hash_equals ( { alias : 'tags' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::CommentResource . _relationship ( :tags ) ) . except! ( :join_options ) )
180
+ assert_hash_equals ( { alias : 'comments_people' , join_type : :left } , join_manager . join_details_by_relationship ( Api ::V10 ::PersonResource . _relationship ( :comments ) ) . except! ( :join_options ) )
167
181
end
168
182
169
183
def test_add_joins_with_sub_relationship_and_filters
@@ -181,11 +195,11 @@ def test_add_joins_with_sub_relationship_and_filters
181
195
records = PostResource . records ( { } )
182
196
records = join_manager . join ( records , { } )
183
197
184
- assert_hash_equals ( { alias : 'comments' , join_type : :inner } , join_manager . source_join_details )
185
- assert_hash_equals ( { alias : 'comments' , join_type : :inner } , join_manager . join_details_by_relationship ( PostResource . _relationship ( :comments ) ) )
186
- assert_hash_equals ( { alias : 'people' , join_type : :left } , join_manager . join_details_by_relationship ( CommentResource . _relationship ( :author ) ) )
187
- assert_hash_equals ( { alias : 'tags' , join_type : :left } , join_manager . join_details_by_relationship ( CommentResource . _relationship ( :tags ) ) )
188
- assert_hash_equals ( { alias : 'comments_people' , join_type : :left } , join_manager . join_details_by_relationship ( PersonResource . _relationship ( :comments ) ) )
198
+ assert_hash_equals ( { alias : 'comments' , join_type : :inner } , join_manager . source_join_details . except! ( :join_options ) )
199
+ assert_hash_equals ( { alias : 'comments' , join_type : :inner } , join_manager . join_details_by_relationship ( PostResource . _relationship ( :comments ) ) . except! ( :join_options ) )
200
+ assert_hash_equals ( { alias : 'people' , join_type : :left } , join_manager . join_details_by_relationship ( CommentResource . _relationship ( :author ) ) . except! ( :join_options ) )
201
+ assert_hash_equals ( { alias : 'tags' , join_type : :left } , join_manager . join_details_by_relationship ( CommentResource . _relationship ( :tags ) ) . except! ( :join_options ) )
202
+ assert_hash_equals ( { alias : 'comments_people' , join_type : :left } , join_manager . join_details_by_relationship ( PersonResource . _relationship ( :comments ) ) . except! ( :join_options ) )
189
203
end
190
204
191
205
def test_polymorphic_join_belongs_to_just_source
@@ -196,10 +210,10 @@ def test_polymorphic_join_belongs_to_just_source
196
210
records = join_manager . join ( records , { } )
197
211
198
212
# assert_equal 'SELECT "pictures".* FROM "pictures" LEFT OUTER JOIN "products" ON "products"."id" = "pictures"."imageable_id" AND "pictures"."imageable_type" = \'Product\' LEFT OUTER JOIN "documents" ON "documents"."id" = "pictures"."imageable_id" AND "pictures"."imageable_type" = \'Document\'', records.to_sql
199
- assert_hash_equals ( { alias : 'products' , join_type : :left } , join_manager . source_join_details ( 'products' ) )
200
- assert_hash_equals ( { alias : 'documents' , join_type : :left } , join_manager . source_join_details ( 'documents' ) )
201
- assert_hash_equals ( { alias : 'products' , join_type : :left } , join_manager . join_details_by_polymorphic_relationship ( PictureResource . _relationship ( :imageable ) , 'products' ) )
202
- assert_hash_equals ( { alias : 'documents' , join_type : :left } , join_manager . join_details_by_polymorphic_relationship ( PictureResource . _relationship ( :imageable ) , 'documents' ) )
213
+ assert_hash_equals ( { alias : 'products' , join_type : :left } , join_manager . source_join_details ( 'products' ) . except! ( :join_options ) )
214
+ assert_hash_equals ( { alias : 'documents' , join_type : :left } , join_manager . source_join_details ( 'documents' ) . except! ( :join_options ) )
215
+ assert_hash_equals ( { alias : 'products' , join_type : :left } , join_manager . join_details_by_polymorphic_relationship ( PictureResource . _relationship ( :imageable ) , 'products' ) . except! ( :join_options ) )
216
+ assert_hash_equals ( { alias : 'documents' , join_type : :left } , join_manager . join_details_by_polymorphic_relationship ( PictureResource . _relationship ( :imageable ) , 'documents' ) . except! ( :join_options ) )
203
217
end
204
218
205
219
def test_polymorphic_join_belongs_to_filter
@@ -210,9 +224,9 @@ def test_polymorphic_join_belongs_to_filter
210
224
records = join_manager . join ( records , { } )
211
225
212
226
# assert_equal 'SELECT "pictures".* FROM "pictures" LEFT OUTER JOIN "products" ON "products"."id" = "pictures"."imageable_id" AND "pictures"."imageable_type" = \'Product\' LEFT OUTER JOIN "documents" ON "documents"."id" = "pictures"."imageable_id" AND "pictures"."imageable_type" = \'Document\'', records.to_sql
213
- assert_hash_equals ( { alias : 'pictures' , join_type : :root } , join_manager . source_join_details )
214
- assert_hash_equals ( { alias : 'products' , join_type : :left } , join_manager . join_details_by_polymorphic_relationship ( PictureResource . _relationship ( :imageable ) , 'products' ) )
215
- assert_hash_equals ( { alias : 'documents' , join_type : :left } , join_manager . join_details_by_polymorphic_relationship ( PictureResource . _relationship ( :imageable ) , 'documents' ) )
227
+ assert_hash_equals ( { alias : 'pictures' , join_type : :root } , join_manager . source_join_details . except! ( :join_options ) )
228
+ assert_hash_equals ( { alias : 'products' , join_type : :left } , join_manager . join_details_by_polymorphic_relationship ( PictureResource . _relationship ( :imageable ) , 'products' ) . except! ( :join_options ) )
229
+ assert_hash_equals ( { alias : 'documents' , join_type : :left } , join_manager . join_details_by_polymorphic_relationship ( PictureResource . _relationship ( :imageable ) , 'documents' ) . except! ( :join_options ) )
216
230
end
217
231
218
232
def test_polymorphic_join_belongs_to_filter_on_resource
@@ -228,9 +242,9 @@ def test_polymorphic_join_belongs_to_filter_on_resource
228
242
records = PictureResource . records ( { } )
229
243
records = join_manager . join ( records , { } )
230
244
231
- assert_hash_equals ( { alias : 'pictures' , join_type : :root } , join_manager . source_join_details )
232
- assert_hash_equals ( { alias : 'products' , join_type : :left } , join_manager . join_details_by_polymorphic_relationship ( PictureResource . _relationship ( :imageable ) , 'products' ) )
233
- assert_hash_equals ( { alias : 'documents' , join_type : :left } , join_manager . join_details_by_polymorphic_relationship ( PictureResource . _relationship ( :imageable ) , 'documents' ) )
234
- assert_hash_equals ( { alias : 'file_properties' , join_type : :left } , join_manager . join_details_by_relationship ( PictureResource . _relationship ( :file_properties ) ) )
245
+ assert_hash_equals ( { alias : 'pictures' , join_type : :root } , join_manager . source_join_details . except! ( :join_options ) )
246
+ assert_hash_equals ( { alias : 'products' , join_type : :left } , join_manager . join_details_by_polymorphic_relationship ( PictureResource . _relationship ( :imageable ) , 'products' ) . except! ( :join_options ) )
247
+ assert_hash_equals ( { alias : 'documents' , join_type : :left } , join_manager . join_details_by_polymorphic_relationship ( PictureResource . _relationship ( :imageable ) , 'documents' ) . except! ( :join_options ) )
248
+ assert_hash_equals ( { alias : 'file_properties' , join_type : :left } , join_manager . join_details_by_relationship ( PictureResource . _relationship ( :file_properties ) ) . except! ( :join_options ) )
235
249
end
236
250
end
0 commit comments