ocket8888 commented on code in PR #7424:
URL: https://github.com/apache/trafficcontrol/pull/7424#discussion_r1156207990


##########
traffic_ops/testing/api_contract/v4/conftest.py:
##########
@@ -336,3 +406,68 @@ def cdn_post_data(to_session: TOSession, cdn_prereq_data: 
list[JSONData]) -> dic
        except IndexError:
                logger.error("No CDN response data from cdns POST request.")
                sys.exit(1)
+
+
[email protected]()
+def cachegroup_post_data(to_session: TOSession, request_template_data: 
list[JSONData]
+                        ) -> dict[str, object]:
+       """
+       PyTest Fixture to create POST data for cachegroup endpoint.
+
+       :param to_session: Fixture to get Traffic Ops session.
+       :param request_template_data: Fixture to get Cachegroup data from a 
prerequisites file.
+       :returns: Sample POST data and the actual API response.
+       """
+       try:
+               cachegroup = request_template_data[0]
+       except IndexError as e:
+               raise TypeError(
+                       "malformed prerequisite data; no Cache group present in 
'cachegroup' array property") from e
+
+       if not isinstance(cachegroup, dict):
+               raise TypeError(
+                       f"malformed prerequisite data; Cache group must be 
objects, not '{type(cachegroup)}'")
+
+       # Return new post data and post response from cachegroups POST request
+       randstr = str(randint(0, 1000))
+       try:
+               name = cachegroup["name"]
+               if not isinstance(name, str):
+                       raise TypeError(f"name must be str, not '{type(name)}'")
+               cachegroup["name"] = name[:4] + randstr
+               short_name = cachegroup["shortName"]
+               if not isinstance(short_name, str):
+                       raise TypeError(f"shortName must be str, not 
'{type(short_name)}")
+               cachegroup["shortName"] = short_name[:5] + randstr
+       except KeyError as e:
+               raise TypeError(f"missing Cache group property '{e.args[0]}'") 
from e
+       # Hitting types GET method to access typeID for cachegroup POST data
+       type_get_response: tuple[
+               dict[str, object] | list[dict[str, object] | list[object] | 
primitive] | primitive,
+               requests.Response
+       ] = to_session.get_types(query_params={"useInTable": "cachegroup"})
+       try:
+               type_data = type_get_response[0]
+               if not isinstance(type_data, list):
+                       raise TypeError("malformed API response; 'response' 
property not an array")
+               first_type = type_data[0]
+               if not isinstance(first_type, dict):
+                       raise TypeError("malformed API response; first Type in 
response is not an object")
+               cachegroup["typeId"] = first_type["id"]
+               type_id = cachegroup["typeId"]
+               logger.info("extracted %s from %s", type_id, type_get_response)
+       except KeyError as e:
+               raise TypeError(f"missing Type property '{e.args[0]}'") from e
+
+       logger.info("New cachegroup data to hit POST method %s", 
request_template_data)
+       # Hitting cachegroup POST method
+       response: tuple[JSONData, requests.Response] = 
to_session.create_cachegroups(data=cachegroup)
+       try:
+               resp_obj = response[0]
+               if not isinstance(resp_obj, dict):
+                       raise TypeError("malformed API response; cache group is 
not an object")
+               return resp_obj
+       except IndexError:
+               logger.error("No Cache group response data from cdns POST 
request.")
+               sys.exit(1)
+               

Review Comment:
   Missing newline at EOF



##########
traffic_ops/testing/api_contract/v4/request_template.json:
##########
@@ -0,0 +1,24 @@
+{
+       "cdns": [
+               {
+                       "name": "test",
+                       "domainName": "quest",
+                       "dnssecEnabled": false
+               }
+       ],
+       "cachegroup": [
+               {
+                       "name": "test",
+                       "shortName": "test",
+                       "latitude": 38.897663,
+                       "longitude": -77.036574,
+                       "fallbackToClosest": true,
+                       "localizationMethods": [
+                               "DEEP_CZ",
+                               "CZ",
+                               "GEO"
+                       ],
+                       "typeId": 23
+               }
+       ]
+}

Review Comment:
   Missing newline at EOF



##########
traffic_ops/testing/api_contract/v4/response_template.json:
##########
@@ -0,0 +1,81 @@
+{
+    "type": "object",
+    "cdns": {
+        "type": "object",
+        "properties": {
+            "name": {
+                "type": "str"
+            },
+            "domainName": {
+                "type": "str"
+            },
+            "dnssecEnabled": {
+                "type": "bool"
+            },
+            "id": {
+                "type": "int"
+            },
+            "lastUpdated": {
+                "type": "str"
+            }
+        }
+    },
+    "cachegroup": {
+        "type": "object",
+        "properties": {
+            "id": {
+                "type": "int"
+            },
+            "name": {
+                "type": "str"
+            },
+            "shortName": {
+                "type": "str"
+            },
+            "latitude": {
+                "type": "float"
+            },
+            "longitude": {
+                "type": "float"
+            },
+            "parentCachegroupName": {
+                "optional": "True",
+                "typeA": "str",
+                "typeB": "NoneType"
+            },
+            "parentCachegroupId": {
+                "optional": "True",
+                "typeA": "int",
+                "typeB": "NoneType"
+            },
+            "secondaryParentCachegroupName": {
+                "optional": "True",
+                "typeA": "str",
+                "typeB": "NoneType"
+            },
+            "secondaryParentCachegroupId": {
+                "optional": "True",
+                "typeA": "int",
+                "typeB": "NoneType"
+            },
+            "fallbackToClosest": {
+                "type": "bool"
+            },
+            "localizationMethods": {
+                "type": "list"
+            },
+            "typeName": {
+                "type": "str"
+            },
+            "typeId": {
+                "type": "int"
+            },
+            "lastUpdated": {
+                "type": "str"
+            },
+            "fallbacks": {
+                "type": "list"
+            }
+        }
+    }
+}

Review Comment:
   Missing newline at EOF



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to