forked from meyersj/TransitSurveyorAPI
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.py
189 lines (156 loc) · 5.31 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# Copyrigh (C) 2015 Jeffrey Meyers
# This program is released under the "MIT License".
# Please see the file COPYING in the source
# distribution of this software for license terms.
import os
import unittest
import tempfile
import json
from uuid import uuid4
import requests
from flask import Flask
from api import app
from api import db
from api.mod_api.views import verify_user
from api.mod_api.insert import InsertScan, InsertPair, buildGeom
ENDPOINT = os.getenv("API_ENDPOINT", "127.0.0.1:9000")
INDEX_RESPONSE = "On-Off Index"
API_RESPONSE = "API Module"
class VerifyUserTestCase(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
def test_VerifyUser_lookup_function(self):
match, user_id = verify_user("testuser", 1234)
assert match is not None
assert user_id is not None
def test_VerifyUser_endpoint(self):
rv = self.app.post(
'/api/verifyUser',
data=dict(
username="testuser",
password="1234"
),
follow_redirects=True
)
data = json.loads(rv.data)
assert "error" not in rv.data
assert "match" in rv.data
assert "user_id" in rv.data
class InsertScanTestCase(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
uuid = str(uuid4())
self.on_scan = dict(
uuid=uuid,
date="2000-01-01 12:00:00",
rte="9",
dir="0",
lon="-122.5",
lat="45.5",
mode="on",
user_id="testuser"
)
self.off_scan = dict(
uuid=uuid,
date="2000-01-01 12:30:00",
rte="9",
dir="0",
lon="-122.5",
lat="45.5",
mode="off",
user_id="testuser"
)
def test_InsertScan_invalid(self):
rv = self.app.post(
'/api/insertScan',
data=dict(invalidparam="something"),
follow_redirects=True
)
data = json.loads(rv.data)
assert "error" in data
assert data["error"] == "invalid input data"
def test_InsertScan_onoff(self):
insert = InsertScan(**self.on_scan)
valid, insertID, match = insert.isSuccessful()
assert valid, "on scan insert failed"
assert insertID != -1, "on scan insert id should not be -1"
insert = InsertScan(**self.off_scan)
valid, insertID, match = insert.isSuccessful()
assert valid, "off scan insert failed"
assert insertID != -1, "off scan insert id should not be -1"
assert match is True, "on-off scans did not get matched"
def test_InsertScan_onoff_requests(self):
rv = self.app.post(
'/api/insertScan',
data=self.on_scan,
follow_redirects=True
)
data = json.loads(rv.data)
assert "success" in data and data["success"], "on scan insert request failed"
rv = self.app.post(
'/api/insertScan',
data=self.off_scan,
follow_redirects=True
)
data = json.loads(rv.data)
assert "success" in data and data["success"], "off scan insert request failed"
assert "match" in data and data["match"], "matching on-off requests failed"
class InsertPairTestCase(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
self.pair = dict(
date="2000-01-01 12:00:00",
rte="193",
dir="0",
on_stop="10764",
off_stop="10770",
user_id="testuser"
)
def test_InsertPair_invalid_params(self):
rv = self.app.post(
'/api/insertPair',
data=dict(invalidparam="something"),
follow_redirects=True
)
data = json.loads(rv.data)
assert "error" in data
assert data["error"] == "invalid input data"
def test_InsertPair_basic(self):
insert = InsertPair(**self.pair)
valid, insertID = insert.isSuccessful()
assert valid, "InsertPair returned invalid response"
assert insertID != -1, "InsertPair insert id should not be -1"
class StopLookupTestCase(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
def test_build_geom(self):
success, geom = buildGeom(45.5, -122)
assert success, "failed to build geometry"
def test_stopLookup_request(self):
rv = self.app.post(
'/api/stopLookup',
data=dict(
lat="45.5",
lon="-122.5",
rte="9",
dir="0"
),
follow_redirects=True
)
assert rv.status_code == 200
data = json.loads(rv.data)
assert not data["error"], "stop lookup returned error"
assert data["stop_name"], "stop name is missing"
class IndexTest(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
def test_index(self):
rv = self.app.get('/', follow_redirects=True)
assert rv.status_code == 200
assert rv.data == INDEX_RESPONSE
def test_api_index(self):
rv = self.app.get('/api', follow_redirects=True)
assert rv.status_code == 200
assert rv.data == API_RESPONSE
if __name__ == '__main__':
unittest.main()