Initial checkin

This commit is contained in:
2023-05-18 18:21:00 -04:00
commit 1c64f16aef
21 changed files with 1020 additions and 0 deletions

24
test/__init__.py Normal file
View File

@@ -0,0 +1,24 @@
import os
import tempfile
import pytest
from app import create_app, db, Vehicle
from flask import json
@pytest.fixture
def client():
app = create_app()
db_fd, app.config['DATABASE'] = tempfile.mkstemp()
app.config['TESTING'] = True
client = app.test_client()
with app.app_context():
db.drop_all()
db.create_all()
yield client
os.close(db_fd)
os.unlink(app.config['DATABASE'])

27
test/test_auto.py Normal file
View File

@@ -0,0 +1,27 @@
from flask import json
from app.extensions import db
from app.models.shift import Shift
from app.models.battery_change import BatteryChange
from test import client
from test.utils import create_vehicles
def test_auto_shift_generation(client):
create_vehicles(8)
rv = client.get('/auto/40.68136179/-73.996421')
data = json.loads(rv.data)
print(data)
ids = [change['vehicle']['id'] for change in data['battery_changes']]
assert ids == [2, 1, 8, 6, 7, 5, 4, 3]
def test_auto_shift_generation_all(client):
create_vehicles()
rv = client.get('/auto/40.68136179/-73.996421')
data = json.loads(rv.data)
ids = [change['vehicle']['id'] for change in data['battery_changes']]
print(ids)
assert ids == [2, 1, 8, 6, 7, 5, 15, 4, 10, 11, 12, 9, 14, 13, 3]

BIN
test/test_auto_example.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

31
test/test_auto_results.py Normal file
View File

@@ -0,0 +1,31 @@
'''
Result of kruskals algorithm with first 8 cars in the system.
The array shows the raw output, where the index and first item in the tuple
is the vehicles position in the sorted `closest_vehicles` array.
The dictionary is the same data, just the keys and first item in of each tuple
replaced with the vehicle_id.
test_auto_example.png shows the position of these vehicles as well as the starting point.
'''
[
[(1, 0.0018223418998603907), (2, 0.005142119115696336)],
[(0, 0.0018223418998603907), (4, 0.0038434786326951562)],
[(0, 0.005142119115696336)],
[(6, 0.003218020664940648), (4, 0.004116640377786014), (5, 0.005380458066000523)],
[(1, 0.0038434786326951562), (3, 0.004116640377786014)],
[(3, 0.005380458066000523)],
[(3, 0.003218020664940648), (7, 0.00569171626137669)],
[(6, 0.00569171626137669)]
]
vehicle_ids = {
1: [(8, 0.0018223418998603907), (2, 0.005142119115696336)],
8: [(1, 0.0018223418998603907), (6, 0.0038434786326951562)],
2: [(1, 0.005142119115696336)],
7: [(5, 0.003218020664940648), (6, 0.004116640377786014), (3, 0.005380458066000523)],
6: [(8, 0.0038434786326951562), (7, 0.004116640377786014)],
3: [(7, 0.005380458066000523)],
5: [(7, 0.003218020664940648), (4, 0.00569171626137669)],
4: [(5, 0.00569171626137669)]
}

207
test/test_shift.py Normal file
View File

@@ -0,0 +1,207 @@
from flask import json
from app.extensions import db
from app.models.shift import Shift
from app.models.battery_change import BatteryChange
from test import client
from test.utils import create_vehicles
def test_list_shifts_empty_db_empty_list(client):
rv = client.get('/shifts')
assert len(json.loads(rv.data)) == 0
def test_list_shifts_single_record(client):
db.session.add(Shift())
rv = client.get('/shifts')
assert len(json.loads(rv.data)) == 1
def test_get_shift(client):
db.session.add(Shift())
db.session.add(Shift())
db.session.commit()
rv = client.get('/shifts')
assert len(json.loads(rv.data)) == 2
rv = client.get('/shifts/1')
data = json.loads(rv.data)
assert data['id'] == 1
rv = client.get('/shifts/2')
data = json.loads(rv.data)
assert data['id'] == 2
def test_create_empty_shift(client):
client.post('/shifts')
rv = client.get('/shifts/1')
data = json.loads(rv.data)
assert data['id'] == 1
assert data['battery_changes'] == []
def test_create_shift_with_vehicles(client):
create_vehicles()
rv = client.get('/vehicles')
assert len(json.loads(rv.data)) == 15
client.post('/shifts', data={
'vehicles': '1,5,10'
})
rv = client.get('/shifts/1')
data = json.loads(rv.data)
assert data['id'] == 1
assert len(data['battery_changes']) == 3
assert data['battery_changes'][0]['vehicle_id'] == 1
assert data['battery_changes'][1]['vehicle_id'] == 5
assert data['battery_changes'][2]['vehicle_id'] == 10
assert data['battery_changes'][0]['completed'] == False
assert data['battery_changes'][0]['shift_id'] == 1
# Get vehicle data directly in shift
assert data['battery_changes'][0]['vehicle']['license_plate'] == 'NY0001'
assert data['battery_changes'][1]['vehicle']['location_long'] == -73.988838
assert data['battery_changes'][2]['vehicle']['battery_level'] == 22
def add_vehicle_to_shift(client):
create_vehicles()
client.post('/shifts')
rv = client.get('/shifts/1')
data = json.loads(rv.data)
assert data['id'] == 1
assert data['battery_changes'] == []
rv = client.post('shifts/1/add/2')
data = json.loads(rv.data)
assert data['id'] == 1
assert len(data['battery_changes']) == 1
assert data['battery_changes'][0]['vehicle_id'] == 2
rv = client.post('shifts/1')
data = json.loads(rv.data)
assert data['id'] == 1
assert len(data['battery_changes']) == 1
assert data['battery_changes'][0]['vehicle_id'] == 2
# Shouldnt be able to add same vehicle to shift
rv = client.post('shifts/1/vehicles/2')
rv = client.post('shifts/1')
data = json.loads(rv.data)
assert data['id'] == 1
assert len(data['battery_changes']) == 1
assert data['battery_changes'][0]['vehicle_id'] == 2
def test_remove_vehicle_from_shift(client):
create_vehicles()
rv = client.get('/vehicles')
assert len(json.loads(rv.data)) == 15
client.post('/shifts', data={
'vehicles': '1,5,10'
})
rv = client.delete('shifts/1/vehicles/5')
data = json.loads(rv.data)
assert data['id'] == 1
assert len(data['battery_changes']) == 2
assert data['battery_changes'][0]['vehicle_id'] == 1
assert data['battery_changes'][1]['vehicle_id'] == 10
rv = client.get('/vehicles/10')
data = json.loads(rv.data)
assert len(data['shifts']) == 1
assert data['shifts'][0]['id'] == 1
# negative testing
rv = client.delete('shifts/1/vehicles/9')
assert rv.status_code == 404
rv = client.delete('shifts/1/vehicles/5')
assert rv.status_code == 404
def test_delete_shift(client):
create_vehicles()
rv = client.get('/vehicles')
assert len(json.loads(rv.data)) == 15
client.post('/shifts', data={
'vehicles': '1,5,10'
})
rv = client.get('/shifts/1')
data = json.loads(rv.data)
assert data['id'] == 1
assert len(data['battery_changes']) == 3
client.delete('/shifts/1')
rv = client.get('/shifts')
assert len(json.loads(rv.data)) == 0
changes = BatteryChange.query.all()
assert len(changes) == 0
# negative testing
rv = client.delete('/shifts/5')
assert rv.status_code == 404
def test_complete_vehicle_charge(client):
create_vehicles()
client.post('/shifts', data={
'vehicles': '1,5,10'
})
rv = client.get('/shifts/1')
data = json.loads(rv.data)
assert data['id'] == 1
assert len(data['battery_changes']) == 3
assert data['battery_changes'][0]['completed'] == False
assert data['battery_changes'][1]['completed'] == False
assert data['battery_changes'][2]['completed'] == False
rv = client.post('/shifts/1/vehicles/5/completed')
data = json.loads(rv.data)
assert data['battery_changes'][0]['completed'] == False
assert data['battery_changes'][1]['completed'] == True
assert data['battery_changes'][2]['completed'] == False
# Negative testing
rv = client.post('/shifts/2/vehicles/5/completed') # shift not found
assert rv.status_code == 404
rv = client.post('/shifts/1/vehicles/6/completed') # vehicle not found
assert rv.status_code == 404
def test_get_vehicle_in_shift(client):
create_vehicles()
client.post('/shifts', data={
'vehicles': '1,5,10'
})
rv = client.get('/shifts/1/vehicles/10')
data = json.loads(rv.data)
assert data['vehicle']['license_plate'] == 'NY0010'
assert data['completed'] == False
rv = client.get('shifts/1/vehicles/11')
assert rv.status_code == 404
def test_shift_all_completed(client):
create_vehicles()
client.post('/shifts', data={
'vehicles': '1,5,10'
})
rv = client.get('/shifts/1')
data = json.loads(rv.data)
assert data['all_completed'] == False
client.post('/shifts/1/vehicles/1/completed')
rv = client.get('/shifts/1')
data = json.loads(rv.data)
assert data['all_completed'] == False
client.post('/shifts/1/vehicles/5/completed')
rv = client.get('/shifts/1')
data = json.loads(rv.data)
assert data['all_completed'] == False
client.post('/shifts/1/vehicles/10/completed')
rv = client.get('/shifts/1')
data = json.loads(rv.data)
assert data['all_completed'] == True

125
test/test_vehicle.py Normal file
View File

@@ -0,0 +1,125 @@
from flask import json
from app.extensions import db
from app.models.vehicle import Vehicle
from test import client
def test_list_vehicles_empty_db_empty_list(client):
rv = client.get('/vehicles')
assert len(json.loads(rv.data)) == 0
def test_list_vehicles_with_data(client):
db.session.add(Vehicle(**{
"id": 1,
"license_plate": "NY0001",
"battery_level": 90,
"in_use": True,
"model": "Niu",
"location_lat": 40.680245,
"location_long": -73.996955,
}))
db.session.commit()
rv = client.get('/vehicles')
assert len(json.loads(rv.data)) == 1
def test_get_single_vehicle(client):
db.session.add(Vehicle(**{
"id": 1,
"license_plate": "NY0002",
"battery_level": 90,
"in_use": True,
"model": "Niu",
"location_lat": 40.680245,
"location_long": -73.996955,
}))
db.session.add(Vehicle(**{
"id": 2,
"license_plate": "NY0003",
"battery_level": 95,
"in_use": True,
"model": "Niu",
"location_lat": 40.680245,
"location_long": -73.996955,
}))
db.session.commit()
rv = client.get('/vehicles')
assert len(json.loads(rv.data)) == 2
rv = client.get('/vehicles/1')
data = json.loads(rv.data)
assert data['id'] == 1
assert data['license_plate'] == 'NY0002'
def test_create_vehicles(client):
client.post('/vehicles', data={
"license_plate": "NY0003",
"battery_level": 90,
"in_use": True,
"model": "Niu",
"location_lat": 40.680245,
"location_long": -73.996955,
})
rv = client.get('/vehicles/1')
data = json.loads(rv.data)
assert data['id'] == 1
assert data['license_plate'] == 'NY0003'
client.post('/vehicles', data={
"license_plate": "NY0004",
"battery_level": 90,
"in_use": True,
"model": "Niu",
"location_lat": 40.680245,
"location_long": -73.996955,
})
rv = client.get('/vehicles/2')
data = json.loads(rv.data)
assert data['id'] == 2
assert data['license_plate'] == 'NY0004'
def test_update_vehicle(client):
client.post('/vehicles', data={
"license_plate": "NY0006",
"battery_level": 90,
"in_use": True,
"model": "Niu",
"location_lat": 40.680245,
"location_long": -73.996955,
})
rv = client.get('/vehicles/1')
data = json.loads(rv.data)
assert data['id'] == 1
assert data['license_plate'] == 'NY0006'
rv = client.post('/vehicles/1', data={
"battery_level": 85,
})
data = json.loads(rv.data)
assert data['id'] == 1
assert data['battery_level'] == 85
rv = client.get('/vehicles/1')
data = json.loads(rv.data)
assert data['battery_level'] == 85
def test_delete_vehicle(client):
client.post('/vehicles', data={
"license_plate": "NY0007",
"battery_level": 90,
"in_use": True,
"model": "Niu",
"location_lat": 40.680245,
"location_long": -73.996955,
})
rv = client.get('/vehicles/1')
data = json.loads(rv.data)
assert data['id'] == 1
assert data['license_plate'] == 'NY0007'
rv = client.delete('/vehicles/1')
data = json.loads(rv.data)
assert data == 1
rv = client.get('/vehicles')
assert len(json.loads(rv.data)) == 0

12
test/utils.py Normal file
View File

@@ -0,0 +1,12 @@
from app.extensions import db
from app.models.vehicle import Vehicle
from .vehicle_data import vehicle_data
def create_vehicles(length = None):
if length:
for vehicle in vehicle_data[:length]:
db.session.add(Vehicle(**vehicle))
else:
for vehicle in vehicle_data:
db.session.add(Vehicle(**vehicle))
db.session.commit()

138
test/vehicle_data.py Normal file
View File

@@ -0,0 +1,138 @@
vehicle_data = [
{
"id": 1,
"license_plate": "NY0001",
"battery_level": 90,
"in_use": True,
"model": "Niu",
"location_lat": 40.680245,
"location_long": -73.996955,
},
{
"id": 2,
"license_plate": "NY0002",
"battery_level": 9,
"in_use": False,
"model": "Niu",
"location_lat": 40.684978,
"location_long": -73.998965,
},
{
"id": 3,
"license_plate": "NY0003",
"battery_level": 65,
"in_use": False,
"model": "Niu",
"location_lat": 40.683574,
"location_long": -73.990715,
},
{
"id": 4,
"license_plate": "NY0004",
"battery_level": 34,
"in_use": False,
"model": "Niu",
"location_lat": 40.67942,
"location_long": -73.983841,
},
{
"id": 5,
"license_plate": "NY0005",
"battery_level": 20,
"in_use": False,
"model": "Niu",
"location_lat": 40.676695,
"location_long": -73.988838,
},
{
"id": 6,
"license_plate": "NY0006",
"battery_level": 15,
"in_use": False,
"model": "Niu",
"location_lat": 40.675496,
"location_long": -73.99468,
},
{
"id": 7,
"license_plate": "NY0007",
"battery_level": 90,
"in_use": False,
"model": "Niu",
"location_lat": 40.678274,
"location_long": -73.991642,
},
{
"id": 8,
"license_plate": "NY0008",
"battery_level": 9,
"in_use": False,
"model": "Niu",
"location_lat": 40.678434,
"location_long": -73.997158,
},
{
"id": 9,
"license_plate": "NY0009",
"battery_level": 90,
"in_use": False,
"model": "Niu",
"location_lat": 40.683456,
"location_long": -73.002047,
},
{
"id": 10,
"license_plate": "NY0010",
"battery_level": 22,
"in_use": True,
"model": "Niu",
"location_lat": 40.677941,
"location_long": -73.982731,
},
{
"id": 11,
"license_plate": "NY0011",
"battery_level": 76,
"in_use": False,
"model": "Niu",
"location_lat": 40.673533,
"location_long": -73.981992,
},
{
"id": 12,
"license_plate": "NY0012",
"battery_level": 90,
"in_use": False,
"model": "Niu",
"location_lat": 40.668346,
"location_long": -73.976115,
},
{
"id": 13,
"license_plate": "NY0013",
"battery_level": 2,
"in_use": False,
"model": "Niu",
"location_lat": 40.669861,
"location_long": -73.989846,
},
{
"id": 14,
"license_plate": "NY0014",
"battery_level": 13,
"in_use": False,
"model": "Niu",
"location_lat": 40.673568,
"location_long": -73.000575,
},
{
"id": 15,
"license_plate": "NY0015",
"battery_level": 17,
"in_use": False,
"model": "Niu",
"location_lat": 40.676001,
"location_long": -73.987382,
},
]