Skip to content

Commit

Permalink
Improve automated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RohanBhattaraiNP authored Feb 7, 2025
1 parent 4ed8944 commit c1b4ab5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
14 changes: 9 additions & 5 deletions .github/workflows/bot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ jobs:
pip install pytest requests s3fs cryptography
pip install .
- name: Run CaltechDATA Metadata Validation
- name: Run against CaltechData Test system
env:
CALTECHDATA_TOKEN: ${{ secrets.CALTECHDATA_TOKEN }}
run: |
python tests/bot_yaml.py
- name: Run Unit Tests
RDMTOK: ${{ secrets.CALTECHDATA_TOKEN }}
run: |
cd tests
pytest test_unit.py
pytest test_rdm.py
- name: Run Medata Validation Test and RDM
env:
RDMTOK: ${{ secrets.CALTECHDATA_TOKEN }}
run: |
cd tests
python bot_yaml.py
5 changes: 3 additions & 2 deletions caltechdata_api/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def decrypt_token(encrypted_token, key):
return f.decrypt(encrypted_token).decode()


# Function to get or set token with support for test systems
# Function to get or set token with support for test system.
def get_or_set_token(production=True):
# First check for environment variable
env_token = os.environ.get("CALTECHDATA_TOKEN")
Expand All @@ -68,7 +68,7 @@ def get_or_set_token(production=True):

key = load_or_generate_key()

# Use different token files for production and test environment
# Use different token files for production and test environment.
token_filename = "token.txt" if production else "token_test.txt"
token_file = os.path.join(caltechdata_directory, token_filename)

Expand Down Expand Up @@ -609,6 +609,7 @@ def create_record(production):
print_upload_message(rec_id, production)
with open(response + ".json", "w") as file:
json.dump(metadata, file, indent=2)
exit()
break
else:
break
Expand Down
10 changes: 5 additions & 5 deletions caltechdata_api/customize_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,11 +508,11 @@ def validate_metadata(json_record):
if creator["nameType"] == "Organizational":
if "name" not in creator:
errors.append("Each organizational 'creator' must have 'name'.")
else:
if "familyName" not in creator:
errors.append(
"Each 'creator' must have a 'familyName' or have type Organizational"
)
else:
if "familyName" not in creator:
errors.append(
"Each 'creator' must have a 'familyName' or have type Organizational"
)
if "affiliation" in creator:
if not isinstance(creator["affiliation"], list):
errors.append("'affiliation' in 'creators' should be a list.")
Expand Down
3 changes: 2 additions & 1 deletion tests/bot_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ def import_cli_module(self):
def generate_test_responses(self):
"""Generate test responses for CLI prompts"""
return {
"Do you want to create or edit a CaltechDATA record? (create/edit): ": "create",
"What would you like to do? (create/edit/profile/exit): ": "create",
"Do you want to use metadata from an existing file or create new metadata? (existing/create): ": "create",
"Enter the title of the dataset: ": f"Test Dataset {self.timestamp}",
"Enter the abstract or description of the dataset: ": "This is an automated test dataset containing sample climate data for validation purposes.",
"Enter the number corresponding to the desired license: ": "1",
"Use saved profile? (y/n): ": "n",
"Enter your ORCID identifier: ": os.environ.get(
"TEST_ORCID", "0000-0002-1825-0097"
),
Expand Down
31 changes: 23 additions & 8 deletions tests/test_rdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,60 @@
get_metadata,
)
import json
import os


def test_datacite_rdm_conversion(full_datacite43_record, full_rdm_record):
converted = customize_schema(full_datacite43_record, schema="43", pilot=True)
converted = customize_schema(full_datacite43_record, schema="43")

assert converted == full_rdm_record


def test_datacite_rdm_create_edit(full_datacite43_record):
env_token = os.environ.get("RDMTOK")
doi = caltechdata_write(
full_datacite43_record, schema="43", pilot=True, publish=True
full_datacite43_record,
schema="43",
production=False,
publish=True,
token=env_token,
)

assert doi.startswith("10.33569")

doi = caltechdata_write(
full_datacite43_record,
schema="43",
pilot=True,
production=False,
files=["codemeta.json"],
publish=True,
token=env_token,
)

assert doi.startswith("10.33569")

# If we don't publish, don't get back a DOI
idv = caltechdata_write(full_datacite43_record, schema="43", pilot=True)
idv = caltechdata_write(
full_datacite43_record, schema="43", production=False, token=env_token
)

assert idv.startswith("10.33569") == False

full_datacite43_record["publisher"] = "Edited"

doi = caltechdata_edit(
idv, full_datacite43_record, schema="43", pilot=True, publish=True
idv,
full_datacite43_record,
schema="43",
production=False,
publish=True,
token=env_token,
)

assert doi.startswith("10.33569")
idv = doi.split("/")[1]

new_metadata = get_metadata(idv, production=False, pilot=True)
new_metadata = get_metadata(idv, production=False, publish=True)

assert new_metadata["publisher"] == "Edited"

Expand All @@ -55,14 +69,15 @@ def test_datacite_rdm_create_edit(full_datacite43_record):
full_datacite43_record,
files=["codemeta.json"],
schema="43",
pilot=True,
production=False,
publish=True,
token=env_token,
)

assert new_doi != doi

idv = new_doi.split("/")[1]

new_metadata = get_metadata(idv, production=False, pilot=True)
new_metadata = get_metadata(idv, production=False)

assert new_metadata["publisher"] == "Again!"

0 comments on commit c1b4ab5

Please sign in to comment.