def testCreate(self): #Can we Create Deployments thisDeployment = models.Deployment() self.assertIsInstance(thisDeployment, models.Deployment) thisDeployment = models.Deployment(description="Test") self.assertEqual(thisDeployment.description, "Test")
def testEq(self): #Test Equality item1 = models.Deployment(id=1, name="Test Deployment") item2 = models.Deployment(id=1, name="Test Deployment") self.assertEqual(item1, item2) self.assertReallyEqual(item1, item2) #And In Equality item2 = models.Deployment(id=1, name="Test DeploymentA") self.assertNotEqual(item1, item2) self.assertReallyNotEqual(item1, item2)
def testCmp(self): #Test Comparator item1 = models.Deployment(id=1, name="Test") item2 = models.Deployment(id=1, name="Test") self.assertEqual(item1, item2) item2 = models.Deployment(id=1, name="A_Test") self.assertGreater(item1, item2) item2 = models.Deployment(id=1, name="Z_Test") item2.id = 2 self.assertLess(item1, item2)
def _serialobj(self): """Helper Method to provde an object to serialise""" theItem = models.Deployment(id=1, name="Test", description="A Testing Deployment", startDate=NOW, endDate=NOW) return theItem
def testAdd(self): #This should not exist in the database after the tests are run session = self.session theDeployment = models.Deployment(name="testA") session.add(theDeployment) session.flush() session.commit() qry = session.query(models.Deployment) for item in qry: print item
def testJSON(self): #Does this return the expected JSON representaion item = models.Deployment( id=1, name="Test", ) basedict = { "__table__": "Deployment", "id": 1, "name": "Test", "description": None, "startDate": None, "endDate": None, } # itemdict = item.toDict() # self.assertEqual(basedict, itemdict) itemdict = item.dict() self.assertEqual(basedict, itemdict)
def populatedata(): """Populate our testing database with an example deployment etc""" LOG = logging.getLogger(__name__) LOG.setLevel(logging.DEBUG) LOG.debug("Populating Testing Data") #The Deployment session = meta.Session() #now = datetime.datetime.now() now = datetime.datetime(2013, 01, 01, 00, 00, 00) thedeployment = models.Deployment(id=1, name="testing", description="a testing deployment", startDate=now) session.merge(thedeployment) session.flush() #We also add a testing house thehouse = models.House(id=1, address="testing house", deploymentId=thedeployment.id, startDate=now) session.merge(thehouse) session.flush() #Lets add two locations thebedroom = models.Location(id=1, houseId=thehouse.id, roomId=1) thebathroom = models.Location(id=2, houseId=thehouse.id, roomId=6) session.merge(thebedroom) session.merge(thebathroom) session.flush() #update the nodes so they have the correct locations thenode = session.query(models.Node).filter_by(id=837).first() if thenode is None: thenode = models.Node(id=837) session.add(thenode) thenode.locationId = 1 thenode = session.query(models.Node).filter_by(id=838).first() if thenode is None: thenode = models.Node(id=838) session.add(thenode) thenode.locationId = 2 session.flush() transaction.commit() #Now we want to add a load of readings thetime = now # - datetime.timedelta(days = 10) endtime = now + datetime.timedelta(days=10) #print "START TIME {0}".format(starttime) transaction.commit() #To test the Yields I also want an incomplte database thehouse = models.House(id=2, address="Poor House", deploymentId=thedeployment.id, startDate=now) session.merge(thehouse) #Add a couple of locations #Lets add two locations thebedroom = models.Location(id=3, houseId=thehouse.id, roomId=1) session.merge(thebedroom) thebathroom = models.Location(id=4, houseId=thehouse.id, roomId=6) session.merge(thebathroom) #And More Nodes thenode = session.query(models.Node).filter_by(id=1061).first() if thenode is None: thenode = models.Node(id=1061) session.add(thenode) thenode.locationId = 3 thenode = session.query(models.Node).filter_by(id=1063).first() if thenode is None: thenode = models.Node(id=1063) session.add(thenode) thenode.locationId = 4 session.flush() transaction.commit() #Add Nodestates thecount = 0.0 seqnum = -1 while thetime < endtime: #Increment and roll over the sequence number seqnum += 1 if seqnum > 255: seqnum = seqnum - 255 for nid in [837, 838, 1061, 1063]: locationid = 1 if nid == 838: locationid = 2 elif nid == 1061: locationid = 3 #Sample very 10 minutes (50% Yield) if thetime.minute % 10 == 0: continue elif nid == 1063: locationid = 4 #And remove every 3rd sample if thetime.minute % 15 == 0: continue ns = models.NodeState(nodeId=nid, parent=1, time=thetime, localtime=seqnum, seq_num=seqnum) session.add(ns) reading = models.Reading( nodeId=nid, typeId=0, time=thetime, locationId=locationid, value=18.0 + (2.0 * math.sin(thecount)), ) session.add(reading) #Increment the time thetime = thetime + datetime.timedelta(minutes=5) thecount = thecount + (3.14 / 144) transaction.commit()
def populatedata(session): """Populate with some testing data""" now = datetime.datetime.utcnow() startdate = now - datetime.timedelta(days=14) cutdate = now - datetime.timedelta(days=7) enddate = now - datetime.timedelta(hours=1) #Add A Deployment thedeployment = models.Deployment(id=1, name="Testing Deployments") session.add(thedeployment) session.flush() #And three servers (However only two are actually deployed) theserver = models.Server(id=1, hostname="server1", baseid=41) session.add(theserver) theserver = models.Server(id=2, hostname="server2", baseid=42) session.add(theserver) theserver = models.Server(id=3, hostname="server3", baseid=44) session.add(theserver) session.flush() #Add some houses thehouse = models.House(id=1, address="address1", deploymentId=1, serverid=1) session.add(thehouse) thehouse = models.House(id=2, address="address2", deploymentId=1, serverid=1) session.add(thehouse) thehouse = models.House(id=3, address="address3", deploymentId=1, serverid=1) session.add(thehouse) thehouse = models.House(id=4, address="address4", deploymentId=1, serverid=2) session.add(thehouse) #House that is out of date thehouse = models.House(id=5, address="address5", deploymentId=1, serverid=2) session.add(thehouse) #House missing one node thehouse = models.House(id=6, address="address6", deploymentId=1) session.add(thehouse) #Next nodes and locations (two for each house) locidx = 1 allnodes = [] for house in range(5): for room in range(2): thisloc = models.Location(id=locidx, houseId=house + 1, roomId=room + 1) session.add(thisloc) nid = house * 10 + room thisnode = models.Node(id=nid, locationId=locidx) session.add(thisnode) allnodes.append(nid) session.flush() locidx += 1 nidx = 1000 for x in range(2): thisnode = models.Node(id=nidx) session.add(thisnode) nidx += 1 session.flush() session.commit() #STUFF FOR PUSH STATUS currenttime = startdate while currenttime <= cutdate: for server in ["server1", "server2", "server3"]: pstat = models.PushStatus(time=currenttime, hostname=server) session.add(pstat) currenttime += datetime.timedelta(hours=2) session.flush() session.commit() while currenttime <= enddate: for server in ["server1", "server2"]: pstat = models.PushStatus(time=currenttime, hostname=server) session.add(pstat) currenttime += datetime.timedelta(hours=2) #Add readings / nodestates every two hours for two weeks #But stop 3 days ago currenttime = startdate seq_num = 1 while currenttime <= cutdate: for nid in allnodes: thereading = models.Reading(time=currenttime, nodeId=nid, typeId=0, value=0) thestate = models.NodeState(time=currenttime, nodeId=nid, seq_num=seq_num) session.add(thereading) session.add(thestate) session.flush() currenttime = currenttime + datetime.timedelta(hours=2) seq_num += 1 session.flush() session.commit() logging.debug("ALL NODES {0}".format(allnodes)) #And then for all but the nodes assocated with house 1 while currenttime <= enddate: for nid in allnodes[3:]: thereading = models.Reading(time=currenttime, nodeId=nid, typeId=0, value=0) thestate = models.NodeState(time=currenttime, nodeId=nid, seq_num=seq_num) session.add(thereading) session.add(thestate) session.flush() currenttime = currenttime + datetime.timedelta(hours=2) seq_num += 1 session.flush() session.commit() #STUFF FOR PUSLE COUNT currenttime = startdate pcount = 0 qry = session.query(models.SensorType).filter_by(name="Gas Pulse Count") gas_sensor = qry.first() while currenttime <= cutdate: #Let node 10, 11 work thereading = models.Reading(time=currenttime, nodeId=40, typeId=gas_sensor.id, value=pcount) session.add(thereading) thereading = models.Reading(time=currenttime, nodeId=41, typeId=gas_sensor.id, value=pcount) session.add(thereading) pcount += 1 currenttime += datetime.timedelta(hours=2) while currenttime <= enddate: thereading = models.Reading(time=currenttime, nodeId=40, typeId=gas_sensor.id, value=pcount) session.add(thereading) thereading = models.Reading(time=currenttime, nodeId=41, typeId=gas_sensor.id, value=100) session.add(thereading) pcount += 1 currenttime += datetime.timedelta(hours=2) session.flush() session.commit()