Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simulator tests #381

Merged
merged 6 commits into from Apr 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 10 additions & 10 deletions ag_data/simulator.py
Expand Up @@ -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
Expand Down
30 changes: 18 additions & 12 deletions ag_data/tests/test_simulator.py
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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}")