-
Notifications
You must be signed in to change notification settings - Fork 456
/
materialized_path_test.rb
125 lines (103 loc) · 3.43 KB
/
materialized_path_test.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
require_relative '../environment'
class MaterializedPathTest < ActiveSupport::TestCase
def test_ancestry_column_values
return if AncestryTestDatabase.materialized_path2?
AncestryTestDatabase.with_model do |model|
root = model.create!
node = model.new
# new node
assert_ancestry node, nil
assert_raises(Ancestry::AncestryException) { node.child_ancestry }
# saved
node.save!
assert_ancestry node, nil, child: "#{node.id}"
# changed
node.ancestor_ids = [root.id]
assert_ancestry node, "#{root.id}", db: nil, child: "#{node.id}"
# changed saved
node.save!
assert_ancestry node, "#{root.id}", child: "#{root.id}/#{node.id}"
# reloaded
node.reload
assert_ancestry node, "#{root.id}", child: "#{root.id}/#{node.id}"
# fresh node
node = model.find(node.id)
assert_ancestry node, "#{root.id}", child: "#{root.id}/#{node.id}"
end
end
def test_ancestry_column_validation
return if AncestryTestDatabase.materialized_path2?
AncestryTestDatabase.with_model do |model|
node = model.create # assuming id == 1
['3', '10/2', '9/4/30', model.ancestry_root].each do |value|
node.send :write_attribute, model.ancestry_column, value
assert node.sane_ancestor_ids?
assert node.valid?
end
end
end
def test_ancestry_column_validation_fails
return if AncestryTestDatabase.materialized_path2?
AncestryTestDatabase.with_model do |model|
node = model.create
['a', 'a/b', '-34'].each do |value|
node.send :write_attribute, model.ancestry_column, value
refute node.sane_ancestor_ids?
refute node.valid?
end
end
end
def test_ancestry_column_validation_string_key
return if AncestryTestDatabase.materialized_path2?
AncestryTestDatabase.with_model(:id => :string, :primary_key_format => /[a-z]/) do |model|
node = model.create(:id => 'z')
['a', 'a/b', 'a/b/c', model.ancestry_root].each do |value|
node.send :write_attribute, model.ancestry_column, value
assert node.sane_ancestor_ids?
assert node.valid?
end
end
end
def test_ancestry_column_validation_string_key_fails
return if AncestryTestDatabase.materialized_path2?
AncestryTestDatabase.with_model(:id => :string, :primary_key_format => /[a-z]/) do |model|
node = model.create(:id => 'z')
['1', '1/2', 'a-b/c'].each do |value|
node.send :write_attribute, model.ancestry_column, value
refute node.sane_ancestor_ids?
refute node.valid?
end
end
end
def test_ancestry_validation_exclude_self
return if AncestryTestDatabase.materialized_path2?
AncestryTestDatabase.with_model do |model|
parent = model.create!
child = parent.children.create!
assert_raise ActiveRecord::RecordInvalid do
parent.parent = child
refute parent.sane_ancestor_ids?
parent.save!
end
end
end
private
def assert_ancestry(node, value, child: :skip, db: :value)
if value.nil?
assert_nil node.ancestry
else
assert_equal value, node.ancestry
end
db = value if db == :value
if db.nil?
assert_nil node.ancestry_in_database
else
assert_equal db, node.ancestry_in_database
end
if child.nil?
assert_nil node.child_ancestry
elsif child != :skip
assert_equal child, node.child_ancestry
end
end
end