def setUp(self): today = datetime.today() self.start_of_first_week = today - timedelta(days=today.weekday(), weeks=12) revisions = ApprovedRevisionFactory.create_batch(3, created=self.start_of_first_week) reviewer = UserFactory() ApprovedRevisionFactory(reviewer=reviewer, created=self.start_of_first_week) ApprovedRevisionFactory(creator=revisions[1].creator, reviewer=reviewer, created=self.start_of_first_week + timedelta(weeks=1, days=2)) ApprovedRevisionFactory(created=self.start_of_first_week + timedelta(weeks=1, days=1)) for r in revisions: lr = ApprovedRevisionFactory(created=self.start_of_first_week + timedelta(days=1), document__locale='es') ApprovedRevisionFactory(created=self.start_of_first_week + timedelta(weeks=2, days=1), creator=lr.creator, document__locale='es') answers = AnswerFactory.create_batch( 7, created=self.start_of_first_week + timedelta(weeks=1, days=2)) AnswerFactory(question=answers[2].question, creator=answers[2].question.creator, created=self.start_of_first_week + timedelta(weeks=1, days=2)) for a in answers[:2]: AnswerFactory(creator=a.creator, created=self.start_of_first_week + timedelta(weeks=2, days=5)) replies = ReplyFactory.create_batch(2, created=self.start_of_first_week) for r in replies: ReplyFactory(user=r.user, created=self.start_of_first_week + timedelta(weeks=2)) call_command('cohort_analysis')
def test_count_replies(self): """Test filtering when counting tweets""" TweetFactory() id = Tweet.latest().tweet_id ReplyFactory(reply_to_tweet_id=id, created=datetime.now()) ReplyFactory(reply_to_tweet_id=id, created=datetime.now()) ReplyFactory(created=datetime.now() - timedelta(days=1, minutes=1)) yesterday = datetime.now() - timedelta(days=1) count_recent_answered = _count_answered_tweets(since=yesterday) eq_(count_recent_answered, 1)
def test_add_and_delete(self): """Adding a reply should add it to the index. Deleting should delete it. """ r = ReplyFactory() self.refresh() eq_(ReplyMetricsMappingType.search().count(), 1) r.delete() self.refresh() eq_(ReplyMetricsMappingType.search().count(), 0)
def test_query_twitter_usernames(self): u1 = UserFactory(username="******", profile__name="Elite Mike") u2 = UserFactory(username="******", profile__name="NotElite Mike") r1 = ReplyFactory(user=u1, twitter_username="******") ReplyFactory(user=u2, twitter_username="******") self.refresh() eq_(UserMappingType.search().query(itwitter_usernames__match="l33tmike").count(), 1) data = UserMappingType.search().query(itwitter_usernames__match="l33tmike")[0] eq_(data["username"], u1.username) eq_(data["display_name"], u1.profile.name) assert r1.twitter_username in data["twitter_usernames"]
def test_top_army_of_awesome(self): r1 = ReplyFactory() r2 = ReplyFactory() self.refresh() response = self.client.get(urlparams( reverse('community.top_contributors', args=['army-of-awesome']))) eq_(200, response.status_code) doc = pq(response.content) eq_(2, len(doc('li.results-user'))) assert str(r1.user.username) in response.content assert str(r2.user.username) in response.content
def test_query_twitter_usernames(self): u1 = UserFactory(username='******', profile__name='Elite Mike') u2 = UserFactory(username='******', profile__name='NotElite Mike') r1 = ReplyFactory(user=u1, twitter_username='******') ReplyFactory(user=u2, twitter_username='******') self.refresh() eq_(UserMappingType.search().query(itwitter_usernames__match='l33tmike').count(), 1) data = UserMappingType.search().query(itwitter_usernames__match='l33tmike')[0] eq_(data['username'], u1.username) eq_(data['display_name'], u1.profile.name) assert r1.twitter_username in data['twitter_usernames']
def test_top_contributors_aoa(self): r1 = ReplyFactory() ReplyFactory(user=r1.user) r3 = ReplyFactory() r4 = ReplyFactory(created=date.today() - timedelta(days=91)) self.refresh() # By default, we should only get 2 top contributors back. top, _ = top_contributors_aoa() eq_(2, len(top)) assert r4.user_id not in [u['term'] for u in top] eq_(r1.user_id, top[0]['term']) eq_(r3.user_id, top[1]['term'])
def setUp(self): now = datetime.now() two_days_ago = now - timedelta(days=2) two_weeks_ago = now - timedelta(days=14) two_months_ago = now - timedelta(days=60) # Moe has the most in the last day, curly has the most in the last # week, larry has the most in the last month, and moe has the most # overall. data = { 'moe': [(now, 3), (two_days_ago, 2), (two_months_ago, 20)], 'curly': [(two_days_ago, 6), (two_weeks_ago, 2)], 'larry': [(two_weeks_ago, 10), (two_months_ago, 4)] } for who, what in data.items(): for when, how_many in what: ReplyFactory.create_batch(how_many, created=when, twitter_username=who)
def test_data_in_index(self): """Verify the data we are indexing.""" u = UserFactory(username="******", email="*****@*****.**", profile__name="Rick Róss") r1 = ReplyFactory(user=u, twitter_username="******") r2 = ReplyFactory(user=u, twitter_username="******") self.refresh() eq_(UserMappingType.search().count(), 1) data = UserMappingType.search()[0] eq_(data["username"], u.username) eq_(data["display_name"], u.profile.name) assert r1.twitter_username in data["twitter_usernames"] assert r2.twitter_username in data["twitter_usernames"] u = UserFactory(username="******", email="*****@*****.**", profile__name="Will Cage") self.refresh() eq_(UserMappingType.search().count(), 2)
def test_data_in_index(self): """Verify the data we are indexing.""" u = UserFactory(username='******', email='*****@*****.**', profile__name='Rick Róss') r1 = ReplyFactory(user=u, twitter_username='******') r2 = ReplyFactory(user=u, twitter_username='******') self.refresh() eq_(UserMappingType.search().count(), 1) data = UserMappingType.search()[0] eq_(data['username'], u.username) eq_(data['display_name'], u.profile.name) assert r1.twitter_username in data['twitter_usernames'] assert r2.twitter_username in data['twitter_usernames'] u = UserFactory(username='******', email='*****@*****.**', profile__name='Will Cage') self.refresh() eq_(UserMappingType.search().count(), 2)
def test_data_in_index(self): """Verify the data we are indexing.""" r = ReplyFactory(locale='de') self.refresh() eq_(ReplyMetricsMappingType.search().count(), 1) data = ReplyMetricsMappingType.search()[0] eq_(data['locale'], r.locale) eq_(data['creator_id'], r.user_id)
def test_aoa_badge(self): """Verify the KB Badge is awarded properly.""" # Create the user and badge. year = date.today().year u = UserFactory() b = BadgeFactory( slug=AOA_BADGE['slug'].format(year=year), title=AOA_BADGE['title'].format(year=year), description=AOA_BADGE['description'].format(year=year)) # Create 49 replies. # FIXME: Do this as a batch for _ in range(49): ReplyFactory(user=u) # User should NOT have the badge yet. assert not b.is_awarded_to(u) # Create 1 more reply. ReplyFactory(user=u) # User should have the badge now. assert b.is_awarded_to(u)
def test_top_contributors(self): """Verify the top contributors appear.""" # FIXME: Change this to batch creation RevisionFactory(document__locale='en-US') d = DocumentFactory(locale='es') RevisionFactory(document=d) RevisionFactory(document=d) AnswerFactory() AnswerFactory() AnswerFactory() ReplyFactory() ReplyFactory() ReplyFactory() ReplyFactory() self.refresh() response = self.client.get(urlparams(reverse('community.home'))) eq_(response.status_code, 200) doc = pq(response.content) eq_(1, len(doc('ul.kb > li'))) eq_(2, len(doc('ul.l10n > li'))) eq_(3, len(doc('ul.questions > li'))) eq_(4, len(doc('ul.army-of-awesome > li')))
def test_last_contribution_date(self): """Verify the last_contribution_date field works properly.""" u = UserFactory(username="******") self.refresh() data = UserMappingType.search().query(username__match="satdav")[0] assert not data["last_contribution_date"] # Add a AoA reply. It should be the last contribution. d = datetime(2014, 1, 1) ReplyFactory(user=u, created=d) self.refresh() data = UserMappingType.search().query(username__match="satdav")[0] eq_(data["last_contribution_date"], d) # Add a Support Forum answer. It should be the last contribution. d = datetime(2014, 1, 2) AnswerFactory(creator=u, created=d) u.profile.save() # we need to resave the profile to force a reindex self.refresh() data = UserMappingType.search().query(username__match="satdav")[0] eq_(data["last_contribution_date"], d) # Add a Revision edit. It should be the last contribution. d = datetime(2014, 1, 3) RevisionFactory(created=d, creator=u) u.profile.save() # we need to resave the profile to force a reindex self.refresh() data = UserMappingType.search().query(username__match="satdav")[0] eq_(data["last_contribution_date"], d) # Add a Revision review. It should be the last contribution. d = datetime(2014, 1, 4) RevisionFactory(reviewed=d, reviewer=u) u.profile.save() # we need to resave the profile to force a reindex self.refresh() data = UserMappingType.search().query(username__match="satdav")[0] eq_(data["last_contribution_date"], d)
def test_active_contributors(self): """Test active contributors API call.""" # 2 en-US revisions by 2 contributors: r1 = RevisionFactory() r2 = RevisionFactory() # A translation with 2 contributors (translator + reviewer): d = DocumentFactory(parent=r1.document, locale='es') RevisionFactory(document=d, reviewed=datetime.now(), reviewer=r1.creator, creator=r2.creator) # 1 active support forum contributor: # A user with 10 answers u1 = UserFactory() for x in range(10): AnswerFactory(creator=u1) # A user with 9 answers u2 = UserFactory() # FIXME: create_batch? for x in range(9): AnswerFactory(creator=u2) # A user with 1 answer u3 = UserFactory() AnswerFactory(creator=u3) # An AoA reply (1 contributor): ReplyFactory() # Create metric kinds and update metrics for tomorrow (today's # activity shows up tomorrow). self._make_contributor_metric_kinds() call_command('update_contributor_metrics', str(date.today() + timedelta(days=1))) r = self._get_api_result('api.kpi.contributors') eq_(r['objects'][0]['en_us'], 2) eq_(r['objects'][0]['non_en_us'], 2) eq_(r['objects'][0]['support_forum'], 1) eq_(r['objects'][0]['aoa'], 1)