import requests import json from tests.createAPI.create_delete_artist import create_artist_entries, test_artist_entries, delete_artist_entries, \ list_artists test_topic_entries = [{"name": "topic_name1", "description": "description1", "id": None, "artists": [0, 1]}, {"name": "topic_name2", "description": "description2", "id": None, "artists": [1, 2]}] def create_topic_entries(url: str, port: int): """ Create many topic entries for testing purposes :param url: :param port: :return: """ for i in range(len(test_topic_entries)): r = create_topic(url, port, name=test_topic_entries[i]["name"], desc=test_topic_entries[i]["description"]) if r.status_code != 200: print(f"Create Topic Entry Test Nr.{i}: failed with {r.status_code} and reason {r.text}") raise Exception("Create Topic Entry Test: FAILED") else: test_topic_entries[i]["id"] = json.loads(r.text)["id"] def update_topic_entries(url: str, port: int): for i in range(len(test_topic_entries)): new_name = test_topic_entries[i]["name"] + "_updated" new_description = test_topic_entries[i]["description"] + "_updated" r = update_topic(url, port, id=test_topic_entries[i]["id"], name=new_name, desc=new_description) if r.status_code != 200: print(f"Updating Topuc Entry Test Nr.{i}: failed with {r.status_code} and reason {r.text}") raise Exception("Update Topic Entry Test: FAILED") topic = get_topic_by_id(url, port, test_topic_entries[i]["id"]) if not topic["name"] == new_name: print(f"Name is not matching the expected one! " f"Current: {topic['name']} Expceted: {new_name}") raise Exception("Update Topic Entry Test: FAILED") if not topic["description"] == new_description: print(f"Description is not matching the expected one! " f"Current: {topic['description']} Expceted: {new_description}") raise Exception("Update Topic Entry Test: FAILED") def delete_topic_entries(url: str, port: int): for i in range(len(test_topic_entries)): r = delete_topic(url, port, id=test_topic_entries[i]["id"]) if r.status_code != 200: print(f"Delete Topic Entry Test Nr.{i}: failed with {r.status_code} and reason {r.text}") raise Exception("Delete Topic Entry Test: FAILED") def delete_all_topics(url: str, port: int): topics = list_topics(url, port) print(f"Removing following topics: {topics}") for topic in topics: delete_topic(url, port, id=topic["id"]) def list_topics(url: str, port: int): r = requests.get(f"http://{url}:{port}/artnet/metadata/topic") if r.status_code != 200: raise Exception(f"Failed querying for topics! Status: {r.status_code} Reason: {r.text}") topics = json.loads(r.text) return topics def get_topic_by_id(url: str, port: int, id: str): r = requests.get(f"http://{url}:{port}/artnet/metadata/topic?id={id}") if r.status_code != 200: raise Exception("Failed querying for topic!") return json.loads(r.text) def create_topic(url: str, port: int, name: str, desc: str): r = requests.post(f"http://{url}:{port}/artnet/metadata/topic", json={"name": name, "description": desc}) return r def update_topic(url: str, port: int, id: str, name: str = None, desc: str = None): r = requests.post(f"http://{url}:{port}/artnet/metadata/topic?id={id}", json={"name": name, "description": desc}) return r def delete_topic(url: str, port: int, id: str): r = requests.delete(f"http://{url}:{port}/artnet/metadata/topic?id={id}") return r def run_topic_tests(url: str, port: int): print() print("----------------") l = len(list_topics(url, port)) print(f"Starting topic tests with ({l}) topics!") if l > 0: print("Deleting leftover topics ...") delete_all_topics(url, port) print(f"Creating {len(test_topic_entries)} topic entries for the test ...") create_topic_entries(url, port) create_topic_result = False if not len(test_topic_entries) == len(list_topics(url, port)) else True print(f"Found {len(list_topics(url, port))} topic entries!") print("Updating topic entries with new data now ...") update_topic_entries(url, port) # throws exception if not working update_topic_result = True print("Finished updating the topic entries.") print(f"Found {len(list_topics(url, port))} topic entries!") print(f"Deleting all topic entries ...") delete_topic_entries(url, port) delete_topic_result = False if not len(list_topics(url, port)) == 0 else True print(f"Found {len(list_topics(url, port))} topic entries!") print("Finished topic tests!") return create_topic_result, update_topic_result, delete_topic_result if __name__ == "__main__": url, port = "127.0.0.1", 8000 run_topic_tests(url, port)