Skip to content

Commit

Permalink
- Refactor dina_api_client and metadata_api_test to avoid code duplic…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
johnphan96 committed Sep 4, 2024
1 parent 98d4bb9 commit 7192db3
Showing 1 changed file with 3 additions and 58 deletions.
61 changes: 3 additions & 58 deletions tests/metadata_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, project_root)

from dinapy.client.dina_api_client import DinaApiClient
from dinapy.client.dina_api_client import DinaApiClient, create_metadatas
from dinapy.client.utils import get_field_from_config
from dinapy.entities.Metadata import (
MetadataAttributesDTO,
Expand All @@ -20,74 +20,19 @@
from dinapy.schemas.metadata_schema import MetadataSchema
from dinapy.utils import get_dina_records_by_field


# TODO: Convert this into proper unit test.
os.environ["keycloak_username"] = "dina-admin"
os.environ["keycloak_password"] = "dina-admin"

DINA_API_CONFIG_TEST_PATH = "./tests/resources/dina-api-config-test.yml"
OBJECT_UPLOAD_PATH = "./tests/resources/object-upload-test"
TEST_GROUP = ""


def main():
dina_api_client = DinaApiClient()
pathlist = Path(OBJECT_UPLOAD_PATH).rglob("*.*")
with open(DINA_API_CONFIG_TEST_PATH, "r", encoding="utf-8") as dina_api_config_file:
dina_api_config: dict = yaml.safe_load(dina_api_config_file)
TEST_GROUP = dina_api_config["group"]
for path in pathlist:
response_json: dict = dina_api_client.upload_file_api.upload(
TEST_GROUP, path.as_posix()
)

acMetadataCreator = get_field_from_config(
dina_api_config,
"objectstore-api",
"metadata",
"relationships",
"acMetadataCreator",
)
dcCreator = get_field_from_config(
dina_api_config,
"objectstore-api",
"metadata",
"relationships",
"dcCreator",
)
relationships = (
RelationshipDTO.Builder()
.add_relationship(
"acMetadataCreator",
"person",
acMetadataCreator.get("data").get("id"),
)
.add_relationship(
"dcCreator", "person", dcCreator.get("data").get("id")
)
.build()
)

attributes = (
dina_api_config.get("objectstore-api").get("metadata").get("attributes")
)
attributes["bucket"] = response_json.get("bucket")
attributes["fileIdentifier"] = response_json.get("uuid")

dto = (
MetadataDTOBuilder()
.set_attributes(attributes)
.set_relationships(relationships)
.build()
)

schema = MetadataSchema()

serialized_metadata = schema.dump(dto)

response = dina_api_client.metadata_api.create_entity(serialized_metadata)
print(response.json())

group = dina_api_config["group"]
create_metadatas(dina_api_client, pathlist, dina_api_config, group)

if __name__ == "__main__":
main()

0 comments on commit 7192db3

Please sign in to comment.