actual tests with nice minimal framework

This commit is contained in:
rgarcia-herrera 2020-08-23 15:03:46 -05:00
parent 6cc1b28958
commit 0b99982c49

View File

@ -2,33 +2,40 @@ import pytest
import requests
from LatLon23 import LatLon
from geopy import distance
from math import isclose
FLOCK_SERVER="http://127.0.0.1:5000"
UACM = (19.37425,-99.17168)
a = (19.37634,-99.12660) # playa pie de la cuesta y mirador
points = [
(19.37634,-99.12660), # playa pie de la cuesta y mirador
(19.37521,-99.11923), # antropólogos y cardiólogos
]
b = (19.37521,-99.11923) # antropólogos y cardiólogos
b1 = None
b2 = None
bikes = []
def flip(coord):
return (coord[1], coord[0])
def as_tuple(coord):
"""
Return LatLon point as (lat, lon) tuple
"""
return (float(coord.lat),
float(coord.lon))
class Bike(dict):
def server_register(self, dest):
self['dest'] = dest
register_url = "{server}/register/?dest_lon={dest_lon}&dest_lat={dest_lat}"
r = requests.get(register_url.format(server=FLOCK_SERVER,
dest_lon=dest[1],
dest_lat=dest[0]))
out = r.json()
assert 'bike_id' in out
self.update({'bike_id': out['bike_id'], })
# "flock_avg_speed":3.5,
@ -40,40 +47,81 @@ class Bike(dict):
def server_update(self):
update_url = "{server}/update/{bike_id}/?lat={lat}&lon={lon}&speed={speed}&bearing={bearing}"
req = requests.get(update_url.format(server=FLOCK_SERVER,
bike_id=self.bike_id,
speed=self.speed,
bearing=self.bearing,
lat=self.point[0],
lon=self.point[1]))
bike_id=self['bike_id'],
speed=self['speed'],
bearing=self['bearing'],
lat=self['lat'],
lon=self['lon']))
resp = req.json()
print(resp)
self.update(resp)
def test_register():
global b1, b2
global bikes
b1 = Bike()
b1.server_register(dest=UACM)
#b1.update({''})
#id2 = out['bike_id']
for i, p in enumerate(points):
# bikes will be bound north (90 degrees), 10km away
t0 = LatLon(*p)
t1 = t0.offset(90, 10)
bikes.append(Bike())
bikes[i].server_register(dest=as_tuple(t1))
assert 'bike_id' in bikes[i]
def test_update():
global b1, b2
"""
test update with coords, which are points of origin for
our test bikes.
"""
global bikes
t0 = LatLon(*a)
t1 = t0.offset(90, 1)
for i, p in enumerate(points):
t0 = LatLon(*p)
bearing = t0.heading_initial(LatLon(*bikes[i]['dest']))
print(t0, t1, t0.heading_initial(t1), distance.geodesic(a,
(t1.lat, t1.lon)).meters)
bikes[i].update(
{'speed': 3,
'bearing': bearing,
'lat': p[0],
'lon': p[1],
})
#b1.server_update(t0[0], lon, speed, bearing)
bikes[i].server_update()
#b2.server_update(lat, lon, speed, bearing)
assert isclose(bikes[i]['bearing'], 90)
# def test_flock_forward():
# """
# Bike 1 is closely followed by 2. Bike 1 updates at t=0, bike 2
# twenty seconds later.
# test for proper distances, flock heading, dest heading, etc.
# """
# global b1, b2
# #
# t0 = LatLon(*a)
# t1 = t0.offset(90, 1)
assert True
# b1.update(
# {'speed': 3,
# 'bearing': t0.heading_initial(t1),
# 'lat': t1.lat,
# 'lon': t1.lon,
# })
# b1.server_update()
# print(b1)
# #b2.server_update(lat, lon, speed, bearing)
# assert True