def test_Rcv_Sensor(self): """Does Recieve work when that sensortype is not in the DB""" now = datetime.datetime.utcnow() #Create our bitmask bs = Bitset(size=Packets.SC_SIZE) #Try to setup a temperature sample bs[Packets.SC_D_TEMPERATURE] = True #Then A packet packet = StateMsg(addr=4242) packet.set_ctp_parent_id(101) #Parent Node Id packet.set_special(0xc7) #Special Id packet.set_packed_state_mask(bs.a) packet.set_packed_state([22.5]) self.testbif.receive(packet) output = self.blogger.mainloop() self.assertTrue(output) #Check stuff gets added to the database correctly session = self.Session() #Does the sensortype exist now (Apparently its not a FK in Reading) #qry = session.query(models.SensorType).filter_by(id=1).first() #self.assertTrue(qry) #Is the name UNKNOWN #self.assertEqual(qry.Name, "UNKNOWN") #NodeState qry = session.query(models.NodeState).filter_by(nodeId=4242) self.assertEqual(qry.count(), 1) qry = qry.first() self.assertTrue(qry) tdiff = qry.time - now self.assertLess(tdiff.seconds, 1.0) self.assertEqual(qry.parent, 101) #And Reading qry = session.query(models.Reading).filter_by(nodeId=4242) #As we just did temperature there should only be one reading self.assertEqual(qry.count(), 1) qry = qry.first() self.assertTrue(qry) self.assertEqual(qry.typeId, 1) self.assertEqual(qry.value, 22.5) #Reset the sensor type to be as it should qry = session.query(models.SensorType).filter_by(id=1).first() if qry is None: qry = models.SensorType(id=1) session.add(qry) qry.name = "Delta Temperature" qry.code = "dT" qry.units = "deg.C/s"
def test_Rcv_Sensor(self): """Does Recieve work when that sensortype is not in the DB""" now = datetime.datetime.utcnow() #Create our bitmask bs = Bitset(size = Packets.SC_SIZE) #Try to setup a temperature sample bs[Packets.SC_D_TEMPERATURE] = True #Then A packet packet = StateMsg(addr = 4242) packet.set_ctp_parent_id(101) #Parent Node Id packet.set_special(0xc7) #Special Id packet.set_packed_state_mask(bs.a) packet.set_packed_state([22.5]) self.testbif.receive(packet) output = self.blogger.mainloop() self.assertTrue(output) #Check stuff gets added to the database correctly session = self.Session() #Does the sensortype exist now (Apparently its not a FK in Reading) #qry = session.query(models.SensorType).filter_by(id=1).first() #self.assertTrue(qry) #Is the name UNKNOWN #self.assertEqual(qry.Name, "UNKNOWN") #NodeState qry = session.query(models.NodeState).filter_by(nodeId = 4242) self.assertEqual(qry.count(), 1) qry = qry.first() self.assertTrue(qry) tdiff = qry.time - now self.assertLess(tdiff.seconds, 1.0) self.assertEqual(qry.parent, 101) #And Reading qry = session.query(models.Reading).filter_by(nodeId = 4242) #As we just did temperature there should only be one reading self.assertEqual(qry.count(), 1) qry = qry.first() self.assertTrue(qry) self.assertEqual(qry.typeId, 1) self.assertEqual(qry.value, 22.5) #Reset the sensor type to be as it should qry = session.query(models.SensorType).filter_by(id = 1).first() if qry is None: qry = models.SensorType(id = 1) session.add(qry) qry.name = "Delta Temperature" qry.code = "dT" qry.units = "deg.C/s"
def testrecvCombined(self): """Can we correctly recieve and packets with multiple readings""" now = datetime.datetime.utcnow() #Create our bitmask bs = Bitset(size=Packets.SC_SIZE) #Try to setup a temperature sample bs[Packets.SC_TEMPERATURE] = True bs[Packets.SC_HUMIDITY] = True bs[Packets.SC_VOLTAGE] = True #Then A packet packet = StateMsg(addr=100) packet.set_ctp_parent_id(101) #Parent Node Id packet.set_special(0xc7) #Special Id packet.set_packed_state_mask(bs.a) packet.set_packed_state([22.5, 50.0, 2.45]) self.testbif.receive(packet) output = self.blogger.mainloop() self.assertTrue(output) #And check the data has arrived session = self.Session() qry = session.query(models.NodeState) #self.assertEqual(qry.count(), 1) qry = qry.first() self.assertEqual(qry.nodeId, 100) self.assertEqual(qry.parent, 101) #Do we get the temperature reading qry = session.query(models.Reading).filter_by(typeId=0) #self.assertEqual(qry.count(), 1) qry = qry.first() self.assertEqual(qry.nodeId, 100) self.assertEqual(qry.value, 22.5) #Humidity qry = session.query(models.Reading).filter_by(typeId=2) #self.assertEqual(qry.count(), 1) qry = qry.first() self.assertEqual(qry.nodeId, 100) self.assertEqual(qry.value, 50.0) #Voltage qry = session.query(models.Reading).filter_by(typeId=6) #self.assertEqual(qry.count(), 1) qry = qry.first() self.assertEqual(qry.nodeId, 100) self.assertAlmostEqual(qry.value, 2.45)
def testDuplicateFunction(self): """Test the duplicate packet function""" #Create our bitmask bs = Bitset(size=Packets.SC_SIZE) #Try to setup a temperature sample bs[Packets.SC_TEMPERATURE] = True #Then A packet packet = StateMsg(addr=4242) packet.set_ctp_parent_id(101) #Parent Node Id packet.set_special(0xc7) #Special Id packet.set_packed_state_mask(bs.a) packet.set_packed_state([22.5]) #Store the initial packet self.testbif.receive(packet) output = self.blogger.mainloop() self.assertTrue(output) now = datetime.datetime.utcnow() #Check the duplicate function #session = self.blogger.getSession() #Duplicate packet has been renamed session = self.Session() out = BaseLogger.duplicate_packet(session=session, receipt_time=now, node_id=4242, localtime=0) #out = self.blogger.duplicate_packet(session = session, # time = now, # nodeId = 4242, # localtime = 0) self.assertTrue(out) now = now + datetime.timedelta(minutes=1) #out = self.blogger.duplicate_packet(session = session, # time = now, # nodeId = 4242, # localtime = 0) out = BaseLogger.duplicate_packet(session=session, receipt_time=now, node_id=4242, localtime=0) self.assertFalse(out)
def testrecvCombined(self): """Can we correctly recieve and packets with multiple readings""" now = datetime.datetime.utcnow() #Create our bitmask bs = Bitset(size=Packets.SC_SIZE) #Try to setup a temperature sample bs[Packets.SC_TEMPERATURE] = True bs[Packets.SC_HUMIDITY] = True bs[Packets.SC_VOLTAGE] = True #Then A packet packet = StateMsg(addr=100) packet.set_ctp_parent_id(101) #Parent Node Id packet.set_special(0xc7) #Special Id packet.set_packed_state_mask(bs.a) packet.set_packed_state([22.5, 50.0, 2.45]) self.testbif.receive(packet) output = self.blogger.mainloop() self.assertTrue(output) #And check the data has arrived session = self.Session() qry = session.query(models.NodeState) #self.assertEqual(qry.count(), 1) qry = qry.first() self.assertEqual(qry.nodeId, 100) self.assertEqual(qry.parent, 101) #Do we get the temperature reading qry = session.query(models.Reading).filter_by(typeId = 0) #self.assertEqual(qry.count(), 1) qry = qry.first() self.assertEqual(qry.nodeId, 100) self.assertEqual(qry.value, 22.5) #Humidity qry = session.query(models.Reading).filter_by(typeId = 2) #self.assertEqual(qry.count(), 1) qry = qry.first() self.assertEqual(qry.nodeId, 100) self.assertEqual(qry.value, 50.0) #Voltage qry = session.query(models.Reading).filter_by(typeId = 6) #self.assertEqual(qry.count(), 1) qry = qry.first() self.assertEqual(qry.nodeId, 100) self.assertAlmostEqual(qry.value, 2.45)
def test_Rcv_Node(self): """Does Recieve work when that node is not in the DB""" now = datetime.datetime.utcnow() #Create our bitmask bs = Bitset(size=Packets.SC_SIZE) #Try to setup a temperature sample bs[Packets.SC_TEMPERATURE] = True #Then A packet packet = StateMsg(addr=100) packet.set_ctp_parent_id(101) #Parent Node Id packet.set_special(0xc7) #Special Id packet.set_packed_state_mask(bs.a) packet.set_packed_state([22.5]) self.testbif.receive(packet) output = self.blogger.mainloop() self.assertTrue(output) #Check stuff gets added to the database correctly session = self.Session() #Has a node been added to the DB qry = session.query(models.Node).filter_by(id=100).first() self.assertTrue(qry) #NodeState qry = session.query(models.NodeState).filter_by(nodeId=100) self.assertEqual(qry.count(), 1) qry = qry.first() self.assertTrue(qry) tdiff = qry.time - now self.assertLess(tdiff.seconds, 1.0) self.assertEqual(qry.parent, 101) #And Reading qry = session.query(models.Reading).filter_by(nodeId=100) #As we just did temperature there should only be one reading self.assertEqual(qry.count(), 1) qry = qry.first() self.assertTrue(qry) self.assertEqual(qry.typeId, 0) self.assertEqual(qry.value, 22.5)
def testDuplicateFunction(self): """Test the duplicate packet function""" #Create our bitmask bs = Bitset(size=Packets.SC_SIZE) #Try to setup a temperature sample bs[Packets.SC_TEMPERATURE] = True #Then A packet packet = StateMsg(addr = 4242) packet.set_ctp_parent_id(101) #Parent Node Id packet.set_special(0xc7) #Special Id packet.set_packed_state_mask(bs.a) packet.set_packed_state([22.5]) #Store the initial packet self.testbif.receive(packet) output = self.blogger.mainloop() self.assertTrue(output) now = datetime.datetime.utcnow() #Check the duplicate function #session = self.blogger.getSession() #Duplicate packet has been renamed session = self.Session() out = BaseLogger.duplicate_packet(session = session, receipt_time = now, node_id = 4242, localtime = 0) #out = self.blogger.duplicate_packet(session = session, # time = now, # nodeId = 4242, # localtime = 0) self.assertTrue(out) now = now+datetime.timedelta(minutes=1) #out = self.blogger.duplicate_packet(session = session, # time = now, # nodeId = 4242, # localtime = 0) out = BaseLogger.duplicate_packet(session = session, receipt_time = now, node_id = 4242, localtime = 0) self.assertFalse(out)
def testRcv(self): """Test a single receive""" now = datetime.datetime.utcnow() #Create our bitmask bs = Bitset(size=Packets.SC_SIZE) #Try to setup a temperature sample bs[Packets.SC_TEMPERATURE] = True #Then A packet packet = StateMsg(addr=4242) packet.set_ctp_parent_id(101) #Parent Node Id packet.set_special(0xc7) #Special Id packet.set_packed_state_mask(bs.a) packet.set_packed_state([22.5]) self.testbif.receive(packet) output = self.blogger.mainloop() self.assertTrue(output) #Check the reading turns up session = self.Session() qry = session.query(models.Node).filter_by(id=4242).first() self.assertTrue(qry) #NodeState qry = session.query(models.NodeState).filter_by(nodeId=4242) self.assertEqual(qry.count(), 1) qry = qry.first() self.assertTrue(qry) tdiff = qry.time - now self.assertLess(tdiff.seconds, 1.0) self.assertEqual(qry.parent, 101) #And Reading qry = session.query(models.Reading).filter_by(nodeId=4242) #As we just did temperature there should only be one reading self.assertEqual(qry.count(), 1) qry = qry.first() self.assertTrue(qry) self.assertEqual(qry.typeId, 0) self.assertEqual(qry.value, 22.5)
def test_Rcv_Node(self): """Does Recieve work when that node is not in the DB""" now = datetime.datetime.utcnow() #Create our bitmask bs = Bitset(size = Packets.SC_SIZE) #Try to setup a temperature sample bs[Packets.SC_TEMPERATURE] = True #Then A packet packet = StateMsg(addr=100) packet.set_ctp_parent_id(101) #Parent Node Id packet.set_special(0xc7) #Special Id packet.set_packed_state_mask(bs.a) packet.set_packed_state([22.5]) self.testbif.receive(packet) output = self.blogger.mainloop() self.assertTrue(output) #Check stuff gets added to the database correctly session = self.Session() #Has a node been added to the DB qry = session.query(models.Node).filter_by(id = 100).first() self.assertTrue(qry) #NodeState qry = session.query(models.NodeState).filter_by(nodeId = 100) self.assertEqual(qry.count(), 1) qry = qry.first() self.assertTrue(qry) tdiff = qry.time - now self.assertLess(tdiff.seconds, 1.0) self.assertEqual(qry.parent, 101) #And Reading qry = session.query(models.Reading).filter_by(nodeId = 100) #As we just did temperature there should only be one reading self.assertEqual(qry.count(), 1) qry = qry.first() self.assertTrue(qry) self.assertEqual(qry.typeId, 0) self.assertEqual(qry.value, 22.5)
def testRcv(self): """Test a single receive""" now = datetime.datetime.utcnow() #Create our bitmask bs = Bitset(size = Packets.SC_SIZE) #Try to setup a temperature sample bs[Packets.SC_TEMPERATURE] = True #Then A packet packet = StateMsg(addr = 4242) packet.set_ctp_parent_id(101) #Parent Node Id packet.set_special(0xc7) #Special Id packet.set_packed_state_mask(bs.a) packet.set_packed_state([22.5]) self.testbif.receive(packet) output = self.blogger.mainloop() self.assertTrue(output) #Check the reading turns up session = self.Session() qry = session.query(models.Node).filter_by(id = 4242).first() self.assertTrue(qry) #NodeState qry = session.query(models.NodeState).filter_by(nodeId = 4242) self.assertEqual(qry.count(), 1) qry = qry.first() self.assertTrue(qry) tdiff = qry.time - now self.assertLess(tdiff.seconds, 1.0) self.assertEqual(qry.parent, 101) #And Reading qry = session.query(models.Reading).filter_by(nodeId = 4242) #As we just did temperature there should only be one reading self.assertEqual(qry.count(), 1) qry = qry.first() self.assertTrue(qry) self.assertEqual(qry.typeId, 0) self.assertEqual(qry.value, 22.5)
def testDuplicate(self): """What about duplicate packets""" now = datetime.datetime.utcnow() #Create our bitmask bs = Bitset(size=Packets.SC_SIZE) #Try to setup a temperature sample bs[Packets.SC_TEMPERATURE] = True #Then A packet packet = StateMsg(addr=4242) packet.set_ctp_parent_id(101) #Parent Node Id packet.set_special(0xc7) #Special Id packet.set_packed_state_mask(bs.a) packet.set_packed_state([22.5]) self.testbif.receive(packet) self.testbif.receive(packet) #Add it twice output = self.blogger.mainloop() self.assertTrue(output) output = self.blogger.mainloop() #And a duplicate self.assertFalse(output)
def testDuplicate(self): """What about duplicate packets""" now = datetime.datetime.utcnow() #Create our bitmask bs = Bitset(size=Packets.SC_SIZE) #Try to setup a temperature sample bs[Packets.SC_TEMPERATURE] = True #Then A packet packet = StateMsg(addr = 4242) packet.set_ctp_parent_id(101) #Parent Node Id packet.set_special(0xc7) #Special Id packet.set_packed_state_mask(bs.a) packet.set_packed_state([22.5]) self.testbif.receive(packet) self.testbif.receive(packet) #Add it twice output = self.blogger.mainloop() self.assertTrue(output) output = self.blogger.mainloop() #And a duplicate self.assertFalse(output)
def testFailOnSpecial(self): """Do We fail gracefully if Special is wrong""" bs = Bitset(size=Packets.SC_SIZE) #Try to setup a temperature sample bs[Packets.SC_TEMPERATURE] = True #Then A packet packet = StateMsg(addr=4242) packet.set_ctp_parent_id(101) #Parent Node Id #packet.set_special(0xc7) #Special Id packet.set_packed_state_mask(bs.a) packet.set_packed_state([22.5]) #The First thing is to see if the code passes nicely (ie no fail) #When run in the normal way self.testbif.receive(packet) output = self.blogger.mainloop() self.assertIsNone(output) #Only when we store return True with self.assertRaises(Exception): self.blogger.store_state(packet)
def testFailOnSpecial(self): """Do We fail gracefully if Special is wrong""" bs = Bitset(size=Packets.SC_SIZE) #Try to setup a temperature sample bs[Packets.SC_TEMPERATURE] = True #Then A packet packet = StateMsg(addr = 4242) packet.set_ctp_parent_id(101) #Parent Node Id #packet.set_special(0xc7) #Special Id packet.set_packed_state_mask(bs.a) packet.set_packed_state([22.5]) #The First thing is to see if the code passes nicely (ie no fail) #When run in the normal way self.testbif.receive(packet) output = self.blogger.mainloop() self.assertIsNone(output) #Only when we store return True with self.assertRaises(Exception): self.blogger.store_state(packet)
def main(): """ run all tests """ testbif = TestBif() s_msg = StateMsg(addr=22) s_msg.set_ctp_parent_id(101) s_msg.set_timestamp(307200) s_msg.set_special(0xc7) s_msg.setElement_packed_state_mask(0, 1) s_msg.setElement_packed_state_mask(1, 0) s_msg.setElement_packed_state_mask(2, 0) s_msg.setElement_packed_state(0, 25.5) testbif.receive(s_msg) # s_msg = StateV1Msg(addr=23) # s_msg.set_ctp_parent_id(101) # s_msg.set_timestamp(307200) # s_msg.set_special(0xc7) # s_msg.setElement_packed_state_mask(0, 1) # s_msg.setElement_packed_state_mask(1, 0) # s_msg.setElement_packed_state(0, 22.5) # testbif.receive(s_msg) # test what happens when you have an unknown nodetype s_msg = StateMsg(addr=(4096 * 5 + 1)) s_msg.set_ctp_parent_id(101) s_msg.set_timestamp(307200) s_msg.set_special(0xc7) s_msg.setElement_packed_state_mask(0, 1) s_msg.setElement_packed_state_mask(1, 0) s_msg.setElement_packed_state_mask(2, 0) s_msg.setElement_packed_state(0, 25.5) testbif.receive(s_msg) logging.basicConfig( #filename="/tmp/BaseLogger.log", #filemode="a", format="%(asctime)s %(levelname)s %(message)s", level=logging.DEBUG) base_logger = BaseLogger(bif=testbif, dbfile='sqlite:///testbif.db') # 'mysql://localhost/ch') base_logger.create_tables() base_logger.run()
def main(): """ run all tests """ testbif = TestBif() s_msg = StateMsg(addr=22) s_msg.set_ctp_parent_id(101) s_msg.set_timestamp(307200) s_msg.set_special(0xc7) s_msg.setElement_packed_state_mask(0, 1) s_msg.setElement_packed_state_mask(1, 0) s_msg.setElement_packed_state_mask(2, 0) s_msg.setElement_packed_state(0, 25.5) testbif.receive(s_msg) # s_msg = StateV1Msg(addr=23) # s_msg.set_ctp_parent_id(101) # s_msg.set_timestamp(307200) # s_msg.set_special(0xc7) # s_msg.setElement_packed_state_mask(0, 1) # s_msg.setElement_packed_state_mask(1, 0) # s_msg.setElement_packed_state(0, 22.5) # testbif.receive(s_msg) # test what happens when you have an unknown nodetype s_msg = StateMsg(addr=(4096*5+1)) s_msg.set_ctp_parent_id(101) s_msg.set_timestamp(307200) s_msg.set_special(0xc7) s_msg.setElement_packed_state_mask(0, 1) s_msg.setElement_packed_state_mask(1, 0) s_msg.setElement_packed_state_mask(2, 0) s_msg.setElement_packed_state(0, 25.5) testbif.receive(s_msg) logging.basicConfig(#filename="/tmp/BaseLogger.log", #filemode="a", format="%(asctime)s %(levelname)s %(message)s", level=logging.DEBUG) base_logger = BaseLogger(bif=testbif, dbfile='sqlite:///testbif.db') # 'mysql://localhost/ch') base_logger.create_tables() base_logger.run()