/vendor/plugins/spatial_adapter/spec/postgis/models_spec.rb

https://github.com/maning/mapwarper · Ruby · 257 lines · 211 code · 46 blank · 0 comment · 44 complexity · 579713728aaac5ed883b6661033122c4 MD5 · raw file

  1. require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
  2. require 'db/postgis_raw'
  3. require 'models/common'
  4. describe "Spatially-enabled Models" do
  5. before :each do
  6. postgis_connection
  7. @connection = ActiveRecord::Base.connection
  8. end
  9. describe "inserting records" do
  10. it 'should save Point objects' do
  11. model = PointModel.new(:extra => 'test', :geom => GeometryFactory.point)
  12. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.point.as_hex_ewkb))
  13. model.save.should == true
  14. end
  15. it 'should save LineString objects' do
  16. model = LineStringModel.new(:extra => 'test', :geom => GeometryFactory.line_string)
  17. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.line_string.as_hex_ewkb))
  18. model.save.should == true
  19. end
  20. it 'should save Polygon objects' do
  21. model = PolygonModel.new(:extra => 'test', :geom => GeometryFactory.polygon)
  22. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.polygon.as_hex_ewkb))
  23. model.save.should == true
  24. end
  25. it 'should save MultiPoint objects' do
  26. model = MultiPointModel.new(:extra => 'test', :geom => GeometryFactory.multi_point)
  27. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.multi_point.as_hex_ewkb))
  28. model.save.should == true
  29. end
  30. it 'should save MultiLineString objects' do
  31. model = MultiLineStringModel.new(:extra => 'test', :geom => GeometryFactory.multi_line_string)
  32. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.multi_line_string.as_hex_ewkb))
  33. model.save.should == true
  34. end
  35. it 'should save MultiPolygon objects' do
  36. model = MultiPolygonModel.new(:extra => 'test', :geom => GeometryFactory.multi_polygon)
  37. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.multi_polygon.as_hex_ewkb))
  38. model.save.should == true
  39. end
  40. it 'should save GeometryCollection objects' do
  41. model = GeometryCollectionModel.new(:extra => 'test', :geom => GeometryFactory.geometry_collection)
  42. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.geometry_collection.as_hex_ewkb))
  43. model.save.should == true
  44. end
  45. it 'should save Geometry objects' do
  46. model = GeometryModel.new(:extra => 'test', :geom => GeometryFactory.point)
  47. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.point.as_hex_ewkb))
  48. model.save.should == true
  49. end
  50. it 'should save 3D Point (with Z coord) objects' do
  51. model = PointzModel.new(:extra => 'test', :geom => GeometryFactory.pointz)
  52. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.pointz.as_hex_ewkb))
  53. model.save.should == true
  54. end
  55. it 'should save 3D Point (with M coord) objects' do
  56. model = PointmModel.new(:extra => 'test', :geom => GeometryFactory.pointm)
  57. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.pointm.as_hex_ewkb))
  58. model.save.should == true
  59. end
  60. it 'should save 4D Point objects' do
  61. model = Point4Model.new(:extra => 'test', :geom => GeometryFactory.point4)
  62. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.point4.as_hex_ewkb))
  63. model.save.should == true
  64. end
  65. it 'should save Point geography objects' do
  66. model = GeographyPointModel.new(:extra => 'test', :geom => GeometryFactory.point)
  67. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.point.as_hex_ewkb))
  68. model.save.should == true
  69. end
  70. it 'should save LineString geography objects' do
  71. model = GeographyLineStringModel.new(:extra => 'test', :geom => GeometryFactory.line_string)
  72. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.line_string.as_hex_ewkb))
  73. model.save.should == true
  74. end
  75. it 'should save Polygon geography objects' do
  76. model = GeographyPolygonModel.new(:extra => 'test', :geom => GeometryFactory.polygon)
  77. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.polygon.as_hex_ewkb))
  78. model.save.should == true
  79. end
  80. it 'should save MultiPoint geography objects' do
  81. model = GeographyMultiPointModel.new(:extra => 'test', :geom => GeometryFactory.multi_point)
  82. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.multi_point.as_hex_ewkb))
  83. model.save.should == true
  84. end
  85. it 'should save MultiLineString geography objects' do
  86. model = GeographyMultiLineStringModel.new(:extra => 'test', :geom => GeometryFactory.multi_line_string)
  87. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.multi_line_string.as_hex_ewkb))
  88. model.save.should == true
  89. end
  90. it 'should save MultiPolygon geography objects' do
  91. model = GeographyMultiPolygonModel.new(:extra => 'test', :geom => GeometryFactory.multi_polygon)
  92. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.multi_polygon.as_hex_ewkb))
  93. model.save.should == true
  94. end
  95. it 'should save GeometryCollection geography objects' do
  96. model = GeographyGeometryCollectionModel.new(:extra => 'test', :geom => GeometryFactory.geometry_collection)
  97. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.geometry_collection.as_hex_ewkb))
  98. model.save.should == true
  99. end
  100. it 'should save Geography objects' do
  101. model = GeographyModel.new(:extra => 'test', :geom => GeometryFactory.point)
  102. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.point.as_hex_ewkb))
  103. model.save.should == true
  104. end
  105. it 'should save 3D Point (with Z coord) geography objects' do
  106. model = GeographyPointzModel.new(:extra => 'test', :geom => GeometryFactory.pointz)
  107. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.pointz.as_hex_ewkb))
  108. model.save.should == true
  109. end
  110. it 'should save 3D Point (with M coord) geography objects' do
  111. model = GeographyPointmModel.new(:extra => 'test', :geom => GeometryFactory.pointm)
  112. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.pointm.as_hex_ewkb))
  113. model.save.should == true
  114. end
  115. it 'should save 4D Point geography objects' do
  116. model = GeographyPoint4Model.new(:extra => 'test', :geom => GeometryFactory.point4)
  117. @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.point4.as_hex_ewkb))
  118. model.save.should == true
  119. end
  120. end
  121. describe "finding records" do
  122. it 'should retrieve Point objects' do
  123. model = PointModel.create(:extra => 'test', :geom => GeometryFactory.point)
  124. PointModel.find(model.id).geom.should == GeometryFactory.point
  125. end
  126. it 'should retrieve LineString objects' do
  127. model = LineStringModel.create(:extra => 'test', :geom => GeometryFactory.line_string)
  128. LineStringModel.find(model.id).geom.should == GeometryFactory.line_string
  129. end
  130. it 'should retrieve Polygon objects' do
  131. model = PolygonModel.create(:extra => 'test', :geom => GeometryFactory.polygon)
  132. PolygonModel.find(model.id).geom.should == GeometryFactory.polygon
  133. end
  134. it 'should retrieve MultiPoint objects' do
  135. model = MultiPointModel.create(:extra => 'test', :geom => GeometryFactory.multi_point)
  136. MultiPointModel.find(model.id).geom.should == GeometryFactory.multi_point
  137. end
  138. it 'should retrieve MultiLineString objects' do
  139. model = MultiLineStringModel.create(:extra => 'test', :geom => GeometryFactory.multi_line_string)
  140. MultiLineStringModel.find(model.id).geom.should == GeometryFactory.multi_line_string
  141. end
  142. it 'should retrieve MultiPolygon objects' do
  143. model = MultiPolygonModel.create(:extra => 'test', :geom => GeometryFactory.multi_polygon)
  144. MultiPolygonModel.find(model.id).geom.should == GeometryFactory.multi_polygon
  145. end
  146. it 'should retrieve GeometryCollection objects' do
  147. model = GeometryCollectionModel.create(:extra => 'test', :geom => GeometryFactory.geometry_collection)
  148. GeometryCollectionModel.find(model.id).geom.should == GeometryFactory.geometry_collection
  149. end
  150. it 'should retrieve Geometry objects' do
  151. model = GeometryModel.create(:extra => 'test', :geom => GeometryFactory.point)
  152. GeometryModel.find(model.id).geom.should == GeometryFactory.point
  153. end
  154. it 'should retrieve 3D Point (with Z coord) objects' do
  155. model = PointzModel.create(:extra => 'test', :geom => GeometryFactory.pointz)
  156. PointzModel.find(model.id).geom.should == GeometryFactory.pointz
  157. end
  158. it 'should retrieve 3D Point (with M coord) objects' do
  159. model = GeographyPointmModel.create(:extra => 'test', :geom => GeometryFactory.pointm)
  160. GeographyPointmModel.find(model.id).geom.should == GeometryFactory.pointm
  161. end
  162. it 'should retrieve 4D Point objects' do
  163. model = GeographyPoint4Model.create(:extra => 'test', :geom => GeometryFactory.point4)
  164. GeographyPoint4Model.find(model.id).geom.should == GeometryFactory.point4
  165. end
  166. it 'should retrieve Point geography objects' do
  167. model = GeographyPointModel.create(:extra => 'test', :geom => GeometryFactory.point)
  168. GeographyPointModel.find(model.id).geom.should == GeometryFactory.point
  169. end
  170. it 'should retrieve LineString geography objects' do
  171. model = GeographyLineStringModel.create(:extra => 'test', :geom => GeometryFactory.line_string)
  172. GeographyLineStringModel.find(model.id).geom.should == GeometryFactory.line_string
  173. end
  174. it 'should retrieve Polygon geography objects' do
  175. model = GeographyPolygonModel.create(:extra => 'test', :geom => GeometryFactory.polygon)
  176. GeographyPolygonModel.find(model.id).geom.should == GeometryFactory.polygon
  177. end
  178. it 'should retrieve MultiPoint geography objects' do
  179. model = GeographyMultiPointModel.create(:extra => 'test', :geom => GeometryFactory.multi_point)
  180. GeographyMultiPointModel.find(model.id).geom.should == GeometryFactory.multi_point
  181. end
  182. it 'should retrieve MultiLineString geography objects' do
  183. model = GeographyMultiLineStringModel.create(:extra => 'test', :geom => GeometryFactory.multi_line_string)
  184. GeographyMultiLineStringModel.find(model.id).geom.should == GeometryFactory.multi_line_string
  185. end
  186. it 'should retrieve MultiPolygon geography objects' do
  187. model = GeographyMultiPolygonModel.create(:extra => 'test', :geom => GeometryFactory.multi_polygon)
  188. GeographyMultiPolygonModel.find(model.id).geom.should == GeometryFactory.multi_polygon
  189. end
  190. it 'should retrieve GeometryCollection geography objects' do
  191. model = GeographyGeometryCollectionModel.create(:extra => 'test', :geom => GeometryFactory.geometry_collection)
  192. GeographyGeometryCollectionModel.find(model.id).geom.should == GeometryFactory.geometry_collection
  193. end
  194. it 'should retrieve Geometry geography objects' do
  195. model = GeographyModel.create(:extra => 'test', :geom => GeometryFactory.point)
  196. GeographyModel.find(model.id).geom.should == GeometryFactory.point
  197. end
  198. it 'should retrieve 3D Point (with Z coord) geography objects' do
  199. model = GeographyPointzModel.create(:extra => 'test', :geom => GeometryFactory.pointz)
  200. GeographyPointzModel.find(model.id).geom.should == GeometryFactory.pointz
  201. end
  202. it 'should retrieve 3D Point (with M coord) geography objects' do
  203. model = GeographyPointmModel.create(:extra => 'test', :geom => GeometryFactory.pointm)
  204. GeographyPointmModel.find(model.id).geom.should == GeometryFactory.pointm
  205. end
  206. it 'should retrieve 4D Point geography objects' do
  207. model = GeographyPoint4Model.create(:extra => 'test', :geom => GeometryFactory.point4)
  208. GeographyPoint4Model.find(model.id).geom.should == GeometryFactory.point4
  209. end
  210. end
  211. end