|
| 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 |
0 commit comments