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.

134 lines
4.8 KiB
Python

import requests
from urllib.parse import quote
import json
from typing import List, Tuple
test_tag_category_entries = [{"name": "category_name1", "id": None},
{"name": "category_name2", "id": None},
{"name": "category_name3", "id": None}]
def list_tag_categories(url: str, port: int):
"""
Return a list of all tag categories
:param url:
:param port:
:return:
"""
r = requests.get(f"http://{url}:{port}/artnet/metadata/category")
if r.status_code != 200:
raise Exception("Failed querying for tag category!")
categories = json.loads(r.text)
return categories
def get_category_by_ID(url: str, port: int, category_id: id):
r = requests.get(f"http://{url}:{port}/artnet/metadata/category?id={category_id}")
if r.status_code != 200:
raise Exception("Failed querying for tag category!")
return json.loads(r.text)
def create_tag_categories(url: str, port: int):
"""
Create many tag categories for testing purposes
:param url:
:param port:
:return:
"""
for i in range(len(test_tag_category_entries)):
r = create_tag_category(url, port, name=test_tag_category_entries[i]["name"])
if r.status_code != 200:
print(f"Create Category Entry Test Nr.{i}: failed with {r.status_code} and reason {r.text}")
raise Exception("Create Category Entry Test: FAILED")
else:
test_tag_category_entries[i]["id"] = int(json.loads(r.text)["id"])
def update_category_entries(url: str, port: int):
for i in range(len(test_tag_category_entries)):
update_tag_category(url, port, category_id=test_tag_category_entries[i]["id"], name=f"Category #{i}")
updated_category = get_category_by_ID(url, port, test_tag_category_entries[i]["id"])
if updated_category['name'] != f"Category #{i}":
raise Exception("Updating the category name failed!")
def delete_category_entries(url: str, port: int):
categories = list_tag_categories(url, port)
for i in range(len(categories)):
if "category_id" in categories[i].keys():
r = delete_tag_category(url, port, category_id=categories[i]["category_id"])
else:
r = delete_tag_category_by_name(url, port, name=categories[i]["name"])
if r.status_code != 200:
raise Exception(f"Could not delete tag category! {r.status_code}, {r.text}")
print("Deleted category entries: SUCCESS")
def create_tag_category(url: str, port: int, name: str):
r = requests.post(f"http://{url}:{port}/artnet/metadata/category",
json={"name": name})
return r
def update_tag_category(url: str, port: int, name: str, category_id: int):
r = requests.post(f"http://{url}:{port}/artnet/metadata/category?id={quote(str(category_id))}",
json={"name": name})
return r
def delete_tag_category(url: str, port: int, category_id: int):
r = requests.delete(f"http://{url}:{port}/artnet/metadata/category?id={quote(str(category_id))}")
return r
def delete_tag_category_by_name(url: str, port: int, name: str):
r1 = requests.get(f"http://{url}:{port}/artnet/metadata/category?name={quote(name.strip())}")
if r1.status_code != 200:
raise ValueError("Could not delete tag category because it could not be found!")
data = json.loads(str(r1.text))
category_id = data['category_id']
r = delete_tag_category(url, port, category_id=category_id)
return r
def run_tag_category_test(url: str, port: int):
print()
print("----------------")
categories = list_tag_categories(url, port)
print(f"Starting tag name test with {len(categories)} categories!")
print("Starting the category test ...")
if len(list_tag_categories(url, port)) > 0:
print("Deleting previous category entries of tests ...")
delete_category_entries(url, port)
print(f"Creating {len(test_tag_category_entries)} category entries ...")
create_tag_categories(url, port)
create_tag_result = False if not len(list_tag_categories(url, port)) == len(test_tag_category_entries) else True
print(f"Found {len(list_tag_categories(url, port))} category entries!")
if not create_tag_result:
print(f"Failure! Unexpected number of category entries!")
print("Updating category entries with new data ...")
update_category_entries(url, port)
update_category_result = True
print("TODO")
print(f"Found {len(list_tag_categories(url, port))} category entries!")
print("Deleting all category entries ...")
delete_category_entries(url, port)
delete_category_result = False if not len(list_tag_categories(url, port)) == 0 else True
print(f"Found {len(list_tag_categories(url, port))} category entries!")
print("Category test complete!")
return create_tag_result, update_category_result, delete_category_result