import requests from typing import List import json # DB Schema Version 2.0 | 2021-10-10 test_artist_entries = [ {"id": None, "name": "Artist1", "topics": []}, {"id": None, "name": "Artist2", "topics": []}, {"id": None, "name": "Artist3", "topics": []} ] def list_artists(url: str, port: int): """ Return a list of all artist known to the database :param url: :param port: :return: """ r = requests.get(f"http://{url}:{port}/artnet/metadata/artist") if r.status_code != 200: print(r.text) raise Exception("Failed querying for artists!") artists = json.loads(r.text) return artists def create_artist_entries(url: str, port: int): for i in range(len(test_artist_entries)): r = create_artist(url, port, test_artist_entries[i]["name"], test_artist_entries[i]["topics"]) if r.status_code != 200: print(f"Status:{r.status_code} Text:{r.text}") raise Exception("Artist Entry creation failed!") artist_id = int(r.text) test_artist_entries[i]["id"] = artist_id def delete_artist_entries(url: str, port: int): artists = list_artists(url, port) for i in range(len(artists)): r = delete_artist(url, port, artists[i]["id"]) if r.status_code != 200: print(f"Deleted artist entry #{artists[i]['id']}, {artists[i]['name']}: FAILURE") else: print(f"Deleted artist entry #{artists[i]['id']}, {artists[i]['name']}: SUCCESS") def create_artist(url: str, port: int, name: str, topics: List[int]): r = requests.post(f"http://{url}:{port}/artnet/metadata/artist", json={"name": name, "topics": topics}) return r def delete_artist(url: str, port: int, id: int): r = requests.delete(f"http://{url}:{port}/artnet/metadata/artist?id={id}") return r def run_artist_test(url: str, port: int): print() print("----------------") print(f"Starting the artist test with ({len(list_artists(url, port))}) artists!") print("Starting the artist test ...") print(f"Creating {len(test_artist_entries)} artists ...") create_artist_entries(url, port) print(f"Found {len(list_artists(url, port))} artist entries") create_artist_result = False if not len(list_artists(url, port)) == 3 else True print("Deleting all artists ...") delete_artist_entries(url, port) delete_artist_result = False if not len(list_artists(url, port)) == 0 else True print(f"Found {len(list_artists(url, port))} artist entries") print("Completed the artist test!") print("----------------") print() return create_artist_result, delete_artist_result