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.

177 lines
6.9 KiB
Python

import requests
import urllib.parse
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
test_art_entries = [
{"hash": "hash1", "path": "artist1/image1", "title": "Image Title 1", "link": "http://localhost/artist1/image1.png",
"presences": [(test_presence_entries[0]["name"], test_presence_entries[0]["domain"])]},
{"hash": "hash2", "path": "artist1/image2", "title": "Image Title 2", "link": "http://localhost/artist1/image2.png",
"presences": [(test_presence_entries[1]["name"], test_presence_entries[1]["domain"])]},
{"hash": "hash3", "path": "artist2/image1", "title": "Image Title 3", "link": "http://localhost/artist2/image3.png",
"presences": [(test_presence_entries[0]["name"], test_presence_entries[0]["domain"]),
(test_presence_entries[1]["name"], test_presence_entries[1]["domain"])]},
]
def list_art(url: str, port: int):
"""
Return a list of all art known to the database
:param url:
:param port:
:return:
"""
r = requests.get(f"http://{url}:{port}/artnet/metadata/art")
if r.status_code != 200:
raise Exception("Failed querying for art!")
art = json.loads(r.text)
return art
def create_art_entries(url: str, port: int):
"""
Creates many art entries for testing purposes
:param url:
:param port:
:return:
"""
for i in range(len(test_art_entries)):
r = create_art(url, port, md5_hash=test_art_entries[i]["hash"], path=test_art_entries[i]["path"],
title=test_art_entries[i]["title"], link=test_art_entries[i]["link"],
presences=test_art_entries[i]["presences"])
if r.status_code != 200:
print(f"Create Art Entry Test Nr.{i}: failed with {r.status_code} and reason {r.text}")
raise Exception("Create Art Entry Test: FAILED")
else:
test_art_entries[i]["ID"] = json.loads(r.text)["id"]
print("Created art entries: SUCCESS")
def update_art_entries(url: str, port: int):
for i in range(len(test_art_entries)):
r = update_art(url, port, md5_hash=test_art_entries[i]["hash"]+"moreHash",
path=test_art_entries[i]["path"]+"evenBetterPath",
title=test_art_entries[i]["title"]+" updated",
presences=test_art_entries[i]["presences"], art_id=test_art_entries[i]["ID"])
if r.status_code != 200:
print(f"Update Art Entry Test Nr.{i}: failed with {r.status_code} and reason {r.text}")
raise Exception("Update Art Entry Test: FAILED")
for i in range(len(test_art_entries)):
r = requests.get(f"http://{url}:{port}/artnet/metadata/art?id={urllib.parse.quote(str(test_art_entries[i]['ID']))}")
if json.loads(r.text)["title"].split(" ")[-1] != "updated":
raise Exception("Update Art Entry Test: Failed (unexpected or no updated title)")
if json.loads(r.text)["link"] != test_art_entries[i]["link"]:
raise Exception("Update Art Entry Test: Failed (unexpected link)")
print("Updated art entries: SUCCESS")
def delete_art_entries(url: str, port: int):
arts = list_art(url, port)
for i in range(len(arts)):
if "id" in arts[i].keys():
r = delete_art_by_id(url, port, art_id=arts[i]["id"])
else:
r = delete_art_by_hash(url, port, md5_hash=arts[i]["hash"])
if r.status_code != 200:
raise Exception(f"Could not delete the art! {r.status_code}, {r.text}")
print("Deleted art entries: SUCCESS")
def create_art(url: str, port: int, md5_hash: str, path: str, title: str, link: str, presences: List[Tuple[str, str]]):
r = requests.post(f"http://{url}:{port}/artnet/metadata/art",
json={"hash": md5_hash, "path": path, "title": title, "link": link,
"presences": [{"name": presence[0], "domain": presence[1]} for presence in presences]})
return r
def update_art(url: str, port: int, art_id: int, md5_hash: str = None, path: str = None, title: str = None,
link: str = None, presences: List[Tuple[str, str]] = None):
body = {}
if md5_hash is not None:
body["hash"] = md5_hash
if path is not None:
body["path"] = path
if title is not None:
body["title"] = title
if link is not None:
body["link"] = link
if presences is not None:
body["presences"] = [{"name": presence[0], "domain": presence[1]} for presence in presences]
r = requests.post(f"http://{url}:{port}/artnet/metadata/art?id={urllib.parse.quote(str(art_id))}",
json=body)
return r
def delete_art_by_id(url: str, port: int, art_id: int):
r = requests.delete(f"http://{url}:{port}/artnet/metadata/art?id={urllib.parse.quote(str(art_id))}")
return r
def delete_art_by_hash(url: str, port: int, md5_hash: str):
r1 = requests.get(f"http://{url}:{port}/artnet/metadata/art?hash={urllib.parse.quote(md5_hash)}")
if r1.status_code != 200:
raise ValueError("Could not delete art because it could not be found!")
data = json.loads(str(r1.text))
art_id = data['id']
r = delete_art_by_id(url, port, art_id=art_id)
return r
def run_art_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, "
f"({len(list_art(url, port))}) art!")
print("Setting up artist and presences for the art test ...")
create_artist_entries(url, port)
create_presence_entries(url, port)
print("Starting the art test ...")
if len(list_art(url, port)) > 0:
print("Deleting previous art entries of tests ...")
delete_art_entries(url, port)
print(f"Creating {len(test_art_entries)} art entries ...")
create_art_entries(url, port)
create_art_result = False if not len(list_art(url, port)) == len(test_art_entries) else True
print(f"Found {len(list_art(url, port))} art entries!")
if not create_art_result:
print(f"Failure! Unexpected number of art entries!")
print("Updating all art entries with new data ...")
update_art_entries(url, port)
update_art_result = True # given, the update function would raise an exception on error
print(f"Found {len(list_art(url, port))} art entries!")
print("Deleting all art entries ...")
delete_art_entries(url, port)
delete_art_result = False if not len(list_art(url, port)) == 0 else True
print(f"Found {len(list_art(url, port))} art entries!")
print("Art test complete! Cleaning up setup ...")
delete_artist_entries(url, port)
print("----------------")
print()
return create_art_result, update_art_result, delete_art_result