def test_question_form(user): from forum.forms import QuestionForm from forum.models import Question instance = Question() instance.course = mommy.make('Course', slug='dbsql') instance.user = user data = { 'title': 'Test Title', 'text': 'asljf asdfhuas dfasdflashfdlusafdlsafdlsa filasdflisalfdiayslfdnsalfdyaslifd', } form = QuestionForm(instance=instance, data=data) assert form.is_valid() is True, form.errors form.save() assert Question.objects.filter(slug='test-title').exists() data = { 'title': '', 'text': 'asljf asdfhuas dfasdflashfdlusafdlsafdlsa filasdflisalfdiayslfdnsalfdyaslifd', } form = QuestionForm(data=data) assert form.is_valid() is False data = { 'title': 'Test Title', 'text': '', } form = QuestionForm(data=data) assert form.is_valid() is False
def post(self, request, *args, **kwargs): new_question = Question() course = get_object_or_404(Course, slug=self.kwargs['course_slug']) new_question.course = course new_question.user = request.user form = QuestionForm(instance=new_question, **self.get_form_kwargs()) if form.is_valid(): form.save() self.success_url = reverse_lazy('forum_question', kwargs={'slug': new_question.slug}) return self.form_valid(form) else: return self.form_invalid(form)
def test_basic_addition(self): user = User.objects.all()[0] point = Point() point.fromuser = user point.save() question = Question(); question.user = user question.title = "hi" question.slug = 'something' question.text = 'else' question.save() question.points.add(point) question.save() self.assertEquals(1, question.total_points())
def test_user_points(self): josh = User.objects.all()[0] question = Question(); question.user = josh question.title = "hi" question.slug = 'something' question.text = 'else' question.save() point = Point() point.fromuser = josh point.save() question.points.add(point) user = User() user.save() point = Point() point.fromuser = user point.save() question.points.add(point) self.assertEquals(2, userPoint(josh))
def test_add_point_to_question(self): self.client.login(username='******', password='******') user = User.objects.all()[0] question = Question(); question.user = user question.title = "hi" question.slug = 'something' question.text = 'else' question.save() response = self.client.post('/ajax/forum/upvote_question.json', {'question_pk': question.pk}) json_response = json.loads(response.content) self.assertTrue(json_response['success']) self.assertTrue(json_response['data']['delta'], 1) self.assertEquals(1, Question.objects.all().count()) response = self.client.post('/ajax/forum/upvote_question.json', {'question_pk': question.pk}) json_response = json.loads(response.content) self.assertTrue(json_response['success']) self.assertTrue(json_response['data']['delta'], -1) print Point.objects.all() self.assertEquals(0, Point.objects.all().count())