Skip to content

Commit

Permalink
#29 Increase test coverage for views, handler (#45)
Browse files Browse the repository at this point in the history
* #29 Update Pipfile

* #28 Update test.py

- [x] Test cases for views
- [x] Test case for 404 / page not found handler

* #28 Update test.py

- [x] Adding test case for invalid date format when calling bookings_by_date method
  • Loading branch information
conorheffron authored Sep 18, 2024
1 parent f3eb150 commit f8cdd46
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 6 deletions.
31 changes: 26 additions & 5 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,32 @@ verify_ssl = true
name = "pypi"

[packages]
django = "*"
djangorestframework = "*"
djangorestframework-xml = "*"
mysqlclient = "*"
pylint = "*"
django = "==5.1.1"
djangorestframework = "==3.15.2"
djangorestframework-xml = "==2.0.0"
mysqlclient = "==2.2.4"
pylint = "==3.2.7"
asgiref = "==3.8.1"
astroid = "==3.2.4"
certifi = "==2024.7.4"
defusedxml = "==0.7.1"
dill = "==0.3.8"
distlib = "==0.3.8"
filelock = "==3.15.4"
isort = "==5.13.2"
mccabe = "==0.7.0"
pipenv = "==2024.0.1"
platformdirs = "==4.2.2"
sqlparse = "==0.5.1"
tomlkit = "==0.13.2"
virtualenv = "==20.26.3"
pytz = "==2024.2"
requests = "==2.32.3"
pylint-django = "==2.5.5"
charset-normalizer = "==3.3.2"
idna = "==3.10"
pylint-plugin-utils = "==0.8.2"
urllib3 = "==2.2.3"

[dev-packages]

Expand Down
117 changes: 116 additions & 1 deletion restaurant/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
Restaurant Tests Suite
"""
from datetime import datetime
import json
from django.test import TestCase
from django.core.exceptions import ValidationError
from .models import Reservation
from .forms import ReservationForm


# Restaurant Tests
class RestaurantTests(TestCase):
"""Restaurant Test cases
Expand Down Expand Up @@ -55,3 +56,117 @@ def test_populate_form(self):
self.assertEqual(result.__dict__['data']["first_name"], "Conor")
self.assertEqual(result.__dict__['data']["reservation_date"], booking_date)
self.assertEqual(result.__dict__['data']["reservation_slot"], booking_slot)

def test_reservations_view_success(self):
"""Restaurant Test case test_reservations_view_success
Parameters
----------
self : TestCase
"""
# when
response = self.client.get('/reservations/')

# then
self.assertContains(response, '<h3>All Bookings</h3>\n ' +
'<table>\n <tr> \n' +
' <th>First Name</th>\n' +
' <th>Booking Date</th>\n' +
' <th>Booking time</th>\n' +
' </tr>\n \n' +
' </table>\n <br />\n <button type="button" ' +
'class="btn btn-primary" onClick="refresh()">Refresh</button>\n ' +
'</div>\n<body>',
status_code=200)
self.assertTemplateUsed(response, 'reservations.html')

def test_bookings_by_date_request_param_success(self):
"""Restaurant Test case test_bookings_by_date_request_param_success
Parameters
----------
self : TestCase
"""
# when
response = self.client.get('/bookings?date=2024-09-18')

# then
self.assertContains(response, json.dumps({"message": "success", "reservations": []}),
status_code=200)

def test_bookings_by_date_request_param_fail(self):
"""Restaurant Test case test_bookings_by_date_request_param_fail
Parameters
----------
self : TestCase
"""
# when / then
with self.assertRaisesMessage(ValidationError,
'“2024-0918” value has an invalid date format. ' +
'It must be in YYYY-MM-DD format.'):
self.client.get('/bookings?date=2024-0918')

def test_bookings_by_date_path_var_success(self):
"""Restaurant Test case test_bookings_by_date_path_var_success
Parameters
----------
self : TestCase
"""
# when
response = self.client.get('/bookings/2024-09-18/')

# then
self.assertContains(response, '', status_code=200)

def test_home_page_success(self):
"""Restaurant Test case test_home_page_success
Parameters
----------
self : TestCase
"""
# when
response = self.client.get('/book/')

# then
self.assertContains(response, '\n <h3>Bookings by Date</h3>\n <table>\n' +
' <tbody id="tableData"></tbody>\n </table>\n ' +
'</div>\n \n</body>\n</html>',
status_code=200 )
self.assertTemplateUsed(response, 'booking.html')

def test_booking_success(self):
"""Restaurant Test case test_booking_success
Parameters
----------
self : TestCase
"""
# given
test_name = 'Conor'
test_date = '2024-09-18'
test_time = '18:29'

# when
response = self.client.post('/book/', data={'first_name': test_name,
'reservation_date': test_date,
'reservation_slot': test_time})

# then
self.assertContains(response, json.dumps({"message": "Booking Complete",
"reservations": [
{"first_name": test_name,
"reservation_date": test_date,
"reservation_slot": test_time + ':00'}]}),
status_code=200)

def test_handler404_success(self):
"""Restaurant Test case test_handler404_success
Parameters
----------
self : TestCase
"""
# when
response = self.client.get('/reservations/invalid')

# then
self.assertContains(response, '<h1>Page Not Found: /reservations/invalid</h1>\n ' +
'</div>\n<body>',
status_code=404 )
self.assertTemplateUsed(response, 'error.html')

0 comments on commit f8cdd46

Please sign in to comment.