-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathactive_record_source_spec.rb
98 lines (78 loc) · 3.41 KB
/
active_record_source_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# frozen_string_literal: true
require "spec_helper"
describe GraphQL::Dataloader::ActiveRecordSource do
if testing_rails?
describe "finding by ID" do
it_dataloads "loads once, then returns from a cache when available" do |d|
log = with_active_record_log(colorize: false) do
r1 = d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load(1)
assert_equal "Vulfpeck", r1.name
end
assert_includes log, 'SELECT "bands".* FROM "bands" WHERE "bands"."id" = ? [["id", 1]]'
log = with_active_record_log(colorize: false) do
r1 = d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load(1)
assert_equal "Vulfpeck", r1.name
end
assert_equal "", log
log = with_active_record_log(colorize: false) do
records = d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load_all([1, 99, 2, 3])
assert_equal ["Vulfpeck", nil, "Tom's Story", "Chon"], records.map { |r| r&.name }
end
assert_includes log, '[["id", 99], ["id", 2], ["id", 3]]'
end
it_dataloads "casts load values to the column type" do |d|
log = with_active_record_log(colorize: false) do
r1 = d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load("1")
assert_equal "Vulfpeck", r1.name
end
assert_includes log, 'SELECT "bands".* FROM "bands" WHERE "bands"."id" = ? [["id", 1]]'
log = with_active_record_log(colorize: false) do
d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load(1)
end
assert_equal "", log
log = with_active_record_log(colorize: false) do
d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load("1")
end
assert_equal "", log
end
end
describe "finding by other columns" do
it_dataloads "uses the alternative primary key" do |d|
log = with_active_record_log(colorize: false) do
r1 = d.with(GraphQL::Dataloader::ActiveRecordSource, AlternativeBand).load("Vulfpeck")
assert_equal "Vulfpeck", r1.name
if Rails::VERSION::STRING > "8"
assert_equal 1, r1["id"]
else
assert_equal 1, r1._read_attribute("id")
end
end
assert_includes log, 'SELECT "bands".* FROM "bands" WHERE "bands"."name" = ? [["name", "Vulfpeck"]]'
end
it_dataloads "uses specified find_by columns" do |d|
log = with_active_record_log(colorize: false) do
r1 = d.with(GraphQL::Dataloader::ActiveRecordSource, Band, find_by: :name).load("Chon")
assert_equal "Chon", r1.name
assert_equal 3, r1.id
end
assert_includes log, 'SELECT "bands".* FROM "bands" WHERE "bands"."name" = ? [["name", "Chon"]]'
end
end
describe "warming the cache" do
it_dataloads "can receive passed-in objects with a class" do |d|
d.with(GraphQL::Dataloader::ActiveRecordSource, Band).merge({ 100 => Band.find(3) })
log = with_active_record_log(colorize: false) do
band3 = d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load(100)
assert_equal "Chon", band3.name
assert_equal 3, band3.id
end
assert_equal "", log
end
it "can infer class of passed-in objects"
end
describe "in queries" do
it "loads records with dataload_record"
it "accepts custom find-by with dataload_record"
end
end
end