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(default.getCarbonFootprint(currUser), None)

        fakeEmail = "*****@*****.**"

        client = Client("testclient")
        client.update(createKey=False)
        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 = default.getCarbonFootprint(currUser)
                self.assertEqual(len(carbonFootprint), 12)
Exemplo n.º 2
0
  def testQueryUnclassifiedSectionsLowConfidence(self):
    from dao.user import User

    fakeEmail = "*****@*****.**"

    client = Client("testclient")
    client.update(createKey = False)
    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
    tests.common.updateUserCreateTime(user.uuid)
    queriedUnclassifiedSections = tripManager.queryUnclassifiedSections(User.fromEmail(fakeEmail).uuid)
    self.assertEqual(queriedUnclassifiedSections.count(), 0)
    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)
        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)
Exemplo n.º 4
0
  def testQueryUnclassifiedSectionsLowConfidence(self):
    from dao.user import User

    fakeEmail = "*****@*****.**"

    client = Client("testclient")
    client.update(createKey = False)
    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
    tests.common.updateUserCreateTime(user.uuid)
    queriedUnclassifiedSections = tripManager.queryUnclassifiedSections(User.fromEmail(fakeEmail).uuid)
    self.assertEqual(queriedUnclassifiedSections.count(), 0)
Exemplo n.º 5
0
  def testQueryUnclassifiedSectionsWeekAgo(self):
    # Add some old sections that shouldn't be returned by the query
    # This one is just over a week old
    old_sec_1 = self.SectionsColl.find_one({'$and': [{'user_id': User.fromEmail('*****@*****.**').uuid}, {'type':'move'}, {'mode':1}]})
    old_sec_1['_id'] = 'old_sec_1'
    old_sec_1['section_start_datetime'] = self.weekago - timedelta(minutes = 30)
    old_sec_1['section_end_datetime'] = self.weekago - timedelta(minutes = 5)
    logging.debug("Inserting old_sec_1 %s" % old_sec_1)
    self.SectionsColl.insert(old_sec_1)

    # This one is a month old
    monthago = self.now - timedelta(days = 30)
    old_sec_2 = self.SectionsColl.find_one({'$and': [{'user_id':User.fromEmail('*****@*****.**').uuid}, {'type':'move'}, {'mode':4}]})
    old_sec_2['_id'] = 'old_sec_2'
    old_sec_2['section_start_datetime'] = monthago - timedelta(minutes = 30)
    old_sec_2['section_end_datetime'] = monthago - timedelta(minutes = 5)
    logging.debug("Inserting old_sec_2 %s" % old_sec_2)
    self.SectionsColl.insert(old_sec_2)

    # This one is missing the predicted mode
    monthago = self.now - timedelta(days = 30)
    un_pred_sec = self.SectionsColl.find_one({'$and': [{'user_id':User.fromEmail('*****@*****.**').uuid}, {'type':'move'}, {'mode':4}]})
    un_pred_sec['_id'] = 'un_pred_sec'
    del un_pred_sec['predicted_mode']
    logging.debug("Inserting un_pred_sec %s" % un_pred_sec)
    self.SectionsColl.insert(un_pred_sec)

    queriedUnclassifiedSections = tripManager.queryUnclassifiedSections(User.fromEmail('*****@*****.**').uuid)
    self.assertEqual(queriedUnclassifiedSections.count(), 2)
Exemplo n.º 6
0
  def testQueryUnclassifiedSectionsWeekAgo(self):
    # Add some old sections that shouldn't be returned by the query
    # This one is just over a week old
    old_sec_1 = self.SectionsColl.find_one({'$and': [{'user_id': User.fromEmail('*****@*****.**').uuid}, {'type':'move'}, {'mode':1}]})
    old_sec_1['_id'] = 'old_sec_1'
    old_sec_1['section_start_datetime'] = self.weekago - timedelta(minutes = 30)
    old_sec_1['section_end_datetime'] = self.weekago - timedelta(minutes = 5)
    logging.debug("Inserting old_sec_1 %s" % old_sec_1)
    self.SectionsColl.insert(old_sec_1)

    # This one is a month old
    monthago = self.now - timedelta(days = 30)
    old_sec_2 = self.SectionsColl.find_one({'$and': [{'user_id':User.fromEmail('*****@*****.**').uuid}, {'type':'move'}, {'mode':4}]})
    old_sec_2['_id'] = 'old_sec_2'
    old_sec_2['section_start_datetime'] = monthago - timedelta(minutes = 30)
    old_sec_2['section_end_datetime'] = monthago - timedelta(minutes = 5)
    logging.debug("Inserting old_sec_2 %s" % old_sec_2)
    self.SectionsColl.insert(old_sec_2)

    # This one is missing the predicted mode
    monthago = self.now - timedelta(days = 30)
    un_pred_sec = self.SectionsColl.find_one({'$and': [{'user_id':User.fromEmail('*****@*****.**').uuid}, {'type':'move'}, {'mode':4}]})
    un_pred_sec['_id'] = 'un_pred_sec'
    del un_pred_sec['predicted_mode']
    logging.debug("Inserting un_pred_sec %s" % un_pred_sec)
    self.SectionsColl.insert(un_pred_sec)

    queriedUnclassifiedSections = tripManager.queryUnclassifiedSections(User.fromEmail('*****@*****.**').uuid)
    self.assertEqual(queriedUnclassifiedSections.count(), 2)
Exemplo n.º 7
0
    def __preRegister(self, userEmail):
        from dao.user import User
        from main import userclient

        if User.isRegistered(userEmail):
            User.fromEmail(userEmail).setStudy(self.clientName)
        else:
            pendingDoc = {
                'user_email': userEmail,
                'study': self.clientName,
                'last_update': datetime.now()
            }
            # Should I do insert or upsert here? If a user has pre-registered for one
            # study and then pre-registers for another study before registering, do we
            # want to throw an error or just update silently?
            # Update silently for now
            writeResult = get_pending_signup_db().update(
                {'user_email': userEmail}, pendingDoc, upsert=True)
            print 'in __preRegister, writeResult = %s' % writeResult
            if 'err' in writeResult and writeResult['err'] is not None:
                e = Exception()
                e.code = writeResult['err'][0]["code"]
                e.msg = writeResult['err'][0]["errmsg"]
                raise e
        return (get_pending_signup_db().find({
            'study': self.clientName
        }).count(), userclient.countForStudy(self.clientName))
Exemplo n.º 8
0
  def testSavePredictionsStepWithClient(self):
    from dao.user import User

    fakeEmail = "*****@*****.**"

    client = Client("testclient")
    client.update(createKey = False)
    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'])
Exemplo n.º 9
0
def getUUIDFromToken(token):
    userEmail = verifyUserToken(token)
    user=User.fromEmail(userEmail)
    if user is None:
      return None
    user_uuid=user.uuid
    return user_uuid
Exemplo n.º 10
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()
        load_database_json.loadTable(self.serverName, "Stage_Sections",
                                     "tests/data/testFilterFile")
        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)

        from dao.user import User
        fakeEmail = "*****@*****.**"

        client = Client("testclient")
        client.update(createKey=False)
        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 loadMovesInputFile(userEmail, fileName):
    from dao.user import User

    user = User.fromEmail(userEmail)
    savedTokens = auth.getAccessToken(user.uuid)
    print savedTokens
    if len(savedTokens) == 0:
        auth.saveAccessToken(sampleAuthMessage, user.uuid)
    result = json.load(open(fileName))
    print json.dumps(result)
    collect.processResult(user.uuid, result)
def loadMovesInputFile(userEmail, fileName):
  from dao.user import User

  user = User.fromEmail(userEmail)
  savedTokens = auth.getAccessToken(user.uuid)
  print savedTokens
  if len(savedTokens) == 0:
    auth.saveAccessToken(sampleAuthMessage, user.uuid)
  result = json.load(open(fileName))
  print json.dumps(result)
  collect.processResult(user.uuid, result)  
Exemplo n.º 13
0
    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 loadMovesInputFile(userEmail, fileName):
  # load_database_json.loadTable("localhost", "Test_Groups", "tests/data/groups.json")
  # load_database_json.loadTable("localhost", "Test_Modes", "tests/data/modes.json")
  from dao.user import User

  user = User.fromEmail(userEmail)
  savedTokens = auth.getAccessToken(user.uuid)
  print savedTokens
  if len(savedTokens) == 0:
    auth.saveAccessToken(sampleAuthMessage, user.uuid)
  result = json.load(open(fileName))
  print json.dumps(result)
  collect.processResult(user.uuid, result)  
Exemplo n.º 15
0
def loadMovesInputFile(userEmail, fileName):
    # load_database_json.loadTable("localhost", "Test_Groups", "tests/data/groups.json")
    # load_database_json.loadTable("localhost", "Test_Modes", "tests/data/modes.json")
    from dao.user import User

    user = User.fromEmail(userEmail)
    savedTokens = auth.getAccessToken(user.uuid)
    print savedTokens
    if len(savedTokens) == 0:
        auth.saveAccessToken(sampleAuthMessage, user.uuid)
    result = json.load(open(fileName))
    print json.dumps(result)
    collect.processResult(user.uuid, result)
Exemplo n.º 16
0
  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')
Exemplo n.º 17
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()
    load_database_json.loadTable(self.serverName, "Stage_Sections", "tests/data/testFilterFile")   
    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)

    from dao.user import User
    fakeEmail = "*****@*****.**"

    client = Client("testclient")
    client.update(createKey = False)
    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'])                     
Exemplo n.º 18
0
  def __preRegister(self, userEmail):
    from dao.user import User
    from main import userclient

    if User.isRegistered(userEmail):
      User.fromEmail(userEmail).setStudy(self.clientName)
    else:
      pendingDoc = {
        'user_email': userEmail,
        'study': self.clientName,
        'last_update': datetime.now()}
      # Should I do insert or upsert here? If a user has pre-registered for one
      # study and then pre-registers for another study before registering, do we
      # want to throw an error or just update silently?
      # Update silently for now
      writeResult = get_pending_signup_db().update({'user_email': userEmail}, pendingDoc, upsert=True)
      print 'in __preRegister, writeResult = %s' % writeResult
      if 'err' in writeResult and writeResult['err'] is not None:
        e = Exception()
        e.code = writeResult['err'][0]["code"]
        e.msg = writeResult['err'][0]["errmsg"]
        raise e
    return (get_pending_signup_db().find({'study': self.clientName}).count(),
            userclient.countForStudy(self.clientName))
Exemplo n.º 19
0
    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')
Exemplo n.º 20
0
def load_config(config_json):
    """
    Loads the specified config file into the specified user cache.
    If there is an existing entry with the same key in the cache, it will be overwritten.
    If there is no existing entry with the same key in the cache, an entry will be inserted.
    """
    for entry in config_json:
        userEmail = entry['userEmail']
        userObj = User.fromEmail(userEmail)
        if userObj is None:
            print "user with email %s is not found, ignoring...." % userEmail
        else:
            uc = ucauc.UserCache.getUserCache(userObj.uuid)
            key = entry['key']
            value = entry['value']
            uc.putDocument(key, value)
            print "successfully loaded key %s for user %s" % (key, userEmail)
Exemplo n.º 21
0
def load_config(config_json):
    """
    Loads the specified config file into the specified user cache.
    If there is an existing entry with the same key in the cache, it will be overwritten.
    If there is no existing entry with the same key in the cache, an entry will be inserted.
    """
    for entry in config_json:
        userEmail = entry['userEmail']
        userObj = User.fromEmail(userEmail)
        if userObj is None:
            print "user with email %s is not found, ignoring...." % userEmail
        else:
            uc = ucauc.UserCache.getUserCache(userObj.uuid)
            key = entry['key']
            value = entry['value']
            uc.putDocument(key, value)
            print "successfully loaded key %s for user %s" % (key, userEmail)
Exemplo n.º 22
0
  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'])
Exemplo n.º 23
0
def updateSections(testCase):
    from dao.user import User
    """
    Updates sections with appropriate test data
    Should be called anytime new data is loaded into the
    'Stage_Sections' table
    """
    testCase.uuid_list = []
    for section in testCase.SectionsColl.find():
      section['section_start_datetime'] = testCase.dayago
      section['section_end_datetime'] = testCase.dayago + timedelta(hours = 1)
      section['predicted_mode'] = [0, 0.4, 0.6, 0]
      section['confirmed_mode'] = ''
      # Replace the user email with the UUID
      curr_uuid = User.fromEmail(section['user_id']).uuid
      section['user_id'] = curr_uuid
      testCase.uuid_list.append(curr_uuid)
      testCase.SectionsColl.save(section)
Exemplo n.º 24
0
  def testStoreSensedTrips(self):
    fakeEmail = "*****@*****.**"
    fakeUUID = User.fromEmail(fakeEmail).uuid

    trip_array = json.load(open("tests/data/sensed_trips.json"))
    self.assertEqual(len(trip_array), 2)
    tripManager.storeSensedTrips(fakeUUID, trip_array)
    insertedTrips = [trip for trip in get_trip_db().find({"user_id": fakeUUID})]
    # We load two sections for each user in the setup. Here we only want to
    # look at sections that we added here. We distinguish between the two by looking
    # to see whether the predicted mode exists
    insertedSections = [section for section in get_section_db().find({"$and":
        [{"user_id": fakeUUID}, {"predicted_mode": {"$exists": False}}]})]
    # insertedSections = [section["predicted_mode"] for section in get_section_db().find({"user_id": fakeUUID})]

    self.assertEqual(len(insertedTrips), 2)

    self.assertEqual(insertedTrips[0]["type"], "place")
    self.assertEqual(insertedTrips[0]["trip_start_time"], "20150101T000153-0500")
    # self.assertEqual(insertedTrips[0]["trip_start_datetime"], datetime(2014,12,31,17,31,52))
    self.assertEqual(insertedTrips[0]["trip_end_time"], "20150102T000252-0500")
    # self.assertEqual(insertedTrips[0]["trip_end_datetime"], datetime(2015,01,02,04,01,51))

    startPlaceLocation = insertedTrips[0]["place"]["place_location"]
    self.assertEqual(startPlaceLocation["coordinates"], [-122.086945, 37.380866])

    self.assertEqual(insertedTrips[1]["type"], "move")
    self.assertEqual(insertedTrips[1]["trip_start_time"], "20150102T000252-0500")
    self.assertEqual(insertedTrips[1]["trip_end_time"], "20150102T000252-0500")

    self.assertEqual(len(insertedSections), 2)
    walkingSection = insertedSections[0]
    walkingTrackPointArray = insertedSections[0]["track_points"]

    self.assertEqual(walkingSection["section_start_time"], "20150102T000252-0500")
    self.assertEqual(walkingSection["section_end_time"], "20150102T000253-0500")
    self.assertEqual(walkingSection["duration"], 180631)
    self.assertAlmostEqual(walkingSection["distance"], 1311.125, places=2)

    self.assertEqual(len(walkingTrackPointArray), 10)
    self.assertEqual(walkingTrackPointArray[0]["track_location"]["coordinates"], [-122.086945, 37.380866])
    self.assertEqual(walkingTrackPointArray[8]["track_location"]["coordinates"], [-122.078265, 37.385461])
Exemplo n.º 25
0
    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'])
Exemplo n.º 26
0
  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)
Exemplo n.º 27
0
  def testStoreSensedTrips(self):
    fakeEmail = "*****@*****.**"
    fakeUUID = User.fromEmail(fakeEmail).uuid

    trip_array = json.load(open("tests/data/sensed_trips.json"))
    self.assertEqual(len(trip_array), 2)
    tripManager.storeSensedTrips(fakeUUID, trip_array)
    insertedTrips = [trip for trip in get_trip_db().find({"user_id": fakeUUID})]
    # We load two sections for each user in the setup. Here we only want to
    # look at sections that we added here. We distinguish between the two by looking
    # to see whether the predicted mode exists
    insertedSections = [section for section in get_section_db().find({"$and":
        [{"user_id": fakeUUID}, {"predicted_mode": {"$exists": False}}]})]
    # insertedSections = [section["predicted_mode"] for section in get_section_db().find({"user_id": fakeUUID})]

    self.assertEqual(len(insertedTrips), 2)

    self.assertEqual(insertedTrips[0]["type"], "place")
    self.assertEqual(insertedTrips[0]["trip_start_time"], "20150101T000153-0500")
    # self.assertEqual(insertedTrips[0]["trip_start_datetime"], datetime(2014,12,31,17,31,52))
    self.assertEqual(insertedTrips[0]["trip_end_time"], "20150102T000252-0500")
    # self.assertEqual(insertedTrips[0]["trip_end_datetime"], datetime(2015,01,02,04,01,51))

    startPlaceLocation = insertedTrips[0]["place"]["place_location"]
    self.assertEqual(startPlaceLocation["coordinates"], [-122.086945, 37.380866])

    self.assertEqual(insertedTrips[1]["type"], "move")
    self.assertEqual(insertedTrips[1]["trip_start_time"], "20150102T000252-0500")
    self.assertEqual(insertedTrips[1]["trip_end_time"], "20150102T000252-0500")

    self.assertEqual(len(insertedSections), 2)
    walkingSection = insertedSections[0]
    walkingTrackPointArray = insertedSections[0]["track_points"]

    self.assertEqual(walkingSection["section_start_time"], "20150102T000252-0500")
    self.assertEqual(walkingSection["section_end_time"], "20150102T000253-0500")
    self.assertEqual(walkingSection["duration"], 180631)
    self.assertAlmostEqual(walkingSection["distance"], 1311.125, places=2)

    self.assertEqual(len(walkingTrackPointArray), 7)
    self.assertEqual(walkingTrackPointArray[0]["track_location"]["coordinates"], [-122.086945, 37.380866])
    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()
Exemplo n.º 29
0
def __getUUIDFromEmail__(userEmail):
    user=User.fromEmail(userEmail)
    if user is None:
      return None
    user_uuid=user.uuid
    return user_uuid
Exemplo n.º 30
0
def __getUUIDFromEmail__(userEmail):
    user = User.fromEmail(userEmail)
    if user is None:
        return None
    user_uuid = user.uuid
    return user_uuid