Ejemplo n.º 1
0
    def test_elasticsearch_upsert_on_save(self, ElasticsearchMock):
        user = get_user_model().objects.create_user(
            username="******",
            password="******",
        )
        question_title = "Unit test"
        question_body = "some long text"
        q = Question(
            title=question_title,
            question=question_body,
            user=user,
        )
        q.save()

        self.assertIsNotNone(q.id)
        self.assertTrue(ElasticsearchMock.called)
        mock_client = ElasticsearchMock.return_value
        mock_client.update.assert_called_once_with(
            settings.ES_INDEX,
            "doc",
            id=q.id,
            body={
                "doc": {
                    "text": "{}\n{}".format(question_title, question_body),
                    "question_body": question_body,
                    "title": question_title,
                    "id": q.id,
                    "created": q.created,
                },
                "doc_as_upsert": True,
            },
        )
    def test_elasticsearch_upsert_on_save(self, ElasticsearchMock):
        user = get_user_model().objects.create_user(
            username='******',
            password='******',
        )
        question_title = 'Unit test'
        question_body = 'some long text'
        q = Question(
            title=question_title,
            question=question_body,
            user=user,
        )
        q.save()

        self.assertIsNotNone(q.id)
        self.assertTrue(ElasticsearchMock.called)

        mock_client = ElasticsearchMock.return_value
        mock_client.update.assert_called_once_with(
            settings.ES_INDEX, 'doc', q.id, {
                'doc': {
                    'text': '{}\n{}'.format(question_title, question_body),
                    'question_body': question_body,
                    'title': question_title,
                    'id': q.id,
                    'created': q.created,
                },
                'doc_as_upsert': True,
            })
Ejemplo n.º 3
0
 def test_match_against_keywords(self):
     question = Question(question='How do I make a widget?',
                         keywords='custom, widgets, easteregg')
     question.save()
     questions = Question.match('Show an easteregg please.')
     self.assertEqual(1, len(list(questions)))
     self.assertEqual(question.id, questions[0].id)
Ejemplo n.º 4
0
 def test_post_not_logged_in(self):
     client = Client()
     client.logout()
     data = {'answer_text': 'a' * 100}
     q = Question(question_title="Xax", question_body="dddd")
     q.save()
     response = client.post(reverse('qanda:detail', args=(q.id, )),
                            data=data)
     self.assertEqual(403, response.status_code)
Ejemplo n.º 5
0
 def test_post_redirect_invalid(self):
     user = User.objects.create(username='******')
     user.set_password('12345')
     user.save()
     client = Client()
     client.login(username='******', password='******')
     data = {}
     q = Question(question_title="Xax", question_body="dddd")
     q.save()
     response = client.post(reverse('qanda:detail', args=(q.id, )),
                            data=data,
                            follow=True)
     self.assertEqual(0, len(response.redirect_chain))
Ejemplo n.º 6
0
 def test_post_creates_question_IT(self):
     user = User.objects.create(username='******')
     user.set_password('12345')
     user.save()
     client = Client()
     client.login(username='******', password='******')
     data = {'answer_text': 'a' * 100}
     q = Question(question_title="Xax", question_body="dddd")
     q.save()
     response = client.post(reverse('qanda:detail', args=(q.id, )),
                            data=data)
     a = q.answer_set.get(answer_text='a' * 100)
     self.assertEqual('a' * 100, a.answer_text)
     self.assertEqual(user.profile, a.author)
Ejemplo n.º 7
0
 def test_post_redirect_valid(self):
     user = User.objects.create(username='******')
     user.set_password('12345')
     user.save()
     client = Client()
     client.login(username='******', password='******')
     data = {'answer_text': 'a' * 100}
     q = Question(question_title="Xax", question_body="dddd")
     q.save()
     response = client.post(reverse('qanda:detail', args=(q.id, )),
                            data=data,
                            follow=True)
     self.assertEqual(200, response.status_code)
     self.assertEqual(reverse('qanda:detail', args=(q.id, )),
                      response.redirect_chain[-1][0])
     self.assertEqual(302, response.redirect_chain[-1][1])
Ejemplo n.º 8
0
 def form_valid(self, form):
     action = self.request.POST.get('action')
     if action == 'SAVE':
         return super().form_valid(form)
     elif action == 'PREVIEW':
         preview = Question(question=form.cleaned_data['question'],
                            title=form.cleaned_data['title'])
         ctx = self.get_context_data(preview=preview)
         return self.render_to_response(context=ctx)
     return HttpResponseBadRequest()
Ejemplo n.º 9
0
	def test_retrieve_next_object_by_created_time(self):
		first_question = Question(text="#1")
		second_question = Question(text="#2")
		first_question.save()
		second_question.save()

		retrieved_question = first_question.get_next_by_created()

		self.assertEqual(retrieved_question.text, second_question.text)
Ejemplo n.º 10
0
 def form_valid(self, form):
     action = self.request.POST.get("action")
     if action == "SAVE":
         # save and redirect as usual.
         return super().form_valid(form)
     elif action == "PREVIEW":
         preview = Question(question=form.cleaned_data["question"],
                            title=form.cleaned_data["title"])
         ctx = self.get_context_data(preview=preview)
         return self.render_to_response(context=ctx)
     return HttpResponseBadRequest()
Ejemplo n.º 11
0
	def test_save_and_retrieve_an_answer(self):
		question_stem = Question()
		question_stem.save()

		first_answer = Answer()
		first_answer.text = "First answer ever"
		first_answer.question = question_stem
		first_answer.save()

		second_answer = Answer()
		second_answer.text = "Second answer!"
		second_answer.question = question_stem
		second_answer.save()

		saved_answers = Answer.objects.all()
		self.assertEqual(saved_answers.count(), 2)

		first_saved_answer = saved_answers[0]
		second_saved_answer = saved_answers[1]
		self.assertEqual('First answer ever', first_saved_answer.text)
		self.assertEqual(first_answer.question, question_stem)
		self.assertIn('Second', second_saved_answer.text)
		self.assertEqual(second_answer.question, question_stem)
Ejemplo n.º 12
0
    def form_valid(self, form):
        action = self.request.POST.get('action')
        if action == 'SAVE':
            question = form.save()
            for tag in form.custom_tags:
                try:
                    tag_model = Tag.objects.get(name=tag)
                except Tag.DoesNotExist:
                    tag_model = Tag.objects.create(name=tag)
                question.tags.add(tag_model)
                tag_model.save()

            question.save()
            # Save and redirect as usual
            return super().form_valid(form)
        elif action == 'PREVIEW':
            preview = Question(
                title=form.cleaned_data['title'],
                body=form.cleaned_data['body'])
            ctx = self.get_context_data(preview=preview)
            return self.render_to_response(context=ctx)
        return HttpResponseBadRequest()
Ejemplo n.º 13
0
    def handle(self, *args, **kwargs):

        obj_lst = []

        # Admin user
        adm = User.objects.get(username='******')

        with open('qanda/management/commands/data2.csv', 'r') as csvfile:
            spamreader = csv.reader(csvfile, delimiter=',')
            for row in spamreader:
                o = Question(
                    title=row[1][:120],
                    question=row[2],
                    user=adm
                )
                obj_lst.append(o)
                
        res = Question.objects.bulk_create(obj_lst)
        
        self.stdout.write(self.style.SUCCESS(
            'Successfully loaded all questions into db'
        ))
Ejemplo n.º 14
0
 def test___str__(self):
     q = Question(question_title="My question", question_body="AAA")
     time = timezone.now()
     a = Answer(answer_text="BBB", question_answered=q, pub_date=time)
     self.assertEqual(str(a),
                      "Answer to My question published " + str(time))
Ejemplo n.º 15
0
 def test_not_post(self):
     client = Client()
     q = Question(question_title="Xax", question_body="dddd")
     q.save()
     response = client.get(reverse('qanda:detail', args=(q.id, )))
     self.assertEqual(200, response.status_code)
Ejemplo n.º 16
0
 def test___str__(self):
     q = Question(question_title="My question", question_body="AAA")
     self.assertEqual(str(q), "My question")
Ejemplo n.º 17
0
 def get(self, request, format=None):
     questions = Question.match(request.GET.get('question'))
     serializer = QuestionSerializer(list(questions), many=True)
     return JSONResponse(serializer.data)