Skip to content

Commit

Permalink
Changed the test_run_import_bag so that it only uses the first 5 lines
Browse files Browse the repository at this point in the history
of the csv when importing. However the "Nummeraanduiding" csv cannot be
imported as it depends on all other csv files to be imported complete.
This is what makes the tests real slow.

Updated the docker-compose to use healthchecks instead of the
wait-for-it.sh file.

Removed VCR package as it is not used.

Updated the Makefile as it is no longer needed to have 2 test commands.
  • Loading branch information
vanbuiten committed Dec 12, 2024
1 parent 21961a9 commit a105f6a
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 71 deletions.
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@ dev: migrate ## Run the development app (and run extra migrations fir
bash: ## Run the container and start bash
$(run) app bash

test: ## Execute non-integration tests
$(run) test pytest -m 'not integration' $(ARGS)

integration_test: lint ## Execute integration tests
$(run) test pytest -m 'integration' $(ARGS)
test: ## Execute tests
$(run) test pytest $(ARGS)

lintfix: ## Execute lint fixes
$(run) test black /src/$(APP)
Expand Down
19 changes: 15 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ x-app: &base-app
context: .
target: app
depends_on:
- database
- azurite
database:
condition: service_healthy
azurite:
condition: service_healthy
volumes:
- ./src:/src
- ./deploy:/deploy
Expand All @@ -24,7 +26,6 @@ x-app: &base-app
PYTHONBREAKPOINT: true
UWSGI_STATIC_MAP: '/iiif-metadata/static=/static'
AZURITE_STORAGE_CONNECTION_STRING: 'AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;DefaultEndpointsProtocol=http;BlobEndpoint=http://azurite:10000/devstoreaccount1/;'
entrypoint: /deploy/wait-for-it.sh database:5432 --

services:
database:
Expand All @@ -37,11 +38,20 @@ services:
POSTGRES_PASSWORD: dev
volumes:
- "~/.ssh/datapunt.key:/root/.ssh/datapunt.key"
healthcheck:
test: [ "CMD", "pg_isready", "-q", "-d", "dev", "-U", "dev" ]
interval: 10s
timeout: 5s
retries: 3

azurite:
image: mcr.microsoft.com/azure-storage/azurite
ports:
- 10000 # Blob service
healthcheck:
test: nc 127.0.0.1 10000 -z
interval: 1s
retries: 30

app:
<<: *base-app
Expand All @@ -50,7 +60,8 @@ services:
target: app
image: ${REGISTRY:-localhost:5000}/${REPOSITORY:-opdrachten/iiif-metadata-server}:${VERSION:-latest}
depends_on:
- database
database:
condition: service_healthy

dev:
<<: *base-app
Expand Down
9 changes: 4 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# This file is autogenerated by pip-compile with Python 3.10
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile --allow-unsafe --output-file=requirements.txt requirements.in
Expand Down Expand Up @@ -107,9 +107,9 @@ drf-extensions==0.7.1
# via drf-amsterdam
drf-yasg==1.21.8
# via -r requirements.in
google-api-core==2.23.0
google-api-core==2.24.0
# via opencensus
google-auth==2.36.0
google-auth==2.37.0
# via google-api-core
googleapis-common-protos==1.66.0
# via google-api-core
Expand Down Expand Up @@ -270,7 +270,7 @@ six==1.17.0
# azure-core
# opencensus
# python-dateutil
sqlparse==0.5.2
sqlparse==0.5.3
# via
# django
# django-debug-toolbar
Expand All @@ -283,7 +283,6 @@ toolz==1.0.0
# via -r requirements.in
typing-extensions==4.12.2
# via
# asgiref
# azure-core
# azure-identity
# azure-keyvault-certificates
Expand Down
1 change: 0 additions & 1 deletion requirements_dev.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

model-bakery
parameterized
vcrpy # replaying requests for more efficient testing
factory-boy
pytest-django
pytest-cov
Expand Down
37 changes: 7 additions & 30 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# This file is autogenerated by pip-compile with Python 3.10
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile --allow-unsafe --output-file=requirements_dev.txt requirements_dev.in
Expand Down Expand Up @@ -35,12 +35,10 @@ azure-keyvault-certificates==4.9.0
# via
# -r ./requirements.txt
# azure-keyvault
azure-keyvault-keys==4.10.0
azure-keyvault-keys==4.10.0
# via
# -r ./requirements.txt
# azure-keyvault
azure-keyvault-secrets==4.9.0
azure-keyvault-secrets==4.9.0
# via
# -r ./requirements.txt
Expand All @@ -67,7 +65,7 @@ charset-normalizer==3.4.0
# requests
click==8.1.7
# via black
coverage[toml]==7.6.8
coverage[toml]==7.6.9
# via pytest-cov
cryptography==44.0.0
# via
Expand Down Expand Up @@ -144,17 +142,15 @@ drf-extensions==0.7.1
# drf-amsterdam
drf-yasg==1.21.8
# via -r ./requirements.txt
exceptiongroup==1.2.2
# via pytest
factory-boy==3.3.1
# via -r requirements_dev.in
faker==33.1.0
# via factory-boy
google-api-core==2.23.0
google-api-core==2.24.0
# via
# -r ./requirements.txt
# opencensus
google-auth==2.36.0
google-auth==2.37.0
# via
# -r ./requirements.txt
# google-api-core
Expand All @@ -166,7 +162,6 @@ idna==3.10
# via
# -r ./requirements.txt
# requests
# yarl
inflection==0.5.1
# via
# -r ./requirements.txt
Expand Down Expand Up @@ -218,8 +213,6 @@ msgpack==1.1.0
# via
# -r ./requirements.txt
# oslo-serialization
multidict==6.1.0
# via yarl
mypy-extensions==1.0.0
# via black
netaddr==1.3.0
Expand Down Expand Up @@ -301,8 +294,6 @@ portalocker==2.10.1
# via
# -r ./requirements.txt
# msal-extensions
propcache==0.2.1
# via yarl
proto-plus==1.25.0
# via
# -r ./requirements.txt
Expand Down Expand Up @@ -377,13 +368,14 @@ pyyaml==6.0.2
# drf-yasg
# oslo-config
# oslo-utils
# vcrpy
referencing==0.35.1
# via
# -r ./requirements.txt
# jsonschema
# jsonschema-specifications
requests==2.32.3
# via
# -r ./requirements.txt
# azure-core
# datapunt-authorization-django
# google-api-core
Expand Down Expand Up @@ -413,7 +405,7 @@ six==1.17.0
# azure-core
# opencensus
# python-dateutil
sqlparse==0.5.2
sqlparse==0.5.3
# via
# -r ./requirements.txt
# django
Expand All @@ -424,29 +416,20 @@ stevedore==5.4.0
# keystoneauth1
# oslo-config
# python-keystoneclient
tomli==2.2.1
# via
# autoflake
# black
# coverage
# pytest
toolz==1.0.0
# via -r ./requirements.txt
typing-extensions==4.12.2
# via
# -r ./requirements.txt
# asgiref
# azure-core
# azure-identity
# azure-keyvault-certificates
# azure-keyvault-keys
# azure-keyvault-secrets
# azure-storage-blob
# black
# faker
# jwcrypto
# keystoneauth1
# multidict
tzdata==2024.2
# via
# -r ./requirements.txt
Expand All @@ -460,16 +443,10 @@ urllib3==2.2.3
# via
# -r ./requirements.txt
# requests
# vcrpy
vcrpy==6.0.2
# via -r requirements_dev.in
wrapt==1.17.0
# via
# -r ./requirements.txt
# debtcollector
# opencensus-ext-requests
# vcrpy
xmltodict==0.14.2
# via -r ./requirements.txt
yarl==1.18.3
# via vcrpy
22 changes: 11 additions & 11 deletions src/bag/tests/test_bag_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@
@override_settings(GEMEENTE_CODE=gemeente_code)
class UtilsTestCase(TestCase):
@parameterized.expand(["", (None,)])
def test_get_object_type_empty_param(self, id):
def test_get_object_type_empty_param(self, _id):
with self.assertRaises(BagIdException):
BagHelper.get_object_type_from_id(id)
BagHelper.get_object_type_from_id(_id)

@parameterized.expand(
["01234000000000000", "012345678901234", "123"] # too long # too short
)
def test_get_object_type_incorrect_length(self, id):
def test_get_object_type_incorrect_length(self, _id):
with self.assertRaises(IncorrectBagIdLengthException):
BagHelper.get_object_type_from_id(id)
BagHelper.get_object_type_from_id(_id)

def test_get_object_type_incorrect_gemeente_code(self):
id = "0123400000000000"
_id = "0123400000000000"
with self.assertRaises(IncorrectGemeenteCodeException):
BagHelper.get_object_type_from_id(id)
BagHelper.get_object_type_from_id(_id)

@parameterized.expand(
[
Expand Down Expand Up @@ -99,12 +99,12 @@ def test_get_object_doesnotexist(self, object_type, model, mocked_get_obj_type):
[BAG_TYPE_VERBLIJFSOBJECT, BAG_TYPE_LIGPLAATS, BAG_TYPE_STANDPLAATS]
)
def test_get_object(self, object_type):
id = f"{gemeente_code}{object_type}0000000000"
verblijfsobject = baker.make(Verblijfsobject, id=id)
ligplaats = baker.make(Ligplaats, id=id)
standplaats = baker.make(Standplaats, id=id)
_id = f"{gemeente_code}{object_type}0000000000"
verblijfsobject = baker.make(Verblijfsobject, id=_id)
ligplaats = baker.make(Ligplaats, id=_id)
standplaats = baker.make(Standplaats, id=_id)

obj = BagHelper.get_object_from_id(id)
obj = BagHelper.get_object_from_id(_id)
if object_type == BAG_TYPE_VERBLIJFSOBJECT:
self.assertEqual(obj, verblijfsobject)
elif object_type == BAG_TYPE_LIGPLAATS:
Expand Down
64 changes: 49 additions & 15 deletions src/importer/tests/test_run_import_bag.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,24 +1,58 @@
import logging
import itertools
from unittest.mock import patch

import pytest
import vcr
from django.core.management import call_command

vcr_log = logging.getLogger("vcr")
vcr_log.setLevel(logging.WARNING)

my_vcr = vcr.VCR(
serializer="yaml",
cassette_library_dir="fixtures/cassettes",
record_mode="new_episodes",
match_on=["uri", "method"],
from bag.bag_api import Zip
from bag.models import (
Ligplaats,
Nummeraanduiding,
Openbareruimte,
Pand,
Standplaats,
Verblijfsobject,
Verblijfsobjectpandrelatie,
)


class TestBagImportBag:

@my_vcr.use_cassette()
@pytest.mark.django_db
@pytest.mark.integration
def test_import_bag_handle(self):
call_command("run_import_bag")
@pytest.mark.parametrize("model,endpoint", [
(Ligplaats, "bag_ligplaatsen.csv.zip"),
(Openbareruimte, "bag_openbareruimtes.csv.zip"),
(Standplaats, "bag_standplaatsen.csv.zip"),
(Pand, "bag_panden.csv.zip"),
(Verblijfsobject, "bag_verblijfsobjecten.csv.zip"),
# (Nummeraanduiding, "bag_nummeraanduidingen.csv.zip"), # Skip this file as it depends on other CSV files being imported
(Verblijfsobjectpandrelatie, "benkagg_bagpandbevatverblijfsobjecten.csv.zip"),
])
def test_import_bag_handle(self, model, endpoint):
"""
This test verifies that the 'run_import_bag' command correctly handles
the import process by patching the 'get_records' method of the 'Zip' class
and the model_to_endpoint class variable.
The patched method will return only the first five records from the original
implementation, allowing for controlled testing of the import functionality.
The patched class variable, using parameterize, makes sure that every csv is
tested in a separate test.
"""
# Keep the original, unpatched, class method "get_records"
original_get_records = Zip.get_records

with (patch.object(Zip, "get_records", autospec=True) as mock_get_records,
patch.object(Zip, "model_to_endpoint", new={model: endpoint})):

def side_effect(self, bag_model):
"""
Call the original class methods get_records and only return the 5 first items as a generator
"""
original_result = original_get_records(self, bag_model=bag_model)
return itertools.islice(original_result, 5)

mock_get_records.side_effect = side_effect

# Call the management command
call_command("run_import_bag")

0 comments on commit a105f6a

Please sign in to comment.