import requests import json from typing import List, Tuple from tests.createAPI.create_delete_artist import test_artist_entries, list_artists, create_artist_entries, delete_artist_entries test_presence_entries = [ {"name": "presence_name1", "domain": "presence_domain1", "artist_id": 0}, {"name": "presence_name2", "domain": "presence_domain1", "artist_id": 1}, {"name": "presence_name3", "domain": "presence_domain2", "artist_id": 1}, ] def create_presence_entries(url: str, port: int): for i in range(len(test_presence_entries)): r = create_presence(url, port, name=test_presence_entries[i]["name"], domain=test_presence_entries[i]["domain"], artist_id=test_artist_entries[test_presence_entries[i]["artist_id"]]["id"]) if r.status_code != 200: print(f"Tried to create presence with name:{test_presence_entries[i]['name']} " f"domain:{test_presence_entries[i]['domain']} artist_id:{test_presence_entries[i]['artist_id']}") print(f"Create Presence failed with {r.status_code} and reason {r.text}!") raise Exception("Create Presence Entry Test: Failed") def create_presence(url: str, port: int, name: str, domain: str, artist_id: int): r = requests.post(f"http://{url}:{port}/artnet/metadata/presence", json={"name": name, "domain": domain, "artist_id": artist_id}) return r def delete_presence(url: str, port: int, name: str, domain: str): r = requests.delete(f"http://{url}:{port}/artnet/metadata/presence?name={name}&domain={domain}") if r.status_code != 200: print(f"Deleting Presence entry {name}@{domain}: FAILED") return False else: print(f"Deleting Presence entry {name}@{domain}: SUCCESS") return True def list_presences(url: str, port: int): """ Return a list of all presences known to the database :param url: :param port: :return: """ r = requests.get(f"http://{url}:{port}/artnet/metadata/presence") if r.status_code != 200: raise Exception("Failed querying for presences!") presences = json.loads(r.text) return presences def run_presence_test(url: str, port: int): print() print("----------------") print(f"Starting presence test with " f"({len(list_artists(url, port))}) artists, ({len(list_presences(url, port))}) presences!") print("Setting up artists for the presence test ...") create_artist_entries(url, port) print("Starting the presence test ...") print(f"Creating {len(test_presence_entries)} presences ...") create_presence_entries(url, port) create_presence_result = False if not len(list_presences(url, port)) == 3 else True print(f"Found {len(list_presences(url, port))} presence entries!") print(f"Deleting (1) specific presence entry ({test_presence_entries[0]['name']}, " f"{test_presence_entries[0]['domain']}) ...") delete_presence_result = delete_presence(url, port, test_presence_entries[0]["name"], test_presence_entries[0]["domain"]) print(f"Found {len(list_presences(url, port))} presence entries!") print(f"Deleting leftover presences ({len(list_presences(url, port))}) via cascading ...") delete_artist_entries(url, port) # artist deletion should cascade into the presences print(f"Found {len(list_presences(url, port))} presence entries!") cascade_delete_presence_result = False if not len(list_presences(url, port)) == 0 else True print("Completed the presence test!") print("----------------") print() return create_presence_result, delete_presence_result, cascade_delete_presence_result