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 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 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 getUUID(request, inHeader=False): retUUID = None if skipAuth: if 'User' in request.headers or 'user' in request.json: # skipAuth = true, so the email will be sent in plaintext userEmail = __getToken__(request, inHeader) retUUID = __getUUIDFromEmail__(userEmail) logging.debug( "skipAuth = %s, returning UUID directly from email %s" % (skipAuth, retUUID)) else: # Return a random user to make it easy to experiment without having to specify a user # TODO: Remove this if it is not actually used from get_database import get_uuid_db user_uuid = get_uuid_db().find_one()['uuid'] retUUID = user_uuid logging.debug("skipAuth = %s, returning arbitrary UUID %s" % (skipAuth, retUUID)) if Client("choice").getClientKey() is None: Client("choice").update(createKey=True) else: userToken = __getToken__(request, inHeader) retUUID = getUUIDFromToken(userToken) if retUUID is None: raise HTTPError(403, "token is valid, but no account found for user") request.params.user_uuid = retUUID return retUUID
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 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 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 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 testUpdateClient(self): client = Client("testclient") self.updateWithTestSettings(client, "emission/tests/coreTests/wrapperTests/testclient/testclient_settings_update.json") self.assertEqual(client.startDatetime, datetime(2015, 01, 13)) self.assertEqual(client.endDatetime, datetime(2015, 04, 25)) self.assertEqual(client.getDates()[0], datetime(2015, 01, 13)) self.assertEqual(client.getDates()[1], datetime(2015, 04, 25))
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 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 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 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 getSettings(self): from emission.core.wrapper.client import Client # Combine profile settings and study settings. # We currently don't have any profile settings retSettings = self.defaultSettings; studyList = self.getStudy() if len(studyList) > 0: logging.debug("To return user settings, combining %s data from %s" % (Client(studyList[0]).getSettings(), studyList[0])) retSettings = User.mergeDicts(retSettings, Client(studyList[0]).getSettings()) logging.debug("After merge retSettings = %s" % retSettings) else: logging.debug("To return user settings, using defaults") return retSettings
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 getConfirmationModeQuery(mode): from emission.core.wrapper.client import Client return { '$or': [{ 'corrected_mode': mode }, { '$and': [{ 'corrected_mode': { '$exists': False } }, { 'confirmed_mode': mode }] }, { '$and': [{ 'corrected_mode': { '$exists': False } }, { 'confirmed_mode': { '$exists': False } }] + Client.getClientConfirmedModeQueries(mode) }] }
def javascriptCallback(clientName, method): from clients.choice import choice client = Client(clientName) client_key = request.query.client_key client.callJavascriptCallback(client_key, method, request.params) return {'status': 'ok'}
def testInitClient(self): emptyClient = Client("testclient") self.assertEqual(emptyClient.clientName, "testclient") self.assertEqual(emptyClient.settings_filename, "emission/clients/testclient/settings.json") self.assertEqual(emptyClient.clientJSON, None) self.assertEqual(emptyClient.startDatetime, None) self.assertEqual(emptyClient.endDatetime, None)
def testConfirmationModeQueryManualAndAuto(self): (user, dummySection, dummyPredModeMap) = self.setupClientTest() clientSetQuery = Client(user.getFirstStudy()).clientSpecificSetters( user.uuid, dummySection, dummyPredModeMap) # Apply the change get_database.get_section_db().update({'_id': dummySection['_id']}, clientSetQuery) retrievedSection = get_database.get_section_db().find_one( {'_id': dummySection['_id']}) self.assertEqual(retrievedSection['test_auto_confirmed']['mode'], 1) get_database.get_section_db().update({'_id': dummySection['_id']}, {'$set': { 'confirmed_mode': 4 }}) retrieveByQuery = get_database.get_section_db().find( common.getConfirmationModeQuery(1)) for entry in retrieveByQuery: print entry self.assertEqual(retrieveByQuery.count(), 0) retrieveByQuery = get_database.get_section_db().find( common.getConfirmationModeQuery(4)) for entry in retrieveByQuery: print entry self.assertEqual(retrieveByQuery.count(), 1)
def testPendingClientRegsDelete(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"]) Client.deletePendingClientRegs(fakeEmail) afterDelList = Client.getPendingClientRegs(fakeEmail) self.assertEqual(afterDelList, [])
def getUserClient(user_uuid): study = User.fromUUID(user_uuid).getFirstStudy() if study != None: client = Client(study) return client else: # User is not part of any study, so no additional filtering is needed return None
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 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 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 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 getUUID(request, inHeader=False): retUUID = None if skipAuth: if 'User' in request.headers or 'user' in request.json: # skipAuth = true, so the email will be sent in plaintext userEmail = __getToken__(request, inHeader) retUUID = __getUUIDFromEmail__(userEmail) logging.debug("skipAuth = %s, returning UUID directly from email %s" % (skipAuth, retUUID)) else: logging.debug("skipAuth = %s, returning None") return None if Client("choice").getClientKey() is None: Client("choice").update(createKey = True) else: userToken = __getToken__(request, inHeader) retUUID = getUUIDFromToken(userToken) if retUUID is None: raise HTTPError(403, "token is valid, but no account found for user") request.params.user_uuid = retUUID return retUUID
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 registeredForStudy(): userEmail = request.query.email client = request.query.client client_key = request.query.client_key logging.debug("request = %s" % (request)) logging.debug("userEmail = %s, client = %s, client_key = %s" % (userEmail, client, client_key)) # try: newSignupCount = Client(client).preRegister(client_key, userEmail) # except Exception as e: # abort(e.code, e.msg) return {'email': userEmail, 'client': client, 'signup_count': newSignupCount }
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 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 testClientSpecificSettersNoOverride(self): fakeEmail = "*****@*****.**" user = User.register("*****@*****.**") self.assertEqual(user.getFirstStudy(), None) 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(user.getFirstStudy()).clientSpecificSetters(user.uuid, dummySection, dummyPredModeMap) self.assertEqual(clientSetQuery, None)
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 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 savePredictionsStep(self): from emission.core.wrapper.user import User from emission.core.wrapper.client import Client uniqueModes = sorted(set(self.cleanedResultVector)) for i in range(self.predictedProb.shape[0]): currSectionId = self.sectionIds[i] currProb = self.convertPredictedProbToMap(self.modeList, uniqueModes, self.predictedProb[i]) logging.debug("Updating probability for section with id = %s" % currSectionId) self.Sections.update({'_id': currSectionId}, {"$set": {"predicted_mode": currProb}}) currUser = User.fromUUID(self.sectionUserIds[i]) clientSpecificUpdate = Client(currUser.getFirstStudy()).clientSpecificSetters(currUser.uuid, currSectionId, currProb) if clientSpecificUpdate != None: self.Sections.update({'_id': currSectionId}, clientSpecificUpdate)
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 getConfirmationModeQuery(mode): from emission.core.wrapper.client import Client return {'$or': [{'corrected_mode': mode}, {'$and': [{'corrected_mode': {'$exists': False}}, {'confirmed_mode': mode}]}, {'$and': [{'corrected_mode': {'$exists': False}}, {'confirmed_mode': {'$exists': False}}] + Client.getClientConfirmedModeQueries(mode)}]}
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})
def testClientConfirmedModeQueries(self): queryDict = Client.getClientConfirmedModeQueries(4)[0] self.assertTrue('$or' in queryDict) queryList = queryDict['$or'] self.assertIn({'test_auto_confirmed.mode': 4}, queryList)
def register(userEmail): import uuid from datetime import datetime from emission.core.wrapper.client import Client # We are accessing three databases here: # - The list of pending registrations (people who have filled out demographic # information but not installed the app) # - The mapping from the userEmail to the user UUID # - The mapping from the UUID to other profile information about the user # The first two are indexed by the user email. We will use the same field # name in both to indicate that it is a shared key. This also allows us to # have a simple query that we can reuse. userEmailQuery = {'user_email': userEmail} # First, we construct the email -> uuid mapping and store it in the appropriate database. # At this point, we don't know or care whether the user is part of a study # We also store a create timestamp just because that's always a good idea # What happens if the user calls register() again? Do we want to generate a new UUID? # Do we want to update the create timestamp? # For now, let's assume that the answer to both of those questions is yes, # because that allows us to use upsert :) # A bonus fix is that if something is messed up in the DB, calling create again will fix it. # This is the UUID that will be stored in the trip database # in order to do some fig leaf of anonymity # Since we now generate truly anonymized UUIDs, and we expect that the # register operation is idempotent, we need to check and ensure that we don't # change the UUID if it already exists. existing_entry = get_uuid_db().find_one({"user_email": userEmail}) if existing_entry is None: anonUUID = uuid.uuid4() else: anonUUID = existing_entry['uuid'] emailUUIDObject = {'user_email': userEmail, 'uuid': anonUUID, 'update_ts': datetime.now()} writeResultMap = get_uuid_db().update(userEmailQuery, emailUUIDObject, upsert=True) # Note, if we did want the create_ts to not be overwritten, we can use the # writeResult to decide how to deal with the values # Now, we look to see if the user is part of a study. We can either store # this information in the profile database, or the mapping, or both. For now, # let us store this in the profile database since it is sufficient for it to # be associated with the UUID, we anticipate using it for customization, and # we assume that other customization stuff will be stored in the profile. # We could also assume that we will create the profile if we created the map # and update if we updated. But that has some reliability issues. For # example, what if creating the map succeeded but creating the profile # failed? Subsequently calling the method again to try and fix the profile # will continue to fail because we will be trying to update. # Much better to deal with it separately by doing a separate upsert # Second decision: what do we do if the user is not part of a study? Create a # profile anyway with an empty list, or defer the creation of the profile? # # Decision: create profile with empty list for two reasons: # a) for most of the functions, we want to use the profile data. We should # only use the email -> uuid map in the API layer to get the UUID, and use # the UUID elsewhere. So we need to have profiles for non-study participants # as well. # b) it will also make the scripts to update the profile in the background # easier to write. They won't have to query the email -> UUID database and # create the profile if it doesn't exist - they can just work off the profile # database. # TODO: Write a script that periodically goes through and identifies maps # that don't have an associated profile and fix them study_list = Client.getPendingClientRegs(userEmail) writeResultProfile = User.createProfile(anonUUID, datetime.now(), study_list) if 'err' not in writeResultProfile: # update was successful! # Either upserted or updatedExisting will be true # We can now cleanup the entry from the pending database # Note that we could also move this to a separate cleanup script because # eventual consistency is good enough for us # If there is a profile entry for a particular signup, then delete it Client.deletePendingClientRegs(userEmail) return User.fromUUID(anonUUID)
def testUpdateClient(self): client = Client("testclient") self.updateWithTestSettings(client, "emission/tests/coreTests/wrapperTests/testclient/testclient_settings_update.json")
def testIsEmptyClientActive(self): emptyClient = Client("testclient") self.assertFalse(emptyClient.isActive(datetime.now()))