Skip to content

Commit f6c65e9

Browse files
committed
Create some basic testing infrastructure
1 parent 0891fec commit f6c65e9

File tree

3 files changed

+176
-0
lines changed

3 files changed

+176
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ Make sure to run `pip3 install -r requirements.txt` to install all necessary req
88

99
Just run `python3 server.py` to start up the server. The server boots up on port 5000, so you can access
1010
the GET commands via the browser by going to, for example, `http://localhost:5000/vehicles/1000`.
11+
12+
## Running tests
13+
14+
Run `pytest -v` to run tests. This will automatically run tests for the Smartcar API and makes sure we
15+
are calling the right things to the GM API also.

requirements.txt

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
aniso8601==8.0.0
22
aspy.yaml==1.3.0
3+
atomicwrites==1.3.0
4+
attrs==19.3.0
35
autopep8==1.4.4
46
beautifulsoup4==4.8.1
57
bs4==0.0.1
@@ -20,12 +22,18 @@ MarkupSafe==1.1.1
2022
more-itertools==7.2.0
2123
nodeenv==1.3.3
2224
numpy==1.17.2
25+
packaging==19.2
2326
pandas==0.25.1
27+
pluggy==0.13.0
2428
post==2019.4.13
2529
pre-commit==1.18.3
2630
public==2019.4.13
31+
py==1.8.0
2732
pycodestyle==2.5.0
33+
pyparsing==2.4.2
2834
PyPDF2==1.26.0
35+
pytest==5.2.1
36+
pytest-mock==1.11.1
2937
python-dateutil==2.8.0
3038
pytz==2019.3
3139
PyYAML==5.1.2
@@ -37,5 +45,6 @@ soupsieve==1.9.4
3745
toml==0.10.0
3846
urllib3==1.25.6
3947
virtualenv==16.7.5
48+
wcwidth==0.1.7
4049
Werkzeug==0.16.0
4150
zipp==0.6.0

test_server.py

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import pytest
2+
from server import app
3+
from gm_api import GM_Api as GM_Api
4+
5+
@pytest.fixture
6+
def client():
7+
app.config['TESTING'] = True
8+
9+
with app.test_client() as client:
10+
yield client
11+
12+
def test_vehicle_name(client, mocker):
13+
mock = mocker.patch('gm_api.GM_Api.post')
14+
mock.return_value = {
15+
'service': 'getVehicleInfo',
16+
'status': '200',
17+
'data': {
18+
'vin': {
19+
'type': 'String',
20+
'value': '123123412412'
21+
},
22+
'color': {
23+
'type': 'String',
24+
'value': 'Metallic Silver'
25+
},
26+
'fourDoorSedan': {
27+
'type': 'Boolean',
28+
'value': 'True'
29+
},
30+
'twoDoorCoupe': {
31+
'type': 'Boolean',
32+
'value': 'False'
33+
},
34+
'driveTrain': {
35+
'type': 'String',
36+
'value': 'v8'
37+
}
38+
}
39+
}
40+
41+
resp = client.get('/vehicles/1234')
42+
mock.assert_called_with('/getVehicleInfoService', {'id': '1234'})
43+
assert(resp.json == {'vin': '123123412412', 'color': 'Metallic Silver', 'doorCount': 4, 'driveTrain': 'v8'})
44+
45+
def test_vehicle_doors(client, mocker):
46+
mock = mocker.patch('gm_api.GM_Api.post')
47+
mock.return_value = {
48+
'service': 'getSecurityStatus',
49+
'status': '200',
50+
'data': {
51+
'doors': {
52+
'type': 'Array',
53+
'values': [
54+
{
55+
'location': {
56+
'type': 'String',
57+
'value': 'frontLeft'
58+
},
59+
'locked': {
60+
'type': 'Boolean',
61+
'value': 'False'
62+
}
63+
},
64+
{
65+
'location': {
66+
'type': 'String',
67+
'value': 'frontRight'
68+
},
69+
'locked': {
70+
'type': 'Boolean',
71+
'value': 'True'
72+
}
73+
},
74+
{
75+
'location': {
76+
'type': 'String',
77+
'value': 'backLeft'
78+
},
79+
'locked': {
80+
'type': 'Boolean',
81+
'value': 'False'
82+
}
83+
},
84+
{
85+
'location': {
86+
'type': 'String',
87+
'value': 'backRight'
88+
},
89+
'locked': {
90+
'type': 'Boolean',
91+
'value': 'True'
92+
}
93+
}
94+
]
95+
}
96+
}
97+
}
98+
99+
resp = client.get('/vehicles/1234/doors')
100+
mock.assert_called_with('/getSecurityStatusService', {'id': '1234'})
101+
assert(resp.json == [
102+
{'location': 'frontLeft', 'locked': False},
103+
{'location': 'frontRight', 'locked': True},
104+
{'location': 'backLeft', 'locked': False},
105+
{'location': 'backRight', 'locked': True},
106+
])
107+
108+
def test_vehicle_fuel(client, mocker):
109+
mock = mocker.patch('gm_api.GM_Api.post')
110+
mock.return_value = {
111+
'service': 'getEnergy',
112+
'status': '200',
113+
'data': {
114+
'tankLevel': {
115+
'type': 'Number',
116+
'value': '30.2'
117+
},
118+
'batteryLevel': {
119+
'type': 'Null',
120+
'value': 'null'
121+
}
122+
}
123+
}
124+
125+
resp = client.get('/vehicles/1234/fuel')
126+
mock.assert_called_with('/getEnergyService', {'id': '1234'})
127+
assert(resp.json == {'percent': 30.2})
128+
129+
def test_vehicle_battery(client, mocker):
130+
mock = mocker.patch('gm_api.GM_Api.post')
131+
mock.return_value = {
132+
'service': 'getEnergy',
133+
'status': '200',
134+
'data': {
135+
'tankLevel': {
136+
'type': 'Number',
137+
'value': '15.2'
138+
},
139+
'batteryLevel': {
140+
'type': 'Number',
141+
'value': '50.3'
142+
}
143+
}
144+
}
145+
146+
resp = client.get('/vehicles/1234/battery')
147+
mock.assert_called_with('/getEnergyService', {'id': '1234'})
148+
assert(resp.json == {'percent': 50.3})
149+
150+
def test_vehicle_engine(client, mocker):
151+
mock = mocker.patch('gm_api.GM_Api.post')
152+
mock.return_value = {
153+
'service': 'actionEngine',
154+
'status': '200',
155+
'actionResult': {
156+
'status': 'EXECUTED'
157+
}
158+
}
159+
160+
resp = client.post('/vehicles/1234/engine', json={'action': 'START'})
161+
mock.assert_called_with('/actionEngineService', {'id': '1234', 'command': 'START_VEHICLE'})
162+
assert(resp.json == {'status': 'success'})

0 commit comments

Comments
 (0)