Exemple #1
0
    def testEq(self):
        """Test for Equality"""
        item1 = models.House(
            id=1,
            deploymentId=1,
            address="10 Test Address",
            startDate=NOW,
            endDate=NOW,
        )

        item2 = models.House(
            id=1,
            deploymentId=1,
            address="10 Test Address",
            startDate=NOW,
            endDate=NOW,
        )

        self.assertEqual(item1, item2)
        self.assertReallyEqual(item1, item2)

        #Id should not make any difference
        item2.id = 5
        self.assertEqual(item1, item2)
        self.assertReallyEqual(item1, item2)
Exemple #2
0
    def testNEQ(self):

        item1 = models.House(
            id=1,
            deploymentId=1,
            address="10 Test Address",
            startDate=NOW,
            endDate=NOW,
        )

        item2 = models.House(
            id=1,
            deploymentId=1,
            address="10 Test Address",
            startDate=NOW,
            endDate=NOW,
        )

        self.assertEqual(item1, item2)

        #AGAIN,  Id and Deployment Id do not mater so much

        item2.address = "FOO"
        self.assertNotEqual(item1, item2)
        self.assertReallyNotEqual(item1, item2)

        item2.address = item1.address
        item2.startDate = datetime.datetime.utcnow()

        self.assertNotEqual(item1, item2)
        self.assertReallyNotEqual(item1, item2)
Exemple #3
0
    def testCmp(self):
        """Test Compaison function

        (actually __lt__ for Py3K Comat)"""

        item1 = models.House(
            id=1,
            deploymentId=1,
            address="10 Test Address",
            startDate=NOW,
            endDate=NOW,
        )

        item2 = models.House(
            id=1,
            deploymentId=1,
            address="10 Test Address",
            startDate=NOW,
            endDate=NOW,
        )

        self.assertEqual(item1, item2)

        #Order On Name
        item2.address = "1 Test"
        self.assertGreater(item1, item2)

        item2.address = "100 Test"
        self.assertLess(item1, item2)

        item2.address = item1.address
        item2.startDate = datetime.datetime.utcnow()
        self.assertLess(item1, item2)

        item1.startDate = datetime.datetime.utcnow()
        self.assertGreater(item1, item2)

        #Order on end-date
        item2.address = item1.address
        item2.endDate = datetime.datetime.utcnow()
        self.assertLess(item2, item1)
        self.assertGreater(item1, item2)

        #Order on start Date
        item2.endDate = item1.endDate
        item2.startDate = datetime.datetime.utcnow()
        self.assertLess(item1, item2)
        self.assertGreater(item2, item1)
Exemple #4
0
    def _serialobj(self):
        """Helper Method to provde an object to serialise"""
        # rType = models.RoomType(id=1,
        #                         name="Test Type")

        theItem = models.House(
            id=1,
            deploymentId=1,
            address="10 Test Address",
            startDate=NOW,
            endDate=NOW,
        )
        return theItem
Exemple #5
0
    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()
Exemple #6
0
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()
Exemple #7
0
    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)