import requests import json from tests.createAPI.create_delete_art import get_art, list_art, create_art_entries, delete_art_entries, \ test_art_entries from tests.createAPI.create_delete_presence import create_presence_entries from tests.createAPI.create_delete_artist import create_artist_entries, delete_artist_entries from tests.createAPI.create_delete_collection import get_collection_by_id, list_collections, create_collection_entries, \ delete_collection_entries, test_collection_entries test_art_2_collection_entries = [{"art_id": 0, "collection_id": 0, "ranking": "aa"}, {"art_id": 1, "collection_id": 0, "ranking": "ab"}, {"art_id": 2, "collection_id": 1, "ranking": "aaa"}, {"art_id": 1, "collection_id": 1, "ranking": "ac"}] def create_art2collections(url: str, port: int): for relation in test_art_2_collection_entries: add_art_to_collection(url, port, art_id=test_art_entries[relation["art_id"]]["ID"], collection_id=test_collection_entries[relation["collection_id"]]["id"]) get_art2collection(url, port, art_id=test_art_entries[relation["art_id"]]["ID"], collection_id=test_collection_entries[relation["collection_id"]]["id"]) return True def update_art2collections(url: str, port: int): for relation in test_art_2_collection_entries: update_art2collection(url, port, art_id=test_art_entries[relation["art_id"]]["ID"], collection_id=test_collection_entries[relation["collection_id"]]["id"], ranking=relation["ranking"]) c = get_art2collection(url, port, art_id=test_art_entries[relation["art_id"]]["ID"], collection_id=test_collection_entries[relation["collection_id"]]["id"]) if c["ranking"] != relation["ranking"]: print(f"Failed to update the ARt2Collection relation! Got: {c['ranking']} Expected: {relation['ranking']}") raise Exception("Failed to update the ranking in Art2Collection") return True def delete_art2collections(url: str, port: int): for relation in test_art_2_collection_entries: delete_art2collection(url, port, art_id=test_art_entries[relation["art_id"]]["ID"], collection_id=test_collection_entries[relation["collection_id"]]["id"]) return True def add_art_to_collection(url: str, port: int, art_id: int, collection_id: int): prev_colls = get_art2collections_by_art_id(url, port, art_id) r = requests.post(f"http://{url}:{port}/artnet/metadata/art?id={art_id}", json={"collections": [{"id": c["collection_id"]} for c in prev_colls] + [{"id": collection_id}]}) if r.status_code != 200: print(f"Failed to set Art2Collection. Status: {r.status_code} Reason: {r.text}") raise Exception("Failed to set Art2Collection relation!") def update_art2collection(url: str, port: int, art_id: int, collection_id: int, ranking: str): r = requests.post(f"http://{url}:{port}/artnet/metadata/collection_entry?art_id={art_id}" f"&collection_id={collection_id}", json={"ranking": ranking}) if r.status_code != 200: print(f"Failed to update Art2Collection relation! Status: {r.status_code} Reason: {r.text}") raise Exception("Failed to update Art2Collection relation!") def get_art2collection(url: str, port: int, art_id: int, collection_id: int): r = requests.get(f"http://{url}:{port}/artnet/metadata/collection_entry?art_id={art_id}" f"&collection_id={collection_id}") if r.status_code != 200: print(f"Failed to fetch specific art-collection relation! Status: {r.status_code} Reason: {r.text}") raise Exception("Failed to fetch Art2Collection!") result = json.loads(r.text) if result["art_id"] != art_id or result["collection_id"] != collection_id: print(f"Got: ({result['art_id']}, {result['collection_id']}) Expected: ({art_id}, {collection_id})") raise Exception("The returned relation does not fit the requested one!") return result def delete_art2collection(url: str, port: int, art_id: int, collection_id: int): r = requests.delete(f"http://{url}:{port}/artnet/metadata/collection_entry?art_id={art_id}" f"&collection_id={collection_id}") if r.status_code != 200: print(f"Failed to delete art-collection relation! Status: {r.status_code} Reason: {r.text}") raise Exception("Failed to delete art-collection relation!") def get_art2collections_by_art_id(url: str, port: int, art_id: int): r = requests.get(f"http://{url}:{port}/artnet/metadata/collection?art_id={art_id}") if r.status_code != 200: print(f"Failed to query for Art2Collection relation via art_id. Status: {r.status_code} Reason: {r.text}") raise Exception("Failed to query Art2Collection relations!") return json.loads(r.text) def run_art2collections_tests(url: str, port: int): print() print("----------------") print(f"Starting art2collection test with ({len(list_collections(url, port))}) collections and " f"({len(list_art(url, port))}) art!") print("Setting up art, presence, artists and collections for the tests ...") create_collection_entries(url, port) create_artist_entries(url, port) create_presence_entries(url, port) create_art_entries(url, port) print(f"Done with the setup! Now have ({len(list_collections(url, port))}) collections and " f"({len(list_art(url, port))}) art!") print("Adding art2collection relation ...") create_result = create_art2collections(url, port) print("Updating rankings ...") update_result = update_art2collections(url, port) print("Removing relations ...") delete_result = delete_art2collections(url, port) print("Done with the art2collection relation test!") print("Cleaning up the setup ...") delete_collection_entries(url, port) delete_artist_entries(url, port) delete_art_entries(url, port) return create_result, update_result, delete_result