test_basic.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. from __future__ import unicode_literals
  2. import json
  3. import re
  4. from flask_admin.contrib.geoa import ModelView
  5. from flask_admin.contrib.geoa.fields import GeoJSONField
  6. from geoalchemy2 import Geometry
  7. from geoalchemy2.shape import to_shape
  8. from nose.tools import eq_, ok_
  9. from . import setup
  10. def create_models(db):
  11. class GeoModel(db.Model):
  12. id = db.Column(db.Integer, primary_key=True)
  13. name = db.Column(db.String(20))
  14. point = db.Column(Geometry("POINT"))
  15. line = db.Column(Geometry("LINESTRING"))
  16. polygon = db.Column(Geometry("POLYGON"))
  17. multi = db.Column(Geometry("MULTIPOINT"))
  18. def __unicode__(self):
  19. return self.name
  20. db.create_all()
  21. return GeoModel
  22. def test_model():
  23. app, db, admin = setup()
  24. GeoModel = create_models(db)
  25. db.create_all()
  26. GeoModel.query.delete()
  27. db.session.commit()
  28. view = ModelView(GeoModel, db.session)
  29. admin.add_view(view)
  30. eq_(view.model, GeoModel)
  31. eq_(view._primary_key, 'id')
  32. # Verify form
  33. eq_(view._create_form_class.point.field_class, GeoJSONField)
  34. eq_(view._create_form_class.point.kwargs['geometry_type'], "POINT")
  35. eq_(view._create_form_class.line.field_class, GeoJSONField)
  36. eq_(view._create_form_class.line.kwargs['geometry_type'], "LINESTRING")
  37. eq_(view._create_form_class.polygon.field_class, GeoJSONField)
  38. eq_(view._create_form_class.polygon.kwargs['geometry_type'], "POLYGON")
  39. eq_(view._create_form_class.multi.field_class, GeoJSONField)
  40. eq_(view._create_form_class.multi.kwargs['geometry_type'], "MULTIPOINT")
  41. # Make some test clients
  42. client = app.test_client()
  43. rv = client.get('/admin/geomodel/')
  44. eq_(rv.status_code, 200)
  45. rv = client.get('/admin/geomodel/new/')
  46. eq_(rv.status_code, 200)
  47. rv = client.post('/admin/geomodel/new/', data={
  48. "name": "test1",
  49. "point": '{"type": "Point", "coordinates": [125.8, 10.0]}',
  50. "line": '{"type": "LineString", "coordinates": [[50.2345, 94.2], [50.21, 94.87]]}',
  51. "polygon": ('{"type": "Polygon", "coordinates": [[[100.0, 0.0], [101.0, 0.0],'
  52. ' [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]]}'),
  53. "multi": '{"type": "MultiPoint", "coordinates": [[100.0, 0.0], [101.0, 1.0]]}',
  54. })
  55. eq_(rv.status_code, 302)
  56. model = db.session.query(GeoModel).first()
  57. eq_(model.name, "test1")
  58. eq_(to_shape(model.point).geom_type, "Point")
  59. eq_(list(to_shape(model.point).coords), [(125.8, 10.0)])
  60. eq_(to_shape(model.line).geom_type, "LineString")
  61. eq_(list(to_shape(model.line).coords), [(50.2345, 94.2), (50.21, 94.87)])
  62. eq_(to_shape(model.polygon).geom_type, "Polygon")
  63. eq_(list(to_shape(model.polygon).exterior.coords),
  64. [(100.0, 0.0), (101.0, 0.0), (101.0, 1.0), (100.0, 1.0), (100.0, 0.0)])
  65. eq_(to_shape(model.multi).geom_type, "MultiPoint")
  66. eq_(len(to_shape(model.multi).geoms), 2)
  67. eq_(list(to_shape(model.multi).geoms[0].coords), [(100.0, 0.0)])
  68. eq_(list(to_shape(model.multi).geoms[1].coords), [(101.0, 1.0)])
  69. rv = client.get('/admin/geomodel/')
  70. eq_(rv.status_code, 200)
  71. html = rv.data.decode('utf-8')
  72. pattern = r'(.|\n)+({.*"type": ?"Point".*})</textarea>(.|\n)+'
  73. group = re.match(pattern, html).group(2)
  74. p = json.loads(group)
  75. eq_(p['coordinates'][0], 125.8)
  76. eq_(p['coordinates'][1], 10.0)
  77. url = '/admin/geomodel/edit/?id=%s' % model.id
  78. rv = client.get(url)
  79. eq_(rv.status_code, 200)
  80. data = rv.data.decode('utf-8')
  81. ok_(r' name="multi">{"type":"MultiPoint","coordinates":[[100,0],[101,1]]}</textarea>' in data)
  82. # rv = client.post(url, data={
  83. # "name": "edited",
  84. # "point": '{"type": "Point", "coordinates": [99.9, 10.5]}',
  85. # "line": '', # set to NULL in the database
  86. # })
  87. # eq_(rv.status_code, 302)
  88. #
  89. # model = db.session.query(GeoModel).first()
  90. # eq_(model.name, "edited")
  91. # eq_(to_shape(model.point).geom_type, "Point")
  92. # eq_(list(to_shape(model.point).coords), [(99.9, 10.5)])
  93. # eq_(to_shape(model.line), None)
  94. # eq_(to_shape(model.polygon).geom_type, "Polygon")
  95. # eq_(list(to_shape(model.polygon).exterior.coords),
  96. # [(100.0, 0.0), (101.0, 0.0), (101.0, 1.0), (100.0, 1.0), (100.0, 0.0)])
  97. # eq_(to_shape(model.multi).geom_type, "MultiPoint")
  98. # eq_(len(to_shape(model.multi).geoms), 2)
  99. # eq_(list(to_shape(model.multi).geoms[0].coords), [(100.0, 0.0)])
  100. # eq_(list(to_shape(model.multi).geoms[1].coords), [(101.0, 1.0)])
  101. url = '/admin/geomodel/delete/?id=%s' % model.id
  102. rv = client.post(url)
  103. eq_(rv.status_code, 302)
  104. eq_(db.session.query(GeoModel).count(), 0)
  105. def test_none():
  106. app, db, admin = setup()
  107. GeoModel = create_models(db)
  108. db.create_all()
  109. GeoModel.query.delete()
  110. db.session.commit()
  111. view = ModelView(GeoModel, db.session)
  112. admin.add_view(view)
  113. # Make some test clients
  114. client = app.test_client()
  115. rv = client.post('/admin/geomodel/new/', data={
  116. "name": "test1",
  117. })
  118. eq_(rv.status_code, 302)
  119. model = db.session.query(GeoModel).first()
  120. url = '/admin/geomodel/edit/?id=%s' % model.id
  121. rv = client.get(url)
  122. eq_(rv.status_code, 200)
  123. data = rv.data.decode('utf-8')
  124. ok_(r' name="point"></textarea>' in data)