Skip to content

Commit

Permalink
First pass at a unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall committed Nov 14, 2024
1 parent d9cb22c commit 7d2f3db
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/assets/create_multi_parents/children.csv
Original file line number Diff line number Diff line change
@@ -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,
8 changes: 8 additions & 0 deletions tests/assets/create_multi_parents/children.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions tests/assets/create_multi_parents/create.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions tests/assets/create_multi_parents/metadata.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
id,title,field_model,field_member_of
001,Parent 1,Collection,
002,Parent 2,Collection,
003,Parent 3,Collection,
150 changes: 150 additions & 0 deletions tests/islandora_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

0 comments on commit 7d2f3db

Please sign in to comment.