def setUp(self): self.testUsers = ["*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**"] self.serverName = 'localhost' # Sometimes, we may have entries left behind in the database if one of the tests failed # or threw an exception, so let us start by cleaning up all entries tests.common.dropAllCollections(get_db()) self.ModesColl = get_mode_db() # self.ModesColl.remove() self.assertEquals(self.ModesColl.find().count(), 0) self.SectionsColl = get_section_db() # self.SectionsColl.remove() self.assertEquals(self.SectionsColl.find().count(), 0) load_database_json.loadTable(self.serverName, "Stage_Modes", "tests/data/modes.json") load_database_json.loadTable(self.serverName, "Stage_Sections", "tests/data/testCarbonFile") # Let's make sure that the users are registered so that they have profiles for userEmail in self.testUsers: User.register(userEmail) self.walkExpect = 1057.2524056424411 self.busExpect = 2162.668467546699 self.busCarbon = 267.0/1609 self.now = datetime.now() self.dayago = self.now - timedelta(days=1) self.weekago = self.now - timedelta(weeks = 1) tests.common.updateSections(self)
def setUp(self): self.testUsers = [ "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**" ] self.serverName = 'localhost' # Sometimes, we may have entries left behind in the database if one of the tests failed # or threw an exception, so let us start by cleaning up all entries tests.common.dropAllCollections(get_db()) self.ModesColl = get_mode_db() # self.ModesColl.remove() self.assertEquals(self.ModesColl.find().count(), 0) self.SectionsColl = get_section_db() # self.SectionsColl.remove() self.assertEquals(self.SectionsColl.find().count(), 0) load_database_json.loadTable(self.serverName, "Stage_Modes", "tests/data/modes.json") load_database_json.loadTable(self.serverName, "Stage_Sections", "tests/data/testCarbonFile") # Let's make sure that the users are registered so that they have profiles for userEmail in self.testUsers: User.register(userEmail) self.walkExpect = 1057.2524056424411 self.busExpect = 2162.668467546699 self.busCarbon = 267.0 / 1609 self.now = datetime.now() self.dayago = self.now - timedelta(days=1) self.weekago = self.now - timedelta(weeks=1) tests.common.updateSections(self)
def testRunBackgroundTasksForDay(self): self.testUsers = [ "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**" ] load_database_json.loadTable(self.serverName, "Stage_Modes", "tests/data/modes.json") load_database_json.loadTable(self.serverName, "Stage_Sections", "tests/data/testCarbonFile") # Let's make sure that the users are registered so that they have profiles for userEmail in self.testUsers: User.register(userEmail) self.SectionsColl = get_section_db() tests.common.updateSections(self) self.assertNotEqual(len(self.uuid_list), 0) # Can access the zeroth element because we know that then length is greater than zero # (see above) test_uuid = self.uuid_list[0] test_user = User.fromUUID(test_uuid) self.assertNotIn('carbon_footprint', test_user.getProfile().keys()) default.runBackgroundTasks(test_user.uuid) self.assertIn('carbon_footprint', test_user.getProfile().keys())
def setUp(self): self.testUsers = [ "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**" ] self.serverName = 'localhost' # Sometimes, we may have entries left behind in the database if one of the tests failed # or threw an exception, so let us start by cleaning up all entries tests.common.dropAllCollections(get_db()) self.ModesColl = get_mode_db() self.assertEquals(self.ModesColl.find().count(), 0) self.SectionsColl = get_section_db() self.assertEquals(self.SectionsColl.find().count(), 0) load_database_json.loadTable(self.serverName, "Stage_Modes", "tests/data/modes.json") load_database_json.loadTable(self.serverName, "Stage_Sections", "tests/data/testModeInferFile") # Let's make sure that the users are registered so that they have profiles for userEmail in self.testUsers: User.register(userEmail) self.now = datetime.now() self.dayago = self.now - timedelta(days=1) self.weekago = self.now - timedelta(weeks=1) for section in self.SectionsColl.find(): section['section_start_datetime'] = self.dayago section['section_end_datetime'] = self.dayago + timedelta(hours=1) if (section['confirmed_mode'] == 5): # We only cluster bus and train trips # And our test data only has bus trips section['section_start_point'] = { u'type': u'Point', u'coordinates': [-122.270039042, 37.8800285728] } section['section_end_point'] = { u'type': u'Point', u'coordinates': [-122.2690412952, 37.8739578595] } # print("Section start = %s, section end = %s" % # (section['section_start_datetime'], section['section_end_datetime'])) # Replace the user email with the UUID section['user_id'] = User.fromEmail(section['user_id']).uuid self.SectionsColl.save(section) self.pipeline = pipeline.ModeInferencePipeline() self.testLoadTrainingData()
def testChangeUpdateTs(self): from datetime import datetime, timedelta user = User.register('*****@*****.**') self.assertTrue(User.isRegistered('*****@*****.**')) user.changeUpdateTs(timedelta(days=-20)) self.assertEqual((datetime.now() - user.getUpdateTS()).days, 20)
def testUnsetStudyExists(self): user = User.register('*****@*****.**') user.setStudy('testclient') self.assertEquals(userclient.countForStudy('testclient'), 1) user.unsetStudy('testclient') self.assertEquals(userclient.countForStudy('testclient'), 0)
def setUp(self): import tests.common # Sometimes, we may have entries left behind in the database if one of the tests failed # or threw an exception, so let us start by cleaning up all entries tests.common.dropAllCollections(get_db()) user = User.register("*****@*****.**") self.uuid = user.uuid
def testClientSpecificSettersWithOverride(self): from dao.user import User 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 testChangeUpdateTs(self): from datetime import datetime, timedelta user = User.register('*****@*****.**') self.assertTrue(User.isRegistered('*****@*****.**')) user.changeUpdateTs(timedelta(days = -20)) self.assertEqual((datetime.now() - user.getUpdateTS()).days, 20)
def setupClientTest(self): # At this point, the more important test is to execute the query and see # how well it works from dao.user import User from dao.client import Client import tests.common from get_database import get_section_db fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) 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 = 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 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 setUp(self): # Sometimes, we may have entries left behind in the database if one of the tests failed # or threw an exception, so let us start by cleaning up all entries tests.common.dropAllCollections(get_db()) user = User.register("*****@*****.**") self.uuid = user.uuid self.serverName = "localhost" self.now = datetime.now() self.dayago = self.now - timedelta(days=1) self.weekago = self.now - timedelta(weeks = 1)
def createUserProfile(): logging.debug("Called createUserProfile") userToken = request.json['user'] # This is the only place we should use the email, since we may not have a # UUID yet. All others should only use the UUID. userEmail = verifyUserToken(userToken) logging.debug("userEmail = %s" % userEmail) user = User.register(userEmail) logging.debug("Looked up user = %s" % user) logging.debug("Returning result %s" % {'uuid': str(user.uuid)}) return {'uuid': str(user.uuid)}
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 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 setUp(self): self.testUsers = ["*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**"] self.serverName = 'localhost' # Sometimes, we may have entries left behind in the database if one of the tests failed # or threw an exception, so let us start by cleaning up all entries tests.common.dropAllCollections(get_db()) self.ModesColl = get_mode_db() # self.ModesColl.remove() self.assertEquals(self.ModesColl.find().count(), 0) self.SectionsColl = get_section_db() # self.SectionsColl.remove() self.assertEquals(self.SectionsColl.find().count(), 0) load_database_json.loadTable(self.serverName, "Stage_Modes", "tests/data/modes.json") load_database_json.loadTable(self.serverName, "Stage_Sections", "tests/data/testCarbonFile") # Let's make sure that the users are registered so that they have profiles for userEmail in self.testUsers: User.register(userEmail) self.walkExpect = 1057.2524056424411 self.busExpect = 2162.668467546699 self.busCarbon = 267.0/1609 self.now = datetime.now() self.dayago = self.now - timedelta(days=1) self.weekago = self.now - timedelta(weeks = 1) for section in self.SectionsColl.find(): section['section_start_datetime'] = self.dayago section['section_end_datetime'] = self.dayago + timedelta(hours = 1) section['predicted_mode'] = [0, 0.4, 0.6, 0] section['confirmed_mode'] = '' # print("Section start = %s, section end = %s" % # (section['section_start_datetime'], section['section_end_datetime'])) # Replace the user email with the UUID section['user_id'] = User.fromEmail(section['user_id']).uuid self.SectionsColl.save(section)
def testRunBackgroundTasksForDay(self): self.testUsers = ["*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**"] load_database_json.loadTable(self.serverName, "Stage_Modes", "tests/data/modes.json") load_database_json.loadTable(self.serverName, "Stage_Sections", "tests/data/testCarbonFile") # Let's make sure that the users are registered so that they have profiles for userEmail in self.testUsers: User.register(userEmail) self.SectionsColl = get_section_db() tests.common.updateSections(self) self.assertNotEqual(len(self.uuid_list), 0) # Can access the zeroth element because we know that then length is greater than zero # (see above) test_uuid = self.uuid_list[0] test_user = User.fromUUID(test_uuid) self.assertNotIn('carbon_footprint', test_user.getProfile().keys()) data.runBackgroundTasks(test_user.uuid) self.assertIn('carbon_footprint', test_user.getProfile().keys())
def setUp(self): self.testUsers = ["*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**"] self.serverName = 'localhost' # Sometimes, we may have entries left behind in the database if one of the tests failed # or threw an exception, so let us start by cleaning up all entries tests.common.dropAllCollections(get_db()) self.ModesColl = get_mode_db() self.assertEquals(self.ModesColl.find().count(), 0) self.SectionsColl = get_section_db() self.assertEquals(self.SectionsColl.find().count(), 0) load_database_json.loadTable(self.serverName, "Stage_Modes", "tests/data/modes.json") load_database_json.loadTable(self.serverName, "Stage_Sections", "tests/data/testModeInferFile") # Let's make sure that the users are registered so that they have profiles for userEmail in self.testUsers: User.register(userEmail) self.now = datetime.now() self.dayago = self.now - timedelta(days=1) self.weekago = self.now - timedelta(weeks = 1) for section in self.SectionsColl.find(): section['section_start_datetime'] = self.dayago section['section_end_datetime'] = self.dayago + timedelta(hours = 1) if (section['confirmed_mode'] == 5): # We only cluster bus and train trips # And our test data only has bus trips section['section_start_point'] = {u'type': u'Point', u'coordinates': [-122.270039042, 37.8800285728]} section['section_end_point'] = {u'type': u'Point', u'coordinates': [-122.2690412952, 37.8739578595]} # print("Section start = %s, section end = %s" % # (section['section_start_datetime'], section['section_end_datetime'])) # Replace the user email with the UUID section['user_id'] = User.fromEmail(section['user_id']).uuid self.SectionsColl.save(section) self.pr = precompute_results.PrecomputeResults()
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 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 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 testSetClientSpecificFields(self): user = User.register('*****@*****.**') self.assertTrue(User.isRegistered('*****@*****.**')) # Check that the field doesn't exist initially self.assertTrue(user.getProfile().get('test_field', 'blank'), 'blank') # Check that a simple value update works user.setClientSpecificProfileFields({'test_field': 'something beautiful'}) self.assertTrue(user.getProfile().get('test_field', 'blank'), 'something beautiful') # Check that a data structure update works user.setClientSpecificProfileFields({'test_field': {'something': 'beautiful'}}) self.assertTrue(user.getProfile().get('test_field', 'blank'), {'something': 'beautiful'})
def createUserProfile(): logging.debug("Called createUserProfile") userToken = request.json['user'] # This is the only place we should use the email, since we may not have a # UUID yet. All others should only use the UUID. if skipAuth: userEmail = userToken else: userEmail = verifyUserToken(userToken) logging.debug("userEmail = %s" % userEmail) user = User.register(userEmail) logging.debug("Looked up user = %s" % user) logging.debug("Returning result %s" % {'uuid': str(user.uuid)}) return {'uuid': str(user.uuid)}
def testPreRegisterExistingUser(self): from dao.user import User 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 testClientSpecificSettersNoOverride(self): from dao.user import User 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 testGetCarbonFootprintForMode(self): user = User.register('*****@*****.**') user.setMpgArray([45, 50, 31]) # Avg MPG = 42 correctCarbonFootprintForMode = {'walking' : 0, 'running' : 0, 'cycling' : 0, 'mixed' : 0, 'bus_short' : 267.0/1609, 'bus_long' : 267.0/1609, 'train_short' : 92.0/1609, 'train_long' : 92.0/1609, 'car_short' : (1/(42*1.6093))*8.91, 'car_long' : (1/(42*1.6093))*8.91, 'air_short' : 217.0/1609, 'air_long' : 217.0/1609 } self.assertEquals(user.getCarbonFootprintForMode(), correctCarbonFootprintForMode)
def testSetClientSpecificFields(self): user = User.register('*****@*****.**') self.assertTrue(User.isRegistered('*****@*****.**')) # Check that the field doesn't exist initially self.assertTrue(user.getProfile().get('test_field', 'blank'), 'blank') # Check that a simple value update works user.setClientSpecificProfileFields( {'test_field': 'something beautiful'}) self.assertTrue(user.getProfile().get('test_field', 'blank'), 'something beautiful') # Check that a data structure update works user.setClientSpecificProfileFields( {'test_field': { 'something': 'beautiful' }}) self.assertTrue(user.getProfile().get('test_field', 'blank'), {'something': 'beautiful'})
def testGetCarbonFootprintForMode(self): user = User.register('*****@*****.**') user.setMpgArray([45, 50, 31]) # Avg MPG = 42 correctCarbonFootprintForMode = { 'walking': 0, 'running': 0, 'cycling': 0, 'mixed': 0, 'bus_short': 267.0 / 1609, 'bus_long': 267.0 / 1609, 'train_short': 92.0 / 1609, 'train_long': 92.0 / 1609, 'car_short': (1 / (42 * 1.6093)) * 8.91, 'car_long': (1 / (42 * 1.6093)) * 8.91, 'air_short': 217.0 / 1609, 'air_long': 217.0 / 1609 } self.assertEquals(user.getCarbonFootprintForMode(), correctCarbonFootprintForMode)
def testGetSectionFilter(self): from dao.user import User 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 setupUserAndClient(self): # At this point, the more important test is to execute the query and see # how well it works from dao.user import User from dao.client import Client import tests.common from datetime import datetime, timedelta from get_database import get_section_db fakeEmail = "*****@*****.**" client = Client("gamified") client.update(createKey = False) 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 setupUserAndClient(self): # At this point, the more important test is to execute the query and see # how well it works from dao.user import User from dao.client import Client import tests.common from datetime import datetime, timedelta from get_database import get_section_db fakeEmail = "*****@*****.**" client = Client("gamified") client.update(createKey=False) 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 testUnregister(self): user = User.register('*****@*****.**') self.assertTrue(User.isRegistered('*****@*****.**')) User.unregister('*****@*****.**') self.assertFalse(User.isRegistered('*****@*****.**'))
def testGetSettingsDefaultUser(self): user = User.register('*****@*****.**') self.assertRegexpMatches(user.getSettings()['result_url'], ".*/compare")
def testRegisterNonStudyUser(self): user = User.register('*****@*****.**') self.assertEquals(user.getStudy(), [])
def testIsRegistered(self): user = User.register('*****@*****.**') self.assertTrue(User.isRegistered('*****@*****.**'))
def testCountForStudyDefault(self): from get_database import get_profile_db, get_uuid_db, get_client_db, get_pending_signup_db user = User.register('*****@*****.**') self.assertEquals(userclient.countForStudy('testclient'), 0) self.assertEquals(userclient.countForStudy(None), 1)
def testGetAvgMpg(self): user = User.register('*****@*****.**') user.setMpgArray([45, 50, 31]) self.assertEquals(user.getAvgMpg(), 42)