Skip to content

Commit 7542e9b

Browse files
committed
Test: convert backend tests to regular unittest
1 parent d527e91 commit 7542e9b

File tree

8 files changed

+133
-129
lines changed

8 files changed

+133
-129
lines changed

backend/api/tests/__init__.py

Whitespace-only changes.

backend/api/tests/models/location_tests.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

backend/api/tests/models/rental_tests.py

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from decimal import Decimal
2+
3+
from api.tests.factories.location import LocationFactory
4+
5+
from django.test import TestCase
6+
7+
class TestLocationModel(TestCase):
8+
9+
def test_create_location(self):
10+
LocationFactory(
11+
lat=Decimal(20.123456789),
12+
lng=Decimal(20.123456789),
13+
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from api.tests.factories.rental import RentalFactory
2+
from api.tests.factories.location import LocationFactory
3+
4+
from django.test import TestCase
5+
6+
class TestRentalModel(TestCase):
7+
8+
def test_create_rental(self):
9+
location = LocationFactory()
10+
RentalFactory(
11+
description='a description',
12+
owner='a owner',
13+
city='a city',
14+
title='a title',
15+
category='House',
16+
image='link to a image',
17+
bedrooms=4,
18+
location=location
19+
)

backend/api/tests/views/rental_tests.py

Lines changed: 0 additions & 91 deletions
This file was deleted.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
from api.tests.factories.rental import RentalFactory
2+
from api.tests.factories.location import LocationFactory
3+
4+
from django.test import TestCase
5+
6+
class TestFoo(TestCase):
7+
8+
# helps clean up JSON:API tests
9+
# TODO: move this to some kind of test helpers file
10+
def get_item_from_response(self, response_data, item_type, item_id) -> dict:
11+
_id = item_id if isinstance(item_id, str) else str(item_id)
12+
for item in response_data:
13+
if item['type'] == item_type and item['id'] == _id:
14+
return item
15+
return {}
16+
17+
def test_get_rentals_returns_single_rental(self):
18+
location1 = LocationFactory()
19+
rental1 = RentalFactory(location=location1)
20+
# we aren't using django's reverse utility for urls
21+
# this is because the frontent is relying on these urls not to change
22+
# if we used reverse we can easliy pass all our API tests but the frontend will break
23+
response = self.client.get(f'/api/rentals/{rental1.pk}/')
24+
# having the first assert for status code is helpful
25+
# because a 200 != 404 is much clearer than "KeyError" or other such error
26+
# can make a huge difference tracking down bugs due to a large refactor
27+
assert 200 == response.status_code
28+
29+
result_rental1 = response.json()['data']['attributes']
30+
assert result_rental1['owner'] == rental1.owner
31+
assert result_rental1['city'] == rental1.city
32+
assert result_rental1['title'] == rental1.title
33+
assert result_rental1['category'] == rental1.category
34+
assert result_rental1['description'] == rental1.description
35+
assert result_rental1['image'] == rental1.image
36+
assert result_rental1['bedrooms'] == rental1.bedrooms
37+
38+
def test_get_rentals_returns_all_rentals(self):
39+
location1 = LocationFactory()
40+
location2 = LocationFactory()
41+
rental1 = RentalFactory(location=location1)
42+
rental2 = RentalFactory(location=location2)
43+
response = self.client.get('/api/rentals/')
44+
assert 200 == response.status_code
45+
assert 2 == len(response.json()['data'])
46+
47+
result_rental1 = self.get_item_from_response(response.json()['data'], 'Rental', rental1.pk)['attributes']
48+
assert result_rental1['owner'] == rental1.owner
49+
assert result_rental1['city'] == rental1.city
50+
assert result_rental1['title'] == rental1.title
51+
assert result_rental1['category'] == rental1.category
52+
assert result_rental1['description'] == rental1.description
53+
assert result_rental1['image'] == rental1.image
54+
assert result_rental1['bedrooms'] == rental1.bedrooms
55+
56+
result_rental2 = self.get_item_from_response(response.json()['data'], 'Rental', rental2.pk)['attributes']
57+
assert result_rental2['owner'] == rental2.owner
58+
assert result_rental2['city'] == rental2.city
59+
assert result_rental2['title'] == rental2.title
60+
assert result_rental2['category'] == rental2.category
61+
assert result_rental2['description'] == rental2.description
62+
assert result_rental2['image'] == rental2.image
63+
assert result_rental2['bedrooms'] == rental2.bedrooms
64+
65+
def test_get_rentals_returns_locations_in_included(self):
66+
rental = RentalFactory()
67+
response = self.client.get('/api/rentals/')
68+
assert 200 == response.status_code
69+
assert 1 == len(response.json()['included'])
70+
71+
result_rental1_location1 = self.get_item_from_response(
72+
response.json()['included'],
73+
'Location',
74+
rental.location.pk
75+
)['attributes']
76+
assert result_rental1_location1['lat'] == str(round(rental.location.lat, 6))
77+
assert result_rental1_location1['lng'] == str(round(rental.location.lng, 6))
78+
79+
def test_minimal_queries_with_many_relations(self):
80+
for i in range(10):
81+
RentalFactory()
82+
with self.assertNumQueries(2):
83+
response = self.client.get('/api/rentals/')
84+
assert 200 == response.status_code

docker-compose.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ services:
1111
environment:
1212
NODE_ENV: development
1313
backend:
14+
image: backend
1415
build:
1516
context: ./backend
1617
volumes:
@@ -20,4 +21,19 @@ services:
2021
poetry run python manage.py loaddata initial-data.json;
2122
poetry run python manage.py runserver 0.0.0.0:8000"
2223
ports:
23-
- 8000:8000
24+
- 8000:8000
25+
test-backend:
26+
image: backend
27+
build:
28+
context: ./backend
29+
volumes:
30+
- ./backend:/code
31+
command: >
32+
sh -c "poetry run python manage.py test"
33+
debug-backend:
34+
image: backend
35+
build:
36+
context: ./backend
37+
volumes:
38+
- ./backend:/code
39+
command: bash

0 commit comments

Comments
 (0)