diff --git a/ag_data/simulator.py b/ag_data/simulator.py index fea0474c..349866c6 100644 --- a/ag_data/simulator.py +++ b/ag_data/simulator.py @@ -35,37 +35,37 @@ def logSingleMeasurement(self, timestamp): utilities.assertEvent(self.event) utilities.assertSensor(self.sensor) - value = self.generateMeasurementPayload() + reading = self.generateMeasurementReading() - return self.logProcessedSingleMeasurement(timestamp, value) + return self.logProcessedSingleMeasurement(timestamp, reading) def checkSensorType(self, typeID): return self.sensor.type_id.id == typeID - def generateMeasurementPayload(self): + def generateMeasurementReading(self): utilities.assertSensor(self.sensor) - payload = None + reading = {} if self.checkSensorType(0): - payload = {"side": (random() < 0.5)} + reading = {"side": (random() < 0.5)} elif self.checkSensorType(2): - payload = {"reading": gauss(23, 1)} + reading = {"reading": gauss(23, 1)} elif self.checkSensorType(4): - payload = {"internal": gauss(15, 3), "external": gauss(20, 2)} + reading = {"internal": gauss(15, 3), "external": gauss(20, 2)} elif self.checkSensorType(6): - payload = {"volumetricFlow": gauss(0.2, 0.15)} + reading = {"volumetricFlow": gauss(0.2, 0.15)} elif self.checkSensorType(8): - payload = {"sample": gauss(0.5, 0.5)} + reading = {"sample": gauss(0.5, 0.5)} else: raise ValueError(f"Unsupported sensor type ({self.sensor.type_id})") - return payload + return reading def logMeasurementsInThePastSeconds( self, seconds, frequencyInHz, printProgress=True diff --git a/ag_data/tests/test_simulator.py b/ag_data/tests/test_simulator.py index c756c5f6..b640d498 100644 --- a/ag_data/tests/test_simulator.py +++ b/ag_data/tests/test_simulator.py @@ -67,21 +67,23 @@ def test_simulator_log_single_measurement(self): # test measurement payload format by cross comparison of all keys in payload # and the expected specification - measurement_payload = measurement_in_database.value - correct_payload_format = self.sim.sensor.type_id.format + value_in_db = measurement_in_database.value + correct_value_format = self.sensor.type_id.format - self.crossCompareKeys( - correct_payload_format["reading"], measurement_payload["reading"] - ) - self.crossCompareKeys( - correct_payload_format["result"], measurement_payload["result"] - ) + self.crossCompareKeys(correct_value_format["reading"], value_in_db["reading"]) + self.crossCompareKeys(correct_value_format["result"], value_in_db["result"]) # FIXME: test measurement value field data type (string/float/bool/...) def test_simulator_log_single_measurement_all_sensor_samples(self): - # FIXME: add tests for all built-in sensor types and supported sample sensors - return + totalSensorSamples = len(sample_user_data.sample_sensors) + + for index in range(totalSensorSamples): + currentSample = presets_generators.createSensorFromPresetAtIndex(index) + + self.updateSensor(currentSample) + + self.test_simulator_log_single_measurement() def test_simulator_log_multiple_measurements(self): import sys @@ -174,6 +176,10 @@ def test_simulator_log_continuous_measurements(self): <= expectedTotal * 1.1 ) + def updateSensor(self, sensor): + self.sensor = sensor + self.sim.sensor = sensor + def randVenueIndex(self): return randint(0, len(sample_user_data.sample_venues) - 1) @@ -185,6 +191,6 @@ def randSensorIndex(self): def crossCompareKeys(self, dictionary1, dictionary2): for field in dictionary1.keys(): - self.assertIn(field, dictionary2.keys()) + self.assertIn(field, dictionary2.keys(), f"{dictionary1}\n{dictionary2}") for field in dictionary2.keys(): - self.assertIn(field, dictionary1.keys()) + self.assertIn(field, dictionary1.keys(), f"{dictionary1}\n{dictionary2}")