pggraphql 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pggraphql/version.rb +1 -1
- data/lib/pggraphql.rb +4 -14
- data/test/test_pggraphql.rb +45 -425
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 310b215ed3294f252ba1a2c446438e1ef2eb36b6
|
4
|
+
data.tar.gz: f9e4f9aa456475b48f65053cff6859018113d8a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ea6027265481e57382bddd85d8cc1c737e8da6e31c9696802bc726ede7cd46e243dbea37b41087e9410cec981de57e32a6c8fdcae1b7aa1f9cca93cd5d7ca29
|
7
|
+
data.tar.gz: 52987c14f1c1427d0a242dbc3826c15815d387fa76617a566aedf161acdf2e40753df529b07590dd60b7edaf3869c6e66f14a819cb24c9448e28954019cc70e1
|
data/lib/pggraphql/version.rb
CHANGED
data/lib/pggraphql.rb
CHANGED
@@ -30,6 +30,8 @@ module PgGraphQl
|
|
30
30
|
type = link ? link.type : self.types[e[0].to_s.singularize.to_sym]
|
31
31
|
ids = e[1][:id]
|
32
32
|
|
33
|
+
raise "missing :fk on link #{link.name.inspect}" if link && !link.fk
|
34
|
+
|
33
35
|
columns = e[1].map do |f|
|
34
36
|
column_name = f[0]
|
35
37
|
|
@@ -55,12 +57,7 @@ module PgGraphQl
|
|
55
57
|
wheres << ("(" + type.filter + ")") if type.filter
|
56
58
|
|
57
59
|
if link
|
58
|
-
wheres <<
|
59
|
-
"id = #{parent.table}.#{link.fk}"
|
60
|
-
else
|
61
|
-
"#{link.fk} = #{parent.table}.id"
|
62
|
-
end
|
63
|
-
|
60
|
+
wheres << ("(" + link.fk + ")")
|
64
61
|
wheres << ("(" + link.filter + ")") if link.filter
|
65
62
|
end
|
66
63
|
|
@@ -115,20 +112,13 @@ module PgGraphQl
|
|
115
112
|
end
|
116
113
|
|
117
114
|
class Link
|
118
|
-
attr_accessor :name, :
|
115
|
+
attr_accessor :name, :filter, :fk, :order_by
|
119
116
|
def initialize(owner, name, many)
|
120
117
|
@owner = owner
|
121
118
|
@name = name
|
122
119
|
@many = many
|
123
|
-
@invert = false
|
124
120
|
@order_by = nil
|
125
121
|
end
|
126
|
-
def fk=(_fk)
|
127
|
-
@_fk = fk
|
128
|
-
end
|
129
|
-
def fk
|
130
|
-
@invert ? "#{@name.to_s.singularize}_id" : "#{@owner.name}_id"
|
131
|
-
end
|
132
122
|
def type=(_type)
|
133
123
|
@_type = _type
|
134
124
|
end
|
data/test/test_pggraphql.rb
CHANGED
@@ -125,16 +125,16 @@ module PgGraphQl
|
|
125
125
|
), token(res)
|
126
126
|
end
|
127
127
|
|
128
|
-
#####################
|
129
|
-
# one
|
130
|
-
#####################
|
128
|
+
# #####################
|
129
|
+
# # one
|
130
|
+
# #####################
|
131
131
|
|
132
132
|
|
133
133
|
def test_link_one
|
134
134
|
res = to_sql({user: {id: 1, email: "email", address: {id: "id"}}}) do |s|
|
135
135
|
s.root :user
|
136
136
|
s.type :user, fields: [:id, :email] do |t|
|
137
|
-
t.one :address
|
137
|
+
t.one :address, fk: "id = users.address_id"
|
138
138
|
end
|
139
139
|
s.type :address
|
140
140
|
end
|
@@ -147,42 +147,31 @@ module PgGraphQl
|
|
147
147
|
(select to_json(x.*)
|
148
148
|
from (select id
|
149
149
|
from addresses
|
150
|
-
where
|
150
|
+
where (id = users.address_id) limit 1) x) as address
|
151
151
|
from users
|
152
152
|
where id = 1 limit 1) x) as value
|
153
153
|
SQL
|
154
154
|
), token(res)
|
155
155
|
end
|
156
156
|
|
157
|
-
def
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
157
|
+
def test_link_one_missing_fk
|
158
|
+
assert_raise_message "missing :fk on link :address" do
|
159
|
+
to_sql({user: {id: 1, email: "email", address: {id: "id"}}}) do |s|
|
160
|
+
s.root :user
|
161
|
+
s.type :user, fields: [:id, :email] do |t|
|
162
|
+
t.one :address
|
163
|
+
end
|
164
|
+
s.type :address
|
162
165
|
end
|
163
|
-
|
164
|
-
end
|
165
|
-
|
166
|
-
assert_equal token(<<-SQL
|
167
|
-
select 'address'::text as key,
|
168
|
-
(select to_json(x.*)
|
169
|
-
from (select id,
|
170
|
-
(select to_json(x.*)
|
171
|
-
from (select id,
|
172
|
-
email
|
173
|
-
from users
|
174
|
-
where id = addresses.user_id limit 1) x) as user
|
175
|
-
from addresses
|
176
|
-
where id = 1 limit 1) x) as value
|
177
|
-
SQL
|
178
|
-
), token(res)
|
166
|
+
end
|
179
167
|
end
|
180
168
|
|
181
|
-
|
169
|
+
|
170
|
+
def test_link_one_fk_sql
|
182
171
|
res = to_sql({user: {id: 1, email: "email", address: {id: "id"}}}) do |s|
|
183
172
|
s.root :user
|
184
173
|
s.type :user, fields: [:id, :email] do |t|
|
185
|
-
t.one :address,
|
174
|
+
t.one :address, fk: "id = (select 100)"
|
186
175
|
end
|
187
176
|
s.type :address
|
188
177
|
end
|
@@ -195,18 +184,18 @@ module PgGraphQl
|
|
195
184
|
(select to_json(x.*)
|
196
185
|
from (select id
|
197
186
|
from addresses
|
198
|
-
where
|
187
|
+
where (id = (select 100)) limit 1) x) as address
|
199
188
|
from users
|
200
189
|
where id = 1 limit 1) x) as value
|
201
190
|
SQL
|
202
191
|
), token(res)
|
203
192
|
end
|
204
193
|
|
205
|
-
def
|
194
|
+
def test_link_one_filter
|
206
195
|
res = to_sql({user: {id: 1, email: "email", address: {id: "id"}}}) do |s|
|
207
196
|
s.root :user
|
208
197
|
s.type :user, fields: [:id, :email] do |t|
|
209
|
-
t.one :address,
|
198
|
+
t.one :address, fk: "user_id = users.id", filter: "id > 100"
|
210
199
|
end
|
211
200
|
s.type :address
|
212
201
|
end
|
@@ -219,27 +208,20 @@ module PgGraphQl
|
|
219
208
|
(select to_json(x.*)
|
220
209
|
from (select id
|
221
210
|
from addresses
|
222
|
-
where user_id = users.id
|
211
|
+
where (user_id = users.id) and (id > 100) limit 1) x) as address
|
223
212
|
from users
|
224
213
|
where id = 1 limit 1) x) as value
|
225
214
|
SQL
|
226
215
|
), token(res)
|
227
216
|
end
|
228
217
|
|
229
|
-
|
230
|
-
|
231
|
-
#####################
|
232
|
-
|
233
|
-
def test_link_one_in_one
|
234
|
-
res = to_sql({user: {id: 1, email: "email", address: {id: "id", country: {id: "id"}}}}) do |s|
|
218
|
+
def test_link_one_order_by
|
219
|
+
res = to_sql({user: {id: 1, email: "email", address: {id: "id"}}}) do |s|
|
235
220
|
s.root :user
|
236
221
|
s.type :user, fields: [:id, :email] do |t|
|
237
|
-
t.one :address
|
222
|
+
t.one :address, fk: "user_id = users.id", order_by: "id desc"
|
238
223
|
end
|
239
|
-
s.type :address
|
240
|
-
t.one :country
|
241
|
-
end
|
242
|
-
s.type :country
|
224
|
+
s.type :address
|
243
225
|
end
|
244
226
|
|
245
227
|
assert_equal token(<<-SQL
|
@@ -248,27 +230,27 @@ module PgGraphQl
|
|
248
230
|
from (select id,
|
249
231
|
email,
|
250
232
|
(select to_json(x.*)
|
251
|
-
from (select id
|
252
|
-
(select to_json(x.*)
|
253
|
-
from (select id
|
254
|
-
from countries
|
255
|
-
where address_id = addresses.id limit 1) x) as country
|
233
|
+
from (select id
|
256
234
|
from addresses
|
257
|
-
where user_id = users.id limit 1) x) as address
|
235
|
+
where (user_id = users.id) order by id desc limit 1) x) as address
|
258
236
|
from users
|
259
237
|
where id = 1 limit 1) x) as value
|
260
238
|
SQL
|
261
239
|
), token(res)
|
262
240
|
end
|
263
241
|
|
264
|
-
|
242
|
+
# #####################
|
243
|
+
# # one-in-one
|
244
|
+
# #####################
|
245
|
+
|
246
|
+
def test_link_one_in_one
|
265
247
|
res = to_sql({user: {id: 1, email: "email", address: {id: "id", country: {id: "id"}}}}) do |s|
|
266
248
|
s.root :user
|
267
249
|
s.type :user, fields: [:id, :email] do |t|
|
268
|
-
t.one :address
|
250
|
+
t.one :address, fk: "user_id = users.id"
|
269
251
|
end
|
270
252
|
s.type :address do |t|
|
271
|
-
t.one :country,
|
253
|
+
t.one :country, fk: "id = addresses.country_id"
|
272
254
|
end
|
273
255
|
s.type :country
|
274
256
|
end
|
@@ -283,25 +265,25 @@ module PgGraphQl
|
|
283
265
|
(select to_json(x.*)
|
284
266
|
from (select id
|
285
267
|
from countries
|
286
|
-
where id = addresses.country_id limit 1) x) as country
|
268
|
+
where (id = addresses.country_id) limit 1) x) as country
|
287
269
|
from addresses
|
288
|
-
where user_id = users.id limit 1) x) as address
|
270
|
+
where (user_id = users.id) limit 1) x) as address
|
289
271
|
from users
|
290
272
|
where id = 1 limit 1) x) as value
|
291
273
|
SQL
|
292
274
|
), token(res)
|
293
275
|
end
|
294
276
|
|
295
|
-
#####################
|
296
|
-
# many
|
297
|
-
#####################
|
277
|
+
# #####################
|
278
|
+
# # many
|
279
|
+
# #####################
|
298
280
|
|
299
281
|
|
300
282
|
def test_link_many
|
301
283
|
res = to_sql({user: {id: 1, email: "email", address: {id: "id"}}}) do |s|
|
302
284
|
s.root :user
|
303
285
|
s.type :user, fields: [:id, :email] do |t|
|
304
|
-
t.many :address
|
286
|
+
t.many :address, fk: "user_id = users.id"
|
305
287
|
end
|
306
288
|
s.type :address
|
307
289
|
end
|
@@ -314,41 +296,18 @@ module PgGraphQl
|
|
314
296
|
(select to_json(coalesce(json_agg(x.*), '[]'::json))
|
315
297
|
from (select id
|
316
298
|
from addresses
|
317
|
-
where user_id = users.id) x) as address
|
299
|
+
where (user_id = users.id)) x) as address
|
318
300
|
from users
|
319
301
|
where id = 1 limit 1) x) as value
|
320
302
|
SQL
|
321
303
|
), token(res)
|
322
304
|
end
|
323
305
|
|
324
|
-
def test_link_many_invert
|
325
|
-
res = to_sql({address: {id: "id", users: {id: "id", email: "email"}}}) do |s|
|
326
|
-
s.root :address
|
327
|
-
s.type :address do |t|
|
328
|
-
t.many :users, invert: true
|
329
|
-
end
|
330
|
-
s.type :user, fields: [:id, :email]
|
331
|
-
end
|
332
|
-
|
333
|
-
assert_equal token(<<-SQL
|
334
|
-
select 'address'::text as key,
|
335
|
-
(select to_json(x.*)
|
336
|
-
from (select id,
|
337
|
-
(select to_json(coalesce(json_agg(x.*), '[]'::json))
|
338
|
-
from (select id,
|
339
|
-
email
|
340
|
-
from users
|
341
|
-
where id = addresses.user_id) x) as users
|
342
|
-
from addresses limit 1) x) as value
|
343
|
-
SQL
|
344
|
-
), token(res)
|
345
|
-
end
|
346
|
-
|
347
306
|
def test_link_many_filter
|
348
307
|
res = to_sql({user: {id: 1, email: "email", address: {id: "id"}}}) do |s|
|
349
308
|
s.root :user
|
350
309
|
s.type :user, fields: [:id, :email] do |t|
|
351
|
-
t.many :address, filter: "id % 2 = 0"
|
310
|
+
t.many :address, fk: "user_id = users.id", filter: "id % 2 = 0"
|
352
311
|
end
|
353
312
|
s.type :address
|
354
313
|
end
|
@@ -361,7 +320,7 @@ module PgGraphQl
|
|
361
320
|
(select to_json(coalesce(json_agg(x.*), '[]'::json))
|
362
321
|
from (select id
|
363
322
|
from addresses
|
364
|
-
where user_id = users.id and (id % 2 = 0)) x) as address
|
323
|
+
where (user_id = users.id) and (id % 2 = 0)) x) as address
|
365
324
|
from users
|
366
325
|
where id = 1 limit 1) x) as value
|
367
326
|
SQL
|
@@ -372,7 +331,7 @@ module PgGraphQl
|
|
372
331
|
res = to_sql({user: {id: 1, email: "email", address: {id: "id"}}}) do |s|
|
373
332
|
s.root :user
|
374
333
|
s.type :user, fields: [:id, :email] do |t|
|
375
|
-
t.many :address, order_by: "id desc"
|
334
|
+
t.many :address, fk: "user_id = users.id", order_by: "id desc"
|
376
335
|
end
|
377
336
|
s.type :address
|
378
337
|
end
|
@@ -385,351 +344,12 @@ module PgGraphQl
|
|
385
344
|
(select to_json(coalesce(json_agg(x.*), '[]'::json))
|
386
345
|
from (select id
|
387
346
|
from addresses
|
388
|
-
where user_id = users.id order by id desc) x) as address
|
347
|
+
where (user_id = users.id) order by id desc) x) as address
|
389
348
|
from users
|
390
349
|
where id = 1 limit 1) x) as value
|
391
350
|
SQL
|
392
351
|
), token(res)
|
393
352
|
end
|
394
353
|
|
395
|
-
|
396
|
-
# # schema
|
397
|
-
|
398
|
-
# def test_schema_root_type
|
399
|
-
# assert_equal :sometype, Schema.new{|e| e.root(:sometype)}.roots[:sometype][:type]
|
400
|
-
# assert_equal :person, Schema.new{|e| e.root(:people)}.roots[:people][:type]
|
401
|
-
# end
|
402
|
-
|
403
|
-
# # type
|
404
|
-
|
405
|
-
# def test_pk_prefixed_by_as
|
406
|
-
# assert_equal :"sometypes.id", Type.new(:sometype).pk!
|
407
|
-
# end
|
408
|
-
|
409
|
-
# type - defaults
|
410
|
-
|
411
|
-
# def test_type_default_fields
|
412
|
-
# assert_equal [:id], Schema::Type.new(:sometype).fields
|
413
|
-
# end
|
414
|
-
|
415
|
-
# def test_type_default_as
|
416
|
-
# assert_equal :sometypes, Type.new(:sometype).as
|
417
|
-
# assert_equal :people, Type.new(:person).as
|
418
|
-
# end
|
419
|
-
|
420
|
-
# def test_type_default_sql
|
421
|
-
# assert_equal "select * from houses", Type.new(:house).sql
|
422
|
-
# assert_equal "select * from people", Type.new(:person).sql
|
423
|
-
# end
|
424
|
-
|
425
|
-
# def test_type_default_pk
|
426
|
-
# assert_equal :id, Type.new(:sometype).pk
|
427
|
-
# end
|
428
|
-
|
429
|
-
# def test_type_link_default_fk
|
430
|
-
# assert_equal :sometype_id, Type.new(:sometype).tap{|e| e.one(:somelink) }.links[:somelink][1][:fk]
|
431
|
-
# assert_equal :sometype_id, Type.new(:sometype).tap{|e| e.many(:somelink) }.links[:somelink][1][:fk]
|
432
|
-
# assert_equal :other_fk, Type.new(:sometype).tap{|e| e.one(:somelink, fk: :other_fk) }.links[:somelink][1][:fk]
|
433
|
-
# assert_equal :other_fk, Type.new(:sometype).tap{|e| e.many(:somelink, fk: :other_fk) }.links[:somelink][1][:fk]
|
434
|
-
# end
|
435
|
-
|
436
|
-
# def test_type_link_default_type
|
437
|
-
# assert_equal :somelink, Type.new(:sometype).tap{|e| e.one(:somelink) }.links[:somelink][1][:type]
|
438
|
-
# assert_equal :somelink, Type.new(:sometype).tap{|e| e.many(:somelink) }.links[:somelink][1][:type]
|
439
|
-
|
440
|
-
# assert_equal :person, Type.new(:sometype).tap{|e| e.one(:people) }.links[:people][1][:type]
|
441
|
-
# assert_equal :person, Type.new(:sometype).tap{|e| e.many(:people) }.links[:people][1][:type]
|
442
|
-
|
443
|
-
# assert_equal :other_type, Type.new(:sometype).tap{|e| e.one(:somelink, type: :other_type) }.links[:somelink][1][:type]
|
444
|
-
# assert_equal :other_type, Type.new(:sometype).tap{|e| e.many(:somelink, type: :other_type) }.links[:somelink][1][:type]
|
445
|
-
# end
|
446
|
-
|
447
|
-
# def test_unknown_field
|
448
|
-
# assert_raise_message "unknown field" do
|
449
|
-
# query([[:user, 1], :id, :name]) do |s|
|
450
|
-
# s.root :user
|
451
|
-
# s.type :user
|
452
|
-
# end
|
453
|
-
# end
|
454
|
-
# end
|
455
|
-
|
456
|
-
# def test_simple_object_query
|
457
|
-
# res = query({user: [[:user, 1], :id, :name]}) do |s|
|
458
|
-
# s.root :user
|
459
|
-
# s.type :user, fields: [:id, :name]
|
460
|
-
# end
|
461
|
-
|
462
|
-
# assert_equal token("select to_json(x.*) res from (select id, name from users where id in (1) limit 1) x"), token(res)
|
463
|
-
# end
|
464
|
-
|
465
|
-
# def test_simple
|
466
|
-
# res = query([[:user, 1], :id, :name]) do |s|
|
467
|
-
# s.root :user
|
468
|
-
# s.type :user, fields: [:id, :name]
|
469
|
-
# end
|
470
|
-
|
471
|
-
# assert_equal token("select to_json(x.*) res from (select id, name from users where id in (1) limit 1) x"), token(res)
|
472
|
-
# end
|
473
|
-
|
474
|
-
# def test_simple_filter
|
475
|
-
# res = query([[:user, 1], :id, :name]) do |s|
|
476
|
-
# s.root :user
|
477
|
-
# s.type :user, fields: [:id, :name], filter: "id % 2 = 0"
|
478
|
-
# end
|
479
|
-
|
480
|
-
# assert_equal token("select to_json(x.*) res from (select id, name from users where id in (1) and id % 2 = 0 limit 1) x"), token(res)
|
481
|
-
|
482
|
-
# res = query([[:user, 1], :id, :name]) do |s|
|
483
|
-
# s.root :user
|
484
|
-
# s.type :user, fields: [:id, :name], filter: "id % 2 = 0 and id > 10"
|
485
|
-
# end
|
486
|
-
|
487
|
-
# assert_equal token("select to_json(x.*) res from (select id, name from users where id in (1) and id % 2 = 0 and id > 10 limit 1) x"), token(res)
|
488
|
-
# end
|
489
|
-
|
490
|
-
# def test_one
|
491
|
-
# res = query([[:user, 1], :id, [[:address], :id]]) do |s|
|
492
|
-
# s.root :user
|
493
|
-
# s.type :user do |t|
|
494
|
-
# t.one :address
|
495
|
-
# end
|
496
|
-
# s.type :address
|
497
|
-
# end
|
498
|
-
|
499
|
-
# expected = <<-SQL
|
500
|
-
# select
|
501
|
-
# to_json(x.*) res
|
502
|
-
# from (
|
503
|
-
# select
|
504
|
-
# id,
|
505
|
-
# (select to_json(x.*) res from (select id from addresses where user_id = users.id limit 1) x) as address
|
506
|
-
# from users
|
507
|
-
# where id in (1)
|
508
|
-
# limit 1
|
509
|
-
# ) x
|
510
|
-
# SQL
|
511
|
-
|
512
|
-
# assert_equal token(expected), token(res)
|
513
|
-
# end
|
514
|
-
|
515
|
-
# def test_one_fk_invert
|
516
|
-
# res = query([[:user, 1], :id, [[:address], :id]]) do |s|
|
517
|
-
# s.root :user
|
518
|
-
# s.type :user do |t|
|
519
|
-
# t.one :address, fk: :address_id, invert: true
|
520
|
-
# end
|
521
|
-
# s.type :address
|
522
|
-
# end
|
523
|
-
|
524
|
-
# expected = <<-SQL
|
525
|
-
# select
|
526
|
-
# to_json(x.*) res
|
527
|
-
# from (
|
528
|
-
# select
|
529
|
-
# id,
|
530
|
-
# (select to_json(x.*) res from (select id from addresses where id = users.address_id limit 1) x) as address
|
531
|
-
# from users
|
532
|
-
# where id in (1)
|
533
|
-
# limit 1
|
534
|
-
# ) x
|
535
|
-
# SQL
|
536
|
-
|
537
|
-
# assert_equal token(expected), token(res)
|
538
|
-
# end
|
539
|
-
|
540
|
-
# # def test_one_polymorph
|
541
|
-
# # res = query([[:user, 1], :id, [[:address], :id]]) do |s|
|
542
|
-
# # s.root :user
|
543
|
-
# # s.type :user do |t|
|
544
|
-
# # t.one :address, polymorph: [:primary, :secondary]
|
545
|
-
# # end
|
546
|
-
# # s.type :address
|
547
|
-
# # s.type :address_primary
|
548
|
-
# # s.type :address_secondary
|
549
|
-
# # end
|
550
|
-
|
551
|
-
# # expected = <<-SQL
|
552
|
-
# # select
|
553
|
-
# # to_json(x.*) res
|
554
|
-
# # from (
|
555
|
-
# # select
|
556
|
-
# # id,
|
557
|
-
# # (select to_json(x.*) res from (
|
558
|
-
# # select 'primary' as type, id from addresses join addresses_primary using (id) where user_id = users.id and addresses.type = 'primary'
|
559
|
-
# # union
|
560
|
-
# # select 'secondary' as type, id from addresses join addresses_secondary using (id) where user_id = users.id and addresses.type = 'secondary'
|
561
|
-
# # ) x) as address
|
562
|
-
# # from users
|
563
|
-
# # where id in (1)
|
564
|
-
# # limit 1
|
565
|
-
# # ) x
|
566
|
-
# # SQL
|
567
|
-
|
568
|
-
# # assert_equal token(expected), token(res)
|
569
|
-
# # end
|
570
|
-
|
571
|
-
# def test_one_filter
|
572
|
-
# res = query([[:user, 1], :id, [[:address], :id]]) do |s|
|
573
|
-
# s.root :user
|
574
|
-
# s.type :user do |t|
|
575
|
-
# t.one :address, filter: "is_default = true"
|
576
|
-
# end
|
577
|
-
# s.type :address
|
578
|
-
# end
|
579
|
-
|
580
|
-
# expected = <<-SQL
|
581
|
-
# select
|
582
|
-
# to_json(x.*) res
|
583
|
-
# from (
|
584
|
-
# select
|
585
|
-
# id,
|
586
|
-
# (select to_json(x.*) res from (select id from addresses where user_id = users.id and is_default = true limit 1) x) as address
|
587
|
-
# from users
|
588
|
-
# where id in (1)
|
589
|
-
# limit 1
|
590
|
-
# ) x
|
591
|
-
# SQL
|
592
|
-
|
593
|
-
# assert_equal token(expected), token(res)
|
594
|
-
# end
|
595
|
-
|
596
|
-
# def test_one_fk_through
|
597
|
-
# res = query([[:user, 1], :id, [[:address], :id]]) do |s|
|
598
|
-
# s.root :user
|
599
|
-
# s.type :user do |t|
|
600
|
-
# t.one :address, fk: ->(l) do
|
601
|
-
# "select id from some_address_table where user_id = #{t.pk!}"
|
602
|
-
# end
|
603
|
-
# end
|
604
|
-
# s.type :address
|
605
|
-
# end
|
606
|
-
|
607
|
-
# expected = <<-SQL
|
608
|
-
# select
|
609
|
-
# to_json(x.*) res
|
610
|
-
# from (
|
611
|
-
# select
|
612
|
-
# id,
|
613
|
-
# (select to_json(x.*) res from (select id from addresses where id in (select id from some_address_table where user_id = users.id) limit 1) x) as address
|
614
|
-
# from users
|
615
|
-
# where id in (1)
|
616
|
-
# limit 1
|
617
|
-
# ) x
|
618
|
-
# SQL
|
619
|
-
|
620
|
-
# assert_equal token(expected), token(res)
|
621
|
-
# end
|
622
|
-
|
623
|
-
# def test_one_in_one
|
624
|
-
# res = query([[:user, 1], :id, [[:address], :id, [[:person], :id]]]) do |s|
|
625
|
-
# s.root :user
|
626
|
-
# s.type :user do |t|
|
627
|
-
# t.one :address
|
628
|
-
# end
|
629
|
-
# s.type :address do |t|
|
630
|
-
# t.one :person
|
631
|
-
# end
|
632
|
-
# s.type :person
|
633
|
-
# end
|
634
|
-
|
635
|
-
# expected = <<-SQL
|
636
|
-
# select
|
637
|
-
# to_json(x.*) res
|
638
|
-
# from (
|
639
|
-
# select
|
640
|
-
# id,
|
641
|
-
# (select to_json(x.*) res from (
|
642
|
-
# select
|
643
|
-
# id,
|
644
|
-
# (select to_json(x.*) res from (select id from people where address_id = addresses.id limit 1) x) as person
|
645
|
-
# from addresses where user_id = users.id limit 1
|
646
|
-
# ) x) as address
|
647
|
-
# from users
|
648
|
-
# where id in (1)
|
649
|
-
# limit 1
|
650
|
-
# ) x
|
651
|
-
# SQL
|
652
|
-
|
653
|
-
# assert_equal token(expected), token(res)
|
654
|
-
# end
|
655
|
-
|
656
|
-
# def test_many
|
657
|
-
# res = query([[:user, 1], :id, [[:addresses], :id]]) do |s|
|
658
|
-
# s.root :user
|
659
|
-
# s.type :user do |t|
|
660
|
-
# t.many :addresses
|
661
|
-
# end
|
662
|
-
# s.type :address
|
663
|
-
# end
|
664
|
-
|
665
|
-
# expected = <<-SQL
|
666
|
-
# select
|
667
|
-
# to_json(x.*) res
|
668
|
-
# from (
|
669
|
-
# select id,
|
670
|
-
# (select to_json(coalesce(json_agg(x.*), '[]'::json)) res from (select id from addresses where user_id = users.id) x) as addresses
|
671
|
-
# from users
|
672
|
-
# where id in (1) limit 1
|
673
|
-
# ) x
|
674
|
-
# SQL
|
675
|
-
|
676
|
-
# assert_equal token(expected), token(res)
|
677
|
-
# end
|
678
|
-
|
679
|
-
# def test_many_with_ids
|
680
|
-
# res = query([[:user, 1], :id, [[:addresses, 100], :id]]) do |s|
|
681
|
-
# s.root :user
|
682
|
-
# s.type :user do |t|
|
683
|
-
# t.many :addresses
|
684
|
-
# end
|
685
|
-
# s.type :address
|
686
|
-
# end
|
687
|
-
|
688
|
-
# expected = <<-SQL
|
689
|
-
# select
|
690
|
-
# to_json(x.*) res
|
691
|
-
# from (
|
692
|
-
# select id,
|
693
|
-
# (select to_json(coalesce(json_agg(x.*), '[]'::json)) res from (select id from addresses where user_id = users.id and id in (100)) x) as addresses
|
694
|
-
# from users
|
695
|
-
# where id in (1) limit 1
|
696
|
-
# ) x
|
697
|
-
# SQL
|
698
|
-
|
699
|
-
# assert_equal token(expected), token(res)
|
700
|
-
# end
|
701
|
-
|
702
|
-
# def test_one_in_many
|
703
|
-
# res = query([[:user, 1], :id, [[:addresses], :id, [[:person], :id]]]) do |s|
|
704
|
-
# s.root :user
|
705
|
-
# s.type :user do |t|
|
706
|
-
# t.many :addresses
|
707
|
-
# end
|
708
|
-
# s.type :address do |t|
|
709
|
-
# t.one :person
|
710
|
-
# end
|
711
|
-
# s.type :person
|
712
|
-
# end
|
713
|
-
|
714
|
-
# expected = <<-SQL
|
715
|
-
# select
|
716
|
-
# to_json(x.*) res
|
717
|
-
# from (
|
718
|
-
# select
|
719
|
-
# id,
|
720
|
-
# (select to_json(coalesce(json_agg(x.*), '[]'::json)) res from (
|
721
|
-
# select
|
722
|
-
# id,
|
723
|
-
# (select to_json(x.*) res from (select id from people where address_id = addresses.id limit 1) x) as person
|
724
|
-
# from addresses where user_id = users.id
|
725
|
-
# ) x) as addresses
|
726
|
-
# from users
|
727
|
-
# where id in (1)
|
728
|
-
# limit 1
|
729
|
-
# ) x
|
730
|
-
# SQL
|
731
|
-
|
732
|
-
# assert_equal token(expected), token(res)
|
733
|
-
# end
|
734
354
|
end
|
735
355
|
end
|