-
Notifications
You must be signed in to change notification settings - Fork 275
/
label_spec.rb
238 lines (196 loc) · 6.46 KB
/
label_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
require 'spec_helper'
#tests = Proc.new do
describe "Labels" do
before(:all) do
@prev_wrapped_classes = Neo4j::ActiveNode::Labels._wrapped_classes.dup
Neo4j::ActiveNode::Labels._wrapped_labels = nil
Neo4j::ActiveNode::Labels._wrapped_classes.clear
class TestClass
include Neo4j::ActiveNode
end
Neo4j::Label.create(:IndexedTestClass).drop_index(:name)
class IndexedTestClass
include Neo4j::ActiveNode
property :name
index :name # will index using the IndexedTestClass label
end
module SomeLabelMixin
def self.mapped_label_name
:some_label
end
def self.neo4j_session
Neo4j::Session.current
end
extend Neo4j::ActiveNode::Query::ClassMethods
extend Neo4j::ActiveNode::Labels::ClassMethods
end
class SomeLabelClass
include Neo4j::ActiveNode
include SomeLabelMixin
end
class RelationTestClass
include Neo4j::ActiveNode
has_one :in, :test_class
end
end
after(:all) do
Neo4j::ActiveNode::Labels._wrapped_classes.concat(@prev_wrapped_classes)
Neo4j::ActiveNode::Labels._wrapped_labels = nil
Object.send(:remove_const, :IndexedTestClass)
Object.send(:remove_const, :TestClass)
end
describe 'create' do
it 'automatically sets a label' do
p = TestClass.create
p.labels.to_a.should == [:TestClass]
end
it "sets label for mixin classes" do
p = SomeLabelClass.create
p.labels.to_a.should =~ [:SomeLabelClass, :some_label]
end
end
describe 'all' do
it "finds it without an index" do
p = TestClass.create
TestClass.all.to_a.should include(p)
end
describe 'when indexed' do
it 'can find it without using the index' do
andreas = IndexedTestClass.create(name: 'andreas')
result = IndexedTestClass.all
result.should include(andreas)
end
it 'does not find it if it has been deleted' do
jimmy = IndexedTestClass.create(name: 'jimmy')
result = IndexedTestClass.all
result.should include(jimmy)
jimmy.destroy
IndexedTestClass.all.should_not include(jimmy)
end
end
end
describe 'find' do
it "finds it without an index" do
p = TestClass.create
TestClass.all.to_a.should include(p)
end
describe 'when indexed' do
it 'can find it using the index' do
IndexedTestClass.destroy_all
kalle = IndexedTestClass.create(name: 'kalle')
IndexedTestClass.where(name: 'kalle').first.should == kalle
end
it 'does not find it if deleted' do
IndexedTestClass.destroy_all
kalle2 = IndexedTestClass.create(name: 'kalle2')
result = IndexedTestClass.where(name: 'kalle2').first
result.should == kalle2
kalle2.destroy
IndexedTestClass.where(name: 'kalle2').should_not include(kalle2)
end
end
context 'a relationship' do
let!(:n1) { TestClass.create }
let!(:n2) { RelationTestClass.create(test_class: n1) }
it 'finds when association matches' do
RelationTestClass.where(test_class: n1).first.should == n2
end
it 'does not find when association does not match' do
RelationTestClass.where(test_class: n2).first.should be_nil
end
end
describe 'when finding using a Module' do
it 'finds it' do
thing = SomeLabelClass.create
SomeLabelMixin.all.should include(thing)
end
end
end
describe 'find_by, find_by!' do
before(:all) { @jasmine = IndexedTestClass.create(name: 'jasmine') }
describe 'find_by' do
it 'finds the expected object' do
expect(IndexedTestClass.find_by(name: 'jasmine')).to eq @jasmine
end
it 'returns nil if no results match' do
expect(IndexedTestClass.find_by(name: 'foo')).to eq nil
end
end
describe 'find_by!' do
it 'finds the expected object' do
expect(IndexedTestClass.find_by!(name: 'jasmine')).to eq @jasmine
end
it 'raises an error if no results match' do
expect{IndexedTestClass.find_by!(name: 'foo')}.to raise_exception Neo4j::ActiveNode::Labels::RecordNotFound
end
end
end
describe 'first and last' do
before(:all) do
class FirstLastTestClass
include Neo4j::ActiveNode
property :name
end
class EmptyTestClass
include Neo4j::ActiveNode
end
@jasmine = FirstLastTestClass.create(name: 'jasmine')
@middle = FirstLastTestClass.create
@lauren = FirstLastTestClass.create(name: 'lauren')
end
describe 'first' do
it 'returns the first object created... sort of, see docs' do
expect(FirstLastTestClass.first).to eq [@jasmine, @middle, @lauren].sort_by(&:uuid).first
end
end
describe 'last' do
it 'returns the last object created... sort of, see docs' do
expect(FirstLastTestClass.last).to eq [@jasmine, @middle, @lauren].sort_by(&:uuid).last
end
it 'returns nil when there are no results' do
expect(EmptyTestClass.last).to eq nil
end
end
end
end
#shared_examples_for 'Neo4j::ActiveNode with Mixin Index'do
# before(:all) do
# Neo4j::ActiveNode::Labels._wrapped_classes = []
# Neo4j::ActiveNode::Labels._wrapped_labels = nil
#
# Neo4j::Label.create(:BarIndexedLabel).drop_index(:baaz)
# sleep(1) # to make it possible to search using this module (?)
#
# module BarIndexedLabel
# extend Neo4j::ActiveNode::Labels::ClassMethods # to make it possible to search using this module (?)
# begin
# index :baaz
# rescue => e
# puts "WARNING: sometimes neo4j has a problem with removing and adding indexes in tests #{e}" # TODO
# end
# end
#
# class TestClassWithBar
# include Neo4j::ActiveNode
# include BarIndexedLabel
# end
# end
#
#
# it "can be found using the Mixin Module" do
# hej = TestClassWithBar.create(:baaz => 'hej')
# BarIndexedLabel.find(:baaz, 'hej').should include(hej)
# TestClassWithBar.find(:baaz, 'hej').should include(hej)
# BarIndexedLabel.find(:baaz, 'hej2').should_not include(hej)
# TestClassWithBar.find(:baaz, 'hej2').should_not include(hej)
# end
#end
#describe 'Neo4j::ActiveNode, server', api: :server do
# it_behaves_like 'Neo4j::ActiveNode'
# it_behaves_like "Neo4j::ActiveNode with Mixin Index"
#end
#
#describe 'Neo4j::ActiveNode, embedded', api: :embedded do
# it_behaves_like 'Neo4j::ActiveNode'
# it_behaves_like "Neo4j::ActiveNode with Mixin Index"
#end