Skip to content

Commit

Permalink
WIP on #557.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjordan committed Feb 22, 2023
1 parent 149adc0 commit ba87b2c
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ task: delete
host: "http://localhost:8000"
username: admin
password: islandora
input_csv: /tmp/workbenchsecondarytaskwithgooglesheetstestnids.txt
input_csv: /tmp/workbenchsecondarytaskwithgooglesheetsandexceltestnids.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
task: create
host: https://islandora.traefik.me
host: http://localhost:8000
username: admin
password: password

password: islandora
nodes_only: true
secondary_tasks: ['tests/assets/secondary_task_with_google_sheets_and_excel_test/excel_secondary.yml']
csv_field_templates:
- field_model: https://schema.org/DigitalDocument
- field_resource_type: 12

id_field: field_local_identifier
input_dir: tests/assets/secondary_task_with_google_sheets_and_excel_test
input_csv: secondary_task_with_excel.xlsx
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
task: create
host: https://islandora.traefik.me
host: http://localhost:8000
username: admin
password: password

password: islandora
nodes_only: true
csv_field_templates:
- field_model: https://schema.org/DigitalDocument
- field_resource_type: 12

id_field: field_local_identifier
input_dir: tests/assets/secondary_task_with_google_sheets_and_excel_test
input_csv: secondary_task_with_excel.xlsx
Expand Down
121 changes: 107 additions & 14 deletions tests/islandora_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,12 @@ def setUp(self):
self.nid_file = os.path.join(self.temp_dir, 'workbenchsecondarytasktestnids.txt')
self.nid_file_preprocessed = os.path.join(self.temp_dir, 'workbenchsecondarytasktestnids.txt.preprocessed')

if os.path.exists(self.nid_file):
os.remove(self.nid_file)

if os.path.exists(self.nid_file_preprocessed):
os.remove(self.nid_file_preprocessed)

def test_secondary_task(self):
nids = list()
create_output = subprocess.check_output(self.create_cmd)
Expand Down Expand Up @@ -502,10 +508,16 @@ def setUp(self):
self.create_cmd = ["./workbench", "--config", create_config_file_path]

self.temp_dir = tempfile.gettempdir()
self.nid_file = os.path.join(self.temp_dir, 'workbenchsecondarytaskwithgooglesheetstestnids.txt')
self.nid_file_preprocessed = os.path.join(self.temp_dir, 'workbenchsecondarytaskwithgooglesheetstestnids.txt.preprocessed')
self.nid_file = os.path.join(self.temp_dir, 'workbenchsecondarytaskwithgooglesheetsandexceltestnids.txt')
self.nid_file_preprocessed = os.path.join(self.temp_dir, 'workbenchsecondarytaskwithgooglesheetsandexceltestnids.txt.preprocessed')

def test_secondary_task(self):
if os.path.exists(self.nid_file):
os.remove(self.nid_file)

if os.path.exists(self.nid_file_preprocessed):
os.remove(self.nid_file_preprocessed)

def test_secondary_task_with_google_sheet(self):
self.nids = list()
create_output = subprocess.check_output(self.create_cmd)
create_output = create_output.decode().strip()
Expand Down Expand Up @@ -541,32 +553,113 @@ def test_secondary_task(self):
self.assertEqual(int(node_json['field_member_of'][0]['target_id']), int(parent_nid))
if node_json['field_local_identifier'][0]['value'] == 'GSC-04':
self.assertEqual(int(node_json['field_member_of'][0]['target_id']), int(parent_nid))
else:
self.assertEqual(node_json['field_member_of'], [])

def tearDown(self):
delete_config_file_path = os.path.join(self.current_dir, 'assets', 'secondary_task_test', 'delete.yml')
delete_config_file_path = os.path.join(self.current_dir, 'assets', 'secondary_task_with_google_sheets_and_excel_test', 'delete.yml')
delete_cmd = ["./workbench", "--config", delete_config_file_path]
delete_output = subprocess.check_output(delete_cmd)
delete_output = delete_output.decode().strip()
delete_lines = delete_output.splitlines()
os.remove(self.nid_file)

preprocessed_csv_path = os.path.join(self.current_dir, 'assets', 'secondary_task_test', 'metadata.csv.preprocessed')
if os.path.exists(preprocessed_csv_path):
os.remove(preprocessed_csv_path)
secondary_preprocessed_csv_path = os.path.join(self.current_dir, 'assets', 'secondary_task_test', 'secondary.csv.preprocessed')
if os.path.exists(secondary_preprocessed_csv_path):
os.remove(secondary_preprocessed_csv_path)
map_file_path = os.path.join(self.current_dir, 'assets', 'secondary_task_with_google_sheets_and_excel_test', 'id_to_node_map.tsv')
if os.path.exists(map_file_path):
os.remove(map_file_path)

map_file_path = os.path.join(self.current_dir, 'assets', 'secondary_task_test', 'id_to_node_map.tsv')
rollback_file_path = os.path.join(self.current_dir, 'assets', 'secondary_task_with_google_sheets_and_excel_test', 'rollback.csv')
if os.path.exists(rollback_file_path):
os.remove(rollback_file_path)

if os.path.exists(self.nid_file):
os.remove(self.nid_file)

if os.path.exists(self.nid_file_preprocessed):
os.remove(self.nid_file_preprocessed)


class TestSecondaryTaskWithExcel(unittest.TestCase):

def setUp(self):
self.current_dir = os.path.dirname(os.path.abspath(__file__))
create_config_file_path = os.path.join(self.current_dir, 'assets', 'secondary_task_with_google_sheets_and_excel_test', 'excel_primary.yml')

yaml = YAML()
with open(create_config_file_path, 'r') as f:
config_file_contents = f.read()
config_data = yaml.load(config_file_contents)
config = {}
for k, v in config_data.items():
config[k] = v
self.islandora_host = config['host']

self.create_cmd = ["./workbench", "--config", create_config_file_path]

self.temp_dir = tempfile.gettempdir()
self.nid_file = os.path.join(self.temp_dir, 'workbenchsecondarytaskwithgooglesheetsandexceltestnids.txt')
self.nid_file_preprocessed = os.path.join(self.temp_dir, 'workbenchsecondarytaskwithgooglesheetsandexceltestnids.txt.preprocessed')

if os.path.exists(self.nid_file):
os.remove(self.nid_file)

if os.path.exists(self.nid_file_preprocessed):
os.remove(self.nid_file_preprocessed)

def test_secondary_task_with_excel(self):
self.nids = list()
create_output = subprocess.check_output(self.create_cmd)
create_output = create_output.decode().strip()

# Write a file to the system's temp directory containing the node IDs of the
# nodes created during this test so they can be deleted in tearDown().
create_lines = create_output.splitlines()
with open(self.nid_file, "a") as fh:
fh.write("node_id\n")
for line in create_lines:
if 'created at' in line:
nid = line.rsplit('/', 1)[-1]
nid = nid.strip('.')
self.nids.append(nid)
fh.write(nid + "\n")

self.assertEqual(len(self.nids), 8)

for nid in self.nids:
node_url = self.islandora_host + '/node/' + nid + '?_format=json'
response = requests.get(node_url)
node_json = json.loads(response.text)
# Get the node ID of the parent node.
if node_json['field_local_identifier'][0]['value'] == 'STTP-02':
parent_nid = node_json['nid'][0]['value']
break

for nid in self.nids:
node_url = self.islandora_host + '/node/' + nid + '?_format=json'
response = requests.get(node_url)
node_json = json.loads(response.text)
if node_json['field_local_identifier'][0]['value'] == 'STTC-01':
self.assertEqual(int(node_json['field_member_of'][0]['target_id']), int(parent_nid))
if node_json['field_local_identifier'][0]['value'] == 'STTC-02':
self.assertEqual(int(node_json['field_member_of'][0]['target_id']), int(parent_nid))

def tearDown(self):
delete_config_file_path = os.path.join(self.current_dir, 'assets', 'secondary_task_with_google_sheets_and_excel_test', 'delete.yml')
delete_cmd = ["./workbench", "--config", delete_config_file_path]
delete_output = subprocess.check_output(delete_cmd)
delete_output = delete_output.decode().strip()
delete_lines = delete_output.splitlines()
os.remove(self.nid_file)

map_file_path = os.path.join(self.current_dir, 'assets', 'secondary_task_with_google_sheets_and_excel_test', 'id_to_node_map.tsv')
if os.path.exists(map_file_path):
os.remove(map_file_path)

rollback_file_path = os.path.join(self.current_dir, 'assets', 'secondary_task_test', 'rollback.csv')
rollback_file_path = os.path.join(self.current_dir, 'assets', 'secondary_task_with_google_sheets_and_excel_test', 'rollback.csv')
if os.path.exists(rollback_file_path):
os.remove(rollback_file_path)

if os.path.exists(self.nid_file):
os.remove(self.nid_file)

if os.path.exists(self.nid_file_preprocessed):
os.remove(self.nid_file_preprocessed)

Expand Down
11 changes: 7 additions & 4 deletions workbench
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,9 @@ def delete():
row['node_id'] = get_nid_from_url_alias(config, row['node_id'])
if not ping_node(config, row['node_id']):
if config['progress_bar'] is False:
print("Node " + row['node_id'] + " not found or not " + "accessible, skipping delete.")
logging.warning("Node " + row['node_id'] + " not found or not " + "accessible, skipping delete.")
message = f"Node {row['node_id']} not found or not accessible, skipping delete."
print(message)
logging.warning(message)
continue

# Delete the node's media first.
Expand Down Expand Up @@ -1419,8 +1420,10 @@ try:

logging.info(f"Islandora Workbench successfully completed.")

os.environ.pop("ISLANDORA_WORKBENCH_PRIMARY_TASK_TEMP_DIR")
os.environ.pop("ISLANDORA_WORKBENCH_SECONDARY_TASKS")
if os.environ.get('ISLANDORA_WORKBENCH_PRIMARY_TASK_TEMP_DIR') is not None:
os.environ.pop('ISLANDORA_WORKBENCH_PRIMARY_TASK_TEMP_DIR')
if os.environ.get('ISLANDORA_WORKBENCH_SECONDARY_TASKS') is not None:
os.environ.pop('ISLANDORA_WORKBENCH_SECONDARY_TASKS')


except KeyboardInterrupt:
Expand Down

0 comments on commit ba87b2c

Please sign in to comment.