yaml_conditions 0.0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require File.join(File.dirname(__FILE__), 'yaml', 'conditions')
3
+ require File.join(File.dirname(__FILE__), 'yaml', 'query_builder')
4
+
5
+ if defined?(ActiveRecord)
6
+ require File.join(File.dirname(__FILE__), 'orms', 'active_record')
7
+
8
+ # Load required Modules
9
+ ActiveRecord::Base.configurations.each do |env, conf|
10
+ require File.expand_path(File.join(File.dirname(__FILE__), 'yaml', 'query_builder', 'sql', conf['adapter']))
11
+ end
12
+
13
+ elsif defined?(DataMapper)
14
+ require File.join(File.dirname(__FILE__), 'orms', 'datamapper')
15
+ end
data/out.txt ADDED
File without changes
@@ -0,0 +1,33 @@
1
+ # AR spec
2
+ require 'rubygems'
3
+ Dir["#{File.dirname(__FILE__)}/../tasks/*.rake"].sort.each { |ext| load ext }
4
+
5
+ if ENV['VERSION_3']
6
+ gem 'activerecord', [ '>= 3.0.0'], :require => 'active_record'
7
+ else
8
+ gem 'activerecord', [ '>= 2.3.4', '<= 2.3.10' ], :require => 'active_record'
9
+ end
10
+ require File.join( File.dirname(__FILE__), 'spec_helper')
11
+ require File.join( File.dirname(__FILE__), '..', 'lib', 'orms', 'active_record')
12
+ require 'connection'
13
+
14
+ # Load required Modules
15
+ ActiveRecord::Base.configurations.each do |env, conf|
16
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'yaml', 'query_builder', 'sql', conf['adapter']))
17
+ end
18
+
19
+ def schema_already_exists?
20
+ ActiveRecord::Base.connection.table_exists?('jobs')
21
+ end
22
+
23
+ unless ActiveRecord::Base.connected?
24
+ begin
25
+ ActiveRecord::Base.connection
26
+ rescue
27
+ Rake::Task["#{$ADAPTER}:build_databases"].invoke
28
+ end
29
+ end
30
+
31
+ unless schema_already_exists?
32
+ load(File.join( File.dirname(__FILE__), 'schema.rb'))
33
+ end
@@ -0,0 +1,177 @@
1
+ require File.join( File.dirname(__FILE__), '..', '..', '..', 'ar_spec_helper')
2
+ require File.join( File.dirname(__FILE__), '..', '..', '..', 'models', 'job')
3
+ require File.join( File.dirname(__FILE__), '..', '..', '..', 'models', 'user')
4
+ require File.join( File.dirname(__FILE__), '..', '..', '..', 'models', 'priority')
5
+ require File.join( File.dirname(__FILE__), '..', '..', '..', 'models', 'delayed', 'job')
6
+ require File.join( File.dirname(__FILE__), '..', '..', '..', 'models', 'delayed', 'performable_method')
7
+
8
+ describe ::Orms::ActiveRecordVersion2 do
9
+
10
+ before do
11
+ Delayed::Job.destroy_all
12
+ User.destroy_all
13
+ end
14
+
15
+ describe 'Delayed::Job#find' do
16
+
17
+ class NotificationMailer; end # define this for test purposes
18
+
19
+ before do
20
+ user = User.create(:name => 'marcelo')
21
+ @performable_method = Delayed::PerformableMethod.new(NotificationMailer, :deliver_admin_login, [ user, nil ])
22
+ @job = Delayed::Job.create(:handler => "--- !ruby/struct:Delayed::PerformableMethod \nobject: CLASS:NotificationMailer\nmethod: :deliver_admin_login\nargs: \n- AR:User:#{user.id}\n- \n")
23
+ end
24
+
25
+ subject { Delayed::Job.first(:yaml_conditions => @yaml_conditions) }
26
+
27
+ context 'test filter by args' do
28
+
29
+ before do
30
+ @yaml_conditions = { :handler => { :args => [ User.find_by_name('marcelo'), nil ] } }
31
+ end
32
+
33
+ #it { should == @job }
34
+ end
35
+
36
+ context 'test filtering by object and args' do
37
+ before do
38
+ @yaml_conditions = { :handler => { :object => NotificationMailer, :args => [ User.find_by_name('marcelo'), nil ] } }
39
+ end
40
+
41
+ #it { should == @job }
42
+ end
43
+
44
+ context 'test filtering by object, method and args' do
45
+ before do
46
+ @yaml_conditions = { :handler => { :object => NotificationMailer, :method => :deliver_admin_login, :args => [ User.find_by_name('marcelo'), nil ] } }
47
+ end
48
+
49
+ #it { should == @job }
50
+ end
51
+
52
+ context 'test filtering by object, method and wrong args' do
53
+ before do
54
+ user2 = User.create(:name => 'andres')
55
+ @yaml_conditions = { :handler => { :object => NotificationMailer, :method => :deliver_admin_login, :args => [ User.find_by_name('andres'), nil ] } }
56
+ end
57
+
58
+ #it { should be_nil }
59
+ end
60
+
61
+ context 'test filtering by object, args and wrong method' do
62
+ before do
63
+ user2 = User.create(:name => 'andres')
64
+ @yaml_conditions = { :handler => { :object => NotificationMailer, :method => :deliver_admin_login2, :args => [ User.find_by_name('andres'), nil ] } }
65
+ end
66
+
67
+ #it { should be_nil }
68
+ end
69
+
70
+ context 'test filtering by method, args and wrong object' do
71
+ before do
72
+ user2 = User.create(:name => 'andres')
73
+ @yaml_conditions = { :handler => { :object => Object, :method => :deliver_admin_login2, :args => [ User.find_by_name('andres'), nil ] } }
74
+ end
75
+
76
+ #it { should be_nil }
77
+ end
78
+
79
+ context 'test filtering by object and args' do
80
+ before do
81
+ user2 = User.create(:name => 'andres')
82
+ @job = Delayed::Job.create(:handler => "--- !ruby/struct:Delayed::PerformableMethod \nobject: CLASS:NotificationMailer\nmethod: :deliver_admin_login\nargs: \n- AR:User:#{user2.id}\n- Pablo\n")
83
+ @yaml_conditions = { :handler => { :object => NotificationMailer, :args => [ User.find_by_name('andres'), 'Pablo' ] } }
84
+ end
85
+
86
+ #it { should == @job }
87
+ end
88
+
89
+ context 'test filter by args using wildcard' do
90
+
91
+ before do
92
+ @yaml_conditions = { :handler => { :args => [ User.find_by_name('marcelo'), '*' ] } }
93
+ end
94
+
95
+ #it { should == @job }
96
+ end
97
+
98
+ context 'test filter by using wildcard on args and specifying object' do
99
+
100
+ before do
101
+ @yaml_conditions = { :handler => { :object => NotificationMailer, :args => [ User.find_by_name('marcelo'), '*' ] } }
102
+ end
103
+
104
+ #it { should == @job }
105
+ end
106
+
107
+ context 'test filter by using wildcard on args and method, also specifying object' do
108
+
109
+ before do
110
+ @yaml_conditions = { :handler => { :object => NotificationMailer, :method => '*', :args => [ User.find_by_name('marcelo'), '*' ] } }
111
+ end
112
+
113
+ #it { should == @job }
114
+ end
115
+
116
+ context 'test filter by using wildcard on object, method and args also specified' do
117
+
118
+ before do
119
+ @yaml_conditions = { :handler => { :object => '*', :method => :deliver_admin_login, :args => [ User.find_by_name('marcelo'), nil ] } }
120
+ end
121
+
122
+ #it { should == @job }
123
+ end
124
+
125
+ context "job with nested hash: { :user => { :first_name => 'Marcelo', :last_name => 'Giorgi', :address => { :city => 'Montevideo', :country => 'Uruguay' } }}" do
126
+ before do
127
+ @job_with_nested = Delayed::Job.create(:handler => "--- !ruby/struct:Delayed::PerformableMethod \nobject: CLASS:NotificationMailer\nmethod: :deliver_test_email\nargs: \n- :user: \n :first_name: Marcelo\n :last_name: Giorgi\n :address: \n :city: Montevideo\n :country: Uruguay\n")
128
+ end
129
+
130
+ context 'query first_name hash value of the delayed::job argument' do
131
+ context 'with proper value' do
132
+ before do
133
+ @yaml_conditions = { :handler => { :args => [ { :user => { :first_name => 'Marcelo' }} ] } }
134
+ end
135
+ #it { should == @job_with_nested }
136
+ end
137
+
138
+ context 'with wrong value' do
139
+ before do
140
+ @yaml_conditions = { :handler => { :args => [ { :user => { :first_name => 'Pablo' }} ] } }
141
+ end
142
+ #it { should be_nil }
143
+ end
144
+
145
+ context 'with wrong hierarchy' do
146
+ before do
147
+ @yaml_conditions = { :handler => { :args => [ { :first_name => 'Marcelo' } ] } }
148
+ end
149
+ #it { should be_nil }
150
+ end
151
+ end
152
+
153
+ context 'query last_name and city vlues of the delayed::job argument' do
154
+ context 'with proper values' do
155
+ before do
156
+ @yaml_conditions = { :handler => { :args => [ { :user => { :last_name => 'Giorgi', :address => { :city => 'Montevideo' } } } ] } }
157
+ end
158
+ #it { should == @job_with_nested }
159
+ end
160
+
161
+ context 'with wrong value' do
162
+ before do
163
+ @yaml_conditions = { :handler => { :args => [ { :user => { :last_name => 'Marcelo', :address => { :city => 'Rio Negro' } } } ] } }
164
+ end
165
+ #it { should be_nil }
166
+ end
167
+
168
+ context 'with wrong hierarchy' do
169
+ before do
170
+ @yaml_conditions = { :handler => { :args => [ { :city => 'Montevideo' } ] } }
171
+ end
172
+ #it { should be_nil }
173
+ end
174
+ end
175
+ end
176
+ end
177
+ end
@@ -0,0 +1,346 @@
1
+ require File.join( File.dirname(__FILE__), '..', '..', '..', 'ar_spec_helper')
2
+ require File.join( File.dirname(__FILE__), '..', '..', '..', 'models', 'job')
3
+ require File.join( File.dirname(__FILE__), '..', '..', '..', 'models', 'user')
4
+ require File.join( File.dirname(__FILE__), '..', '..', '..', 'models', 'user_data')
5
+ require File.join( File.dirname(__FILE__), '..', '..', '..', 'models', 'priority')
6
+ require File.join( File.dirname(__FILE__), '..', '..', '..', 'models', 'period')
7
+
8
+ describe Orms::ActiveRecordVersion2 do
9
+
10
+ before do
11
+ Job.destroy_all
12
+ User.destroy_all
13
+ end
14
+
15
+ describe '#find' do
16
+ context 'there is a Job(data: #User{:name => marcelo})' do
17
+
18
+ before do
19
+ user = User.create(:name => 'marcelo')
20
+ @job = Job.create(:name => 'job1', :data => YAML.dump(user))
21
+ @job.priorities.create(:value => 1)
22
+ @job.priorities.create(:value => 2)
23
+ @job.priorities.create(:value => 3)
24
+ @job2 = Job.create(:name => 'job2', :data => YAML.dump(user))
25
+ @job2.priorities.create(:value => 3)
26
+ @job3 = Job.create(:name => 'job3')
27
+ @yaml_conditions = {}
28
+ @conditions = {}
29
+ @includes = {}
30
+ @selector = :first
31
+ end
32
+
33
+ subject { Job.find(@selector, :include => @includes, :yaml_conditions => @yaml_conditions, :conditions => @conditions) }
34
+
35
+ context ":yaml_conditions == { :data => { :class => User, :name => 'marcelo' } }" do
36
+ before do
37
+ @yaml_conditions = { :data => { :class => User, :name => 'marcelo' } }
38
+ end
39
+
40
+ it { should == @job }
41
+
42
+ context 'used with standard conditions (String version), that should match the existing Job.' do
43
+ before do
44
+ @conditions = "name = 'job1'"
45
+ end
46
+
47
+ it { should == @job }
48
+ end
49
+
50
+ context 'used with standard conditions (Hash version), that should match the existing Job.' do
51
+ before do
52
+ @conditions = { :name => 'job1' }
53
+ end
54
+
55
+ it { should == @job }
56
+ end
57
+
58
+ context 'used with standard conditions (Array version), that should match the existing Job.' do
59
+ before do
60
+ @conditions = [ 'name = ?', 'job1' ]
61
+ end
62
+
63
+ it { should == @job }
64
+ end
65
+
66
+ context 'used with std nested contidions (Hash version), that should match' do
67
+ before do
68
+ @includes = :priorities
69
+ @conditions = { :name => 'job1', :priorities => { :value => 1 } }
70
+ end
71
+
72
+ it { should == @job }
73
+ end
74
+
75
+ context 'used with std nested contidions (Hash version), that should match MANY priorities' do
76
+ before do
77
+ @includes = :priorities
78
+ @conditions = { :name => 'job1', :priorities => { :value => [1, 3] } }
79
+ end
80
+
81
+ it { should == @job }
82
+ end
83
+
84
+ context 'used with std nested contidions (Array version), that should match MANY jobs' do
85
+ before do
86
+ @selector = :all
87
+ @includes = :priorities
88
+ @conditions = [ '(jobs.name = ? OR jobs.name = ?) and priorities.value IN (?)', 'job1', 'job2', [1, 3] ]
89
+ end
90
+
91
+ it { should == [@job, @job2] }
92
+ end
93
+
94
+ context 'used with std nested contidions (Hash version), that should NOT match' do
95
+ before do
96
+ @includes = :priorities
97
+ @conditions = { :name => 'job1', :priorities => { :value => 14 } }
98
+ end
99
+
100
+ it { should be_nil }
101
+ end
102
+
103
+ context 'used with std nested contidions (Array version), that should match' do
104
+ before do
105
+ @includes = :priorities
106
+ @conditions = [ 'jobs.name = ? AND priorities.value = ?', 'job1', 2 ]
107
+ end
108
+
109
+ it { should == @job }
110
+ end
111
+
112
+ context 'used with std nested contidions (Array version), that should NOT match' do
113
+ before do
114
+ @includes = :priorities
115
+ @conditions = [ 'jobs.name = ? AND priorities.value = ?', 'job1', 14 ]
116
+ end
117
+
118
+ it { should be_nil }
119
+ end
120
+
121
+ context 'used with standard conditions (that should NOT match the existing Job)' do
122
+ before do
123
+ @conditions = { :name => 'unexisting' }
124
+ end
125
+
126
+ it { should be_nil }
127
+ end
128
+ end
129
+
130
+ context ":yaml_conditions == { :data => { :class => User, :name => 'andres' } }" do
131
+ before do
132
+ @yaml_conditions = { :data => { :class => User, :name => 'andres' } }
133
+ end
134
+
135
+ it { should be_nil }
136
+ end
137
+ end
138
+
139
+ context 'there is a Job(data: #Struct{:method => :new_email, :handler => User#(email => marcelo) })' do
140
+
141
+ before do
142
+ with_warnings(nil) { @struct = Struct.new('MyStruct', :method, :handler, :email, :number, :some_rate) }
143
+ @user = User.create(:name => 'marcelo', :address => 'address1')
144
+ struct_instance = @struct.new(:new_email, @user.to_yaml, 'foo@gmail.com', 4, 0.005)
145
+ @job = Job.create(:name => 'job1', :data => struct_instance)
146
+ @yaml_conditions = {}
147
+ @conditions = {}
148
+ end
149
+
150
+ subject { Job.find(:first, :yaml_conditions => @yaml_conditions, :conditions => @conditions) }
151
+
152
+ context 'filter for the proper class but wrong float' do
153
+ before do
154
+ @yaml_conditions = { :data => { :class => 'MyStruct', :some_rate => 0.003 } }
155
+ end
156
+
157
+ it { should be_nil }
158
+ end
159
+
160
+ context 'filter for the proper class and float number' do
161
+ before do
162
+ @yaml_conditions = { :data => { :class => 'MyStruct', :some_rate => 0.005 } }
163
+ end
164
+
165
+ it { should == @job }
166
+ end
167
+
168
+ context 'filter for the class of the serialized field' do
169
+ before do
170
+ @yaml_conditions = { :data => { :class => 'Struct::MyStruct' } }
171
+ end
172
+
173
+ it { should == @job }
174
+ end
175
+
176
+ context 'filter for the class of the serialized field' do
177
+ before do
178
+ @yaml_conditions = { :data => { :class => 'MyStruct' } }
179
+ end
180
+
181
+ it { should == @job }
182
+ end
183
+
184
+ context 'filter for string attribute stored on the serialized field' do
185
+ before do
186
+ @yaml_conditions = { :data => { :class => 'Struct', :email => 'foo@gmail.com' } }
187
+ end
188
+
189
+ it { should == @job }
190
+ end
191
+
192
+ context 'filter by the proper symbol, an integer and a float number' do
193
+ before do
194
+ @yaml_conditions = { :data => { :class => 'Struct', :method => :new_email, :number => 4, :some_rate => 0.005 } }
195
+ end
196
+
197
+ it { should == @job }
198
+ end
199
+
200
+ context 'filter by the correct symbol and integer, but a wrong float number' do
201
+ before do
202
+ @yaml_conditions = { :data => { :class => 'Struct', :method => :new_email, :number => 4, :some_rate => 0.002 } }
203
+ end
204
+
205
+ it { should be_nil }
206
+ end
207
+
208
+ context 'filter by the proper symbol and number' do
209
+ before do
210
+ @yaml_conditions = { :data => { :class => 'Struct', :method => :new_email, :number => 4 } }
211
+ end
212
+
213
+ it { should == @job }
214
+ end
215
+
216
+ context 'filtering with the correct symbol and a wrong number' do
217
+ before do
218
+ @yaml_conditions = { :data => { :class => 'Struct', :method => :new_email, :number => 3 } }
219
+ end
220
+
221
+ it { should be_nil }
222
+ end
223
+
224
+ context 'filter for symbol attribute stored on the serialized field' do
225
+ before do
226
+ @yaml_conditions = { :data => { :class => 'Struct', :method => :new_email } }
227
+ end
228
+
229
+ it { should == @job }
230
+ end
231
+
232
+ context "yaml_conditions have the right hierarchy" do
233
+ before do
234
+ @yaml_conditions = { :data => { :class => 'Struct', :handler => { :class => 'User', :name => 'marcelo', :address => 'address1' } } }
235
+ end
236
+
237
+ it { should == @job }
238
+
239
+ context 'used with standard conditions (that should match the existing Job)' do
240
+ before do
241
+ @conditions = { :name => 'job1' }
242
+ end
243
+
244
+ it { should == @job }
245
+ end
246
+
247
+ context 'used with standard conditions (that should NOT match the existing Job)' do
248
+ before do
249
+ @conditions = { :name => 'unexisting' }
250
+ end
251
+
252
+ it { should be_nil }
253
+ end
254
+ end
255
+
256
+ context "yaml_conditions that match key/value but WRONG hierarchy" do
257
+ before do
258
+ @yaml_conditions = { :data => { :class => 'Struct', :struct => { :handler => { :another_nested_object => { :class => 'User', :name => 'marcelo' } } } } }
259
+ end
260
+
261
+ it { should be_nil }
262
+ end
263
+
264
+ context "correct yaml_conditions but WRONG class name" do
265
+ before do
266
+ @yaml_conditions = { :data => { :class => 'WrongModel', :struct => { :handler => { :class => 'User', :name => 'marcelo' } } } }
267
+ end
268
+
269
+ it { should be_nil }
270
+ end
271
+
272
+ context ":yaml_conditions == { :data => { :class => 'Struct', :struct => { :handler => { :class => 'User', :name => 'bad-name' } } } }" do
273
+ before do
274
+ @yaml_conditions = { :data => { :class => 'Struct', :struct => { :handler => { :class => 'User', :name => 'bad-name' } } } }
275
+ end
276
+
277
+ it { should be_nil }
278
+ end
279
+
280
+ context 'query a nested ActiveRecord value' do
281
+ before do
282
+ period = Period.create(:year => 2000)
283
+ user_data = UserData.create(:social_number => 123, :title => 'Computer Engineer', :period => period)
284
+ user = User.create(:name => 'marcelo', :details => user_data)
285
+ @complex_job = Job.create(:name => 'job1', :data => { :owner => user })
286
+ end
287
+
288
+ context 'query using correct social_number' do
289
+ before do
290
+ @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marcelo', :details => { :social_number => 123 } } } }
291
+ end
292
+
293
+ it { should == @complex_job }
294
+ end
295
+
296
+ context 'query using correct social_number' do
297
+ before do
298
+ @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marcelo', :details => { :social_number => 124 } } } }
299
+ end
300
+
301
+ it { should be_nil }
302
+ end
303
+
304
+ context 'query using the UserData class' do
305
+ before do
306
+ @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marcelo', :details => { :class => UserData } } } }
307
+ end
308
+
309
+ it { should == @complex_job }
310
+ end
311
+
312
+ context 'query using the UserData class and title and social_number' do
313
+ before do
314
+ @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marcelo', :details => { :class => UserData, :title => 'Computer Engineer' } } } }
315
+ end
316
+
317
+ it { should == @complex_job }
318
+ end
319
+
320
+ context 'query using the UserData class, social_number and wrong title' do
321
+ before do
322
+ @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marcelo', :details => { :class => UserData, :title => 'Teacher' } } } }
323
+ end
324
+
325
+ it { should be_nil }
326
+ end
327
+
328
+ context 'query using the UserData class and title and social_number' do
329
+ before do
330
+ @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marcelo', :details => { :class => UserData, :title => 'Computer Engineer', :period => { :class => Period, :year => 2000 } } } } }
331
+ end
332
+
333
+ it { should == @complex_job }
334
+ end
335
+
336
+ context 'query using NotSerializedField' do
337
+ before do
338
+ @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marcelo', :details => { :class => UserData, :title => 'Computer Engineer', :period => { :class => Period, :year => Yaml::NotSerializedField.new(2000) } } } } }
339
+ end
340
+
341
+ it { should == @complex_job }
342
+ end
343
+ end
344
+ end
345
+ end
346
+ end