Ejemplo n.º 1
0
    def test_question_no_answers_deleted(self):
        search = QuestionMappingType.search()

        q = QuestionFactory(title=u'Does this work?')
        self.refresh()
        eq_(search.query(question_title__match='work').count(), 1)

        q.delete()
        self.refresh()
        eq_(search.query(question_title__match='work').count(), 0)
Ejemplo n.º 2
0
    def test_question_no_answers_deleted(self):
        search = QuestionMappingType.search()

        q = QuestionFactory(title='Does this work?')
        self.refresh()
        eq_(search.query(question_title__match='work').count(), 1)

        q.delete()
        self.refresh()
        eq_(search.query(question_title__match='work').count(), 0)
Ejemplo n.º 3
0
    def test_delete_question_removes_flag(self):
        """Deleting a question also removes the flags on that question."""
        q = QuestionFactory(title='Test Question', content='Lorem Ipsum Dolor')

        u = UserFactory()
        FlaggedObject.objects.create(
            status=0, content_object=q, reason='language', creator_id=u.id)
        eq_(1, FlaggedObject.objects.count())

        q.delete()
        eq_(0, FlaggedObject.objects.count())
Ejemplo n.º 4
0
    def test_delete_question_removes_flag(self):
        """Deleting a question also removes the flags on that question."""
        q = QuestionFactory(title="Test Question", content="Lorem Ipsum Dolor")

        u = UserFactory()
        FlaggedObject.objects.create(
            status=0, content_object=q, reason="language", creator_id=u.id
        )
        eq_(1, FlaggedObject.objects.count())

        q.delete()
        eq_(0, FlaggedObject.objects.count())
Ejemplo n.º 5
0
    def test_num_questions(self):
        """Answers are counted correctly on a user."""
        u = UserFactory()
        eq_(num_questions(u), 0)

        q1 = QuestionFactory(creator=u)
        eq_(num_questions(u), 1)

        q2 = QuestionFactory(creator=u)
        eq_(num_questions(u), 2)

        q1.delete()
        eq_(num_questions(u), 1)

        q2.delete()
        eq_(num_questions(u), 0)
Ejemplo n.º 6
0
    def test_num_questions(self):
        """Answers are counted correctly on a user."""
        u = UserFactory()
        eq_(num_questions(u), 0)

        q1 = QuestionFactory(creator=u)
        eq_(num_questions(u), 1)

        q2 = QuestionFactory(creator=u)
        eq_(num_questions(u), 2)

        q1.delete()
        eq_(num_questions(u), 1)

        q2.delete()
        eq_(num_questions(u), 0)
Ejemplo n.º 7
0
    def test_question_one_answer_deleted(self):
        search = QuestionMappingType.search()

        q = QuestionFactory(title='are model makers the new pink?')
        a = AnswerFactory(content='yes.', question=q)
        self.refresh()

        # Question and its answers are a single document--so the index count should be only 1.
        eq_(search.query(question_title__match='pink').count(), 1)

        # After deleting the answer, the question document should remain.
        a.delete()
        self.refresh()
        eq_(search.query(question_title__match='pink').count(), 1)

        # Delete the question and it should be removed from the index.
        q.delete()
        self.refresh()
        eq_(search.query(question_title__match='pink').count(), 0)
Ejemplo n.º 8
0
    def test_question_one_answer_deleted(self):
        search = QuestionMappingType.search()

        q = QuestionFactory(title=u'are model makers the new pink?')
        a = AnswerFactory(content=u'yes.', question=q)
        self.refresh()

        # Question and its answers are a single document--so the index count should be only 1.
        eq_(search.query(question_title__match='pink').count(), 1)

        # After deleting the answer, the question document should remain.
        a.delete()
        self.refresh()
        eq_(search.query(question_title__match='pink').count(), 1)

        # Delete the question and it should be removed from the index.
        q.delete()
        self.refresh()
        eq_(search.query(question_title__match='pink').count(), 0)
Ejemplo n.º 9
0
class QuestionDocumentSignalsTests(Elastic7TestCase):
    def setUp(self):
        self.question = QuestionFactory()
        self.question_id = self.question.id

    def get_doc(self):
        return QuestionDocument.get(self.question_id)

    def test_question_save(self):
        self.question.title = "foobar"
        self.question.save()

        self.assertEqual(self.get_doc().question_title["en-US"], "foobar")

    def test_answer_save(self):
        AnswerFactory(question=self.question, content="foobar")

        self.assertIn("foobar", self.get_doc().answer_content["en-US"])

    def test_vote_save(self):
        QuestionVoteFactory(question=self.question)

        self.assertEqual(self.get_doc().question_num_votes, 1)

    def test_tags_change(self):
        tag = TagFactory()
        self.question.tags.add(tag)

        self.assertIn(tag.id, self.get_doc().question_tag_ids)

        self.question.tags.remove(tag)

        self.assertNotIn(tag.id, self.get_doc().question_tag_ids)

    def test_question_delete(self):
        self.question.delete()

        with self.assertRaises(NotFoundError):
            self.get_doc()

    def test_answer_delete(self):
        answer = AnswerFactory(question=self.question, content="foobar")
        answer.delete()

        self.assertNotIn("en-US", self.get_doc().answer_content)

    def test_vote_delete(self):
        vote = QuestionVoteFactory(question=self.question)
        vote.delete()

        self.assertEqual(self.get_doc().question_num_votes, 0)

    def test_tag_delete(self):
        tag = TagFactory()
        self.question.tags.add(tag)
        tag.delete()

        self.assertEqual(self.get_doc().question_tag_ids, [])

    @patch("kitsune.search.v2.signals.questions.index_object.delay")
    def test_kb_tag(self, mock_index_object):
        # the tag m2m relation is shared across all models which use it
        # so will trigger signals on all models which use it, but we don't
        # want this to happen
        mock_index_object.reset_mock()
        document = DocumentFactory(tags=["foobar"])
        self.assertNotIn(call("QuestionDocument", document.id),
                         mock_index_object.call_args_list)
Ejemplo n.º 10
0
class TestQuestionUpdates(TestCaseBase):
    """Tests that questions are only updated in the right cases."""

    client_class = LocalizingClient

    date_format = "%Y%M%d%H%m%S"

    def setUp(self):
        super(TestQuestionUpdates, self).setUp()
        self.u = UserFactory(is_superuser=True)
        self.client.login(username=self.u.username, password="******")

        self.q = QuestionFactory(updated=datetime(2012, 7, 9, 9, 0, 0))
        self.a = AnswerFactory(question=self.q)

        # Get the question from the database so we have a consistent level of
        # precision during the test.
        self.q = Question.objects.get(pk=self.q.id)

    def tearDown(self):
        self.client.logout()
        self.u.delete()
        self.q.delete()

    def _request_and_no_update(self, url, req_type="POST", data={}):
        updated = self.q.updated

        if req_type == "POST":
            self.client.post(url, data, follow=True)
        elif req_type == "GET":
            self.client.get(url, data, follow=True)
        else:
            raise ValueError('req_type must be either "GET" or "POST"')

        self.q = Question.objects.get(pk=self.q.id)
        eq_(
            updated.strftime(self.date_format),
            self.q.updated.strftime(self.date_format),
        )

    def test_no_update_edit(self):
        url = urlparams(reverse("questions.edit_question", args=[self.q.id]))
        self._request_and_no_update(
            url,
            req_type="POST",
            data={
                "title": "A new title.",
                "content": "Some new content."
            },
        )

    def test_no_update_solve(self):
        url = urlparams(reverse("questions.solve", args=[self.q.id,
                                                         self.a.id]))
        self._request_and_no_update(url)

    def test_no_update_unsolve(self):
        url = urlparams(
            reverse("questions.unsolve", args=[self.q.id, self.a.id]))
        self._request_and_no_update(url)

    def test_no_update_vote(self):
        url = urlparams(reverse("questions.vote", args=[self.q.id]))
        self._request_and_no_update(url, req_type="POST")

    def test_no_update_lock(self):
        url = urlparams(reverse("questions.lock", args=[self.q.id]))
        self._request_and_no_update(url, req_type="POST")
        # Now unlock
        self._request_and_no_update(url, req_type="POST")

    def test_no_update_tagging(self):
        url = urlparams(reverse("questions.add_tag", args=[self.q.id]))
        self._request_and_no_update(url,
                                    req_type="POST",
                                    data={"tag-name": "foo"})

        url = urlparams(reverse("questions.remove_tag", args=[self.q.id]))
        self._request_and_no_update(url,
                                    req_type="POST",
                                    data={"remove-tag-foo": 1})
Ejemplo n.º 11
0
class TestQuestionUpdates(TestCaseBase):
    """Tests that questions are only updated in the right cases."""
    client_class = LocalizingClient

    date_format = '%Y%M%d%H%m%S'

    def setUp(self):
        super(TestQuestionUpdates, self).setUp()
        self.u = UserFactory(is_superuser=True)
        self.client.login(username=self.u.username, password='******')

        self.q = QuestionFactory(updated=datetime(2012, 7, 9, 9, 0, 0))
        self.a = AnswerFactory(question=self.q)

        # Get the question from the database so we have a consistent level of
        # precision during the test.
        self.q = Question.objects.get(pk=self.q.id)

    def tearDown(self):
        self.client.logout()
        self.u.delete()
        self.q.delete()

    def _request_and_no_update(self, url, req_type='POST', data={}):
        updated = self.q.updated

        if req_type == 'POST':
            self.client.post(url, data, follow=True)
        elif req_type == 'GET':
            self.client.get(url, data, follow=True)
        else:
            raise ValueError('req_type must be either "GET" or "POST"')

        self.q = Question.objects.get(pk=self.q.id)
        eq_(updated.strftime(self.date_format),
            self.q.updated.strftime(self.date_format))

    def test_no_update_edit(self):
        url = urlparams(reverse('questions.edit_question', args=[self.q.id]))
        self._request_and_no_update(url, req_type='POST', data={
            'title': 'A new title.',
            'content': 'Some new content.'
        })

    def test_no_update_solve(self):
        url = urlparams(reverse('questions.solve',
                        args=[self.q.id, self.a.id]))
        self._request_and_no_update(url)

    def test_no_update_unsolve(self):
        url = urlparams(reverse('questions.unsolve',
                                args=[self.q.id, self.a.id]))
        self._request_and_no_update(url)

    def test_no_update_vote(self):
        url = urlparams(reverse('questions.vote', args=[self.q.id]))
        self._request_and_no_update(url, req_type='POST')

    def test_no_update_lock(self):
        url = urlparams(reverse('questions.lock', args=[self.q.id]))
        self._request_and_no_update(url, req_type='POST')
        # Now unlock
        self._request_and_no_update(url, req_type='POST')

    def test_no_update_tagging(self):
        url = urlparams(reverse('questions.add_tag', args=[self.q.id]))
        self._request_and_no_update(url, req_type='POST', data={
            'tag-name': 'foo'
        })

        url = urlparams(reverse('questions.remove_tag', args=[self.q.id]))
        self._request_and_no_update(url, req_type='POST', data={
            'remove-tag-foo': 1
        })