From 2f61df2c650f4cbef67f1a81849a728154199f7a Mon Sep 17 00:00:00 2001 From: littlewolf Date: Tue, 24 Sep 2019 16:20:04 +0300 Subject: [PATCH 1/6] Add address CRUD tests --- apps/location/tests.py | 74 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/apps/location/tests.py b/apps/location/tests.py index 7ce503c2..cab871a0 100644 --- a/apps/location/tests.py +++ b/apps/location/tests.py @@ -1,3 +1,73 @@ -from django.test import TestCase +from rest_framework.test import APITestCase +from account.models import User +from rest_framework import status +from http.cookies import SimpleCookie -# Create your tests here. +from location.models import City, Region, Country + + +class BaseTestCase(APITestCase): + + def setUp(self): + self.username = 'sedragurda' + self.password = 'sedragurdaredips19' + self.email = 'sedragurda@desoz.com' + self.newsletter = True + self.user = User.objects.create_user( + username=self.username, email=self.email, password=self.password) + + # get tokens + + tokkens = User.create_jwt_tokens(self.user) + self.client.cookies = SimpleCookie( + {'access_token': tokkens.get('access_token'), + 'refresh_token': tokkens.get('refresh_token')}) + + +class AddressTests(BaseTestCase): + + def setUp(self): + super().setUp() + + self.country = Country( + name="Test country", + code="+7" + ) + + self.region = Region( + name="Test region", + code="812", + country=self.country + ) + + self.city = City( + name="Test region", + code="812", + region=self.region, + country=self.country + ) + + def test_address_CRUD(self): + response = self.client.get('/api/back/location/addresses/', format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + + data = { + 'user': self.user.id, + 'name': 'Test name' + } + + response = self.client.post('/api/back/location/addresses/', data=data) + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + + response = self.client.get('/api/back/location/addresses/1/', format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + + update_data = { + 'name': 'Test new name' + } + + response = self.client.patch('/api/back/location/addresses/1/', data=update_data) + self.assertEqual(response.status_code, status.HTTP_200_OK) + + response = self.client.delete('/api/back/location/addresses/1/', format='json') + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) From d62cee341cddc5195f8aa97c43cbd254c942dde7 Mon Sep 17 00:00:00 2001 From: littlewolf Date: Tue, 24 Sep 2019 21:03:50 +0300 Subject: [PATCH 2/6] Add address tests --- apps/location/tests.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/apps/location/tests.py b/apps/location/tests.py index cab871a0..a3213e69 100644 --- a/apps/location/tests.py +++ b/apps/location/tests.py @@ -1,8 +1,11 @@ +import json + from rest_framework.test import APITestCase from account.models import User from rest_framework import status from http.cookies import SimpleCookie +from django.contrib.gis.db.models import PointField from location.models import City, Region, Country @@ -28,19 +31,18 @@ class AddressTests(BaseTestCase): def setUp(self): super().setUp() - - self.country = Country( - name="Test country", - code="+7" + self.country = Country.objects.create( + name=json.dumps({"en-GB": "Test country"}), + code="test" ) - self.region = Region( + self.region = Region.objects.create( name="Test region", code="812", country=self.country ) - self.city = City( + self.city = City.objects.create( name="Test region", code="812", region=self.region, @@ -52,18 +54,25 @@ class AddressTests(BaseTestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) data = { - 'user': self.user.id, - 'name': 'Test name' + 'city_id': self.city.id, + 'number': '+79999999', + "coordinates": { + "latitude": 37.0625, + "longitude": -95.677068 + }, + "geo_lon": -95.677068, + "geo_lat": 37.0625 } - response = self.client.post('/api/back/location/addresses/', data=data) + response = self.client.post('/api/back/location/addresses/', data=data, format='json') + print(f"=========RESPONSE: {response.json()}") self.assertEqual(response.status_code, status.HTTP_201_CREATED) response = self.client.get('/api/back/location/addresses/1/', format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) update_data = { - 'name': 'Test new name' + 'number': '+79999991' } response = self.client.patch('/api/back/location/addresses/1/', data=update_data) From 6b3558e3acf56ade095cd7989b072983c640162d Mon Sep 17 00:00:00 2001 From: littlewolf Date: Tue, 24 Sep 2019 21:28:11 +0300 Subject: [PATCH 3/6] Add country tests --- apps/location/tests.py | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/apps/location/tests.py b/apps/location/tests.py index a3213e69..0d7803bc 100644 --- a/apps/location/tests.py +++ b/apps/location/tests.py @@ -27,6 +27,35 @@ class BaseTestCase(APITestCase): 'refresh_token': tokkens.get('refresh_token')}) +class CountryTests(BaseTestCase): + + def test_country_CRUD(self): + response = self.client.get('/api/back/location/countries/', format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + + data = { + 'name': 'Test country', + 'code': 'test' + } + + response = self.client.post('/api/back/location/countries/', data=data, format='json') + response_data = response.json() + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + + response = self.client.get(f'/api/back/location/countries/{response_data["id"]}/', format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + + update_data = { + 'name': json.dumps({"en-GB": "Test new country"}) + } + + response = self.client.patch(f'/api/back/location/countries/{response_data["id"]}/', data=update_data) + self.assertEqual(response.status_code, status.HTTP_200_OK) + + response = self.client.delete(f'/api/back/location/countries/{response_data["id"]}/', format='json') + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) + + class AddressTests(BaseTestCase): def setUp(self): @@ -65,18 +94,18 @@ class AddressTests(BaseTestCase): } response = self.client.post('/api/back/location/addresses/', data=data, format='json') - print(f"=========RESPONSE: {response.json()}") + response_data = response.json() self.assertEqual(response.status_code, status.HTTP_201_CREATED) - response = self.client.get('/api/back/location/addresses/1/', format='json') + response = self.client.get(f'/api/back/location/addresses/{response_data["id"]}/', format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) update_data = { 'number': '+79999991' } - response = self.client.patch('/api/back/location/addresses/1/', data=update_data) + response = self.client.patch(f'/api/back/location/addresses/{response_data["id"]}/', data=update_data) self.assertEqual(response.status_code, status.HTTP_200_OK) - response = self.client.delete('/api/back/location/addresses/1/', format='json') + response = self.client.delete(f'/api/back/location/addresses/{response_data["id"]}/', format='json') self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) From 7455d2c8564144b4a746b930042f3399ec3404b6 Mon Sep 17 00:00:00 2001 From: littlewolf Date: Tue, 24 Sep 2019 21:30:59 +0300 Subject: [PATCH 4/6] Add region tests --- apps/location/tests.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/apps/location/tests.py b/apps/location/tests.py index 0d7803bc..ceeb2043 100644 --- a/apps/location/tests.py +++ b/apps/location/tests.py @@ -56,6 +56,43 @@ class CountryTests(BaseTestCase): self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) +class RegionTests(BaseTestCase): + + def setUp(self): + super().setUp() + self.country = Country.objects.create( + name=json.dumps({"en-GB": "Test country"}), + code="test" + ) + + def test_region_CRUD(self): + response = self.client.get('/api/back/location/countries/', format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + + data = { + 'name': 'Test country', + 'code': 'test', + 'country_id': self.country.id + } + + response = self.client.post('/api/back/location/regions/', data=data, format='json') + response_data = response.json() + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + + response = self.client.get(f'/api/back/location/regions/{response_data["id"]}/', format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + + update_data = { + 'name': json.dumps({"en-GB": "Test new country"}) + } + + response = self.client.patch(f'/api/back/location/regions/{response_data["id"]}/', data=update_data) + self.assertEqual(response.status_code, status.HTTP_200_OK) + + response = self.client.delete(f'/api/back/location/regions/{response_data["id"]}/', format='json') + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) + + class AddressTests(BaseTestCase): def setUp(self): From c50bdc863745caf442a8a51f77e964a43be7bbf9 Mon Sep 17 00:00:00 2001 From: littlewolf Date: Tue, 24 Sep 2019 21:34:21 +0300 Subject: [PATCH 5/6] Add city tests --- apps/location/tests.py | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/apps/location/tests.py b/apps/location/tests.py index ceeb2043..d85db414 100644 --- a/apps/location/tests.py +++ b/apps/location/tests.py @@ -66,7 +66,7 @@ class RegionTests(BaseTestCase): ) def test_region_CRUD(self): - response = self.client.get('/api/back/location/countries/', format='json') + response = self.client.get('/api/back/location/regions/', format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) data = { @@ -93,6 +93,46 @@ class RegionTests(BaseTestCase): self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) +class CityTests(RegionTests): + + def setUp(self): + super().setUp() + + self.region = Region.objects.create( + name="Test region", + code="812", + country=self.country + ) + + def test_city_CRUD(self): + response = self.client.get('/api/back/location/cities/', format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + + data = { + 'name': 'Test country', + 'code': 'test', + 'country_id': self.country.id, + 'region_id': self.region.id + } + + response = self.client.post('/api/back/location/cities/', data=data, format='json') + response_data = response.json() + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + + response = self.client.get(f'/api/back/location/cities/{response_data["id"]}/', format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + + update_data = { + 'name': json.dumps({"en-GB": "Test new country"}) + } + + response = self.client.patch(f'/api/back/location/cities/{response_data["id"]}/', data=update_data) + self.assertEqual(response.status_code, status.HTTP_200_OK) + + response = self.client.delete(f'/api/back/location/cities/{response_data["id"]}/', format='json') + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) + + class AddressTests(BaseTestCase): def setUp(self): From 2261875d3c64c00d449bbe988e0f6ccf8a4309ee Mon Sep 17 00:00:00 2001 From: littlewolf Date: Tue, 24 Sep 2019 21:36:59 +0300 Subject: [PATCH 6/6] Fix tests --- apps/location/tests.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/apps/location/tests.py b/apps/location/tests.py index d85db414..f68ba56b 100644 --- a/apps/location/tests.py +++ b/apps/location/tests.py @@ -5,7 +5,6 @@ from account.models import User from rest_framework import status from http.cookies import SimpleCookie -from django.contrib.gis.db.models import PointField from location.models import City, Region, Country @@ -93,11 +92,16 @@ class RegionTests(BaseTestCase): self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) -class CityTests(RegionTests): +class CityTests(BaseTestCase): def setUp(self): super().setUp() + self.country = Country.objects.create( + name=json.dumps({"en-GB": "Test country"}), + code="test" + ) + self.region = Region.objects.create( name="Test region", code="812", @@ -137,6 +141,7 @@ class AddressTests(BaseTestCase): def setUp(self): super().setUp() + self.country = Country.objects.create( name=json.dumps({"en-GB": "Test country"}), code="test"