-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
relatable_spec.rb
145 lines (111 loc) · 4.91 KB
/
relatable_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
require "rails_helper"
describe SDG::Relatable do
let(:goal) { SDG::Goal[1] }
let(:target) { SDG::Target["1.2"] }
let(:local_target) { create(:sdg_local_target, code: "1.2.1") }
let(:another_goal) { SDG::Goal[2] }
let(:another_target) { SDG::Target["2.3"] }
let(:another_local_target) { create(:sdg_local_target, code: "2.3.1") }
let(:relatable) { create(:proposal) }
describe "#sdg_goals" do
it "can assign goals to a model" do
relatable.sdg_goals = [goal, another_goal]
expect(SDG::Relation.count).to be 2
expect(SDG::Relation.first.relatable).to eq relatable
expect(SDG::Relation.last.relatable).to eq relatable
expect(SDG::Relation.first.related_sdg).to eq goal
expect(SDG::Relation.last.related_sdg).to eq another_goal
end
it "can obtain the list of goals" do
relatable.sdg_goals = [goal, another_goal]
expect(relatable.reload.sdg_goals).to match_array [goal, another_goal]
end
end
describe "#sdg_targets" do
it "can assign targets to a model" do
relatable.sdg_targets = [target, another_target]
expect(SDG::Relation.count).to be 2
expect(SDG::Relation.first.relatable).to eq relatable
expect(SDG::Relation.last.relatable).to eq relatable
expect(SDG::Relation.first.related_sdg).to eq target
expect(SDG::Relation.last.related_sdg).to eq another_target
end
it "can obtain the list of targets" do
relatable.sdg_targets = [target, another_target]
expect(relatable.reload.sdg_targets).to match_array [target, another_target]
end
end
describe "#sdg_local_targets" do
it "can assign local targets to a model" do
relatable.sdg_local_targets = [local_target, another_local_target]
expect(SDG::Relation.count).to be 2
expect(SDG::Relation.first.relatable).to eq relatable
expect(SDG::Relation.last.relatable).to eq relatable
expect(SDG::Relation.first.related_sdg).to eq local_target
expect(SDG::Relation.last.related_sdg).to eq another_local_target
end
it "can obtain the list of local targets" do
relatable.sdg_local_targets = [local_target, another_local_target]
expect(relatable.reload.sdg_local_targets).to match_array [local_target, another_local_target]
end
end
describe "#related_sdgs" do
it "returns all related goals and targets" do
relatable.sdg_goals = [goal, another_goal]
relatable.sdg_targets = [target, another_target]
relatable.sdg_local_targets = [local_target, another_local_target]
related_sdgs = [goal, another_goal, target, another_target, local_target, another_local_target]
expect(relatable.reload.related_sdgs).to match_array related_sdgs
end
end
describe "#sdg_target_list=" do
it "assigns a single target" do
relatable.sdg_target_list = "1.1"
expect(relatable.reload.sdg_targets).to match_array [SDG::Target["1.1"]]
end
it "assigns multiple targets" do
relatable.sdg_target_list = "1.1,2.3"
expect(relatable.reload.sdg_targets).to match_array [SDG::Target["1.1"], SDG::Target["2.3"]]
end
it "ignores trailing spaces and spaces between commas" do
relatable.sdg_target_list = " 1.1, 2.3 "
expect(relatable.reload.sdg_targets).to match_array [SDG::Target["1.1"], SDG::Target["2.3"]]
end
it "assigns goals" do
relatable.sdg_target_list = "1.1,1.2,2.3"
expect(relatable.reload.sdg_goals).to match_array [SDG::Goal[1], SDG::Goal[2]]
end
end
describe ".by_goal" do
it "returns everything if no code is provided" do
expect(relatable.class.by_goal("")).to eq [relatable]
expect(relatable.class.by_goal(nil)).to eq [relatable]
end
it "returns records associated with that goal" do
same_association = create(:proposal, sdg_goals: [goal])
both_associations = create(:proposal, sdg_goals: [goal, another_goal])
expect(relatable.class.by_goal(goal.code)).to match_array [same_association, both_associations]
end
it "does not return records not associated with that goal" do
create(:proposal)
create(:proposal, sdg_goals: [another_goal])
expect(relatable.class.by_goal(goal.code)).to be_empty
end
end
describe ".by_target" do
it "returns everything if no code is provided" do
expect(relatable.class.by_target("")).to eq [relatable]
expect(relatable.class.by_target(nil)).to eq [relatable]
end
it "returns records associated with that target" do
same_association = create(:proposal, sdg_targets: [target])
both_associations = create(:proposal, sdg_targets: [target, another_target])
expect(relatable.class.by_target(target.code)).to match_array [same_association, both_associations]
end
it "does not return records not associated with that target" do
create(:proposal)
create(:proposal, sdg_targets: [another_target])
expect(relatable.class.by_target(target.code)).to be_empty
end
end
end