forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunique_within_region_validator_spec.rb
153 lines (125 loc) · 5.11 KB
/
unique_within_region_validator_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
146
147
148
149
150
151
152
153
RSpec.describe UniqueWithinRegionValidator do
describe "#unique_within_region" do
context "class without STI" do
let(:case_sensitive_class) do
Class.new(ApplicationRecord) do
self.table_name = "users"
validates :name, :unique_within_region => true
end
end
let(:case_insensitive_class) do
Class.new(ApplicationRecord) do
self.table_name = "users"
validates :name, :unique_within_region => {:match_case => false}
end
end
let(:scoped_class) do
Class.new(ApplicationRecord) do
self.table_name = "users"
validates :name, :unique_within_region => {:scope => :email}
end
end
let(:test_name) { "thename" }
let(:in_first_region_id) do
case_sensitive_class.create!(
:id => case_sensitive_class.id_in_region(1, 0),
:name => test_name,
).id
end
let(:also_in_first_region_id) do
case_sensitive_class.create!(
:id => case_sensitive_class.id_in_region(2, 0),
:name => test_name.upcase,
).id
end
let(:in_second_region_id) do
case_sensitive_class.create!(
:id => case_sensitive_class.id_in_region(2, 1),
:name => test_name,
).id
end
it "returns true if the field is unique for the records in the region" do
expect(case_sensitive_class.find(in_first_region_id).valid?).to be true
expect(case_sensitive_class.find(also_in_first_region_id).valid?).to be true
expect(case_sensitive_class.find(in_second_region_id).valid?).to be true
end
it "returns false if the field is not unique for the records in the region" do
in_first_region_rec = case_sensitive_class.find(in_first_region_id)
also_in_first_region_rec = case_sensitive_class.find(also_in_first_region_id)
in_second_region_rec = case_sensitive_class.find(in_second_region_id)
also_in_first_region_rec.name = in_first_region_rec.name
expect(in_first_region_rec.valid?).to be true
expect(also_in_first_region_rec.valid?).to be false
expect(in_second_region_rec.valid?).to be true
end
it "is case insensitive if match_case is set to false" do
in_first_region_rec = case_insensitive_class.find(in_first_region_id)
also_in_first_region_rec = case_insensitive_class.new(:id => case_sensitive_class.id_in_region(2, 0), :name => test_name)
in_second_region_rec = case_insensitive_class.find(in_second_region_id)
expect(in_first_region_rec.valid?).to be true
expect(also_in_first_region_rec.valid?).to be false
expect(in_second_region_rec.valid?).to be true
end
it "applies the passed scope" do
in_first_region_rec = scoped_class.find(in_first_region_id)
also_in_first_region_rec = scoped_class.find(also_in_first_region_id)
in_second_region_rec = scoped_class.find(in_second_region_id)
expect(in_first_region_rec.valid?).to be true
expect(also_in_first_region_rec.valid?).to be true
expect(in_second_region_rec.valid?).to be true
also_in_first_region_rec.name = test_name
expect(also_in_first_region_rec.valid?).to be false
end
it "gracefully handles no name" do
rec = case_sensitive_class.new
expect { expect(rec.valid?).to eq(true) }.not_to make_database_queries
end
it "queries for a new record" do
rec = case_sensitive_class.new(:name => 'abc')
expect { expect(rec.valid?).to eq(true) }.to make_database_queries(:count => 1)
end
it "queries for a changed record" do
rec = case_sensitive_class.create!(:name => 'abc')
rec.name += '2'
expect { expect(rec.valid?).to eq(true) }.to make_database_queries(:count => 1)
end
it "doesn't query for an unchanged record" do
rec = case_sensitive_class.create!(:name => 'abc')
expect { expect(rec.valid?).to eq(true) }.not_to make_database_queries
end
end
context "class with STI" do
let(:test_base_class) do
Class.new(ApplicationRecord) do
validates :name, :unique_within_region => true
self.table_name = "vms"
end
end
let(:test_subclass1) do
Class.new(test_base_class) do
def self.name
"Subclass1"
end
end
end
let(:test_subclass2) do
Class.new(test_base_class) do
def self.name
"Subclass2"
end
end
end
context "two subclasses" do
it "raises error with non-unique names in same region" do
test_subclass1.create!(:name => "foo")
expect { test_subclass2.create!(:name => "foo") }
.to raise_error(ActiveRecord::RecordInvalid, / Name is not unique within region/)
end
it "doesn't raise error with unique names" do
test_subclass1.create!(:name => "foo")
expect { test_subclass2.create!(:name => "bar") }.to_not raise_error
end
end
end
end
end