Introduced TagCategory, bugfixes
Fixed several bugs from previous commits (Bad distribution of changes across multiple commits). Introduced striping of string fields before send back as replies in main.py. Implemented POST and DELETE calls for /artnet/metadata/category Implemented tests for the new calls.master
parent
25c18651e4
commit
9fe017a6e3
@ -0,0 +1,133 @@
|
||||
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
|
||||
|
Loading…
Reference in New Issue