forked from codeforamerica/OpenTrails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
116 lines (92 loc) · 3.59 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# -- coding: utf-8 --
from shutil import rmtree
from unittest import TestCase, main
from os.path import join, dirname
from urlparse import urljoin
from tempfile import mkdtemp
from bs4 import BeautifulSoup
from open_trails import app, transformers
class FakeUpload:
''' Pretend to be a file upload in flask.
'''
def __init__(self, path):
self._file = open(path, 'r')
self.filename = path
def save(self, path):
with open(path, 'w') as file:
file.write(self._file.read())
class TestTransformers (TestCase):
def setUp(self):
self.tmp = mkdtemp(prefix='plats-')
app.config['UPLOAD_FOLDER'] = self.tmp
def tearDown(self):
rmtree(self.tmp)
def testConvert(self):
''' Test basic SHP to GeoJSON conversion.
'''
names = ('test-files/lake-man.zip',
'test-files/lake-man-GGNRA.zip',
'test-files/lake-man-San-Antonio.zip',
'test-files/lake-man-Santa-Clara.zip',
'test-files/lake-man-Portland.zip')
for name in names:
self.doFileConversion(name)
def doFileConversion(self, name):
''' Test conversion results for named file.
'''
file = FakeUpload(join(dirname(__file__), name))
geojson = transformers.transform_shapefile(file)
#
# Is it GeoJSON?
#
self.assertEqual(geojson['type'], 'FeatureCollection')
self.assertEqual(len(geojson['features']), 6)
self.assertEqual(set([f['geometry']['type'] for f in geojson['features']]), set(['LineString']))
#
# Does it cover the expected geographic area?
#
lons, lats = [], []
for f in geojson['features']:
lons.extend([y for (x, y) in f['geometry']['coordinates']])
lats.extend([x for (x, y) in f['geometry']['coordinates']])
self.assertTrue(37.80071 < min(lons) and max(lons) < 37.80436)
self.assertTrue(-122.25925 < min(lats) and max(lats) < -122.25671)
class TestApp (TestCase):
def setUp(self):
self.app = app.test_client()
self.tmp = mkdtemp(prefix='plats-')
app.config['UPLOAD_FOLDER'] = self.tmp
def tearDown(self):
rmtree(self.tmp)
def testUpload(self):
''' Check basic file upload flow.
'''
names = ('test-files/lake-man.zip',
'test-files/lake-man-GGNRA.zip',
'test-files/lake-man-San-Antonio.zip',
'test-files/lake-man-Santa-Clara.zip',
'test-files/lake-man-Portland.zip')
for name in names:
self.doUpload(name)
def doUpload(self, name):
''' Check basic file upload flow for named file.
'''
response = self.app.get('/')
self.assertEqual(response.status_code, 200)
#
# Check for a file upload field in the home page form.
#
soup = BeautifulSoup(response.data)
form = soup.find('input', attrs=dict(type='file')).find_parent('form')
self.assertTrue('multipart/form-data' in form['enctype'])
self.assertTrue(form.find_all('input', attrs=dict(type='file')))
#
# Attempt to upload a test shapefile.
#
action = urljoin('/', form['action'])
input = form.find('input', attrs=dict(type='file'))['name']
file = open(join(dirname(__file__), name))
response = self.app.post(action, data={input: file})
self.assertEqual(response.status_code, 200)
if __name__ == '__main__':
main()