handle none mileage 2

This commit is contained in:
Florian Bezannier
2021-09-14 17:59:07 +02:00
parent bed3f09011
commit 62488d076c
3 changed files with 39 additions and 6 deletions
+30 -1
View File
@@ -20,7 +20,7 @@ from mylogger import my_logger
from otp.otp import load_otp, save_otp
from charge_control import ChargeControls
from test.utils import DATA_DIR, record_position, latitude, longitude, date0, date1, date2, date3, record_charging, \
vehicule_list, get_new_test_db
vehicule_list, get_new_test_db, get_date
from trip import Trips
from libs.utils import get_temp, parse_hour
from web.db import Database
@@ -212,6 +212,35 @@ class TestUnit(unittest.TestCase):
'consumption': 1.08,
'consumption_fuel_km': 10.53}])
def test_none_mileage(self):
get_new_test_db()
ElecPrice.CONFIG_FILENAME = DATA_DIR + "config.ini"
car = self.vehicule_list[1]
Database.record_position(None, car.vin, None, latitude, longitude, 22, get_date(1), 40, 30, False)
Database.record_position(None, car.vin, None, latitude, longitude, 22, get_date(2), 35, 29, False)
Database.record_position(None, car.vin, None, latitude, longitude, 22, get_date(3), 30, 28, False)
start = get_date(4)
end = get_date(6)
Database.record_position(None, car.vin, 11, latitude, longitude, 22, start, 40, 30, False)
Database.record_position(None, car.vin, 20, latitude, longitude, 22, get_date(5), 35, 29, False)
Database.record_position(None, car.vin, 30, latitude, longitude, 22, end, 30, 28, False)
trips = Trips.get_trips(self.vehicule_list)
res = trips[car.vin].get_trips_as_dict()
print(res)
assert compare_dict(res, [{'consumption_km': 5.684210526315789,
'start_at': start,
'consumption_by_temp': None,
'positions': {'lat': [latitude],
'long': [longitude]},
'duration': 120.0,
'speed_average': 9.5,
'distance': 19.0,
'mileage': 30.0,
'altitude_diff': 0,
'id': 1,
'consumption': 1.08,
'consumption_fuel_km': 10.53}])
def test_db_callback(self):
old_dummy_value = dummy_value
get_new_test_db()
+4
View File
@@ -48,3 +48,7 @@ def record_charging():
Charging.record_charging(car, "InProgress", date2, 85, latitude, longitude, "FR", "slow", 20, 60)
Charging.record_charging(car, "InProgress", date3, 90, latitude, longitude, "FR", "slow", 20, 60)
Charging.record_charging(car, "Stopped", date4, 91, latitude, longitude, "FR", "slow", 20, 60)
def get_date(offset):
return date3 + timedelta(minutes=60 * offset)
+5 -5
View File
@@ -159,14 +159,14 @@ class Trips(list):
logger.debugv("%s mileage:%.1f level:%s level_fuel:%s",
res[x]['Timestamp'], res[x]['mileage'], res[x]['level'], res[x]['level_fuel'])
next_el = res[x + 2]
distance = 0
try:
distance = end["mileage"] - start["mileage"]
duration = (end["Timestamp"] - start["Timestamp"]).total_seconds() / 3600
speed_average = distance / duration
except TypeError:
logger.exception("Bad value : ")
distance = 0
speed_average = 0
logger.debug("Bad mileage value in DB")
duration = (end["Timestamp"] - start["Timestamp"]).total_seconds() / 3600
try:
speed_average = distance / duration
except ZeroDivisionError:
speed_average = 0