-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from maykinmedia/feature/32-location-api
Feature/32 location api
- Loading branch information
Showing
22 changed files
with
1,951 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from django.urls import include, path | ||
|
||
from rest_framework.routers import DefaultRouter | ||
|
||
from open_producten.locations.views import ( | ||
ContactViewSet, | ||
LocationViewSet, | ||
NeighbourhoodViewSet, | ||
OrganisationTypeViewSet, | ||
OrganisationViewSet, | ||
) | ||
|
||
LocationRouter = DefaultRouter() | ||
|
||
LocationRouter.register("locations", LocationViewSet, basename="locations") | ||
LocationRouter.register("organisations", OrganisationViewSet, basename="organisations") | ||
LocationRouter.register("contacts", ContactViewSet, basename="contacts") | ||
LocationRouter.register( | ||
"neighbourhoods", NeighbourhoodViewSet, basename="neighbourhoods" | ||
) | ||
LocationRouter.register( | ||
"organisationtypes", OrganisationTypeViewSet, basename="organisationtypes" | ||
) | ||
|
||
location_urlpatterns = [ | ||
path("", include(LocationRouter.urls)), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from rest_framework import serializers | ||
|
||
from open_producten.locations.models import ( | ||
Contact, | ||
Location, | ||
Neighbourhood, | ||
Organisation, | ||
OrganisationType, | ||
) | ||
|
||
|
||
class LocationSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Location | ||
fields = "__all__" | ||
|
||
|
||
class NeighbourhoodSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Neighbourhood | ||
fields = "__all__" | ||
|
||
|
||
class OrganisationTypeSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = OrganisationType | ||
fields = "__all__" | ||
|
||
|
||
class OrganisationSerializer(serializers.ModelSerializer): | ||
neighbourhood = NeighbourhoodSerializer(read_only=True) | ||
type = OrganisationTypeSerializer(read_only=True) | ||
|
||
neighbourhood_id = serializers.PrimaryKeyRelatedField( | ||
write_only=True, queryset=Neighbourhood.objects.all(), source="neighbourhood" | ||
) | ||
|
||
type_id = serializers.PrimaryKeyRelatedField( | ||
write_only=True, queryset=OrganisationType.objects.all(), source="type" | ||
) | ||
|
||
class Meta: | ||
model = Organisation | ||
fields = "__all__" | ||
|
||
|
||
class ContactSerializer(serializers.ModelSerializer): | ||
organisation = OrganisationSerializer(read_only=True) | ||
organisation_id = serializers.PrimaryKeyRelatedField( | ||
write_only=True, queryset=Organisation.objects.all(), source="organisation" | ||
) | ||
|
||
class Meta: | ||
model = Contact | ||
fields = "__all__" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from rest_framework.test import APIClient | ||
|
||
from open_producten.locations.models import Contact | ||
from open_producten.utils.tests.cases import BaseApiTestCase | ||
from open_producten.utils.tests.helpers import model_to_dict_with_id | ||
|
||
from ..factories import ContactFactory, OrganisationFactory | ||
|
||
|
||
def contact_to_dict(contact): | ||
contact_dict = model_to_dict_with_id(contact) | ||
contact_dict["organisation"] = model_to_dict_with_id(contact.organisation) | ||
contact_dict["organisation"]["type"] = model_to_dict_with_id( | ||
contact.organisation.type | ||
) | ||
contact_dict["organisation"]["neighbourhood"] = model_to_dict_with_id( | ||
contact.organisation.neighbourhood | ||
) | ||
return contact_dict | ||
|
||
|
||
class TestContact(BaseApiTestCase): | ||
|
||
def setUp(self): | ||
super().setUp() | ||
organisation = OrganisationFactory.create() | ||
self.data = { | ||
"first_name": "bob", | ||
"last_name": "de vries", | ||
"organisation_id": organisation.id, | ||
} | ||
self.path = "/api/v1/contacts/" | ||
|
||
self.contact = ContactFactory.create() | ||
|
||
def test_read_contact_without_credentials_returns_error(self): | ||
response = APIClient().get(self.path) | ||
self.assertEqual(response.status_code, 401) | ||
|
||
def test_create_contact(self): | ||
response = self.post(self.data) | ||
|
||
self.assertEqual(response.status_code, 201) | ||
self.assertEqual(Contact.objects.count(), 2) | ||
|
||
def test_update_contact(self): | ||
data = self.data | {"first_name": "updated"} | ||
response = self.put(self.contact.id, data) | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(Contact.objects.count(), 1) | ||
self.assertEqual(Contact.objects.first().first_name, "updated") | ||
|
||
def test_partial_update_contact(self): | ||
data = {"first_name": "updated"} | ||
response = self.patch(self.contact.id, data) | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(Contact.objects.count(), 1) | ||
self.assertEqual(Contact.objects.first().first_name, "updated") | ||
|
||
def test_read_contacts(self): | ||
response = self.get() | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(response.data["count"], 1) | ||
self.assertEqual(response.data["results"], [contact_to_dict(self.contact)]) | ||
|
||
def test_read_contact(self): | ||
response = self.get(self.contact.id) | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(response.data, contact_to_dict(self.contact)) | ||
|
||
def test_delete_contact(self): | ||
response = self.delete(self.contact.id) | ||
|
||
self.assertEqual(response.status_code, 204) | ||
self.assertEqual(Contact.objects.count(), 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from rest_framework.test import APIClient | ||
|
||
from open_producten.locations.models import Location | ||
from open_producten.utils.tests.cases import BaseApiTestCase | ||
from open_producten.utils.tests.helpers import model_to_dict_with_id | ||
|
||
from ..factories import LocationFactory | ||
|
||
|
||
def location_to_dict(location): | ||
return model_to_dict_with_id(location) | ||
|
||
|
||
class TestLocation(BaseApiTestCase): | ||
|
||
def setUp(self): | ||
super().setUp() | ||
self.data = {"name": "locatie", "postcode": "1111 AA", "city": "Amsterdam"} | ||
self.path = "/api/v1/locations/" | ||
|
||
self.location = LocationFactory.create() | ||
|
||
def test_read_location_without_credentials_returns_error(self): | ||
response = APIClient().get(self.path) | ||
self.assertEqual(response.status_code, 401) | ||
|
||
def test_create_location(self): | ||
response = self.post(self.data) | ||
|
||
self.assertEqual(response.status_code, 201) | ||
self.assertEqual(Location.objects.count(), 2) | ||
|
||
def test_update_location(self): | ||
data = self.data | {"name": "updated"} | ||
response = self.put(self.location.id, data) | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(Location.objects.count(), 1) | ||
self.assertEqual(Location.objects.first().name, "updated") | ||
|
||
def test_partial_update_location(self): | ||
data = {"name": "updated"} | ||
response = self.patch(self.location.id, data) | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(Location.objects.count(), 1) | ||
self.assertEqual(Location.objects.first().name, "updated") | ||
|
||
def test_read_locations(self): | ||
response = self.get() | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(response.data["count"], 1) | ||
self.assertEqual(response.data["results"], [location_to_dict(self.location)]) | ||
|
||
def test_read_location(self): | ||
response = self.get(self.location.id) | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(response.data, location_to_dict(self.location)) | ||
|
||
def test_delete_location(self): | ||
response = self.delete(self.location.id) | ||
|
||
self.assertEqual(response.status_code, 204) | ||
self.assertEqual(Location.objects.count(), 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from rest_framework.test import APIClient | ||
|
||
from open_producten.locations.models import Neighbourhood | ||
from open_producten.utils.tests.cases import BaseApiTestCase | ||
from open_producten.utils.tests.helpers import model_to_dict_with_id | ||
|
||
from ..factories import NeighbourhoodFactory | ||
|
||
|
||
def neighbourhood_to_dict(neighbourhood): | ||
return model_to_dict_with_id(neighbourhood) | ||
|
||
|
||
class TestNeighbourhood(BaseApiTestCase): | ||
|
||
def setUp(self): | ||
super().setUp() | ||
self.data = {"name": "buurt"} | ||
self.path = "/api/v1/neighbourhoods/" | ||
|
||
self.neighbourhood = NeighbourhoodFactory.create() | ||
|
||
def test_read_neighbourhood_without_credentials_returns_error(self): | ||
response = APIClient().get(self.path) | ||
self.assertEqual(response.status_code, 401) | ||
|
||
def test_create_neighbourhood(self): | ||
response = self.post(self.data) | ||
|
||
self.assertEqual(response.status_code, 201) | ||
self.assertEqual(Neighbourhood.objects.count(), 2) | ||
|
||
def test_update_neighbourhood(self): | ||
data = self.data | {"name": "updated"} | ||
response = self.put(self.neighbourhood.id, data) | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(Neighbourhood.objects.count(), 1) | ||
self.assertEqual(Neighbourhood.objects.first().name, "updated") | ||
|
||
def test_partial_update_neighbourhood(self): | ||
data = {"name": "updated"} | ||
response = self.patch(self.neighbourhood.id, data) | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(Neighbourhood.objects.count(), 1) | ||
self.assertEqual(Neighbourhood.objects.first().name, "updated") | ||
|
||
def test_read_neighbourhoods(self): | ||
response = self.get() | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(response.data["count"], 1) | ||
self.assertEqual( | ||
response.data["results"], [neighbourhood_to_dict(self.neighbourhood)] | ||
) | ||
|
||
def test_read_neighbourhood(self): | ||
response = self.get(self.neighbourhood.id) | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(response.data, neighbourhood_to_dict(self.neighbourhood)) | ||
|
||
def test_delete_neighbourhood(self): | ||
response = self.delete(self.neighbourhood.id) | ||
|
||
self.assertEqual(response.status_code, 204) | ||
self.assertEqual(Neighbourhood.objects.count(), 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
from rest_framework.test import APIClient | ||
|
||
from open_producten.locations.models import Organisation | ||
from open_producten.utils.tests.cases import BaseApiTestCase | ||
from open_producten.utils.tests.helpers import model_to_dict_with_id | ||
|
||
from ..factories import ( | ||
NeighbourhoodFactory, | ||
OrganisationFactory, | ||
OrganisationTypeFactory, | ||
) | ||
|
||
|
||
def organisation_to_dict(organisation): | ||
organisation_dict = model_to_dict_with_id(organisation) | ||
organisation_dict["type"] = model_to_dict_with_id(organisation.type) | ||
organisation_dict["neighbourhood"] = model_to_dict_with_id( | ||
organisation.neighbourhood | ||
) | ||
return organisation_dict | ||
|
||
|
||
class TestOrganisation(BaseApiTestCase): | ||
|
||
def setUp(self): | ||
super().setUp() | ||
organisation_type = OrganisationTypeFactory.create() | ||
neighbourhood = NeighbourhoodFactory.create() | ||
self.data = { | ||
"name": "locatie", | ||
"postcode": "1111 AA", | ||
"city": "Amsterdam", | ||
"type_id": organisation_type.id, | ||
"neighbourhood_id": neighbourhood.id, | ||
} | ||
self.path = "/api/v1/organisations/" | ||
|
||
self.organisation = OrganisationFactory.create() | ||
|
||
def test_read_organisation_without_credentials_returns_error(self): | ||
response = APIClient().get(self.path) | ||
self.assertEqual(response.status_code, 401) | ||
|
||
def test_create_organisation(self): | ||
response = self.post(self.data) | ||
|
||
self.assertEqual(response.status_code, 201) | ||
self.assertEqual(Organisation.objects.count(), 2) | ||
|
||
def test_update_organisation(self): | ||
data = self.data | {"name": "updated"} | ||
response = self.put(self.organisation.id, data) | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(Organisation.objects.count(), 1) | ||
self.assertEqual(Organisation.objects.first().name, "updated") | ||
|
||
def test_partial_update_organisation(self): | ||
data = {"name": "updated"} | ||
response = self.patch(self.organisation.id, data) | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(Organisation.objects.count(), 1) | ||
self.assertEqual(Organisation.objects.first().name, "updated") | ||
|
||
def test_read_organisations(self): | ||
response = self.get() | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(response.data["count"], 1) | ||
self.assertEqual( | ||
response.data["results"], [organisation_to_dict(self.organisation)] | ||
) | ||
|
||
def test_read_organisation(self): | ||
response = self.get(self.organisation.id) | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(response.data, organisation_to_dict(self.organisation)) | ||
|
||
def test_delete_organisation(self): | ||
response = self.delete(self.organisation.id) | ||
|
||
self.assertEqual(response.status_code, 204) | ||
self.assertEqual(Organisation.objects.count(), 0) |
Oops, something went wrong.