def test_questions_num_votes(self): """Tests advanced search for questions num_votes filter""" q = QuestionFactory(title=u'tags tags tags') # Add two question votes QuestionVoteFactory(question=q) QuestionVoteFactory(question=q) self.refresh() # Advanced search for questions with num_votes > 5. The above # question should be not in this set. response = self.client.get(reverse('search.advanced'), { 'q': '', 'tags': 'desktop', 'w': '2', 'a': '1', 'num_voted': 2, 'num_votes': 5, 'format': 'json' }) eq_(200, response.status_code) content = json.loads(response.content) eq_(content['total'], 0) # Advanced search for questions with num_votes < 1. The above # question should be not in this set. response = self.client.get(reverse('search.advanced'), { 'q': '', 'tags': 'desktop', 'w': '2', 'a': '1', 'num_voted': 1, 'num_votes': 1, 'format': 'json' }) eq_(200, response.status_code) content = json.loads(response.content) eq_(content['total'], 0)
def test_questions_num_votes(self): """Tests advanced search for questions num_votes filter""" q = QuestionFactory(title="tags tags tags") # Add two question votes QuestionVoteFactory(question=q) QuestionVoteFactory(question=q) self.refresh() # Advanced search for questions with num_votes > 5. The above # question should be not in this set. response = self.client.get( reverse("search.advanced"), { "q": "", "tags": "desktop", "w": "2", "a": "1", "num_voted": 2, "num_votes": 5, "format": "json", }, ) eq_(200, response.status_code) content = json.loads(response.content) eq_(content["total"], 0) # Advanced search for questions with num_votes < 1. The above # question should be not in this set. response = self.client.get( reverse("search.advanced"), { "q": "", "tags": "desktop", "w": "2", "a": "1", "num_voted": 1, "num_votes": 1, "format": "json", }, ) eq_(200, response.status_code) content = json.loads(response.content) eq_(content["total"], 0)
def test_cron_updates_counts(self): q = QuestionFactory() self.refresh() eq_(q.num_votes_past_week, 0) # NB: Need to call .values_dict() here and later otherwise we # get a Question object which has data from the database and # not the index. document = (QuestionMappingType.search().filter(id=q.id))[0] eq_(document['question_num_votes_past_week'], 0) QuestionVoteFactory(question=q, anonymous_id='abc123') q.num_votes_past_week = 0 q.save() update_weekly_votes() self.refresh() q = Question.objects.get(pk=q.pk) eq_(1, q.num_votes_past_week) document = (QuestionMappingType.search().filter(id=q.id))[0] eq_(document['question_num_votes_past_week'], 1)
def test_vote_updates_count(self): q = QuestionFactory() eq_(0, q.num_votes_past_week) QuestionVoteFactory(question=q, anonymous_id='abc123') q = Question.objects.get(id=q.id) eq_(1, q.num_votes_past_week)
def test_num_votes_none(self): """Tests num_voted filtering where num_votes is ''""" q = QuestionFactory() QuestionVoteFactory(question=q) self.refresh() qs = {"q": "", "w": 2, "a": 1, "num_voted": 2, "num_votes": ""} response = self.client.get(reverse("search.advanced"), qs) eq_(200, response.status_code)
def test_num_votes_none(self): """Tests num_voted filtering where num_votes is ''""" q = QuestionFactory() QuestionVoteFactory(question=q) self.refresh() qs = {'q': '', 'w': 2, 'a': 1, 'num_voted': 2, 'num_votes': ''} response = self.client.get(reverse('search.advanced'), qs) eq_(200, response.status_code)
def test_helpful_double_vote(self): q = QuestionFactory() u = UserFactory() QuestionVoteFactory(question=q, creator=u) self.client.force_authenticate(user=u) res = self.client.post(reverse('question-helpful', args=[q.id])) eq_(res.status_code, 409) # It's 1, not 0, because one was created above. The failure cause is # if the number of votes is 2, one from above and one from the api call. eq_(Question.objects.get(id=q.id).num_votes, 1)
def test_question_questionvote(self): search = QuestionMappingType.search() # Create a question and verify it doesn't show up in a # query for num_votes__gt=0. q = QuestionFactory(title='model makers will inherit the earth') self.refresh() eq_(search.filter(question_num_votes__gt=0).count(), 0) # Add a QuestionVote--it should show up now. QuestionVoteFactory(question=q) self.refresh() eq_(search.filter(question_num_votes__gt=0).count(), 1)
def test_cron_updates_counts(self): q = QuestionFactory() eq_(0, q.num_votes_past_week) QuestionVoteFactory(question=q, anonymous_id='abc123') q.num_votes_past_week = 0 q.save() update_weekly_votes() q = Question.objects.get(pk=q.pk) eq_(1, q.num_votes_past_week)
def test_update_question_vote_chunk(self): # Reset the num_votes_past_week counts, I suspect the data gets # loaded before I disconnect the signal and they get zeroed out. q1 = QuestionFactory() QuestionVoteFactory(question=q1) q1.num_votes_past_week = 1 q1.save() q2 = QuestionFactory() # Actually test the task. qs = Question.objects.all().order_by('-num_votes_past_week') eq_(q1.pk, qs[0].pk) QuestionVoteFactory(question=q2) QuestionVoteFactory(question=q2) qs = Question.objects.all().order_by('-num_votes_past_week') eq_(q1.pk, qs[0].pk) update_question_vote_chunk([q.pk for q in qs]) qs = Question.objects.all().order_by('-num_votes_past_week') eq_(q2.pk, qs[0].pk)
def test_vote_delete(self): vote = QuestionVoteFactory(question=self.question) vote.delete() self.assertEqual(self.get_doc().question_num_votes, 0)
def test_vote_save(self): QuestionVoteFactory(question=self.question) self.assertEqual(self.get_doc().question_num_votes, 1)
def test_add_metadata_over_1000_chars(self): qv = QuestionVoteFactory() qv.add_metadata('test1', 'a'*1001) metadata = VoteMetadata.objects.all()[0] eq_('a'*1000, metadata.value)
def test_add_metadata_over_1000_chars(self): qv = QuestionVoteFactory() qv.add_metadata("test1", "a" * 1001) metadata = VoteMetadata.objects.all()[0] eq_("a" * 1000, metadata.value)
def test_with_votes(self): QuestionVoteFactory(question=self.question) QuestionVoteFactory(question=self.question) QuestionVoteFactory() serializer = api.QuestionSerializer(instance=self.question) eq_(serializer.data['num_votes'], 2)