Esempio n. 1
0
 def test_leaders_weekly(self):
     session = self.db_master_session
     for i in range(3):
         user = User(nickname=unicode(i))
         session.add(user)
         session.flush()
         score1 = Score(userid=user.id, value=i)
         score1.name = 'new_cell'
         session.add(score1)
         score2 = Score(userid=user.id, value=i)
         score2.name = 'new_wifi'
         session.add(score2)
     session.commit()
     request = DummyRequest()
     request.db_slave_session = self.db_master_session
     inst = self._make_view(request)
     result = inst.leaders_weekly_view()
     for score_name in ('new_cell', 'new_wifi'):
         self.assertEqual(result['scores'][score_name]['leaders1'],
                          [{
                              'nickname': u'2',
                              'num': 2,
                              'pos': 1
                          }, {
                              'nickname': u'1',
                              'num': 1,
                              'pos': 2
                          }])
         self.assertEqual(result['scores'][score_name]['leaders2'],
                          [{
                              'nickname': u'0',
                              'num': 0,
                              'pos': 3
                          }])
Esempio n. 2
0
 def test_leaders(self):
     session = self.db_master_session
     today = datetime.utcnow().date()
     yesterday = today - timedelta(days=1)
     for i in range(3):
         user = User(nickname=unicode(i))
         session.add(user)
         session.flush()
         score1 = Score(userid=user.id, time=today, value=i)
         score1.name = 'location'
         session.add(score1)
         score2 = Score(userid=user.id, time=yesterday, value=i + 1)
         score2.name = 'location'
         session.add(score2)
     session.commit()
     request = DummyRequest()
     request.db_slave_session = self.db_master_session
     inst = self._make_view(request)
     result = inst.leaders_view()
     self.assertEqual(
         result['leaders1'],
         [{'nickname': u'2', 'num': 5}, {'nickname': u'1', 'num': 3}])
     self.assertEqual(
         result['leaders2'],
         [{'nickname': u'0', 'num': 1}])
Esempio n. 3
0
 def test_nickname_header_update(self):
     app = self.app
     nickname = 'World Tr\xc3\xa4veler'
     utcday = util.utcnow().date()
     session = self.db_master_session
     user = User(nickname=nickname.decode('utf-8'))
     session.add(user)
     session.flush()
     session.add(Score(userid=user.id, key=SCORE_TYPE['location'], value=7))
     session.add(Score(userid=user.id, key=SCORE_TYPE['new_wifi'], value=3))
     session.commit()
     app.post_json(
         '/v1/submit', {"items": [
             {"lat": 1.0,
              "lon": 2.0,
              "wifi": [{"key": "00AAAAAAAAAA"}]},
         ]},
         headers={'X-Nickname': nickname},
         status=204)
     result = session.query(User).all()
     self.assertEqual(len(result), 1)
     self.assertEqual(result[0].nickname, nickname.decode('utf-8'))
     result = session.query(Score).all()
     self.assertEqual(len(result), 2)
     self.assertEqual(set([r.name for r in result]),
                      set(['location', 'new_wifi']))
     for r in result:
         if r.name == 'location':
             self.assertEqual(r.value, 8)
             self.assertEqual(r.time, utcday)
         elif r.name == 'new_wifi':
             self.assertEqual(r.value, 4)
             self.assertEqual(r.time, utcday)
Esempio n. 4
0
 def test_leaders(self):
     from ichnaea.content.stats import leaders
     session = self.db_master_session
     test_data = []
     for i in range(20):
         test_data.append((u'nick-%s' % i, 7))
     highest = u'nick-high-too-long_'
     highest += (128 - len(highest)) * u'x'
     test_data.append((highest, 10))
     lowest = u'nick-low'
     test_data.append((lowest, 5))
     for nick, value in test_data:
         user = User(nickname=nick)
         session.add(user)
         session.flush()
         score = Score(userid=user.id, value=value)
         score.name = 'location'
         session.add(score)
     session.commit()
     # check the result
     result = leaders(session)
     self.assertEqual(len(result), 22)
     self.assertEqual(result[0]['nickname'], highest[:24] + u'...')
     self.assertEqual(result[0]['num'], 10)
     self.assertTrue(lowest in [r['nickname'] for r in result])
Esempio n. 5
0
 def test_email_header_update(self):
     app = self.app
     nickname = 'World Tr\xc3\xa4veler'
     old_email = 'world_tr\xc3\[email protected]'
     new_email = 'world_tr\xc3\[email protected]'
     session = self.db_master_session
     user = User(nickname=nickname, email=old_email.decode('utf-8'))
     session.add(user)
     session.flush()
     session.commit()
     app.post_json('/v1/submit', {
         "items": [
             {
                 "lat": 1.0,
                 "lon": 2.0,
                 "wifi": [{
                     "key": "00AAAAAAAAAA"
                 }]
             },
         ]
     },
                   headers={
                       'X-Nickname': nickname,
                       'X-Email': new_email,
                   },
                   status=204)
     result = session.query(User).all()
     self.assertEqual(len(result), 1)
     self.assertEqual(result[0].email, new_email.decode('utf-8'))
Esempio n. 6
0
def process_user(nickname, session):
    userid = None
    if (2 <= len(nickname) <= 128):
        # automatically create user objects and update nickname
        rows = session.query(User).filter(User.nickname == nickname)
        old = rows.first()
        if not old:
            user = User(nickname=nickname)
            session.add(user)
            session.flush()
            userid = user.id
        else:
            userid = old.id
    return (userid, nickname)
Esempio n. 7
0
    def test_leaders(self):
        session = self.db_master_session
        today = util.utcnow().date()
        yesterday = today - timedelta(days=1)
        for i in range(7, 1, -1):
            user = User(nickname=unicode(i))
            session.add(user)
            session.flush()
            score1 = Score(userid=user.id, time=today, value=i)
            score1.name = 'location'
            session.add(score1)
            score2 = Score(userid=user.id, time=yesterday, value=i + 1)
            score2.name = 'location'
            session.add(score2)
        session.commit()
        request = DummyRequest()
        request.db_slave_session = self.db_master_session
        request.registry.redis_client = self.redis_client
        inst = self._make_view(request)
        result = inst.leaders_view()
        self.assertEqual(result['leaders1'], [{
            'anchor': u'7',
            'nickname': u'7',
            'num': 15,
            'pos': 1
        }, {
            'anchor': u'6',
            'nickname': u'6',
            'num': 13,
            'pos': 2
        }])
        self.assertEqual(result['leaders2'], [{
            'anchor': u'5',
            'nickname': u'5',
            'num': 11,
            'pos': 3
        }])

        # call the view again, without a working db session, so
        # we can be sure to use the cached result
        inst = self._make_view(request)
        request.db_slave_session = None
        second_result = inst.leaders_view()
        self.assertEqual(second_result, result)
Esempio n. 8
0
 def test_userid(self):
     func, tmpfile = self._make_one()
     session = self.db_master_session
     user = User(nickname='test'.decode('ascii'))
     session.add(user)
     session.flush()
     userid = user.id
     os.write(tmpfile[0], LINE)
     os.write(tmpfile[0], '\n1' + LINE)
     os.write(tmpfile[0], '\n2' + LINE)
     counter = func(session, tmpfile[1], batch_size=2, userid=userid)
     self.assertEqual(counter, 3)
     # test user scores and mapstat
     scores = session.query(Score).filter(Score.userid == userid).all()
     scores = dict([(SCORE_TYPE_INVERSE[s.key], s.value) for s in scores])
     self.assertEqual(scores, {'new_wifi': 1, 'location': 3})
     mapstats = session.query(MapStat).all()
     mapstats = [(m.lat, m.lon) for m in mapstats]
     self.assertEqual(mapstats, [(37871, -122273)])
Esempio n. 9
0
    def test_leaders_weekly(self):
        session = self.db_master_session
        for i in range(3):
            user = User(nickname=unicode(i))
            session.add(user)
            session.flush()
            score1 = Score(userid=user.id, value=i)
            score1.name = 'new_cell'
            session.add(score1)
            score2 = Score(userid=user.id, value=i)
            score2.name = 'new_wifi'
            session.add(score2)
        session.commit()
        request = DummyRequest()
        request.db_slave_session = self.db_master_session
        request.registry.redis_client = self.redis_client
        inst = self._make_view(request)
        result = inst.leaders_weekly_view()
        for score_name in ('new_cell', 'new_wifi'):
            self.assertEqual(result['scores'][score_name]['leaders1'],
                             [{
                                 'nickname': u'2',
                                 'num': 2,
                                 'pos': 1
                             }, {
                                 'nickname': u'1',
                                 'num': 1,
                                 'pos': 2
                             }])
            self.assertEqual(result['scores'][score_name]['leaders2'],
                             [{
                                 'nickname': u'0',
                                 'num': 0,
                                 'pos': 3
                             }])

        # call the view again, without a working db session, so
        # we can be sure to use the cached result
        inst = self._make_view(request)
        request.db_slave_session = None
        second_result = inst.leaders_weekly_view()
        self.assertEqual(second_result, result)
Esempio n. 10
0
def process_user(nickname, email, session):
    userid = None
    if len(email) > 255:
        email = ''
    if (2 <= len(nickname) <= 128):
        # automatically create user objects and update nickname
        rows = session.query(User).filter(User.nickname == nickname)
        old = rows.first()
        if not old:
            user = User(nickname=nickname, email=email)
            session.add(user)
            session.flush()
            userid = user.id
        else:
            userid = old.id
            # update email column on existing user
            if old.email != email:
                old.email = email

    return (userid, nickname, email)
Esempio n. 11
0
    def test_leaders_weekly(self):
        from ichnaea.content.stats import leaders_weekly
        session = self.db_master_session
        test_data = []
        for i in range(1, 11):
            test_data.append((u'nick-%s' % i, i))
        for nick, value in test_data:
            user = User(nickname=nick)
            session.add(user)
            session.flush()
            score = Score(userid=user.id, value=value)
            score.name = 'new_cell'
            session.add(score)
            score = Score(userid=user.id, value=21 - value)
            score.name = 'new_wifi'
            session.add(score)
        session.commit()

        # check the result
        result = leaders_weekly(session, batch=5)
        self.assertEqual(len(result), 2)
        self.assertEqual(set(result.keys()), set(['new_cell', 'new_wifi']))

        # check the cell scores
        scores = result['new_cell']
        self.assertEqual(len(scores), 5)
        self.assertEqual(scores[0]['nickname'], 'nick-10')
        self.assertEqual(scores[0]['num'], 10)
        self.assertEqual(scores[-1]['nickname'], 'nick-6')
        self.assertEqual(scores[-1]['num'], 6)

        # check the wifi scores
        scores = result['new_wifi']
        self.assertEqual(len(scores), 5)
        self.assertEqual(scores[0]['nickname'], 'nick-1')
        self.assertEqual(scores[0]['num'], 20)
        self.assertEqual(scores[-1]['nickname'], 'nick-5')
        self.assertEqual(scores[-1]['num'], 16)
Esempio n. 12
0
 def _make_one(self, **kw):
     from ichnaea.content.models import User
     return User(**kw)