Repository: farmOS/farmOS.py (language: python)

Issue:
Add test for Subrequests integration
It would be good to include a test for subrequests regardless, but this will be easier once farmOS depends on it: https://www.drupal.org/project/farm/issues/3241652

The test could be similar to this example: https://github.com/farmOS/farmOS.py/blob/1.x/docs/client_2x.md#subrequests


Discussion / hints:
https://github.com/farmOS/farmOS/commit/703f31d0e19ab4b8ae58f49a23aea912984a0d0b :tada: 

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9e4b7a5..7be6398 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [Unreleased]
 
+### Added
+- Add subrequests tests. [#52](https://github.com/farmOS/farmOS.py/issues/52)
+
 ### Changed
 - Move README sections into /docs with farmOS.org config.yml.
 - Add github workflow to build netlify docs.
diff --git a/docs/subrequests.md b/docs/subrequests.md
index e142e2e..e4a913f 100644
--- a/docs/subrequests.md
+++ b/docs/subrequests.md
@@ -47,7 +47,7 @@ plant = {
         "type": "asset--plant",
         "attributes": {
             "name": "My new plant",
-            "description": "Created in the first request.",
+            "notes": "Created in the first request.",
         },
         "relationships": {
             "plant_type": {


Test patch:
diff --git a/tests/functional/test_subrequests.py b/tests/functional/test_subrequests.py
new file mode 100644
index 0000000..ae4b3af
--- /dev/null
+++ b/tests/functional/test_subrequests.py
@@ -0,0 +1,142 @@
+import json
+from datetime import datetime, timezone
+
+from farmOS.subrequests import Action, Format, Subrequest, SubrequestsBlueprint
+from tests.conftest import farmOS_testing_server
+
+curr_time = datetime.now(timezone.utc)
+timestamp = curr_time.isoformat(timespec="seconds")
+
+
+@farmOS_testing_server
+def test_subrequests(test_farm):
+
+    plant_type = {
+        "data": {
+            "type": "taxonomy_term--plant_type",
+            "attributes": {"name": "New plant type"},
+        }
+    }
+
+    new_plant_type = Subrequest(
+        action=Action.create,
+        requestId="create-plant-type",
+        endpoint="api/taxonomy_term/plant_type",
+        body=plant_type,
+    )
+
+    plant = {
+        "data": {
+            "type": "asset--plant",
+            "attributes": {
+                "name": "My new plant",
+            },
+            "relationships": {
+                "plant_type": {
+                    "data": [
+                        {
+                            "type": "taxonomy_term--plant_type",
+                            "id": "{{create-plant-type.body@$.data.id}}",
+                        }
+                    ]
+                }
+            },
+        }
+    }
+    new_asset = Subrequest(
+        action=Action.create,
+        requestId="create-asset",
+        waitFor=["create-plant-type"],
+        endpoint="api/asset/plant",
+        body=plant,
+    )
+
+    log = {
+        "data": {
+            "type": "log--seeding",
+            "attributes": {
+                "name": "Seeding my new plant",
+            },
+            "relationships": {
+                "asset": {
+                    "data": [
+                        {
+                            "type": "asset--plant",
+                            "id": "{{create-asset.body@$.data.id}}",
+                        }
+                    ]
+                }
+            },
+        }
+    }
+    new_log = Subrequest(
+        action=Action.create,
+        requestId="create-log",
+        waitFor=["create-asset"],
+        endpoint="api/log/seeding",
+        body=log,
+    )
+
+    # Create a blueprint object
+    blueprint = SubrequestsBlueprint.parse_obj([new_plant_type, new_asset, new_log])
+
+    # Send the blueprint.
+    post_response = test_farm.subrequests.send(blueprint, format=Format.json)
+
+    # Expected results.
+    response_keys = {
+        "create-plant-type": {
+            "attributes": {"name": "New plant type"},
+            "relationships": {},
+        },
+        "create-asset#body{0}": {
+            "attributes": {
+                "name": "My new plant",
+            },
+            "relationships": {
+                "plant_type": [
+                    {
+                        "type": "taxonomy_term--plant_type",
+                        "id": "{{create-plant-type.body@$.data.id}}",
+                    }
+                ]
+            },
+        },
+        "create-log#body{0}": {
+            "attributes": {
+                "name": "Seeding my new plant",
+            },
+            "relationships": {
+                "asset": [
+                    {"type": "asset--plant", "id": "{{create-asset.body@$.data.id}}"}
+                ]
+            },
+        },
+    }
+    for response_key, expected_data in response_keys.items():
+
+        # Test that each response succeeded.
+        assert response_key in post_response
+        assert "headers" in post_response[response_key]
+        assert 201 == post_response[response_key]["headers"]["status"][0]
+
+        # Test that each resource was created.
+        assert "body" in post_response[response_key]
+        body = json.loads(post_response[response_key]["body"])
+        resource_id = body["data"]["id"]
+        entity_type, bundle = body["data"]["type"].split("--")
+        created_resource = test_farm.resource.get_id(entity_type, bundle, resource_id)
+
+        assert created_resource is not None
+        assert created_resource["data"]["id"] == resource_id
+
+        # Test for correct attributes.
+        for key, value in expected_data["attributes"].items():
+            assert created_resource["data"]["attributes"][key] == value
+
+        # Test for correct relationships.
+        for field_name, relationships in expected_data["relationships"].items():
+            relationship_field = created_resource["data"]["relationships"][field_name]
+            assert len(relationship_field["data"]) == len(relationships)
+            for resource in relationships:
+                assert relationship_field["data"][0]["type"] == resource["type"]