class LocationHandler: def __init__(self, helperHandler): self.helperHandler = helperHandler self.locationdao = LocationDao() self.containerdao = ContainerDAO() def locationcheckandAuth(self, request, locationDao): locationDic = None keys = ["qrcode", 'email', 'auth_token'] try: locationDic = self.helperHandler.handleRequestAndAuth( request, keys) res = self.locationdao.selectByLocationQRcode( locationDic['qrcode']) self.helperHandler.falseQueryCheck(res) except Exception as e: return json.dumps({"success": False, "message": str(e)}) if "/selectLocation" in str(request): return self.selectLocation(request, locationDao, res) elif "/clearLocation" in str(request): return self.clearlocation(request, locationDao, res) elif "/deleteLocation" in str(request): return self.deleteLocation(request, locationDao, res) def selectLocation(self, request, locationDao, res): res = res[0], res[1].locationToDict() return self.helperHandler.handleResponse(res) def clearlocation(self, request, locationDao, res): res[1].lastPickup = datetime.now().strftime('%Y-%m-%d %H:%M:%S') res2 = self.locationdao.updateLocation(res[1]) return self.helperHandler.handleResponse(res2) def addLocation(self, request, locationDao): locationDic = None keys = ['location_qrcode', 'email', 'auth_token', 'description'] try: locationDic = self.helperHandler.handleRequestAndAuth( request, keys) except Exception as e: return json.dumps({"success": False, "message": str(e)}) locationDic['lastPickup'] = datetime.now().strftime( '%Y-%m-%d %H:%M:%S') location = Location() location.dictToLocation(locationDic) res = self.locationdao.insertLocation(location) return self.helperHandler.handleResponse(res) def deleteLocation(self, request, locationDao, res): res2 = locationDao.deleteLocation(res[1]) return self.helperHandler.handleResponse(res2) def allLocations(self, request, locationDao): locationDic = None keys = ['email', 'auth_token'] locations = [] try: locationDic = self.helperHandler.handleRequestAndAuth(request, keys, t="args", hasAuth=True) res = self.locationdao.selectAll() self.helperHandler.falseQueryCheck(res) BinCounts = self.containerdao.totalContainersInBins() self.helperHandler.falseQueryCheck(BinCounts) except Exception as e: return json.dumps({"success": False, "message": str(e)}) Bin = BinCounts[1] for r in res[1]: tempLoc = r.locationToDict() counter = 0 for item in Bin: if item['location_qrcode'] == tempLoc['location_qrcode']: counter = counter + 1 tempLoc.update({'count': counter}) locations.append(tempLoc) res = res[0], locations return self.helperHandler.handleResponse(res) def updateLocation(self, request, locationDao): locationDic = None keys = ['qrcode', 'email', 'auth_token', 'description'] try: locationDic = self.helperHandler.handleRequestAndAuth( request, keys) res = self.locationdao.selectByLocationQRcode( locationDic['qrcode']) self.helperHandler.falseQueryCheck(res) except Exception as e: return json.dumps({"success": False, "message": str(e)}) res[1].description = locationDic['description'] res2 = self.locationdao.updateLocation(res[1]) return self.helperHandler.handleResponse(res2)
class unitTestLocationDAO(unittest.TestCase): """ Test the locationDAO class methods using the unit test framework. To run these tests: python3 -m unittest unitTestLocationDAO.py """ def setUp(self): """ Setup a temporary database """ def tearDown(self): """ Delete the temporary database """ self.dao = LocationDao() loc = Location("L042", "Drop-off bin outside Testing Center", "2021-01-01 01:01:01") # loc is location object self.dao.deleteLocation(loc) # test creating location def testInsertLocationSmoke(self): loc = Location("L042", "Drop-off bin outside Testing Center", "2021-01-01 01:01:01") self.dao = LocationDao() return self.dao.insertLocation(loc) def testInsertLocation(self): """ Test that we can add a location that doesn't exist in the database """ rc, msg = self.testInsertLocationSmoke() self.assertTrue(rc) def testUpdateLocation(self): rc, msg = self.testInsertLocationSmoke() self.assertTrue(rc) loc = Location("L042", "Drop-off bin outside New Hall", "2021-02-02 01:01:01") rc, msg = self.dao.updateLocation(loc) self.assertTrue(rc) rc, msg = self.dao.selectByLocationQRcode("L042") self.assertTrue(rc) self.assertTrue( str(msg.lastPickup) == "2021-02-02 01:01:01" and str(msg.description) == "Drop-off bin outside New Hall") def testInsertLocationTwice(self): """ Test that we can't add a location twice First add should work correctly """ rc, msg = self.testInsertLocationSmoke() self.assertTrue(rc) """ Second add should fail """ rc, msg = self.testInsertLocationSmoke() self.assertFalse(rc) self.assertEqual(msg, "Duplicate Entry") def testInsertLocationNoneType(self): loc = Location(None, "Drop-off bin outside Testing Center", "2021-01-01 01:01:01", 0) self.dao = LocationDao() rc, insertLocation = self.dao.insertLocation(loc) self.assertFalse(rc) loc = Location("L043", None, "2021-01-01 01:01:01") rc, insertLocation = self.dao.insertLocation(loc) self.assertFalse(rc) loc = Location("L043", "Drop-off bin outside Testing Center", None) rc, insertLocation = self.dao.insertLocation(loc) self.assertFalse(rc) def testselectByLocationQRcode(self): """ Test that we can select a location that exists in the database already """ rc, msg = self.testInsertLocationSmoke() self.assertTrue(rc) qrcode = "L042" self.dao = LocationDao() rc, selectByLocationQRcode = self.dao.selectByLocationQRcode(qrcode) self.assertTrue(rc) self.assertEqual(qrcode, selectByLocationQRcode.location_qrcode) def testselectByLocationQRcodeDoesntExist(self): """ Test that we can't select a location that doesnt exist in the database already """ rc, msg = self.testInsertLocationSmoke() self.assertTrue(rc) qrcode = "L043" self.dao = LocationDao() rc, selectByLocationQRcode = self.dao.selectByLocationQRcode(qrcode) self.assertFalse(rc) rc, selectByLocationQRcode = self.dao.selectByLocationQRcode(None) self.assertFalse(rc) def testDeleteLocation(self): """ Test that we can delete a location from the database """ rc, msg = self.testInsertLocationSmoke() self.assertTrue(rc) loc = Location("L042", "Drop-off bin outside Testing Center", "2021-01-01 01:01:01") self.dao = LocationDao() rc, deleteLocation = self.dao.deleteLocation(loc) self.assertTrue(rc) rc, selectLocation = self.dao.selectByLocationQRcode("L042") self.assertFalse(rc) def testDeleteLocationDoesntExist(self): """ Test that we can't delete a location that doesn't exist """ rc, msg = self.testInsertLocationSmoke() self.assertTrue(rc) loc = Location("L043", "Drop-off bin outside Roche Commons", "2021-01-01 01:01:01") self.dao = LocationDao() rc, deleteLocation = self.dao.deleteLocation(loc) self.assertFalse(rc) rc, deleteLocation = self.dao.deleteLocation(None) self.assertFalse(rc) def testInsertLocationQRCodeTooLong(self): """ Test that we cannot add a location_qrcode that is over 45 characters long """ loc = Location("L043xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "Drop-off bin outside Roche Commons", "2021-01-01 01:01:01") self.dao = LocationDao() rc, insertLocation = self.dao.insertLocation(loc) self.assertFalse(rc) def testInsertDescriptionTooLong(self): """ Test that we cannot add a description that is over 128 characters long """ loc = Location( "L042", "Drop-off bin outside Roche Commons xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "2021-01-01 01:01:01") self.dao = LocationDao() rc, insertLocation = self.dao.insertLocation(loc) self.assertFalse(rc) def testInsertWrongDate(self): """ Test that we cannot add a incorrectly formatted date """ loc = Location("L042", "Drop-off bin outside Roche Commons", "01:01:01 2021-01-01") self.dao = LocationDao() rc, insertLocation = self.dao.insertLocation(loc) self.assertFalse(rc)