Merge pull request #116 from danieldupriest/testing-mock
Implemented tests for create_report and get_report
This commit is contained in:
commit
4a3cac48bd
4 changed files with 65 additions and 2 deletions
|
@ -1,10 +1,11 @@
|
|||
import datetime
|
||||
import datetime as mydate
|
||||
|
||||
#### Classes for policy, sections. Do not edit these.
|
||||
#####################################################
|
||||
|
||||
def to_date(iso8601):
|
||||
return datetime.datetime.strptime(iso8601, "%Y-%m-%d")
|
||||
return mydate.datetime.strptime(iso8601, "%Y-%m-%d")
|
||||
|
||||
class Policy():
|
||||
"""
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from django.test import TestCase
|
||||
from .policy import pol
|
||||
from unittest import mock
|
||||
import datetime
|
||||
|
||||
class PolicyTests(TestCase):
|
||||
report = {"key":"value"}
|
||||
|
@ -50,7 +51,9 @@ class PolicyTests(TestCase):
|
|||
result = pol.sections[1].rules[2]['rule'](self.report, fields)
|
||||
self.assertEqual(result, "Flights must be booked at least 14 days in advance.")
|
||||
|
||||
def test_pre_flight_section_departure_date_too_early(self):
|
||||
@mock.patch('datetime.date')
|
||||
def test_pre_flight_section_departure_date_too_early(self, mocked_date):
|
||||
mocked_date.today = mock.Mock(return_value=datetime.date(2019,1,1))
|
||||
fields = {'departure_date':'2020-03-10','screenshot_date':'2019-03-01'}
|
||||
result = pol.sections[1].rules[2]['rule'](self.report, fields)
|
||||
self.assertEqual(result, "Flights must be booked no more than 365 days in advance.")
|
||||
|
|
59
back/backend/test_report.py
Normal file
59
back/backend/test_report.py
Normal file
|
@ -0,0 +1,59 @@
|
|||
from django.test import TestCase
|
||||
from rest_framework.test import APIRequestFactory, force_authenticate
|
||||
from backend.models import Report
|
||||
from users.models import CustomUser
|
||||
from unittest.mock import Mock, patch
|
||||
from datetime import date
|
||||
from backend.views import *
|
||||
|
||||
class ReportTests(TestCase):
|
||||
|
||||
def create_test_user(self, email, first, last, password):
|
||||
user = CustomUser.objects.create_user(username=email, email=email, first_name=first, last_name=last, password=password)
|
||||
return user
|
||||
|
||||
def mock_report():
|
||||
r = Mock()
|
||||
r.report_pk = 1
|
||||
r.title = 'Report Title'
|
||||
r.date_created = '2019-03-01'
|
||||
r.date_submitted = '2019-03-01'
|
||||
r.submitted = False
|
||||
r.reference_number = '12345'
|
||||
return r
|
||||
|
||||
def setUp(self):
|
||||
self.test_user_1 = self.create_test_user('one@one.com', 'One', 'Mr. One', '1password')
|
||||
self.test_user_1.save()
|
||||
|
||||
def test_create_report_logged_in(self):
|
||||
factory = APIRequestFactory()
|
||||
request = factory.post('/api/v1/report', {'title':'Test Report', 'reference':'12345'})
|
||||
user = CustomUser.objects.get(email='one@one.com')
|
||||
force_authenticate(request, user=user)
|
||||
response = create_report(request)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
report = Report.objects.get(user_id=user)
|
||||
self.assertEqual(report.title, 'Test Report')
|
||||
|
||||
def test_create_report_logged_out(self):
|
||||
factory = APIRequestFactory()
|
||||
request = factory.post('/api/v1/report', {'title':'Test Report', 'reference':'12345'})
|
||||
response = create_report(request)
|
||||
self.assertEqual(response.status_code, 401)
|
||||
|
||||
@patch('backend.models.Report.objects.filter', Mock(return_value=[mock_report()]))
|
||||
@patch('backend.views.get_sections', Mock(return_value={}))
|
||||
def test_get_report(self):
|
||||
result = get_report(1)
|
||||
self.assertEqual(
|
||||
result,
|
||||
{
|
||||
'date_created':'2019-03-01',
|
||||
'reference_number':'12345',
|
||||
'report_pk':1,
|
||||
'title':'Report Title',
|
||||
'date_submitted':'2019-03-01',
|
||||
'submitted':False
|
||||
}
|
||||
)
|
BIN
back/db.sqlite3
BIN
back/db.sqlite3
Binary file not shown.
Loading…
Reference in a new issue