Skip to content

Commit

Permalink
Save file_path metadata field (#131)
Browse files Browse the repository at this point in the history
* DPMETA-9: Move backend tests into backend folder. New __init__.py in backend folder. Setting up first test for save filepath value

* Added test

* Organize imports

* Exclude tooling files from mypy checks

* Update gitignore

* Black format

---------

Co-authored-by: Jan Sander <[email protected]>
Co-authored-by: mmwinther <[email protected]>
  • Loading branch information
3 people authored Jan 23, 2024
1 parent 96e6a6f commit 6c2116a
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 12 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Project spesific ignores
tests/resources/*.json

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down Expand Up @@ -51,6 +54,7 @@ coverage.xml
.hypothesis/
.pytest_cache/


# Translations
*.mo
*.pot
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ warn_unreachable = true
pretty = true
show_column_numbers = true
show_error_context = true
exclude = ["docs/conf.py", "noxfile.py", "gunicorn.conf.py"]

[[tool.mypy.overrides]]
# Allow missing type hints in third-party libraries without type information.
Expand Down
2 changes: 1 addition & 1 deletion src/datadoc/backend/datadoc_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def write_metadata_document(self) -> None:
self.meta.dataset.metadata_created_by = self.current_user
self.meta.dataset.metadata_last_updated_date = timestamp
self.meta.dataset.metadata_last_updated_by = self.current_user

self.meta.dataset.file_path = str(self.dataset)
if self.container:
self.container.datadoc = self.meta
else:
Expand Down
1 change: 1 addition & 0 deletions tests/backend/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Unit tests for backend Datadoc."""
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
from datadoc.enums import DatasetState
from datadoc.enums import DataType
from datadoc.enums import VariableRole

from .utils import TEST_EXISTING_METADATA_DIRECTORY
from .utils import TEST_EXISTING_METADATA_FILE_NAME
from .utils import TEST_PARQUET_FILEPATH
from .utils import TEST_RESOURCES_DIRECTORY
from tests.utils import TEST_EXISTING_METADATA_DIRECTORY
from tests.utils import TEST_EXISTING_METADATA_FILE_NAME
from tests.utils import TEST_PARQUET_FILEPATH
from tests.utils import TEST_RESOURCES_DIRECTORY
from tests.utils import TEST_RESOURCES_METADATA_DOCUMENT

if TYPE_CHECKING:
from datetime import datetime
Expand Down Expand Up @@ -201,3 +201,27 @@ def test_variable_role_default_value(metadata: DataDocMetadata):

def test_direct_person_identifying_default_value(metadata: DataDocMetadata):
assert all(not v.direct_person_identifying for v in metadata.meta.variables)


# Test with existing dataset and metadata document
def test_save_file_path_metadata_field(
existing_metadata_file: str,
metadata: DataDocMetadata,
):
metadata.write_metadata_document()
with Path.open(Path(existing_metadata_file)) as f:
saved_file_path = json.load(f)["datadoc"]["dataset"]["file_path"]
assert saved_file_path == str(metadata.dataset)


# Test with dataset and no metadata document
def test_save_file_path_dataset_and_no_metadata(
metadata: DataDocMetadata,
):
metadata.write_metadata_document()
with Path.open(Path(TEST_RESOURCES_METADATA_DOCUMENT)) as f:
saved_file_path = json.load(f)["datadoc"]["dataset"]["file_path"]
assert saved_file_path == str(metadata.dataset)


# Test with metadata document and no dataset
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
from datadoc.backend.dataset_parser import DatasetParserParquet
from datadoc.enums import DataType
from datadoc.enums import SupportedLanguages

from .utils import TEST_PARQUET_FILEPATH
from .utils import TEST_PARQUET_GZIP_FILEPATH
from .utils import TEST_SAS7BDAT_FILEPATH
from tests.utils import TEST_PARQUET_FILEPATH
from tests.utils import TEST_PARQUET_GZIP_FILEPATH
from tests.utils import TEST_SAS7BDAT_FILEPATH


def test_use_abstract_class_directly():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
from datadoc.backend.datadoc_metadata import DataDocMetadata
from datadoc.backend.model_backwards_compatibility import UnknownModelVersionError
from datadoc.backend.model_backwards_compatibility import upgrade_metadata

from .utils import TEST_COMPATIBILITY_DIRECTORY
from tests.utils import TEST_COMPATIBILITY_DIRECTORY

BACKWARDS_COMPATIBLE_VERSION_DIRECTORIES = [
d for d in TEST_COMPATIBILITY_DIRECTORY.iterdir() if d.is_dir()
Expand Down
File renamed without changes.

0 comments on commit 6c2116a

Please sign in to comment.