fix toggling tests with mocking datetime.datetime.now()

This commit is contained in:
Marco Zeisler 2021-06-17 21:20:13 +02:00
parent 643f98f121
commit 50bf465add

View File

@ -6,6 +6,7 @@ from unittest.mock import patch
import geopy import geopy
from dse_shared_libs.daf import DAF from dse_shared_libs.daf import DAF
from dse_shared_libs.mock.datetime import MyDate
from dse_shared_libs.target_velocity import TargetVelocity from dse_shared_libs.target_velocity import TargetVelocity
from dse_shared_libs.traffic_light_color import TrafficLightColor from dse_shared_libs.traffic_light_color import TrafficLightColor
@ -31,6 +32,9 @@ class TestVehicle(unittest.TestCase):
self.v.last_update = self.timestamp self.v.last_update = self.timestamp
self.v._driven_kms = 0 self.v._driven_kms = 0
def tearDown(self) -> None:
time.sleep(0.1)
def test_nce_prop(self): def test_nce_prop(self):
with patch('time.sleep', return_value=None): with patch('time.sleep', return_value=None):
# initially false # initially false
@ -43,25 +47,37 @@ class TestVehicle(unittest.TestCase):
self.assertEqual(self.v.nce, False) self.assertEqual(self.v.nce, False)
def test_daf_prop(self): def test_daf_prop(self):
# test if daf object created properly with patch('datetime.datetime', MyDate):
daf = DAF(vehicle_identification_number=self.vin, # noinspection PyUnresolvedReferences
gps_location=self.starting_point, datetime.datetime.set_timestamp(self.timestamp)
velocity=self.starting_velocity,
timestamp=self.timestamp, # test if daf object created properly
near_crash_event=False) daf = DAF(vehicle_identification_number=self.vin,
self.assertEqual(self.v.daf, daf) gps_location=self.starting_point,
velocity=self.starting_velocity,
timestamp=self.timestamp,
near_crash_event=False)
self.assertEqual(self.v.daf, daf)
def test_current_location_prop(self): def test_current_location_prop(self):
# initial position should be starting point with patch('datetime.datetime', MyDate):
self.assertEqual(self.v.gps_location, self.starting_point) # noinspection PyUnresolvedReferences
datetime.datetime.set_timestamp(self.timestamp)
# lets say last time was about an hour ago # assert timestamp mocking worked
self.v.last_update = self.timestamp - datetime.timedelta(hours=1) self.assertEqual(type(datetime.datetime), type(MyDate))
self.assertEqual(self.timestamp, datetime.datetime.now())
# we drive "almost" directly north # initial position should be starting point
loc = self.v.gps_location self.assertEqual(self.v.gps_location, self.starting_point)
self.assertEqual(round(loc.latitude, 2), 1.17)
self.assertEqual(round(loc.longitude, 2), 0.04) # lets say last time was about an hour ago
self.v.last_update = self.timestamp - datetime.timedelta(hours=1)
# we drive "almost" directly north
loc = self.v.gps_location
self.assertEqual(round(loc.latitude, 2), 1.17)
self.assertEqual(round(loc.longitude, 2), 0.04)
def test_driven_kms_prop(self): def test_driven_kms_prop(self):
# initial driven kms are 0 # initial driven kms are 0