Repository: PrefectHQ/prefect

Issue:
Add an integration test that creates a deployment
### First check

- [X] I am a contributor to the Prefect codebase

### Description

We run integration tests by executing all of the files in the `flows/` directory. It'd be good to have test coverage of deployments there.

### Impact

Avoid issues like #8110 

### Additional context

_No response_


Discussion:

Resolving patch:
diff --git a/flows/deployment.py b/flows/deployment.py
new file mode 100644
--- /dev/null
+++ b/flows/deployment.py
@@ -0,0 +1,36 @@
+import anyio
+from packaging.version import Version
+
+import prefect
+from prefect.deployments import Deployment
+from prefect.utilities.callables import parameter_schema
+
+
+@prefect.flow
+def hello(name: str = "world"):
+    prefect.get_run_logger().info(f"Hello {name}!")
+
+
+async def apply_deployment(deployment):
+    async with prefect.get_client() as client:
+        flow_id = await client.create_flow_from_name(deployment.flow_name)
+        await client.create_deployment(flow_id=flow_id, name=deployment.name)
+
+
+if __name__ == "__main__":
+    # Create deployment
+    if Version(prefect.__version__) < Version("2.1.0"):
+        deployment = Deployment(
+            name="test-deployment",
+            flow_name=hello.name,
+            parameter_openapi_schema=parameter_schema(hello),
+        )
+        anyio.run(apply_deployment, deployment)
+    else:
+        deployment = Deployment.build_from_flow(flow=hello, name="test-deployment")
+        deployment.apply()
+
+    # Update deployment
+    deployment.tags = ["test"]
+    if Version(prefect.__version__) >= Version("2.1.0"):
+        deployment.apply()
diff --git a/src/prefect/client/orion.py b/src/prefect/client/orion.py
--- a/src/prefect/client/orion.py
+++ b/src/prefect/client/orion.py
@@ -1305,8 +1305,6 @@ async def create_deployment(
             parameters=dict(parameters or {}),
             tags=list(tags or []),
             work_queue_name=work_queue_name,
-            work_pool_name=work_pool_name,
-            work_pool_queue_name=work_pool_queue_name,
             description=description,
             storage_document_id=storage_document_id,
             path=path,
@@ -1317,8 +1315,23 @@ async def create_deployment(
             parameter_openapi_schema=parameter_openapi_schema,
         )
 
+        if work_pool_name is not None:
+            deployment_create.work_pool_name = work_pool_name
+        if work_pool_queue_name is not None:
+            deployment_create.work_pool_queue_name = work_pool_queue_name
+
+        # Exclude newer fields that are not set to avoid compatibility issues
+        exclude = {
+            field
+            for field in ["work_pool_name", "work_pool_queue_name"]
+            if field not in deployment_create.__fields_set__
+        }
+
+        json = deployment_create.dict(json_compatible=True, exclude=exclude)
+
         response = await self._client.post(
-            "/deployments/", json=deployment_create.dict(json_compatible=True)
+            "/deployments/",
+            json=json,
         )
         deployment_id = response.json().get("id")
         if not deployment_id:
diff --git a/src/prefect/orion/schemas/actions.py b/src/prefect/orion/schemas/actions.py
--- a/src/prefect/orion/schemas/actions.py
+++ b/src/prefect/orion/schemas/actions.py
@@ -3,7 +3,7 @@
 """
 import re
 import warnings
-from copy import deepcopy
+from copy import copy
 from typing import Any, Dict, Generator, List, Optional, Union
 from uuid import UUID
 
@@ -85,7 +85,7 @@ def remove_old_fields(cls, values):
         # worker_pool_queue_name. Those fields were later renamed to work_pool_name
         # and work_pool_queue_name. This validator removes old fields provided
         # by older clients to avoid 422 errors.
-        values_copy = deepcopy(values)
+        values_copy = copy(values)
         worker_pool_queue_id = values_copy.pop("worker_pool_queue_id", None)
         worker_pool_name = values_copy.pop("worker_pool_name", None)
         worker_pool_queue_name = values_copy.pop("worker_pool_queue_name", None)
@@ -149,7 +149,7 @@ def remove_old_fields(cls, values):
         # worker_pool_queue_name. Those fields were later renamed to work_pool_name
         # and work_pool_queue_name. This validator removes old fields provided
         # by older clients to avoid 422 errors.
-        values_copy = deepcopy(values)
+        values_copy = copy(values)
         worker_pool_queue_id = values_copy.pop("worker_pool_queue_id", None)
         worker_pool_name = values_copy.pop("worker_pool_name", None)
         worker_pool_queue_name = values_copy.pop("worker_pool_queue_name", None)


Test patch: