def test_leaders_weekly(self): session = self.db_master_session today = util.utcnow().date() for i in range(3): user = User(nickname=unicode(i)) session.add(user) session.flush() score1 = Score(key=ScoreKey.new_cell, userid=user.id, time=today, value=i) session.add(score1) score2 = Score(key=ScoreKey.new_wifi, userid=user.id, time=today, value=i) 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)
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'))
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(key=ScoreKey.location, userid=user.id, time=today, value=i) session.add(score1) score2 = Score(key=ScoreKey.location, userid=user.id, time=yesterday, value=i + 1) 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)
def test_leaders(self): session = self.db_master_session today = util.utcnow().date() test_data = [] for i in range(20): test_data.append((u'nick-%s' % i, 30)) highest = u'nick-high-too-long_' highest += (128 - len(highest)) * u'x' test_data.append((highest, 40)) lowest = u'nick-low' test_data.append((lowest, 20)) for nick, value in test_data: user = User(nickname=nick) session.add(user) session.flush() score = Score(key=ScoreKey.location, userid=user.id, time=today, value=value) 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'], 40) self.assertTrue(lowest in [r['nickname'] for r in result])
def _add_nicks(self, names): users = {} for name in names: users[name] = user = User(nickname=name) self.session.add(user) self.session.flush() return users
def test_fields(self): nickname = u'World Tr\xc3\xa4veler' email = u'world_tr\xc3\[email protected]' self.session.add(User(nickname=nickname, email=email)) self.session.flush() result = self.session.query(User).first() self.assertEqual(result.nickname, nickname) self.assertEqual(result.email, email)
def test_fields(self): nickname = u'World Tr\xc3\xa4veler' email = u'world_tr\xc3\[email protected]' user = User(nickname=nickname, email=email) session = self.db_master_session session.add(user) session.commit() result = session.query(user.__class__).first() self.assertEqual(result.nickname, nickname) self.assertEqual(result.email, email)
def test_leaders_weekly(self): session = self.db_master_session today = util.utcnow().date() 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(key=ScoreKey.new_cell, userid=user.id, time=today, value=value) session.add(score) score = Score(key=ScoreKey.new_wifi, userid=user.id, time=today, value=21 - value) 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)
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)
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(key=ScoreKey.location, userid=user.id, time=utcday, value=7)) session.add( Score(key=ScoreKey.new_wifi, userid=user.id, time=utcday, 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.key.name for r in result]), set(['location', 'new_wifi'])) for r in result: if r.key.name == 'location': self.assertEqual(r.value, 8) self.assertEqual(r.time, utcday) elif r.key.name == 'new_wifi': self.assertEqual(r.value, 4) self.assertEqual(r.time, utcday)