Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NvdLaan committed Oct 10, 2024
1 parent 7f27257 commit dee9810
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 56 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,3 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.local.env
Empty file added .local.env
Empty file.
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ docker-compose -f docker-compose.local.yml up

Visit the Admin at http://localhost:8081/admin/


If you want to make use of the DSO api the following vars need to be set in the .local.env file:

```bash
DSO_API_URL=<url>
DSO_CLIENT_SECRET=<key>
DSO_AUTH_URL=<key>
DSO_API_URL=<key>
```



## Swagger

http://localhost:8081/api/schema/swagger/
Expand Down Expand Up @@ -55,17 +67,6 @@ bash bin/install_pre_commit.sh
## Running tests

Containers should be running to run tests via docker.
If you want to make use of the DSO api the following vars need to be set in the .local.env file:

```bash
DSO_API_URL=<url>
DSO_CLIENT_SECRET=<key>
DSO_AUTH_URL=<key>
DSO_API_URL=<key>
```



```bash
docker compose -f docker-compose.local.yml -f docker-compose.override.yml up -d
docker compose exec -T zwd-backend python manage.py test /app/apps
Expand Down
1 change: 0 additions & 1 deletion app/apps/homeownerassociation/tests.py

This file was deleted.

29 changes: 0 additions & 29 deletions app/apps/homeownerassociation/tests/tests_api.py

This file was deleted.

72 changes: 60 additions & 12 deletions app/apps/homeownerassociation/tests/tests_models.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,64 @@
from apps.homeownerassociation.models import HomeownerAssociation
from django.core import management
from django.test import TestCase
from model_bakery import baker
from unittest.mock import patch
from apps.homeownerassociation.models import HomeownerAssociation


class HomeownerAssociationModelTest(TestCase):

@patch("apps.homeownerassociation.models.DsoClient")
def test_get_or_create_hoa_by_bag_id_existing_hoa(self, MockDsoClient):
# Mock the DsoClient and its methods
mock_client = MockDsoClient.return_value
mock_client.get_hoa_name_by_bag_id.return_value = "Test HOA"

# Create an existing HOA
existing_hoa = HomeownerAssociation.objects.create(
name="Test HOA", build_year=2000, number_of_appartments=10
)

# Call the method
result = HomeownerAssociation.get_or_create_hoa_by_bag_id("some_bag_id")

# Assert the existing HOA is returned
self.assertEqual(result, existing_hoa)
mock_client.get_hoa_name_by_bag_id.assert_called_once_with("some_bag_id")

@patch("apps.homeownerassociation.models.DsoClient")
def test_get_or_create_hoa_by_bag_id_new_hoa(self, MockDsoClient):
# Mock the DsoClient and its methods
mock_client = MockDsoClient.return_value
mock_client.get_hoa_name_by_bag_id.return_value = "New HOA"
mock_client.get_hoa_by_name.return_value = [
{"pndOorspronkelijkBouwjaar": 2010},
{"pndOorspronkelijkBouwjaar": 2010},
]

# Call the method
result = HomeownerAssociation.get_or_create_hoa_by_bag_id("some_bag_id")

# Assert a new HOA is created
self.assertIsInstance(result, HomeownerAssociation)
self.assertEqual(result.name, "New HOA")
self.assertEqual(result.build_year, 2010)
self.assertEqual(result.number_of_appartments, 2)
mock_client.get_hoa_name_by_bag_id.assert_called_once_with("some_bag_id")
mock_client.get_hoa_by_name.assert_called_once_with("New HOA")

@patch("apps.homeownerassociation.models.DsoClient")
def test_get_or_create_hoa_by_bag_id_existing_hoa_no_new_hoa(self, MockDsoClient):
# Mock the DsoClient and its methods
mock_client = MockDsoClient.return_value
mock_client.get_hoa_name_by_bag_id.return_value = "Test HOA"

# Create an existing HOA
existing_hoa = HomeownerAssociation.objects.create(
name="Test HOA", build_year=2000, number_of_appartments=10
)

class HomeownerAssociationTest(TestCase):
def setUp(self):
management.call_command("flush", verbosity=0, interactive=False)
super().setUp()
# Call the method
result = HomeownerAssociation.get_or_create_hoa_by_bag_id("some_bag_id")

def test_can_create_hoa(self):
"""A case can be created"""
self.assertEqual(HomeownerAssociation.objects.count(), 0)
baker.make(HomeownerAssociation)
self.assertEqual(HomeownerAssociation.objects.count(), 1)
# Assert the existing HOA is returned
self.assertEqual(result, existing_hoa)
mock_client.get_hoa_name_by_bag_id.assert_called_once_with("some_bag_id")
mock_client.get_hoa_by_name.assert_not_called()
4 changes: 2 additions & 2 deletions app/clients/dso_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ def _get_access_token(self):
"client_secret": settings.DSO_CLIENT_SECRET,
"scope": "openid email",
}
response = requests.post(url, data=payload)
return response.json()["access_token"]
response = requests.post(url, data=payload).json()
return response["access_token"]

0 comments on commit dee9810

Please sign in to comment.