Commit 9667f1a6 authored by David Baumgold's avatar David Baumgold

Added test for geoa

parent e74e5f8b
...@@ -5,11 +5,20 @@ python: ...@@ -5,11 +5,20 @@ python:
- "3.3" - "3.3"
env: env:
- REQUIREMENTS=travis_wtf1.txt - WTFORMS_VERSION=1
- REQUIREMENTS=travis_wtf2.txt - WTFORMS_VERSION=2
install: "pip install -r $REQUIREMENTS --use-mirrors" services:
- postgresql
- mongodb
before_script:
- psql -U postgres -c 'CREATE DATABASE flask_admin_test;'
- psql -U postgres -c "CREATE EXTENSION postgis;" flask_admin_test
install:
- pip install "wtforms<$WTFORMS_VERSION.99"
- pip install -r dev-requirements.txt
services: mongodb
script: nosetests flask_admin/tests script: nosetests flask_admin/tests
from flask import Flask
from flask.ext.admin import Admin
from flask.ext.sqlalchemy import SQLAlchemy
def setup():
app = Flask(__name__)
app.config['SECRET_KEY'] = '1'
app.config['CSRF_ENABLED'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://localhost/flask_admin_test'
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)
admin = Admin(app)
return app, db, admin
from __future__ import unicode_literals
from nose.tools import eq_, ok_
from flask.ext.admin.contrib.geoa import ModelView
from flask.ext.admin.contrib.geoa.sqltypes import Geometry
from flask.ext.admin.contrib.geoa.fields import GeoJSONField
from . import setup
def create_models(db):
class GeoModel(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(20))
point = db.Column(Geometry("POINT"))
line = db.Column(Geometry("LINESTRING"))
polygon = db.Column(Geometry("POLYGON"))
multi = db.Column(Geometry("MULTIPOINT"))
def __unicode__(self):
return self.name
db.create_all()
return GeoModel
def test_model():
app, db, admin = setup()
GeoModel = create_models(db)
db.create_all()
view = ModelView(GeoModel, db.session)
admin.add_view(view)
eq_(view.model, GeoModel)
eq_(view._primary_key, 'id')
# Verify form
eq_(view._create_form_class.point.field_class, GeoJSONField)
eq_(view._create_form_class.point.kwargs['geometry_type'], "POINT")
eq_(view._create_form_class.line.field_class, GeoJSONField)
eq_(view._create_form_class.line.kwargs['geometry_type'], "LINESTRING")
eq_(view._create_form_class.polygon.field_class, GeoJSONField)
eq_(view._create_form_class.polygon.kwargs['geometry_type'], "POLYGON")
eq_(view._create_form_class.multi.field_class, GeoJSONField)
eq_(view._create_form_class.multi.kwargs['geometry_type'], "MULTIPOINT")
# Make some test clients
client = app.test_client()
rv = client.get('/admin/geomodel/')
eq_(rv.status_code, 200)
rv = client.get('/admin/geomodel/new/')
eq_(rv.status_code, 200)
rv = client.post('/admin/geomodel/new/', data={
"name": "test1",
"point": '{"type": "Point", "coordinates": [125.8, 10.0]}',
"line": '{"type": "LineString", "coordinates": [[50.2345, 94.2], [50.21, 94.87]]}',
"polygon": '{"type": "Polygon", "coordinates": [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]]}',
"multi": '{"type": "MultiPoint", "coordinates": [[100.0, 0.0], [101.0, 1.0]]}',
})
eq_(rv.status_code, 302)
model = db.session.query(GeoModel).first()
eq_(model.name, "test1")
eq_(model.point.geom_type, "Point")
eq_(list(model.point.coords), [(125.8, 10.0)])
eq_(model.line.geom_type, "LineString")
eq_(list(model.line.coords), [(50.2345, 94.2), (50.21, 94.87)])
eq_(model.polygon.geom_type, "Polygon")
eq_(list(model.polygon.exterior.coords),
[(100.0, 0.0), (101.0, 0.0), (101.0, 1.0), (100.0, 1.0), (100.0, 0.0)])
eq_(model.multi.geom_type, "MultiPoint")
eq_(len(model.multi.geoms), 2)
eq_(list(model.multi.geoms[0].coords), [(100.0, 0.0)])
eq_(list(model.multi.geoms[1].coords), [(101.0, 1.0)])
rv = client.get('/admin/geomodel/')
eq_(rv.status_code, 200)
point_opt_1 = '>{"type": "Point", "coordinates": [125.8, 10.0]}</textarea>'
point_opt_2 = '>{"coordinates": [125.8, 10.0], "type": "Point"}</textarea>'
html = rv.data.decode('utf-8')
ok_(point_opt_1 in html or point_opt_2 in html, html)
url = '/admin/geomodel/edit/?id=%s' % model.id
rv = client.get(url)
eq_(rv.status_code, 200)
rv = client.post(url, data={
"name": "edited",
"point": '{"type": "Point", "coordinates": [99.9, 10.5]}',
"line": '', # set to NULL in the database
})
eq_(rv.status_code, 302)
model = db.session.query(GeoModel).first()
eq_(model.name, "edited")
eq_(model.point.geom_type, "Point")
eq_(list(model.point.coords), [(99.9, 10.5)])
eq_(model.line, None)
eq_(model.polygon.geom_type, "Polygon")
eq_(list(model.polygon.exterior.coords),
[(100.0, 0.0), (101.0, 0.0), (101.0, 1.0), (100.0, 1.0), (100.0, 0.0)])
eq_(model.multi.geom_type, "MultiPoint")
eq_(len(model.multi.geoms), 2)
eq_(list(model.multi.geoms[0].coords), [(100.0, 0.0)])
eq_(list(model.multi.geoms[1].coords), [(101.0, 1.0)])
url = '/admin/geomodel/delete/?id=%s' % model.id
rv = client.post(url)
eq_(rv.status_code, 302)
eq_(db.session.query(GeoModel).count(), 0)
...@@ -56,7 +56,10 @@ setup( ...@@ -56,7 +56,10 @@ setup(
'sqlalchemy', 'sqlalchemy',
'flask-mongoengine', 'flask-mongoengine',
'flask-sqlalchemy', 'flask-sqlalchemy',
'flask-babelex' 'flask-babelex',
'shapely',
'geoalchemy2',
'psycopg2',
], ],
classifiers=[ classifiers=[
'Development Status :: 4 - Beta', 'Development Status :: 4 - Beta',
......
Flask>=0.7
wtforms>=2.0
Flask-SQLAlchemy>=0.15
peewee
wtf-peewee
flask-mongoengine
pillow
flask-babelex
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment