def setupClientTest(self): # At this point, the more important test is to execute the query and see # how well it works fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey=False) emission.tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) studyList = Client.getPendingClientRegs(fakeEmail) self.assertEqual(studyList, ["testclient"]) user = User.register("*****@*****.**") self.assertEqual(user.getFirstStudy(), 'testclient') dummyPredModeMap = {'walking': 1.0} dummySection = emission.tests.common.createDummySection( startTime=datetime.now() - timedelta(seconds=60 * 60), endTime=datetime.now(), startLoc=[-122, 34], endLoc=[-122, 35], predictedMode=dummyPredModeMap) return (user, dummySection, dummyPredModeMap)
def testPreRegisterNewUser(self): client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 1) self.assertEqual(resultReg, 0)
def setupClientTest(self): # At this point, the more important test is to execute the query and see # how well it works fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) emission.tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) studyList = Client.getPendingClientRegs(fakeEmail) self.assertEqual(studyList, ["testclient"]) user = User.register("*****@*****.**") self.assertEqual(user.getFirstStudy(), 'testclient') dummyPredModeMap = {'walking': 1.0} dummySection = emission.tests.common.createDummySection( startTime = datetime.now() - timedelta(seconds = 60 * 60), endTime = datetime.now(), startLoc = [-122, 34], endLoc = [-122, 35], predictedMode = dummyPredModeMap) return (user, dummySection, dummyPredModeMap)
def testQueryUnclassifiedSectionsLowConfidence(self): fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) emission.tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail(fakeEmail) self.assertEqual(user.getFirstStudy(), 'testclient') queriedUnclassifiedSections = tripManager.queryUnclassifiedSections(User.fromEmail(fakeEmail).uuid) self.assertEqual(queriedUnclassifiedSections.count(), 2) # Set the auto_confirmed values for the trips for section in queriedUnclassifiedSections: print section['_id'] self.SectionsColl.update({'_id': section['_id']}, {'test_auto_confirmed': {'mode': section['mode'], 'prob': 0.95}}) # Now, set the update timestamp to two weeks ago so that we will start filtering emission.tests.common.updateUserCreateTime(user.uuid) queriedUnclassifiedSections = tripManager.queryUnclassifiedSections(User.fromEmail(fakeEmail).uuid) self.assertEqual(queriedUnclassifiedSections.count(), 0)
def testSavePredictionsStepWithClient(self): from emission.core.wrapper.user import User fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) etc.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail(fakeEmail) self.assertEqual(user.getFirstStudy(), 'testclient') self.testPredictedProb() self.pipeline.savePredictionsStep() # Confirm that the predictions are saved correctly test_id_1_sec = self.SectionsColl.find_one({'_id': 'test_id_1'}) self.assertIsNotNone(test_id_1_sec['predicted_mode']) self.assertEquals(test_id_1_sec['predicted_mode'], {'walking': 1}) self.assertEquals(test_id_1_sec['test_auto_confirmed'], {'mode': 1, 'prob': 1.0}) test_id_2_sec = self.SectionsColl.find_one({'_id': 'test_id_2'}) self.assertIsNotNone(test_id_2_sec['predicted_mode']) self.assertEquals(test_id_2_sec['predicted_mode'], {'bus': 1}) self.assertEquals(test_id_2_sec['test_auto_confirmed'], {'mode': 5, 'prob': 1.0}) # Let's make sure that we didn't accidentally mess up other fields self.assertIsNotNone(test_id_1_sec['distance']) self.assertIsNotNone(test_id_2_sec['trip_id'])
def testClientSpecificSettersWithOverride(self): fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) studyList = Client.getPendingClientRegs(fakeEmail) self.assertEqual(studyList, ["testclient"]) user = User.register("*****@*****.**") self.assertEqual(user.getFirstStudy(), 'testclient') dummyPredModeMap = {'walking': 1.0} dummySection = common.createDummySection(startTime = datetime.now() - timedelta(seconds = 60 * 60), endTime = datetime.now(), startLoc = [-122, 34], endLoc = [-122, 35], predictedMode = dummyPredModeMap) clientSetQuery = client.clientSpecificSetters(user.uuid, dummySection['_id'], dummyPredModeMap) self.assertEqual(clientSetQuery, {'$set': {'test_auto_confirmed': {'mode': 1, 'prob': 1.0}}}) # Apply the change get_section_db().update({'_id': dummySection['_id']}, clientSetQuery) retrievedSection = get_section_db().find_one({'_id': dummySection['_id']}) self.assertEqual(retrievedSection['test_auto_confirmed']['mode'], 1)
def testClientSpecificPrecompute(self): for email in self.testUsers: currUser = User.fromEmail(email) self.assertEqual(currUser.getProfile().get("testfield1"), None) self.assertEqual(currUser.getProfile().get("testfield2"), None) self.assertEqual(data.getCarbonFootprint(currUser), None) fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) emission.tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) user = User.fromEmail(fakeEmail) self.assertEqual(user.getFirstStudy(), 'testclient') self.pr.precomputeResults() self.assertEqual(user.getProfile()['testfield1'], 'value1') self.assertEqual(user.getProfile()['testfield2'], 'value2') for email in self.testUsers: if email != fakeEmail: currUser = User.fromEmail(email) carbonFootprint = data.getCarbonFootprint(currUser) self.assertEqual(len(carbonFootprint), 12)
def testSavePredictionsStepWithClient(self): from emission.core.wrapper.user import User fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) emission.tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail(fakeEmail) self.assertEqual(user.getFirstStudy(), 'testclient') self.testPredictedProb() self.pipeline.savePredictionsStep() # Confirm that the predictions are saved correctly test_id_1_sec = self.SectionsColl.find_one({'_id': 'test_id_1'}) self.assertIsNotNone(test_id_1_sec['predicted_mode']) self.assertEquals(test_id_1_sec['predicted_mode'], {'walking': 1}) self.assertEquals(test_id_1_sec['test_auto_confirmed'], {'mode': 1, 'prob': 1.0}) test_id_2_sec = self.SectionsColl.find_one({'_id': 'test_id_2'}) self.assertIsNotNone(test_id_2_sec['predicted_mode']) self.assertEquals(test_id_2_sec['predicted_mode'], {'bus': 1}) self.assertEquals(test_id_2_sec['test_auto_confirmed'], {'mode': 5, 'prob': 1.0}) # Let's make sure that we didn't accidentally mess up other fields self.assertIsNotNone(test_id_1_sec['distance']) self.assertIsNotNone(test_id_2_sec['trip_id'])
def testClientSpecificPrecompute(self): for email in self.testUsers: currUser = User.fromEmail(email) self.assertEqual(currUser.getProfile().get("testfield1"), None) self.assertEqual(currUser.getProfile().get("testfield2"), None) self.assertEqual(data.getCarbonFootprint(currUser), None) fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey=False) emission.tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) user = User.fromEmail(fakeEmail) self.assertEqual(user.getFirstStudy(), 'testclient') self.pr.precomputeResults() self.assertEqual(user.getProfile()['testfield1'], 'value1') self.assertEqual(user.getProfile()['testfield2'], 'value2') for email in self.testUsers: if email != fakeEmail: currUser = User.fromEmail(email) carbonFootprint = data.getCarbonFootprint(currUser) self.assertEqual(len(carbonFootprint), 12)
def testPreRegisterFail(self): client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 1) self.assertEqual(resultReg, 0) pendingRegs = Client.getPendingClientRegs("*****@*****.**") self.assertEqual(pendingRegs, ["testclient"])
def testCountForStudy(self): client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 1) self.assertEqual(resultReg, 0) user = User.register('*****@*****.**') self.assertEquals(userclient.countForStudy('testclient'), 1)
def testGetSettingsCustomUser(self): client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 1) self.assertEqual(resultReg, 0) user = User.register('*****@*****.**') self.assertRegexpMatches(user.getSettings()['result_url'], ".*/test/test/test")
def testRegisterStudyUser(self): client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 1) self.assertEqual(resultReg, 0) user = User.register('*****@*****.**') self.assertEquals(user.getStudy(), ['testclient'])
def testCreateClient(self): client = Client("testclient") client.update(createKey = False) # Reset the times in the client so that it will show as active and we will # get a valid set of settings common.makeValid(client) self.assertNotEqual(client.getSettings(), None) self.assertNotEqual(client.getSettings(), {}) print(client.getSettings()) self.assertNotEqual(client.getSettings()['result_url'], None)
def testPendingClientRegs(self): fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) studyList = Client.getPendingClientRegs(fakeEmail) self.assertEqual(studyList, ["testclient"]) brokenStudyList = Client.getPendingClientRegs("*****@*****.**") self.assertEqual(brokenStudyList, [])
def testPreRegisterExistingUser(self): user = User.register("*****@*****.**") client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) self.assertEqual(user.getStudy(), ['testclient']) pendingRegs = Client.getPendingClientRegs("*****@*****.**") self.assertEqual(pendingRegs, [])
def testGetFirstStudy(self): user = User.register('*****@*****.**') self.assertTrue(User.isRegistered('*****@*****.**')) client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail('*****@*****.**') self.assertEqual(user.getFirstStudy(), 'testclient')
def setupUserAndClient(self): # At this point, the more important test is to execute the query and see # how well it works fakeEmail = "*****@*****.**" client = Client("gamified") client.update(createKey = False) etc.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) studyList = Client.getPendingClientRegs(fakeEmail) self.assertEqual(studyList, ["gamified"]) user = User.register("*****@*****.**") self.assertEqual(user.getFirstStudy(), 'gamified') self.user = user
def testCreateClient(self): client = Client("testclient") client.update(createKey = False) self.assertEqual(client.startDatetime, datetime(2014, 10, 13)) self.assertEqual(client.endDatetime, datetime(2016, 12, 25)) self.assertEqual(client.isActive(datetime(2014, 11, 7)), True) self.assertEqual(client.getDates()[0], datetime(2014, 10, 13)) self.assertEqual(client.getDates()[1], datetime(2016, 12, 25)) # Reset the times in the client so that it will show as active and we will # get a valid set of settings common.makeValid(client) self.assertEqual(client.isActive(datetime.now()), True) self.assertNotEqual(client.getSettings(), None) self.assertNotEqual(client.getSettings(), {}) print client.getSettings() self.assertNotEqual(client.getSettings()['result_url'], None)
def testGetSectionFilter(self): fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) studyList = Client.getPendingClientRegs(fakeEmail) self.assertEqual(studyList, ["testclient"]) user = User.register("*****@*****.**") self.assertEqual(user.getFirstStudy(), 'testclient') self.assertEqual(client.getSectionFilter(user.uuid), []) # Now, set the update timestamp to two weeks ago common.updateUserCreateTime(user.uuid) self.assertEqual(client.getSectionFilter(user.uuid), [{'test_auto_confirmed.prob': {'$lt': 0.9}}])
def testRegisterExistingUser(self): user = User.register('*****@*****.**') self.assertEquals(user.getStudy(), []) client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail("*****@*****.**") self.assertEquals(user.getStudy(), ['testclient']) # Here's the key difference, now register again user = User.register('*****@*****.**') self.assertEquals(user.getStudy(), ['testclient'])
def setupUserAndClient(self): # At this point, the more important test is to execute the query and see # how well it works fakeEmail = "*****@*****.**" client = Client("gamified") client.update(createKey=False) emission.tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) studyList = Client.getPendingClientRegs(fakeEmail) self.assertEqual(studyList, ["gamified"]) user = User.register("*****@*****.**") self.assertEqual(user.getFirstStudy(), 'gamified') self.user = user
def testGetUnclassifiedSectionsFiltered(self): """ Tests that queryUnclassifiedSections never returns a section with section['retained'] == False. A section is only returned if section['retained'] == True and all other query conditions are met """ # Clear previous Stage_Sections data and load new data # specific to filtering self.SectionsColl.remove() emission.tests.common.loadTable(self.serverName, "Stage_Sections", "emission/tests/data/testFilterFile") emission.tests.common.updateSections(self) # Extra updates to Sections necessary for testing filtering for section in self.SectionsColl.find(): section['section_start_point'] = "filler start point" section['section_end_point'] = "filler end point" self.SectionsColl.save(section) fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey=False) emission.tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail(fakeEmail) self.assertEqual(user.getFirstStudy(), 'testclient') unclassifiedSections = tripManager.getUnclassifiedSections( User.fromEmail(fakeEmail).uuid)['sections'] # Check that of the valid sections in the testFilterFile (2/3), only one of them is returned by the query self.assertEqual(len(unclassifiedSections), 1) # Check that the second entry in the testFilterFile is the only section # that is loaded into the database self.assertEqual('20140401T095742-0700', unclassifiedSections[0]['trip_id'])
def testQueryUnclassifiedSectionsLowConfidence(self): fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey=False) emission.tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail(fakeEmail) self.assertEqual(user.getFirstStudy(), 'testclient') queriedUnclassifiedSections = tripManager.queryUnclassifiedSections( User.fromEmail(fakeEmail).uuid) self.assertEqual(queriedUnclassifiedSections.count(), 2) # Set the auto_confirmed values for the trips for section in queriedUnclassifiedSections: print section['_id'] self.SectionsColl.update({'_id': section['_id']}, { 'test_auto_confirmed': { 'mode': section['mode'], 'prob': 0.95 } }) # Now, set the update timestamp to two weeks ago so that we will start filtering emission.tests.common.updateUserCreateTime(user.uuid) queriedUnclassifiedSections = tripManager.queryUnclassifiedSections( User.fromEmail(fakeEmail).uuid) self.assertEqual(queriedUnclassifiedSections.count(), 0)
def testGetUnclassifiedSectionsFiltered(self): """ Tests that queryUnclassifiedSections never returns a section with section['retained'] == False. A section is only returned if section['retained'] == True and all other query conditions are met """ # Clear previous Stage_Sections data and load new data # specific to filtering self.SectionsColl.remove() emission.tests.common.loadTable(self.serverName, "Stage_Sections", "emission/tests/data/testFilterFile") emission.tests.common.updateSections(self) # Extra updates to Sections necessary for testing filtering for section in self.SectionsColl.find(): section['section_start_point'] = "filler start point" section['section_end_point'] = "filler end point" self.SectionsColl.save(section) fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) emission.tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail(fakeEmail) self.assertEqual(user.getFirstStudy(), 'testclient') unclassifiedSections = tripManager.getUnclassifiedSections(User.fromEmail(fakeEmail).uuid)['sections'] # Check that of the valid sections in the testFilterFile (2/3), only one of them is returned by the query self.assertEqual(len(unclassifiedSections), 1) # Check that the second entry in the testFilterFile is the only section # that is loaded into the database self.assertEqual('20140401T095742-0700',unclassifiedSections[0]['trip_id'])
def testClientConfirmedModeQuery(self): client = Client("testclient") client.update(createKey = False) common.makeValid(client) self.assertEqual(client.getClientConfirmedModeQuery(4), {'test_auto_confirmed.mode': 4})
def testCallMethod(self): client = Client("testclient") client.update(createKey = False) result = client.callMethod("classifiedCount", {'user': '******'}) self.assertEqual(result, {'count': 0})