Commit 38bd3661afb91fd34bc89d35cfc2096773212474

Authored by will-r
1 parent 9bdb4158

tidy up Region test model, add specific test for lat: lon: formatted input

Showing 2 changed files with 42 additions and 20 deletions   Show diff stats
test/geo_shape_test.rb
@@ -4,9 +4,30 @@ class GeoShapeTest < Minitest::Test @@ -4,9 +4,30 @@ class GeoShapeTest < Minitest::Test
4 def setup 4 def setup
5 super 5 super
6 store [ 6 store [
7 - {name: "Region A", text: "The witch had a cat", territory: "30,40,35,45,40,40,40,30,30,30,30,40"},  
8 - {name: "Region B", text: "and a very tall hat", territory: "50,60,55,65,60,60,60,50,50,50,50,60"},  
9 - {name: "Region C", text: "and long ginger hair which she wore in a plait", territory: "10,20,15,25,20,20,20,10,10,10,10,20"} 7 + {
  8 + name: "Region A",
  9 + text: "The witch had a cat",
  10 + territory: {
  11 + type: "polygon",
  12 + coordinates: [[[30,40],[35,45],[40,40],[40,30],[30,30],[30,40]]]
  13 + }
  14 + },
  15 + {
  16 + name: "Region B",
  17 + text: "and a very tall hat",
  18 + territory: {
  19 + type: "polygon",
  20 + coordinates: [[[50,60],[55,65],[60,60],[60,50],[50,50],[50,60]]]
  21 + }
  22 + },
  23 + {
  24 + name: "Region C",
  25 + text: "and long ginger hair which she wore in a plait",
  26 + territory: {
  27 + type: "polygon",
  28 + coordinates: [[[10,20],[15,25],[20,20],[20,10],[10,10],[10,20]]]
  29 + }
  30 + }
10 ], Region 31 ], Region
11 end 32 end
12 33
@@ -87,7 +108,7 @@ class GeoShapeTest < Minitest::Test @@ -87,7 +108,7 @@ class GeoShapeTest < Minitest::Test
87 geo_shape: { 108 geo_shape: {
88 type: "envelope", 109 type: "envelope",
89 relation: "within", 110 relation: "within",
90 - coordinates: [[20, 50], [50, 20]] 111 + coordinates: [[20,50], [50,20]]
91 } 112 }
92 } 113 }
93 } 114 }
@@ -134,4 +155,18 @@ class GeoShapeTest < Minitest::Test @@ -134,4 +155,18 @@ class GeoShapeTest < Minitest::Test
134 } 155 }
135 }, Region 156 }, Region
136 end 157 end
  158 +
  159 + def test_latlon
  160 + assert_search "*", ["Region A"], {
  161 + where: {
  162 + territory: {
  163 + geo_shape: {
  164 + type: "envelope",
  165 + coordinates: [{lat: 42, lon: 28}, {lat: 38, lon: 32}]
  166 + }
  167 + }
  168 + }
  169 + }, Region
  170 + end
  171 +
137 end 172 end
test/test_helper.rb
@@ -102,7 +102,6 @@ if defined?(Mongoid) @@ -102,7 +102,6 @@ if defined?(Mongoid)
102 102
103 field :name 103 field :name
104 field :text 104 field :text
105 - field :territory  
106 end 105 end
107 106
108 class Speaker 107 class Speaker
@@ -161,7 +160,6 @@ elsif defined?(NoBrainer) @@ -161,7 +160,6 @@ elsif defined?(NoBrainer)
161 field :id, type: Object 160 field :id, type: Object
162 field :name, type: String 161 field :name, type: String
163 field :text, type: Text 162 field :text, type: Text
164 - field :territory, type: Text  
165 end 163 end
166 164
167 class Speaker 165 class Speaker
@@ -258,7 +256,6 @@ else @@ -258,7 +256,6 @@ else
258 ActiveRecord::Migration.create_table :regions do |t| 256 ActiveRecord::Migration.create_table :regions do |t|
259 t.string :name 257 t.string :name
260 t.text :text 258 t.text :text
261 - t.text :territory  
262 end 259 end
263 260
264 ActiveRecord::Migration.create_table :speakers do |t| 261 ActiveRecord::Migration.create_table :speakers do |t|
@@ -374,25 +371,15 @@ class Region @@ -374,25 +371,15 @@ class Region
374 territory: {tree: "quadtree", precision: "10km"} 371 territory: {tree: "quadtree", precision: "10km"}
375 } 372 }
376 373
  374 + attr_accessor :territory
  375 +
377 def search_data 376 def search_data
378 { 377 {
379 name: name, 378 name: name,
380 text: text, 379 text: text,
381 - territory: as_geo_json 380 + territory: territory
382 } 381 }
383 end 382 end
384 -  
385 - def as_geo_json  
386 - {  
387 - type: "polygon",  
388 - coordinates: [territory_path] # enclosing array because polygon can also have exclusion paths.  
389 - }  
390 - end  
391 -  
392 - def territory_path  
393 - path = territory.split(',').map(&:to_f).each_slice(2).to_a  
394 - path  
395 - end  
396 end 383 end
397 384
398 class Speaker 385 class Speaker