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 for Equality""" #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 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 __init__(self): log.debug("Initialising Push Script") engine = sqlalchemy.create_engine(LOCAL_URL) #Intialise the databse models.initialise_sql(engine) models.populate_data() #Get relevant models etc session = meta.Session() #Check if our data exists theDeployment = session.query( models.Deployment).filter_by(name="PushTest").first() if theDeployment is None: log.debug("--> Create New Deployment") #Create a new Deployment theDeployment = models.Deployment(name="PushTest") session.add(theDeployment) session.flush() log.debug("Deployment is {0}".format(theDeployment)) theHouse = session.query( models.House).filter_by(address="Push Address").first() if theHouse is None: log.debug("--> Create new House") theHouse = models.House(address="Push Address", deploymentId=theDeployment.id) session.add(theHouse) session.flush() log.debug("House is {0}".format(theHouse)) #Two Rooms roomType = session.query( models.RoomType).filter_by(name="Bedroom").first() masterBed = session.query( models.Room).filter_by(name="Master Bedroom").first() if masterBed is None: masterBed = models.Room(name="Master Bedroom", roomTypeId=roomType.id) session.add(masterBed) secondBed = session.query( models.Room).filter_by(name="Second Bedroom").first() if secondBed is None: secondBed = models.Room(name="Second Bedroom", roomTypeId=roomType.id) session.add(secondBed) session.flush() #Locations masterLoc = session.query(models.Location).filter_by( houseId=theHouse.id, roomId=masterBed.id).first() if masterLoc is None: log.debug("Create New Master Location") masterLoc = models.Location(houseId=theHouse.id, roomId=masterBed.id) session.add(masterLoc) secondLoc = session.query(models.Location).filter_by( houseId=theHouse.id, roomId=secondBed.id).first() if secondLoc is None: log.debug("Create New Second Location") secondLoc = models.Location(houseId=theHouse.id, roomId=secondBed.id) session.add(secondLoc) session.flush() log.debug("Master Location {0}".format(masterLoc)) log.debug("Second Location {0}".format(secondLoc)) #Add Nodes to each Location node37 = session.query(models.Node).filter_by(id=37).first() if not node37: node37 = models.Node(id=37) session.add(node37) node38 = session.query(models.Node).filter_by(id=38).first() if node38 is None: node38 = models.Node(id=38) session.add(node38) node37.location = masterLoc node38.location = secondLoc session.flush() #All Our Data adding script needs to worry about is the Nodes self.node37 = node37 self.node38 = node38 #Finally add an Upload URL if required # theUrl = session.query(models.UploadURL).filter_by(url="[email protected]").first() # if not theUrl: # theUrl = models.UploadURL(url="[email protected]", # dburl="mysql://*****:*****@localhost:3307/pushTest") # session.add(theUrl) # theUrl = session.query(models.UploadURL).filter_by(url="*****@*****.**").first() # if not theUrl: # theUrl = models.UploadURL(url="*****@*****.**", # dburl="mysql://*****:*****@localhost:3307/chtest") # session.add(theUrl) session.commit()
def populatedata(session = None): """Populate our testing database with an example deployment etc""" #The Deployment if not session: print "Creating a new Session" session = meta.Session() #Remove Existing nodes as they just confuse things qry = session.query(models.Node) qry.delete() session.flush() session.commit() transaction.commit() #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 want a server theserver = models.Server(id=1, hostname = "Testing Server") session.merge(theserver) session.flush() #We also add a testing house thehouse = models.House(id=1, address="testing house", deploymentId = thedeployment.id, startDate = now, serverid = 1) 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() session.commit() #update the nodes so they have the correct locations thenode = session.query(models.Node).filter_by(id=837).first() if thenode is None: print "Create Node 837" thenode = models.Node(id=837, locationId=1) session.add(thenode) thenode = session.query(models.Node).filter_by(id=838).first() if thenode is None: thenode = models.Node(id=838, locationId=2) print "Create Node 838" session.add(thenode) session.flush() transaction.commit() session.commit() #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, serverid=1) 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) session.flush() session.commit() transaction.commit() thenode = session.query(models.Node).filter_by(id=1061).first() if thenode is None: thenode = models.Node(id=1061, locationId=3) print "Create Node 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, locationId=4) print "Create Node 1063" session.add(thenode) session.flush() session.commit()
def test_sync_houses(self): # Make sure the DB is in a sensible state before we get started session = self.Session() rurl = "{0}house/".format(RESTURL) #Reset mapped houses self.pusher.mappedHouses = {} #Check that the push works first time around houses = {1: 1, 2: 2} self.pusher.sync_houses() self.assertEqual(self.pusher.mappedHouses, houses) #Next test is a simple sync between houses with matching details testHouse = models.House(id=3, deploymentId=1, address="test address") session = self.Session() session.add(testHouse) session.flush() session.commit() #Push the same house to the remote db #req = requests.post(rurl, testHouse.json()) session.close() #And see if our push updates correctly self.pusher.sync_houses() self.assertEqual(self.pusher.mappedHouses, {1: 1, 2: 2, 3: 3}) #Now map a house to a new ID in the database testHouse = models.House(id=10, deploymentId=1, address="second test") session = self.Session() session.add(testHouse) session.flush() session.commit() session.close() #And check if we update coreretly self.pusher.sync_houses() self.assertEqual(self.pusher.mappedHouses, {1: 1, 2: 2, 3: 3, 10: 4}) #We also want to make sure the deployments map properly As this would #normally be taken care of earlier in the process we need to Do a quick #sync here session = self.Session() newdeployment = models.Deployment(id=10, name="Testing Deployment") session.add(newdeployment) session.flush() session.commit() self.pusher.sync_deployments() #Check deploymets worked properly self.assertEqual(self.pusher.mappedDeployments, {1: 1, 10: 2}) testHouse = models.House(id=6, deploymentId=10, address="Mixed Test") session.add(testHouse) session.flush() session.commit() session.close() self.pusher.sync_houses() #self.fail() #return #First do we have a new house mapped. self.assertEqual(self.pusher.mappedHouses, { 1: 1, 2: 2, 3: 3, 10: 4, 6: 5 }) #return #But do we also have everything correct at the remote req = requests.get("{0}5".format(rurl)) jsn = req.json()[0] self.assertEqual(jsn["id"], 5) self.assertEqual(jsn["deploymentId"], 2) #Finally we want to ensure that no remote houses are picked up newhouse = models.House(id=8, address="Excluded House") requests.post(rurl, newhouse.json()) self.pusher.sync_houses() #So is this local session = self.Session() qry = session.query(models.House) self.assertEqual(qry.count(), 5) qry = requests.get(rurl) jsn = qry.json() self.assertEqual(len(jsn), 6)
def test_syncDeployments(self): """Does Syncronising deployments work correctly Another Bi-Directional Sync""" rurl = "{0}deployment/".format(RESTURL) session = self.Session() qry = session.query(models.Deployment) self.assertEqual(qry.count(), 1) qry = requests.get(rurl) self.assertEqual(len(qry.json()), 1) #Add a local deployment theDeployment = models.Deployment(id=2, name="Test Deployment", description="Test") session.add(theDeployment) session.flush() session.commit() session.close() self.pusher.sync_deployments() #We should now have two at each end session = self.Session() qry = session.query(models.Deployment) self.assertEqual(qry.count(), 2) qry = requests.get(rurl) self.assertEqual(len(qry.json()), 2) session.close() #Check Mappings thedict = {1: 1, 2: 2} self.assertEqual(thedict, self.pusher.mappedDeployments) #And add a remote version theDeployment = models.Deployment( id=10, name="Foobar", ) requests.post(rurl, theDeployment.json()) self.pusher.sync_deployments() #We should now have three at each end session = self.Session() qry = session.query(models.Deployment) self.assertEqual(qry.count(), 3) qry = requests.get(rurl) self.assertEqual(len(qry.json()), 3) session.close() #Check Mappings thedict = {1: 1, 2: 2, 10: 3} self.assertEqual(thedict, self.pusher.mappedDeployments) self.pusher.mappedDeployment = {}