You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
7.1 KiB
Python

import requests
import json
from typing import List, Tuple
from tests.createAPI.create_delete_artist import list_artists, create_artist_entries, delete_artist_entries
from tests.createAPI.create_delete_presence import test_presence_entries, list_presences, create_presence_entries
from tests.createAPI.create_delete_art import test_art_entries, list_art, create_art_entries, delete_art_entries
test_art_presence_relations = [
{"art_hash": test_art_entries[0]["hash"], "presence_name": test_art_entries[0]["presences"][0][0], "presence_domain": test_art_entries[0]["presences"][0][1]},
{"art_hash": test_art_entries[1]["hash"], "presence_name": test_art_entries[1]["presences"][0][0], "presence_domain": test_art_entries[1]["presences"][0][1]},
{"art_hash": test_art_entries[2]["hash"], "presence_name": test_art_entries[2]["presences"][0][0], "presence_domain": test_art_entries[1]["presences"][0][1]},
{"art_hash": test_art_entries[2]["hash"], "presence_name": test_art_entries[2]["presences"][1][0], "presence_domain": test_art_entries[2]["presences"][1][1]},
]
def list_art_to_presence_relations(url: str, port: int):
"""
List all relations between art and presence entries
:param url:
:param port:
:return:
"""
results = []
arts = list_art(url, port)
for art in arts:
r = requests.get(f"http://{url}:{port}/artnet/metadata/presence?art_id={art['id']}")
if r.status_code != 200:
raise Exception("Failed to query presences with art id!")
result = json.loads(r.text)
results.append(result)
return results
def art_hash_to_id(arts: List[dict]):
"""
Converts a list of arts into the format of {hash: id, hash1: id1}
This allows reverse mapping of the usual ID->hash to hash->ID.
:param arts:
:return:
"""
result = {}
for i in range(len(arts)):
result[arts[i]["md5_hash"].strip()] = arts[i]["id"]
return result
def create_art_presence_relations(url: str, port: int):
arts = list_art(url, port)
art_hash_id = art_hash_to_id(arts)
for i in range(len(test_art_presence_relations)):
print(f"Creating art-presence relation with id:{art_hash_id[test_art_presence_relations[i]['art_hash']]} "
f"presence_name:{test_art_presence_relations[i]['presence_name']} "
f"presence_domain:{test_art_presence_relations[i]['presence_domain']}")
r = create_art_presence_relation(url, port, art_id=art_hash_id[test_art_presence_relations[i]["art_hash"]],
presence_name=test_art_presence_relations[i]["presence_name"],
presence_domain=test_art_presence_relations[i]["presence_domain"])
if r.status_code != 200:
print(f"Tried to create art-presence relation with name:{test_art_presence_relations[i]['presence_name']} "
f"domain:{test_art_presence_relations[i]['presence_domain']}"
f" art_id:{test_art_presence_relations[i]['art_id']}")
print(f"Create Relation failed with {r.status_code} and reason {r.text}!")
raise Exception("Couldn't create art-presence relation!")
l = list_art_to_presence_relations(url, port)
if len(l) == 3 and len(l[-1]) == 2:
return True
else:
return False
def create_art_presence_relation(url: str, port: int, presence_name: str, presence_domain: str, art_id: int):
r = requests.post(f"http://{url}:{port}/artnet/metadata/art?id={art_id}",
json={"presences": [{"name": presence_name, "domain": presence_domain}]})
return r
def delete_art_presence_relations(url: str, port: int):
arts = list_art(url, port)
art_hash_id = art_hash_to_id(arts)
for i in range(len(test_art_presence_relations)):
print(f"Deleting art-presence relation with id:{art_hash_id[test_art_presence_relations[i]['art_hash']]} "
f"presence_name:{test_art_presence_relations[i]['presence_name']} "
f"presence_domain:{test_art_presence_relations[i]['presence_domain']}")
r = delete_art_presence_relation(url, port, art_id=art_hash_id[test_art_presence_relations[i]["art_hash"]],
presence_name=test_art_presence_relations[i]["presence_name"],
presence_domain=test_art_presence_relations[i]["presence_domain"])
if r.status_code != 200:
print(f"Tried to delete art-presence relation with name:{test_art_presence_relations[i]['presence_name']} "
f"domain:{test_art_presence_relations[i]['presence_domain']}"
f" art_id:{art_hash_id[test_art_presence_relations[i]['art_hash']]}")
print(f"Deleting Relation failed with {r.status_code} and reason {r.text}!")
raise Exception("Couldn't delete art-presence relation!")
l = list_art_to_presence_relations(url, port)
if len(l[0]) == 0 and len(l[1]) == 0 and len(l[2]) == 0:
return True
else:
return False
def delete_art_presence_relation(url: str, port: int, presence_name: str, presence_domain: str, art_id: int):
return requests.delete(f"http://{url}:{port}/artnet/metadata/art?id={art_id}&presence_name={presence_name}"
f"&presence_domain={presence_domain}")
def run_art_presence_relation_test(url, port):
print()
print("----------------")
if len(list_presences(url, port)) > 0:
print("Deleting leftover presence entries via cascading from artists ...")
delete_artist_entries(url, port) # deleting presences via cascading
if len(list_art(url, port)) > 0:
print("Deleting leftover art entries ...")
delete_art_entries(url, port)
print(f"Leftover entries for the test are "
f"({len(list_presences(url, port))}) presences, "
f"({len(list_art(url, port))}) art!")
print("Setting up art and presences for the art-presence-relation test ...")
create_artist_entries(url, port)
create_presence_entries(url, port)
create_art_entries(url, port)
print(f"Starting art2presence test with "
f"({len(list_presences(url, port))}) presences, "
f"({len(list_art(url, port))}) art!")
art = list_art(url, port) # debug
presences = list_presences(url, port) # debug
print()
print("Starting the art-presence test ...")
if sum([len(x) for x in list_art_to_presence_relations(url, port)]) != 4: # 3 expected from create_art_entries()
print("Anomalous amount of presence-art relations ...")
exit(1)
print("Creating art-presence relations ...")
create_result = create_art_presence_relations(url, port)
print(f"Found {sum([len(x) for x in list_art_to_presence_relations(url, port)])} art-presence relations!")
delete_result = delete_art_presence_relations(url, port)
print(f"Found {sum([len(x) for x in list_art_to_presence_relations(url, port)])} art-presence relations!")
delete_art_entries(url, port)
delete_artist_entries(url, port)
print(f"Ending art2presence test with "
f"({len(list_presences(url, port))}) presences, "
f"({len(list_art(url, port))}) art!")
return create_result, delete_result