diff --git a/tests/assets/create_multi_parents/children.csv b/tests/assets/create_multi_parents/children.csv new file mode 100644 index 0000000..a35bc3b --- /dev/null +++ b/tests/assets/create_multi_parents/children.csv @@ -0,0 +1,5 @@ +id,title,field_model,field_member_of +001,Child 1 & 2,Image,1|2 +002,Child 1,Image,1 +004,Child 1, 2, & 3,Image,1|2|3 +005,No parent,Image, diff --git a/tests/assets/create_multi_parents/children.yml b/tests/assets/create_multi_parents/children.yml new file mode 100644 index 0000000..da113e0 --- /dev/null +++ b/tests/assets/create_multi_parents/children.yml @@ -0,0 +1,8 @@ +task: create +host: https://islandora.traefik.me +username: admin +password: password +input_dir: "tests/assets/create_multi_parents" +nodes_only: true +secure_ssl_only: false +input_csv: children.csv diff --git a/tests/assets/create_multi_parents/create.yml b/tests/assets/create_multi_parents/create.yml new file mode 100644 index 0000000..32da1db --- /dev/null +++ b/tests/assets/create_multi_parents/create.yml @@ -0,0 +1,7 @@ +task: create +host: https://islandora.traefik.me +username: admin +password: password +input_dir: "tests/assets/create_multi_parents" +nodes_only: true +secure_ssl_only: false diff --git a/tests/assets/create_multi_parents/metadata.csv b/tests/assets/create_multi_parents/metadata.csv new file mode 100644 index 0000000..0205bf2 --- /dev/null +++ b/tests/assets/create_multi_parents/metadata.csv @@ -0,0 +1,4 @@ +id,title,field_model,field_member_of +001,Parent 1,Collection, +002,Parent 2,Collection, +003,Parent 3,Collection, diff --git a/tests/islandora_tests.py b/tests/islandora_tests.py index badee46..dad4de6 100644 --- a/tests/islandora_tests.py +++ b/tests/islandora_tests.py @@ -18,6 +18,7 @@ import unittest import time import copy +import csv sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import workbench_utils @@ -1530,5 +1531,154 @@ def tearDown(): pass +class TestMultipleParents(unittest.TestCase): + + def setUp(self): + self.current_dir = os.path.dirname(os.path.abspath(__file__)) + self.create_config_file_path = os.path.join( + self.current_dir, "assets", "create_multi_parents", "create.yml" + ) + + yaml = YAML() + with open(self.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", self.create_config_file_path] + self.temp_dir = tempfile.gettempdir() + + def test_secondary_task(self): + requests.packages.urllib3.disable_warnings() + self.nids = list() + create_output = subprocess.check_output(self.create_cmd) + create_output = create_output.decode().strip() + + create_lines = create_output.splitlines() + for line in create_lines: + if "created at" in line: + nid = line.rsplit("/", 1)[-1] + nid = nid.strip(".") + self.nids.append(nid) + + self.assertEqual(len(self.nids), 3) + + # create the child CSV based on the parents created + file_path = os.path.join( + self.current_dir, "assets", "create_multi_parents", "children.csv" + ) + headers = ["id", "title", "field_model", "field_member_of"] + data = [ + { + "id": "001", + "title": "Child 1, 2", + "field_model": "Image", + "field_member_of": "|".join(map(str, self.nids[:2])), + }, + { + "id": "002", + "title": "Child 1", + "field_model": "Image", + "field_member_of": str(self.nids[0]), + }, + { + "id": "004", + "title": "Child 1, 2, 3", + "field_model": "Image", + "field_member_of": "|".join(map(str, self.nids[:3])), + }, + { + "id": "005", + "title": "No parent", + "field_model": "Image", + "field_member_of": "", + }, + ] + + # Write to CSV + with open(file_path, mode="w", newline="") as file: + writer = csv.DictWriter(file, fieldnames=headers) + writer.writeheader() + writer.writerows(data) + + self.child_config_file_path = os.path.join( + self.current_dir, "assets", "create_multi_parents", "children.yml" + ) + child_cmd = ["./workbench", "--config", self.child_config_file_path] + create_output = subprocess.check_output(child_cmd) + create_output = create_output.decode().strip() + + self.child_nids = list() + create_output = subprocess.check_output(child_cmd) + create_output = create_output.decode().strip() + + create_lines = create_output.splitlines() + for line in create_lines: + if "created at" in line: + nid = line.rsplit("/", 1)[-1] + nid = nid.strip(".") + self.child_nids.append(nid) + + for nid in self.child_nids: + node_url = self.islandora_host + "/node/" + nid + "?_format=json" + response = requests.get(node_url, verify=False) + node_json = json.loads(response.text) + if node_json["title"][0]["value"] == "Child 1, 2": + self.assertEqual(len(node_json["field_member_of"]), 2) + elif node_json["title"][0]["value"] == "Child 1": + self.assertEqual(len(node_json["field_member_of"]), 1) + elif node_json["title"][0]["value"] == "Child 1, 2, 3": + self.assertEqual(len(node_json["field_member_of"]), 3) + + def tearDown(self): + for nid in self.nids: + quick_delete_cmd = [ + "./workbench", + "--config", + self.create_config_file_path, + "--quick_delete_node", + self.islandora_host + "/node/" + nid, + ] + subprocess.check_output(quick_delete_cmd) + for nid in self.child_nids: + quick_delete_cmd = [ + "./workbench", + "--config", + self.child_config_file_path, + "--quick_delete_node", + self.islandora_host + "/node/" + nid, + ] + subprocess.check_output(quick_delete_cmd) + preprocessed_csv_path = os.path.join( + self.current_dir, + "assets", + "create_multi_parents", + "metadata.csv.preprocessed", + ) + if os.path.exists(preprocessed_csv_path): + os.remove(preprocessed_csv_path) + + secondary_preprocessed_csv_path = os.path.join( + self.temp_dir, "children.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", "create_multi_parents", "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", "create_multi_parents", "rollback.csv" + ) + if os.path.exists(rollback_file_path): + os.remove(rollback_file_path) + + if __name__ == "__main__": unittest.main()