From e8d5449fcf85ccd810937dfa93455eef28433e34 Mon Sep 17 00:00:00 2001 From: Anna Shamray Date: Tue, 12 Mar 2024 16:47:47 +0100 Subject: [PATCH] :sparkles: add management command to test integration between objects api and objecttypes api --- src/objects/config/management/__init__.py | 0 .../config/management/commands/__init__.py | 0 .../management/commands/create_test_object.py | 103 ++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 src/objects/config/management/__init__.py create mode 100644 src/objects/config/management/commands/__init__.py create mode 100644 src/objects/config/management/commands/create_test_object.py diff --git a/src/objects/config/management/__init__.py b/src/objects/config/management/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/objects/config/management/commands/__init__.py b/src/objects/config/management/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/objects/config/management/commands/create_test_object.py b/src/objects/config/management/commands/create_test_object.py new file mode 100644 index 00000000..b5004cd0 --- /dev/null +++ b/src/objects/config/management/commands/create_test_object.py @@ -0,0 +1,103 @@ +from django.conf import settings +from django.core.management import BaseCommand, CommandError +from django.urls import reverse + +import requests + + +class Command(BaseCommand): + help = "Create a test object as an integration test for Objects and Objecttypes API" + + def add_arguments(self, parser): + super().add_arguments(parser) + + parser.add_argument( + "--keep-data", + dest="keep_data", + action="store_true", + help="The component name to define urlconf and schema info", + ) + + def handle(self, **options): + # For test purposes the same DEMO_TOKEN is used for both Objects API and Objecttypes API + if not settings.DEMO_TOKEN: + raise CommandError("DEMO_TOKEN env var should be configured") + + req = requests.session() + req.headers.update( + { + "Authorization": f"Token {settings.DEMO_TOKEN}", + "Accept": "application/json", + "Content-Type": "application/json", + "Content-Crs": "EPSG:4326", + } + ) + req.hooks = {"response": [lambda r, *args, **kwargs: r.raise_for_status()]} + + # 1. create an Object Type + objecttype_data = { + "name": "boom", + "namePlural": "bomen", + "description": "tree type description", + } + objecttypes_list_url = "http://localhost:8001/api/v2/objecttypes" + response = req.post(objecttypes_list_url, json=objecttype_data) + + objecttype_url = response.json()["url"] + objecttype_uuid = response.json()["uuid"] + self.stdout.write(f"Demo objecttype {objecttype_url} was created") + + # 2. Create an Object Type version + objectversion_data = { + "status": "draft", + "jsonSchema": { + "type": "object", + "title": "Tree", + "$schema": "http://json-schema.org/draft-07/schema#", + "required": ["diameter"], + "properties": { + "diameter": {"type": "integer", "description": "size in cm."} + }, + }, + } + objectversion_list_url = ( + f"http://localhost:8001/api/v2/objecttypes/{objecttype_uuid}/versions" + ) + response = req.post(objectversion_list_url, json=objectversion_data) + + objectversion_url = response.json()["url"] + objectversion_version = response.json()["version"] + self.stdout.write(f"Demo objectversion {objectversion_url} was created") + + # 3. Create an Object + object_data = { + "type": objecttype_url.replace("localhost:8001", "objecttypes:8000"), + "record": { + "typeVersion": objectversion_version, + "data": {"diameter": 30}, + "startAt": "2024-01-01", + }, + } + object_list_url = "http://localhost:8000/api/v2/objects" + response = req.post(object_list_url, json=object_data) + + object_url = response.json()["url"] + self.stdout.write(f"Demo object {object_url} was created") + + if options["keep_data"]: + self.stdout.write("Demo objecttype and object are remained in DB") + return + + # 4. Delete an Object + req.delete(object_url) + self.stdout.write(f"Demo object {object_url} was deleted") + + # 5. Delete an Object Version + req.delete(objectversion_url) + self.stdout.write(f"Demo objectversion {objectversion_url} was deleted") + + # 6. Delete an Object Type + req.delete(objecttype_url) + self.stdout.write(f"Demo objecttype {objecttype_url} was deleted") + + self.stdout.write(self.style.SUCCESS("Demo process is finished"))