forked from ontoportal/goo
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_inverse.rb
113 lines (90 loc) · 2.88 KB
/
test_inverse.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
require_relative 'test_case'
class Task < Goo::Base::Resource
model :task, name_with: :description
attribute :description, enforce: [ :existence, :unique]
#one task can be linked to many projects
attribute :project, enforce: [ :list, :project ]
def initialize(attributes = {})
super(attributes)
end
end
class Project < Goo::Base::Resource
model :project, name_with: :name
attribute :name, enforce: [ :existence, :unique ]
attribute :active, enforce: [ :boolean ]
attribute :tasks, inverse: { on: Task, attribute: :project }
end
class TestInverse < MiniTest::Unit::TestCase
def initialize(*args)
super(*args)
end
def self.before_suite
Task.all.each do |x|
x.delete
end
Project.all.each do |x|
x.delete
end
end
def self.after_suite
Task.all.each do |x|
x.delete
end
Project.all.each do |x|
x.delete
end
end
def test_inverse_retrieval
assert Project.range(:tasks) == Task
assert Task.range(:project) == Project
assert Goo.models[:task] == Task
assert Goo.models[:project] == Project
assert Project.attributes(:list).include?(:tasks)
project = Project.new(name: "Goo")
Project.find("Goo").first.delete if project.exist?
assert project.valid?
project.save
assert Project.where.include(:tasks).all.first.tasks == []
assert project.persistent?
assert_equal(0,
GooTest.count_pattern(
"#{project.id.to_ntriples} #{project.class.attribute_uri(:tasks).to_ntriples} ?x " ))
5.times do |i|
task = Task.new(description: "task_#{i}", project: [ project ])
Task.find("task_#{i}").first.delete if task.exist?
assert task.valid?
task.save
end
#task => project
5.times do |i|
task = Task.find("task_#{i}").include(:project).first
assert task.project.first.id == project.id
end
#project => task
project = Project.find("Goo").include(:tasks).first
assert_equal(5, project.tasks.length)
assert project.tasks.map { |x| x.id.to_s[33].to_i }.sort == [0,1,2,3,4]
#do not allow to assign inverse properties
assert_raises ArgumentError do
project.tasks = Task.find("task_1")
end
3.times do |i|
Task.find("task_#{i}").first.delete()
end
project = Project.find("Goo").include(Project.attributes(:all)).first
assert_equal(2, project.tasks.length)
#on save no persist inverse
project.active = true
project.save
assert_equal(2, project.tasks.length)
assert_equal(0,
GooTest.count_pattern(
"#{project.id.to_ntriples} #{project.class.attribute_uri(:tasks).to_ntriples} ?x " ))
Task.find("task_3").first.delete()
Task.find("task_4").first.delete()
assert_equal(2, project.tasks.length)
assert project.tasks.map { |x| x.id.to_s[33].to_i }.sort == [3,4]
project = Project.find("Goo").include(:tasks).first
project.delete
end
end