-
Notifications
You must be signed in to change notification settings - Fork 0
Testing
crugas edited this page Jan 3, 2025
·
3 revisions
This Wiki page is used to describe our best practices for testing scripts.
- Testing should be included for every script written.
- We currently use Python unittests to build tests for our Python functions. Unittests should be written in a separate file under the
tests
directory.- Unittests should have at least one test per function, unless it's
main()
. Tests can test a function on the expected input/output, as well as error handling of unexpected input/output/transformations.
- Unittests should have at least one test per function, unless it's
- Any unittests that generate files as their output should be directed to the test_data directory and be deleted after the unittest is completed.
- Make sure to use any functions written to utilities.py where possible, as they have tests included in utilities_tests.py that can be run without having to build your own tests.
import os
import unittest
from python_scripts.repeatable.update_fileuri import *
from test_data.fileuri_testdata import *
from python_scripts.delete_dometadata import *
from test_data.dometadata_testdata import *
env_file = find_dotenv(f'.env.dev')
load_dotenv(env_file)
local_aspace = client_login(os.getenv('as_api'), os.getenv('as_un'), os.getenv('as_pw'))
class TestGetDigitalObject(unittest.TestCase):
def test_get_digital_object(self):
"""Tests that the existing digital object can be retrieved"""
repo_id = test_update_file_uri_metadata['repository']['ref'].split('/repositories/')[1]
digital_object_id = test_update_file_uri_metadata['uri'].rsplit('/',1)[1]
test_response = get_digital_object(local_aspace, repo_id, digital_object_id)
self.assertIsInstance(test_response, dict)
self.assertNotIn('error', test_response)
def test_get_missing_digital_object(self):
"""Tests that a missing digital object returns an error"""
repo_id = test_update_file_uri_metadata['repository']['ref'].split('/repositories/')[1]
test_response = get_digital_object(local_aspace, repo_id, '600') # Some unusually high made up number
self.assertIsNone(test_response)
class TestWriteToFile(unittest.TestCase):
def test_new_data(self):
test_filepath = str(Path('../test_data', 'test_delete_dometadata_original_data.jsonl'))
if os.path.isfile(test_filepath):
os.remove(test_filepath)
write_to_file(test_filepath, test_digital_object_dates)
self.assertTrue(os.path.isfile(test_filepath))
with open(test_filepath, 'r') as test_reader:
for row_data in test_reader:
test_data = json.loads(row_data)
self.assertEqual(test_data['digital_object_id'], test_digital_object_dates['digital_object_id'])
os.remove(test_filepath)