Skip to content

Commit

Permalink
added unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
sumandari committed Feb 12, 2022
1 parent 75fe979 commit 6560b4c
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# coding=utf-8
import mock.mock
from unittest.mock import patch, call
from django.urls import reverse
from django.test import TestCase, override_settings
from django.test.client import Client
Expand Down Expand Up @@ -82,7 +84,8 @@ def test_preview_certificate_no_data_posted(self):
self.assertEqual(response.status_code, 200)

@override_settings(VALID_DOMAIN=['testserver', ])
def test_preview_certificate_with_posted_data(self):
@patch('certification.views.certificate.generate_pdf')
def test_preview_certificate_with_posted_data(self, mock_gen_pdf):
client = Client(HTTP_HOST='testserver')
client.login(username='anita', password='password')
post_data = {
Expand All @@ -98,3 +101,61 @@ def test_preview_certificate_with_posted_data(self):
'organisation_slug': self.test_certifying_organisation.slug
}), post_data)
self.assertEqual(response.status_code, 200)
# Only 6 args in generate_pdf call,
# Since there's no CertificateType id in POST body
self.assertEqual(len(mock_gen_pdf.call_args[0]), 6)
self.assertIn(self.test_project, mock_gen_pdf.call_args[0])
self.assertNotIn(
self.certificate_type.wording, mock_gen_pdf.call_args[0])

@override_settings(VALID_DOMAIN=['testserver', ])
@patch('certification.views.certificate.generate_pdf')
def test_preview_certificate_with_posted_data_and_certificate_type(
self, mock_gen_pdf):
client = Client(HTTP_HOST='testserver')
client.login(username='anita', password='password')
post_data = {
'course_convener': self.convener.pk,
'training_center': self.training_center.pk,
'course_type': self.course_type.pk,
'start_date': '2018-01-01',
'end_date': '2018-02-01',
'template_certificate': '',
'certificate_type': self.certificate_type.id
}
response = client.post(reverse('preview-certificate', kwargs={
'project_slug': self.test_project.slug,
'organisation_slug': self.test_certifying_organisation.slug
}), post_data)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(mock_gen_pdf.call_args[0]), 7)
self.assertIn(self.test_project, mock_gen_pdf.call_args[0])
self.assertIn(self.certificate_type.wording, mock_gen_pdf.call_args[0])


@override_settings(VALID_DOMAIN=['testserver', ])
@patch('certification.views.certificate.generate_pdf')
def test_preview_certificate_with_posted_data_cert_type_not_found(
self, mock_gen_pdf):
client = Client(HTTP_HOST='testserver')
client.login(username='anita', password='password')
post_data = {
'course_convener': self.convener.pk,
'training_center': self.training_center.pk,
'course_type': self.course_type.pk,
'start_date': '2018-01-01',
'end_date': '2018-02-01',
'template_certificate': '',
'certificate_type': 99999
}
response = client.post(reverse('preview-certificate', kwargs={
'project_slug': self.test_project.slug,
'organisation_slug': self.test_certifying_organisation.slug
}), post_data)
self.assertEqual(response.status_code, 200)
# Only 6 args in generate_pdf call,
# Since there's the CertificateType id doesn't exist
self.assertEqual(len(mock_gen_pdf.call_args[0]), 6)
self.assertIn(self.test_project, mock_gen_pdf.call_args[0])
self.assertNotIn(
self.certificate_type.wording, mock_gen_pdf.call_args[0])
27 changes: 27 additions & 0 deletions django_project/certification/tests/views/test_course_view.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# coding=utf-8
import logging
from bs4 import BeautifulSoup as Soup

from django.test import TestCase, override_settings
from django.test.client import Client
from django.urls import reverse
from certification.tests.model_factories import (
ProjectF,
UserF,
CertifyingOrganisationF,
CertificateTypeF,
ProjectCertificateTypeF,
CourseF
)

Expand Down Expand Up @@ -41,6 +45,11 @@ def setUp(self):
self.course = CourseF.create(
certifying_organisation=self.certifying_organisation
)
self.certificate_type = CertificateTypeF.create()
self.project_cert_type = ProjectCertificateTypeF.create(
project=self.project,
certificate_type=self.certificate_type
)

@override_settings(VALID_DOMAIN=['testserver', ])
def tearDown(self):
Expand All @@ -55,6 +64,24 @@ def tearDown(self):
self.project.delete()
self.user.delete()

@override_settings(VALID_DOMAIN=['testserver', ])
def test_create_course_must_showing_CertificateTypes(self):
self.client.login(username='anita', password='password')
response = self.client.get(reverse('course-create', kwargs={
'project_slug': self.project.slug,
'organisation_slug': self.certifying_organisation.slug,
}))
self.assertEqual(response.status_code, 200)
soup = Soup(response.content, "html5lib")
cert_type_option = soup.find(
'select',
{'id': 'id_certificate_type'}
).find_all('option')
self.assertIn(
self.certificate_type.name,
[cert_type.text for cert_type in cert_type_option]
)

@override_settings(VALID_DOMAIN=['testserver', ])
def test_detail_view(self):
client = Client()
Expand Down

0 comments on commit 6560b4c

Please sign in to comment.