class Module05Test(unittest.TestCase): """ Use this to setup your tests. This is where you may want to load configuration information (if needed), initialize class-scoped variables, create class-scoped instances of complex objects, initialize any requisite connections, etc. """ def setUp(self): self.ac = ActuatorData() self.ac.addValue(15) self.sensor = SensorData() self.sensor.addValue(15) self.hsa = HumiditySensorAdaptorTask() self.hsa.objectLoader() self.pu = PersistenceUtil() pass """ Use this to tear down any allocated resources after your tests are complete. This is where you may want to release connections, zero out any long-term data, etc. """ def tearDown(self): pass """ Place your comments describing the test here. """ def testSenseHatActivator(self): hat = SenseHatLedActivator() self.assertTrue(hat.updateLed("Test"),"Led Update Failed") pass def testSensorData(self): a = self.hsa.readSensorValue() self.assertGreaterEqual(a.current, 0, "Issue with sensor") pass def testWriteActuator(self): self.assertTrue(self.pu.writeActuatorToDataDbms(self.ac)) pass def testWriteSensor(self): self.assertTrue(self.pu.writeSensorToDataDbms(self.sensor)) pass
class TempSensorAdaptor(Thread): curTemp = 0 updateTime = 0 rateInSec = 0 sensorData = 0 senseHat = 0 connector = 0 alertDiff = 0 senseHatLed = 0 actuatorData = 0 TAE = 0 def __init__(self): Thread.__init__(self) self.highVal = 30 self.lowVal = 0 self.updateTime = 2 self.senseHat = SenseHat() self.sensorData = SensorData() self.actuatorData = ActuatorData() self.connector = Connector() self.alertDiff = 10 self.config = ConfigUtil('D:/git/repository/iot-device/apps/data/ConnectedDevicesConfig.props') self.config.loadConfig() print('Configuration data...\n' + str(self.config)) def getCurrValue(self): return self.currValue def setEnableTempEmulator(self, flag): self.enableTempEmulator = flag def run(self): while True: TAE = TempActuatorEmulator() TAE.start() self.curTemp = self.senseHat.get_temperature() self.sensorData.addValue(self.curTemp) print('\n--------------------') print('New sensor readings: \n') print(' ' + str(self.sensorData)) print('New actuator readings: \n') print(' ' + str(self.actuatorData)) if (abs(self.curTemp - float(self.config.getProperty(ConfigConst.CONSTRAINED_DEVICE,ConfigConst.TEMP_KEY))) > self.alertDiff): print('\n Current temp exceeds average by > ' + str(self.alertDiff) + '. Triggering alert...') try: self.connector.publishMessage('Exceptional sensor data [test]', self.sensorData) except Exception as e: print("Failed to send email\n" + str(e)) print('\n Actuator activating...') if (self.curTemp > float(self.config.getProperty(ConfigConst.CONSTRAINED_DEVICE,ConfigConst.TEMP_KEY))): self.actuatorData.addValue(1, abs(self.curTemp - float(self.config.getProperty(ConfigConst.CONSTRAINED_DEVICE,ConfigConst.TEMP_KEY)))) else: self.actuatorData.addValue(2, abs(self.curTemp - float(self.config.getProperty(ConfigConst.CONSTRAINED_DEVICE,ConfigConst.TEMP_KEY)))) # LED Display #self.senseHatLed.displayMsg(abs(self.curTemp - self.config.getProperty(ConfigConst.CONSTRAINED_DEVICE,ConfigConst.TEMP_KEY))) else: self.actuatorData.addValue(0, 0) self.enableTempEmulator.updateData(self.actuatorData) sleep(self.updateTime)
class DataUtilTest(unittest.TestCase): """ Use this to setup your tests. This is where you may want to load configuration information (if needed), initialize class-scoped variables, create class-scoped instances of complex objects, initialize any requisite connections, etc. """ def setUp(self): self.du = DataUtil() self.sd = SensorData() self.sd.addValue(15) self.sd.setName("Test") self.ad = ActuatorData() self.ad.addValue(44) self.ad.setName("Test") self.ad.setCommand("Test") pass """ Use this to tear down any allocated resources after your tests are complete. This is where you may want to release connections, zero out any long-term data, etc. """ def tearDown(self): pass """ Place your comments describing the test here. """ def testSomething(self): pass '@Test' def testCheckJsonData(self): jstring = self.du.toJsonFromSensorData(self.sd) #print(jstring) sd1 = self.du.toSensorDataFromJson(jstring) #print(str(self.sd.getCurrentValue())) #print(str(sd1.getCurrentValue())) self.assertTrue(self.sd.getCount() == sd1.getCount(), "count does not match") self.assertTrue(self.sd.getCurrentValue() == sd1.getCurrentValue(), "current does not match") pass '@Test' def testCheckActuatorData(self): jstring = self.du.toJsonFromActuatorData(self.ad) '@Test' def testtoActuatorDataFromJsonFile(self): self.assertTrue(self.du.toActuatorDataFromJsonFile(), "File to actuator failed") pass '@Test' def testwriteActuatorDataToFile(self): self.assertTrue(self.du.writeActuatorDataToFile(self.ad), "File to actuator failed") pass '@Test' def testwriteSensorDataToFile(self): self.assertTrue(self.du.toActuatorDataFromJsonFile(), "File to actuator failed") pass
class ActuatorDataTest(unittest.TestCase): """ Use this to setup your tests. This is where you may want to load configuration information (if needed), initialize class-scoped variables, create class-scoped instances of complex objects, initialize any requisite connections, etc. """ def setUp(self): self.sensor = ActuatorData() self.sensor.addValue(11) self.sensor.setCommand("Temp") pass """ Use this to tear down any allocated resources after your tests are complete. This is where you may want to release connections, zero out any long-term data, etc. """ def tearDown(self): pass """ Place your comments describing the test here. """ def testSomething(self): pass '@Test' def testgetCurrent(self): self.assertGreaterEqual( self.sensor.getValue(), 0.0, 'Current temperature value coming less than 0') pass '@Test' def testgetAvg(self): self.assertGreaterEqual(self.sensor.getAverageValue(), 0.0, 'Avg coming less than 0') pass '@Test' def testgetMin(self): self.assertGreaterEqual(self.sensor.getMivValue(), 0.0, 'Min coming less than 0') pass '@Test' def testgetMax(self): self.assertGreaterEqual(self.sensor.getMaxValue(), 0.0, 'Max coming less than 0') pass '@Test' def testgetCount(self): self.assertGreaterEqual(self.sensor.getCount(), 0.0, 'sample count coming less than 0') pass '@Test' def testgetCommand(self): self.assertEqual(self.sensor.getCommand(), "Temp", "Returned value did not match") pass
class Module04Test(unittest.TestCase): """ Use this to setup your tests. This is where you may want to load configuration information (if needed), initialize class-scoped variables, create class-scoped instances of complex objects, initialize any requisite connections, etc. """ def setUp(self): self.sd = SensorData() self.sd.addValue(11) self.sd.setName("Test") self.sdm = SensorDataManager() self.sdm.actuator.addValue(11) self.actuator = ActuatorData() self.actuator.addValue(2) self.actuator.setName("Test") self.tsa = MultiSensorAdaptor() self.taa = MultiActuatorAdaptor() self.tat = HumiditySensorAdaptorTask() self.tat.sensorData = SensorData() self.shla = SenseHatLedActivator() """ Use this to tear down any allocated resources after your tests are complete. This is where you may want to release connections, zero out any long-term data, etc. """ def tearDown(self): pass """ Place your comments describing the test here. """ '@Test' def testSendNotification(self): self.assertTrue(self.sdm.sendNotification(), "Notification Unsucessful") pass '@Test' def testhadleSensorData(self): self.assertTrue(self.sdm.hadleSensorData(self.sd), "Sensor handle data method not working") '@Test' def testgetSensorData(self): self.assertTrue(self.tsa.getSensorData(), "Issue in temperature adaptor") '@Test' def testreadSensorValueMin(self): self.assertGreaterEqual(self.tat.readSensorValue(), 0.0, 'sensor value coming less than 0') self.assertGreaterEqual(100, self.tat.readSensorValue(), 'sensor value coming more than 100') '@Test' def testupdateActuator(self): self.assertTrue(self.taa.updateActuator(self.actuator), "Actuator update failed") '@Test' def testupdateLed(self): self.assertTrue(self.shla.updateLed("Test Message"), "Led update failed") '@Test' def testreadSensorValuePushNotification(self): self.tat.sensorData = SensorData() self.tat.sensorData.addValue(12) self.tat.sensorData.setName("Test") self.tat.sdm = SensorDataManager() self.assertTrue(self.tat.pushData(), "Message not getting pushed to Sensor Data Manager")