mongoid_geospatial 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. data/Gemfile +5 -4
  2. data/Guardfile +24 -0
  3. data/README.md +9 -3
  4. data/lib/mongoid_geospatial/{contexts → contextual}/mongo.rb +11 -6
  5. data/lib/mongoid_geospatial/criterion/complex.rb +7 -0
  6. data/lib/mongoid_geospatial/criterion/inclusion.rb +12 -12
  7. data/lib/mongoid_geospatial/extensions/rgeo_spherical_point_impl.rb +13 -0
  8. data/lib/mongoid_geospatial/fields/line_string.rb +7 -6
  9. data/lib/mongoid_geospatial/fields/point.rb +24 -11
  10. data/lib/mongoid_geospatial/fields/polygon.rb +14 -10
  11. data/lib/mongoid_geospatial/geospatial/geo_near_results.rb +1 -1
  12. data/lib/mongoid_geospatial/geospatial.rb +1 -29
  13. data/lib/mongoid_geospatial/version.rb +1 -1
  14. data/lib/mongoid_geospatial.rb +9 -5
  15. data/mongoid_geospatial.gemspec +4 -5
  16. data/spec/models/address.rb +19 -14
  17. data/spec/models/bar.rb +3 -1
  18. data/spec/models/event.rb +2 -2
  19. data/spec/models/farm.rb +3 -4
  20. data/spec/models/person.rb +18 -60
  21. data/spec/models/phone.rb +3 -1
  22. data/spec/models/river.rb +2 -2
  23. data/spec/{functional/contexts → mongoid_geospatial/contextual}/mongo_spec.rb +16 -8
  24. data/spec/mongoid_geospatial/criterion/inclusion_spec.rb +375 -0
  25. data/spec/mongoid_geospatial/fields/point_spec.rb +34 -0
  26. data/spec/mongoid_geospatial/fields/polygon_spec.rb +6 -0
  27. data/spec/{functional → mongoid_geospatial}/geospatial/geo_near_results_spec.rb +0 -0
  28. data/spec/mongoid_geospatial/geospatial_spec.rb +2 -0
  29. data/spec/mongoid_geospatial/mongoid_geospatial_spec.rb +83 -0
  30. data/spec/spec_helper.rb +36 -20
  31. data/spec/support/authentication.rb +23 -23
  32. metadata +23 -161
  33. data/spec/functional/criterion/inclusion_spec.rb +0 -360
  34. data/spec/functional/mongoid_geospatial_spec.rb +0 -54
  35. data/spec/models/account.rb +0 -19
  36. data/spec/models/acolyte.rb +0 -9
  37. data/spec/models/address_component.rb +0 -5
  38. data/spec/models/agent.rb +0 -10
  39. data/spec/models/alert.rb +0 -5
  40. data/spec/models/animal.rb +0 -21
  41. data/spec/models/answer.rb +0 -4
  42. data/spec/models/birthday.rb +0 -13
  43. data/spec/models/book.rb +0 -5
  44. data/spec/models/business.rb +0 -7
  45. data/spec/models/callbacks.rb +0 -57
  46. data/spec/models/category.rb +0 -13
  47. data/spec/models/circus.rb +0 -7
  48. data/spec/models/comment.rb +0 -13
  49. data/spec/models/country_code.rb +0 -6
  50. data/spec/models/description.rb +0 -11
  51. data/spec/models/division.rb +0 -5
  52. data/spec/models/drug.rb +0 -5
  53. data/spec/models/employer.rb +0 -5
  54. data/spec/models/entry.rb +0 -6
  55. data/spec/models/favorite.rb +0 -6
  56. data/spec/models/fruits.rb +0 -11
  57. data/spec/models/game.rb +0 -18
  58. data/spec/models/ghost.rb +0 -7
  59. data/spec/models/house.rb +0 -4
  60. data/spec/models/inheritance.rb +0 -90
  61. data/spec/models/league.rb +0 -5
  62. data/spec/models/location.rb +0 -5
  63. data/spec/models/login.rb +0 -6
  64. data/spec/models/membership.rb +0 -4
  65. data/spec/models/mixed_drink.rb +0 -4
  66. data/spec/models/name.rb +0 -13
  67. data/spec/models/namespacing.rb +0 -11
  68. data/spec/models/observed.rb +0 -41
  69. data/spec/models/override.rb +0 -16
  70. data/spec/models/owner.rb +0 -6
  71. data/spec/models/page.rb +0 -5
  72. data/spec/models/page_question.rb +0 -4
  73. data/spec/models/paranoid_post.rb +0 -18
  74. data/spec/models/parents.rb +0 -32
  75. data/spec/models/patient.rb +0 -15
  76. data/spec/models/pet.rb +0 -7
  77. data/spec/models/pet_owner.rb +0 -6
  78. data/spec/models/player.rb +0 -23
  79. data/spec/models/post.rb +0 -26
  80. data/spec/models/preference.rb +0 -9
  81. data/spec/models/question.rb +0 -8
  82. data/spec/models/quiz.rb +0 -6
  83. data/spec/models/rating.rb +0 -8
  84. data/spec/models/role.rb +0 -5
  85. data/spec/models/service.rb +0 -6
  86. data/spec/models/shelf.rb +0 -5
  87. data/spec/models/slave_address_numbers.rb +0 -14
  88. data/spec/models/survey.rb +0 -5
  89. data/spec/models/tag.rb +0 -6
  90. data/spec/models/tracking_id_validation_history.rb +0 -25
  91. data/spec/models/translation.rb +0 -5
  92. data/spec/models/tree.rb +0 -9
  93. data/spec/models/user.rb +0 -9
  94. data/spec/models/user_account.rb +0 -10
  95. data/spec/models/vet_visit.rb +0 -5
  96. data/spec/models/video.rb +0 -9
  97. data/spec/models/wiki_page.rb +0 -6
@@ -1,6 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Mongoid::Contexts::Mongo do
3
+ describe (Mongoid::VERSION > '3' ? Mongoid::Contextual::Mongo : Mongoid::Contexts::Mongo) do
4
4
  describe "#geo_near" do
5
5
 
6
6
  before do
@@ -77,9 +77,7 @@ describe Mongoid::Contexts::Mongo do
77
77
  it 'should limit 1' do
78
78
  Bar.limit(1).geo_near(jfk.location).size.should == 1
79
79
  end
80
-
81
80
  end
82
-
83
81
  end
84
82
 
85
83
  context ':page' do
@@ -110,17 +108,27 @@ describe Mongoid::Contexts::Mongo do
110
108
 
111
109
  context ":paginator :kaminari" do
112
110
  let(:near) {Bar.geo_near([1,1], :page => 1)}
113
- it "should have current_page" do
114
- near.current_page.should == 1
115
- end
116
111
 
117
- it "should have num_pages" do
118
- near.num_pages.should == 2
112
+ it 'should have 50 Bars' do
113
+ Bar.all.count.should == 50
119
114
  end
120
115
 
121
116
  it "should have limit_value" do
122
117
  near.limit_value.should == 25
123
118
  end
119
+
120
+ # check results['results'] in GeoNearResults
121
+ it 'should find 25 items' do
122
+ near.size.should == 25
123
+ end
124
+
125
+ it "should have current_page" do
126
+ near.current_page.should == 1
127
+ end
128
+
129
+ it "should have num_pages" do
130
+ near.num_pages.should == 1
131
+ end
124
132
  end
125
133
  end
126
134
 
@@ -0,0 +1,375 @@
1
+ require "spec_helper"
2
+
3
+ # describe "Mongoid::Criterion::Inclusion" do
4
+ describe Mongoid::Criteria do
5
+
6
+ before do
7
+ Person.delete_all
8
+ end
9
+
10
+ describe "#where" do
11
+
12
+ let(:dob) do
13
+ 33.years.ago.to_date
14
+ end
15
+
16
+ let(:lunch_time) do
17
+ 30.minutes.ago
18
+ end
19
+
20
+ let!(:person) do
21
+ Person.create(
22
+ :title => "Sir",
23
+ :dob => dob,
24
+ :lunch_time => lunch_time,
25
+ :age => 33,
26
+ :aliases => [ "D", "Durran" ],
27
+ :things => [ { :phone => 'HTC Incredible' } ]
28
+ )
29
+ end
30
+
31
+ context "when providing 24 character strings" do
32
+
33
+ context "when the field is not an id field" do
34
+
35
+ let(:string) do
36
+ bson_object_id_class.new.to_s
37
+ end
38
+
39
+ let!(:person) do
40
+ Person.create(:title => string)
41
+ end
42
+
43
+ let(:from_db) do
44
+ Person.where(:title => string)
45
+ end
46
+
47
+ it "does not convert the field to a bson id" do
48
+ from_db.should == [ person ]
49
+ end
50
+ end
51
+ end
52
+
53
+ context "when providing string object ids" do
54
+
55
+ context "when providing a single id" do
56
+
57
+ let(:from_db) do
58
+ Person.where(:_id => person.id.to_s).first
59
+ end
60
+
61
+ it "returns the matching documents" do
62
+ from_db.should == person
63
+ end
64
+ end
65
+ end
66
+
67
+ context "chaining multiple wheres" do
68
+
69
+ context "when chaining on the same key" do
70
+
71
+ let(:from_db) do
72
+ Person.where(:title => "Maam").where(:title => "Sir")
73
+ end
74
+
75
+ it "overrides the previous key" do
76
+ from_db.should == [ person ]
77
+ end
78
+ end
79
+
80
+ context "with different criteria on the same key" do
81
+
82
+ it "merges criteria" do
83
+ Person.where(:age.gt => 30).where(:age.lt => 40).should == [person]
84
+ end
85
+
86
+ it "typecasts criteria" do
87
+ before_dob = (dob - 1.month).to_s
88
+ after_dob = (dob + 1.month).to_s
89
+ Person.where(:dob.gt => before_dob).and(:dob.lt => after_dob).should == [person]
90
+ end
91
+
92
+ end
93
+ end
94
+
95
+ context "with untyped criteria" do
96
+
97
+ it "typecasts integers" do
98
+ pending "seems typecasts not work fo mongoid 3.0" if Mongoid::VERSION > '3'
99
+ Person.where(:age => "33").should == [ person ]
100
+ end
101
+
102
+ it "typecasts datetimes" do
103
+ pending "seems typecasts not work fo mongoid 3.0" if Mongoid::VERSION > '3'
104
+ Person.where(:lunch_time => lunch_time.to_s).should == [ person ]
105
+ end
106
+
107
+ it "typecasts dates" do
108
+ pending "seems typecasts not work fo mongoid 3.0" if Mongoid::VERSION > '3'
109
+ Person.where({:dob => dob.to_s}).should == [ person ]
110
+ end
111
+
112
+ it "typecasts times with zones" do
113
+ pending "seems typecasts not work fo mongoid 3.0" if Mongoid::VERSION > '3'
114
+ time = lunch_time.in_time_zone("Alaska")
115
+ Person.where(:lunch_time => time).should == [ person ]
116
+ end
117
+
118
+ it "typecasts array elements" do
119
+ pending "seems typecasts not work fo mongoid 3.0" if Mongoid::VERSION > '3'
120
+ Person.where(:age.in => [17, "33"]).should == [ person ]
121
+ end
122
+
123
+ it "typecasts size criterion to integer" do
124
+ pending "seems typecasts not work fo mongoid 3.0" if Mongoid::VERSION > '3'
125
+ Person.where(:aliases.size => "2").should == [ person ]
126
+ end
127
+
128
+ it "typecasts exists criterion to boolean" do
129
+ pending "seems typecasts not work fo mongoid 3.0" if Mongoid::VERSION > '3'
130
+ Person.where(:score.exists => "f").should == [ person ]
131
+ end
132
+ end
133
+
134
+ context "with multiple complex criteria" do
135
+
136
+ before do
137
+ Person.create(:title => "Mrs", :age => 29)
138
+ Person.create(:title => "Ms", :age => 41)
139
+ end
140
+
141
+ it "returns those matching both criteria" do
142
+ Person.where(:age.gt => 30, :age.lt => 40).should == [person]
143
+ end
144
+
145
+ it "returns nothing if in and nin clauses cancel each other out" do
146
+ Person.any_in(:title => ["Sir"]).not_in(:title => ["Sir"]).should == []
147
+ end
148
+
149
+ it "returns nothing if in and nin clauses cancel each other out ordered the other way" do
150
+ Person.not_in(:title => ["Sir"]).any_in(:title => ["Sir"]).should == []
151
+ end
152
+
153
+ it "returns the intersection of in and nin clauses" do
154
+ Person.any_in(:title => ["Sir", "Mrs"]).not_in(:title => ["Mrs"]).should == [person]
155
+ end
156
+
157
+ it "returns the intersection of two in clauses" do
158
+ pending 'seems not work fo mongoid 3.0 (selector: {"title"=>{"$in"=>["Sir", "Ms"]})' if Mongoid::VERSION > '3'
159
+ Person.where(:title.in => ["Sir", "Mrs"]).where(:title.in => ["Sir", "Ms"]).should == [person]
160
+ end
161
+ end
162
+
163
+ context "with complex criterion" do
164
+
165
+ context "#all" do
166
+
167
+ it "returns those matching an all clause" do
168
+ Person.where(:aliases.all => ["D", "Durran"]).should == [person]
169
+ end
170
+ end
171
+
172
+ context "#exists" do
173
+
174
+ it "returns those matching an exists clause" do
175
+ Person.where(:title.exists => true).should == [person]
176
+ end
177
+ end
178
+
179
+ context "#gt" do
180
+
181
+ it "returns those matching a gt clause" do
182
+ Person.where(:age.gt => 30).should == [person]
183
+ end
184
+ end
185
+
186
+ context "#gte" do
187
+
188
+ it "returns those matching a gte clause" do
189
+ Person.where(:age.gte => 33).should == [person]
190
+ end
191
+ end
192
+
193
+ context "#in" do
194
+
195
+ it "returns those matching an in clause" do
196
+ Person.where(:title.in => ["Sir", "Madam"]).should == [person]
197
+ end
198
+
199
+ it "allows nil" do
200
+ Person.where(:ssn.in => [nil]).should == [person]
201
+ end
202
+ end
203
+
204
+ context "#lt" do
205
+
206
+ it "returns those matching a lt clause" do
207
+ Person.where(:age.lt => 34).should == [person]
208
+ end
209
+ end
210
+
211
+ context "#lte" do
212
+
213
+ it "returns those matching a lte clause" do
214
+ Person.where(:age.lte => 33).should == [person]
215
+ end
216
+ end
217
+
218
+ context "#ne" do
219
+
220
+ it "returns those matching a ne clause" do
221
+ Person.where(:age.ne => 50).should == [person]
222
+ end
223
+ end
224
+
225
+ context "#nin" do
226
+
227
+ it "returns those matching a nin clause" do
228
+ Person.where(:title.nin => ["Esquire", "Congressman"]).should == [person]
229
+ end
230
+ end
231
+
232
+ context "#size" do
233
+
234
+ it "returns those matching a size clause" do
235
+ query_key = Mongoid::VERSION > '3' ? :aliases.with_size : :aliases.size
236
+ Person.where( query_key => 2).should == [person]
237
+ end
238
+ end
239
+
240
+ context "#match" do
241
+
242
+ it "returns those matching a partial element in a list" do
243
+ if Mongoid::VERSION > '3'
244
+ Person.where({'things.phone' => "HTC Incredible" }).should == [person]
245
+ else
246
+ Person.where(:things.matches => { :phone => "HTC Incredible" }).should == [person]
247
+ end
248
+ end
249
+ end
250
+
251
+ end
252
+
253
+ context "Geo Spatial Complex Where" do
254
+
255
+ let!(:home) do
256
+ [-73.98,40.77]
257
+ end
258
+
259
+ describe "#near" do
260
+ before do
261
+ Bar.delete_all
262
+ Bar.create_indexes
263
+ end
264
+
265
+ let!(:berlin) do
266
+ Bar.create(:name => :berlin, :location => [ 52.30, 13.25 ])
267
+ end
268
+
269
+ let!(:prague) do
270
+ Bar.create(:name => :prague, :location => [ 50.5, 14.26 ])
271
+ end
272
+
273
+ let!(:paris) do
274
+ Bar.create(:name => :paris, :location => [ 48.48, 2.20 ])
275
+ end
276
+
277
+ it "returns the documents sorted closest to furthest" do
278
+ Bar.where(:location.near => [ 41.23, 2.9 ]).should == [ paris, prague, berlin ]
279
+ end
280
+
281
+ it "returns the documents sorted closest to furthest" do
282
+ pending "NearSpatial#to_mongo_query seems not work for mongoid 3 " if Mongoid::VERSION > '3'
283
+ Bar.where(:location.near => {:point=>[ 41.23, 2.9 ],:max => 20}).should == [ paris, prague, berlin ]
284
+ end
285
+
286
+ it "returns the documents sorted closest to furthest" do
287
+ Bar.where(:location.near_sphere => [ 41.23, 2.9 ]).should == [ paris, prague, berlin ]
288
+ end
289
+
290
+ it "should find closest using rgeo point" do
291
+ Bar.where(:location.near => paris.location).to_a.should == [paris, berlin, prague]
292
+ end
293
+
294
+ end
295
+
296
+ context "#within" do
297
+
298
+ context ":box, :polygon" do
299
+ before do
300
+ Bar.delete_all
301
+ Bar.create_indexes
302
+ end
303
+
304
+ let!(:berlin) do
305
+ Bar.create(:name => 'berlin', :location => [ 52.30, 13.25 ])
306
+ end
307
+
308
+ let!(:prague) do
309
+ Bar.create(:name => 'prague',:location => [ 50.5, 14.26 ])
310
+ end
311
+
312
+ let!(:paris) do
313
+ Bar.create(:name => 'prague',:location => [ 48.48, 2.20 ])
314
+ end
315
+
316
+ it "returns the documents within a box" do
317
+ Bar.where(:location.within(:box) => [[ 47, 1 ],[ 49, 3 ]]).should == [ paris ]
318
+ end
319
+
320
+ it "returns the documents within a polygon" do
321
+ Bar.where(:location.within(:polygon) => [[ 47, 1 ],[49,1.5],[ 49, 3 ],[46,5]]).should == [ paris ]
322
+ end
323
+
324
+ it "returns the documents within a center" do
325
+ Bar.where(:location.within(:center) => [[ 47, 1 ],4]).should == [ paris ]
326
+ end
327
+
328
+ it "returns the documents within a center_sphere" do
329
+ Bar.where(:location.within(:center_sphere) => [[ 48, 2 ],0.1]).should == [ paris ]
330
+ end
331
+
332
+ end
333
+ context ":circle :center_sphere" do
334
+ before do
335
+ Bar.delete_all
336
+ Bar.create_indexes
337
+ end
338
+ let!(:mile1) do
339
+ Bar.create(:name => 'mile1', :location => [-73.997345, 40.759382])
340
+ end
341
+
342
+ let!(:mile3) do
343
+ Bar.create(:name => 'mile2', :location => [-73.927088, 40.752151])
344
+ end
345
+
346
+ let!(:mile7) do
347
+ Bar.create(:name => 'mile3', :location => [-74.0954913, 40.7161472])
348
+ end
349
+
350
+ let!(:mile11) do
351
+ Bar.create(:name => 'mile4', :location => [-74.0604951, 40.9178011])
352
+ end
353
+
354
+ it "returns the documents within a center_sphere" do
355
+ Bar.where(:location.within(:center_sphere) => {:point => home,:max => 2, :unit => :mi}).should == [ mile1 ]
356
+ end
357
+
358
+ it "returns the documents within a center_sphere" do
359
+ Bar.where(:location.within(:center_sphere) => {:point => home,:max => 4, :unit => :mi}).should include(mile3)
360
+ end
361
+
362
+ it "returns the documents within a center_sphere" do
363
+ Bar.where(:location.within(:center_sphere) => {:point => home,:max => 8, :unit => :mi}).should include(mile7)
364
+ end
365
+
366
+ it "returns the documents within a center_sphere" do
367
+ Bar.where(:location.within(:center_sphere) => {:point => home,:max => 12, :unit => :mi}).should include(mile11)
368
+ end
369
+ end
370
+ end
371
+ end
372
+
373
+ end
374
+
375
+ end
@@ -0,0 +1,34 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Geospatial::Point do
4
+
5
+ describe "in use" do
6
+
7
+ it "should mongoize array" do
8
+ bar = Bar.new(location: [10, -9])
9
+ bar.location.class.should eql(RGeo::Geographic::SphericalPointImpl)
10
+ bar.location.x.should be_within(0.1).of(10)
11
+ bar.location.y.should be_within(0.1).of(-9)
12
+ end
13
+
14
+ it "should mongoize hash" do
15
+ bar = Bar.new(location: {x: 10, y: -9})
16
+ bar.location.class.should eql(RGeo::Geographic::SphericalPointImpl)
17
+ bar.location.x.should be_within(0.1).of(10)
18
+ bar.location.y.should be_within(0.1).of(-9)
19
+ end
20
+
21
+ describe "instantiated" do
22
+
23
+ let(:bar) { Bar.create!(name: 'Vitinho', location: [10,10]) }
24
+
25
+ it "should demongoize to rgeo" do
26
+ bar.location.class.should eql(RGeo::Geographic::SphericalPointImpl)
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+
33
+
34
+ end
@@ -0,0 +1,6 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Geospatial::Polygon do
4
+
5
+
6
+ end
@@ -18,6 +18,8 @@ describe Mongoid::Geospatial do
18
18
 
19
19
 
20
20
  context "Included" do
21
+
22
+ # HO
21
23
  end
22
24
 
23
25
 
@@ -0,0 +1,83 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Geospatial do
4
+
5
+ it "should not inferfer with mongoid" do
6
+ Bar.create!(name: "Moe's")
7
+ Bar.count.should eql(1)
8
+ end
9
+
10
+ describe "fields" do
11
+
12
+ it "should have a field mapped as point" do
13
+ bar = Bar.create!(location: [5,5])
14
+ bar.location.should be_a RGeo::Geographic::SphericalPointImpl
15
+ end
16
+
17
+ it "should support a field mapped as linestring" do
18
+ river = River.create!(source: [[5,5],[6,5],[6,6],[5,6]])
19
+ river.source.should be_a RGeo::Geographic::SphericalPolygonImpl
20
+ end
21
+
22
+ it "should support a field mapped as polygon" do
23
+ farm = Farm.create!(area: [[5,5],[6,5],[6,6],[5,6]])
24
+ farm.area.should be_a RGeo::Geographic::SphericalPolygonImpl
25
+ end
26
+
27
+
28
+ it "should accept an RGeo object" do
29
+ point = RGeo::Geographic.spherical_factory.point 1, 2
30
+ bar = Bar.create!(location: point)
31
+ bar.location.x.should be_within(0.1).of(1)
32
+ bar.location.y.should be_within(0.1).of(2)
33
+ end
34
+ end
35
+
36
+ describe "methods" do
37
+
38
+ it "should have a .to_a" do
39
+ bar = Bar.create!(location: [3,2])
40
+ bar.location.to_a[0..1].should == [3.0, 2.0]
41
+ end
42
+
43
+ it "should have an array [] accessor" do
44
+ bar = Bar.create!(location: [3,2])
45
+ bar.location[0].should == 3.0
46
+ end
47
+
48
+ it "should have an ActiveModel symbol accessor" do
49
+ bar = Bar.create!(location: [3,2])
50
+ bar[:location].should == [3,2]
51
+ end
52
+
53
+ end
54
+
55
+
56
+ it "should calculate distance between points" do
57
+ bar = Bar.create!(location: [5,5])
58
+ bar2 = Bar.create!(location: [15,15])
59
+ bar.location.distance(bar2.location).should be_within(1).of(1561283.8)
60
+ end
61
+
62
+ it "should calculate distance between points miles" do
63
+ pending
64
+ bar = Bar.create!(location: [5,5])
65
+ bar2 = Bar.create!(location: [15,15])
66
+ bar.location.distance(bar2.location).should be_within(1).of(1561283.8)
67
+ end
68
+
69
+ it "should calculate 3d distances by default" do
70
+ bar = Bar.create! location: [-73.77694444, 40.63861111 ]
71
+ bar2 = Bar.create! location: [-118.40, 33.94] #,:unit=>:mi, :spherical => true)
72
+ bar.location.distance(bar2.location).to_i.should be_within(1).of(2469)
73
+ end
74
+
75
+ it "should have a nice simple way to ovewrite geo factory" do
76
+ pending
77
+ bar = Bar.create!(location: [5,5])
78
+ bar2 = Bar.create!(location: [15,15])
79
+ bar.location.distance(bar2.location).should be_within(1).of(1561283.8)
80
+ end
81
+
82
+
83
+ end
data/spec/spec_helper.rb CHANGED
@@ -12,43 +12,59 @@ require "mocha"
12
12
  require "rspec"
13
13
  require "mongoid_geospatial"
14
14
 
15
+ # These environment variables can be set if wanting to test against a database
16
+ # that is not on the local machine.
17
+ ENV["MONGOID_SPEC_HOST"] ||= "localhost"
18
+ ENV["MONGOID_SPEC_PORT"] ||= "27018"
19
+
20
+ # These are used when creating any connection in the test suite.
21
+ HOST = ENV["MONGOID_SPEC_HOST"]
22
+ PORT = ENV["MONGOID_SPEC_PORT"].to_i
23
+
15
24
  LOGGER = Logger.new($stdout)
16
25
 
17
26
  if RUBY_VERSION >= '1.9.2'
18
27
  YAML::ENGINE.yamler = 'syck'
19
28
  end
20
29
 
30
+ puts "version: #{Mongoid::VERSION}"
31
+
32
+ require 'mongoid_setup'
33
+
21
34
  Mongoid.configure do |config|
22
- opts = YAML.load(File.read(File.dirname(__FILE__) + '/support/mongoid.yml'))["test"]
23
- name = opts.delete("database")
24
- host = opts.delete("host")
25
- port = opts.delete("port")
26
- config.master = Mongo::Connection.new(host, port, opts).db(name)
27
- config.logger = nil
28
- config.allow_dynamic_fields = true
35
+ Mongoid::VersionSetup.configure config
36
+ end
37
+
38
+ # Autoload every model for the test suite that sits in spec/app/models.
39
+ Dir[ File.join(MODELS, "*.rb") ].sort.each do |file|
40
+ name = File.basename(file, ".rb")
41
+ autoload name.camelize.to_sym, name
29
42
  end
30
43
 
31
- Dir[ File.join(MODELS, "*.rb") ].sort.each { |file| require File.basename(file) }
32
44
  Dir[ File.join(SUPPORT, "*.rb") ].each { |file| require File.basename(file) }
33
45
 
46
+ def bson_object_id_class
47
+ Mongoid::VERSION > '3' ? Moped::BSON:: ObjectId : BSON::ObjectId
48
+ end
49
+
34
50
  RSpec.configure do |config|
35
51
  config.mock_with(:mocha)
36
52
 
37
- config.after(:suite) { Mongoid.purge! }
38
- config.after(:each) do
39
- Mongoid.database.collections.each do |collection|
40
- unless collection.name =~ /^system\./
41
- collection.remove
42
- end
43
- end
53
+ config.before(:each) do
54
+ Mongoid.purge!
55
+ # Mongoid.database.collections.each do |collection|
56
+ # unless collection.name =~ /^system\./
57
+ # collection.remove
58
+ # end
59
+ # end
44
60
  end
45
61
 
46
62
  # We filter out the specs that require authentication if the database has not
47
63
  # had the mongoid user set up properly.
48
- user_configured = Support::Authentication.configured?
49
- warn(Support::Authentication.message) unless user_configured
64
+ # user_configured = Support::Authentication.configured?
65
+ # warn(Support::Authentication.message) unless user_configured
50
66
 
51
- config.filter_run_excluding(:config => lambda { |value|
52
- return true if value == :user && !user_configured
53
- })
67
+ # config.filter_run_excluding(:config => lambda { |value|
68
+ # return true if value == :user && !user_configured
69
+ # })
54
70
  end
@@ -1,29 +1,29 @@
1
1
  # encoding: utf-8
2
2
  module Support #:nodoc:
3
- module Authentication
4
- extend self
3
+ # module Authentication
4
+ # extend self
5
5
 
6
- def configured?
7
- begin
8
- master_uri = "mongodb://mongoid:test@localhost:27017/mongoid_geospatial_test"
9
- Mongo::Connection.from_uri(master_uri)
10
- true
11
- rescue Mongo::AuthenticationError => e
12
- false
13
- end
14
- end
6
+ # def configured?
7
+ # begin
8
+ # master_uri = "mongodb://mongoid:test@localhost:27017/mongoid_geospatial_test"
9
+ # Mongo::Connection.from_uri(master_uri)
10
+ # true
11
+ # rescue Mongo::AuthenticationError => e
12
+ # false
13
+ # end
14
+ # end
15
15
 
16
- def message
17
- %Q{
18
- ---------------------------------------------------------------------
19
- A user needs to be configured for authentication, otherwise some
20
- configuration specs will not get run. You may set it up from the
21
- mongo console:
16
+ # def message
17
+ # %Q{
18
+ # ---------------------------------------------------------------------
19
+ # A user needs to be configured for authentication, otherwise some
20
+ # configuration specs will not get run. You may set it up from the
21
+ # mongo console:
22
22
 
23
- $ use mongoid_geospatial_test;
24
- $ db.addUser("mongoid", "test");
25
- ---------------------------------------------------------------------
26
- }
27
- end
28
- end
23
+ # $ use mongoid_geospatial_test;
24
+ # $ db.addUser("mongoid", "test");
25
+ # ---------------------------------------------------------------------
26
+ # }
27
+ # end
28
+ # end
29
29
  end