Repository: PrefectHQ/prefect

Issue:
Add tests for CLI project / no project logic
As of `0.10.0` some of the CLI commands now make the project name specification optional due to the API differences between Core's server and Cloud. Tests should be added for this behavior

```
# when using Cloud
prefect get flows -n my-flow -p my-project

# when using the server
prefect get flows -n my-flow
```


Discussion:

Resolving patch:
diff --git a/src/prefect/cli/get.py b/src/prefect/cli/get.py
--- a/src/prefect/cli/get.py
+++ b/src/prefect/cli/get.py
@@ -2,6 +2,7 @@
 import pendulum
 from tabulate import tabulate
 
+from prefect import config
 from prefect.client import Client
 from prefect.utilities.graphql import EnumValue, with_args
 
@@ -80,7 +81,7 @@ def flows(name, version, project, limit, all_versions):
 
     headers = ["NAME", "VERSION", "AGE"]
 
-    if project:
+    if config.backend == "cloud":
         where_clause["_and"]["project"] = {"name": {"_eq": project}}
         query_results["project"] = {"name": True}
         headers.append("PROJECT NAME")
@@ -219,14 +220,14 @@ def flow_runs(limit, flow, project, started):
             }
         }
 
-        if project:
+        if config.backend == "cloud":
             where["_and"]["flow"]["_and"]["project"] = {"name": {"_eq": project}}
     else:
         order = {"created": EnumValue("desc")}
 
         where = {"flow": {"_and": {"name": {"_eq": flow},}}}
 
-        if project:
+        if config.backend == "cloud":
             where["flow"]["_and"]["project"] = {"name": {"_eq": project}}
 
     query = {
@@ -307,7 +308,7 @@ def tasks(name, flow_name, flow_version, project, limit):
         }
     }
 
-    if project:
+    if config.backend == "cloud":
         where_clause["_and"]["flow"]["project"] = {"name": {"_eq": project}}
 
     query = {


Test patch:
diff --git a/tests/cli/test_get.py b/tests/cli/test_get.py
--- a/tests/cli/test_get.py
+++ b/tests/cli/test_get.py
@@ -25,7 +25,7 @@ def test_get_help():
     assert "Get commands that refer to querying Prefect API metadata." in result.output
 
 
-def test_get_flows(monkeypatch):
+def test_get_flows_server(monkeypatch, server_api):
     post = MagicMock(
         return_value=MagicMock(json=MagicMock(return_value=dict(data=dict(flow=[]))))
     )
@@ -33,9 +33,38 @@ def test_get_flows(monkeypatch):
     session.return_value.post = post
     monkeypatch.setattr("requests.Session", session)
 
-    with set_temporary_config(
-        {"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
-    ):
+    runner = CliRunner()
+    result = runner.invoke(get, ["flows"])
+    assert result.exit_code == 0
+    assert (
+        "NAME" in result.output
+        and "VERSION" in result.output
+        and "AGE" in result.output
+    )
+
+    query = """
+    query {
+        flow(where: { _and: { name: { _eq: null }, version: { _eq: null } } }, order_by: { name: asc, version: desc }, distinct_on: name, limit: 10) {
+            name
+            version
+            created
+        }
+    }
+    """
+
+    assert post.called
+    assert post.call_args[1]["json"]["query"].split() == query.split()
+
+
+def test_get_flows_cloud(monkeypatch, cloud_api):
+    post = MagicMock(
+        return_value=MagicMock(json=MagicMock(return_value=dict(data=dict(flow=[]))))
+    )
+    session = MagicMock()
+    session.return_value.post = post
+    monkeypatch.setattr("requests.Session", session)
+
+    with set_temporary_config({"cloud.auth_token": "secret_token"}):
         runner = CliRunner()
         result = runner.invoke(get, ["flows"])
         assert result.exit_code == 0
@@ -43,14 +72,18 @@ def test_get_flows(monkeypatch):
             "NAME" in result.output
             and "VERSION" in result.output
             and "AGE" in result.output
+            and "PROJECT NAME" in result.output
         )
 
         query = """
         query {
-            flow(where: { _and: { name: { _eq: null }, version: { _eq: null } } }, order_by: { name: asc, version: desc }, distinct_on: name, limit: 10) {
+            flow(where: { _and: { name: { _eq: null }, version: { _eq: null }, project: { name: { _eq: null } } } }, order_by: { name: asc, version: desc }, distinct_on: name, limit: 10) {
                 name
                 version
                 created
+                project {
+                    name
+                }
             }
         }
         """
@@ -59,7 +92,7 @@ def test_get_flows(monkeypatch):
         assert post.call_args[1]["json"]["query"].split() == query.split()
 
 
-def test_get_flows_populated(monkeypatch):
+def test_get_flows_populated(monkeypatch, cloud_api):
     post = MagicMock(
         return_value=MagicMock(json=MagicMock(return_value=dict(data=dict(flow=[]))))
     )
@@ -67,9 +100,7 @@ def test_get_flows_populated(monkeypatch):
     session.return_value.post = post
     monkeypatch.setattr("requests.Session", session)
 
-    with set_temporary_config(
-        {"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
-    ):
+    with set_temporary_config({"cloud.auth_token": "secret_token"}):
         runner = CliRunner()
         result = runner.invoke(
             get,
@@ -105,7 +136,7 @@ def test_get_flows_populated(monkeypatch):
         assert post.call_args[1]["json"]["query"].split() == query.split()
 
 
-def test_get_projects(monkeypatch):
+def test_get_projects(monkeypatch, cloud_api):
     post = MagicMock(
         return_value=MagicMock(json=MagicMock(return_value=dict(data=dict(project=[]))))
     )
@@ -113,9 +144,7 @@ def test_get_projects(monkeypatch):
     session.return_value.post = post
     monkeypatch.setattr("requests.Session", session)
 
-    with set_temporary_config(
-        {"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
-    ):
+    with set_temporary_config({"cloud.auth_token": "secret_token"}):
         runner = CliRunner()
         result = runner.invoke(get, ["projects"])
         assert result.exit_code == 0
@@ -145,7 +174,7 @@ def test_get_projects(monkeypatch):
         assert post.call_args[1]["json"]["query"].split() == query.split()
 
 
-def test_get_projects_populated(monkeypatch):
+def test_get_projects_populated(monkeypatch, cloud_api):
     post = MagicMock(
         return_value=MagicMock(json=MagicMock(return_value=dict(data=dict(project=[]))))
     )
@@ -153,9 +182,7 @@ def test_get_projects_populated(monkeypatch):
     session.return_value.post = post
     monkeypatch.setattr("requests.Session", session)
 
-    with set_temporary_config(
-        {"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
-    ):
+    with set_temporary_config({"cloud.auth_token": "secret_token"}):
         runner = CliRunner()
         result = runner.invoke(get, ["projects", "--name", "name"])
         assert result.exit_code == 0
@@ -179,7 +206,7 @@ def test_get_projects_populated(monkeypatch):
         assert post.call_args[1]["json"]["query"].split() == query.split()
 
 
-def test_get_flow_runs(monkeypatch):
+def test_get_flow_runs_server(monkeypatch, server_api):
     post = MagicMock(
         return_value=MagicMock(
             json=MagicMock(return_value=dict(data=dict(flow_run=[])))
@@ -189,9 +216,7 @@ def test_get_flow_runs(monkeypatch):
     session.return_value.post = post
     monkeypatch.setattr("requests.Session", session)
 
-    with set_temporary_config(
-        {"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
-    ):
+    with set_temporary_config({"cloud.auth_token": "secret_token"}):
         runner = CliRunner()
         result = runner.invoke(get, ["flow-runs"])
         assert result.exit_code == 0
@@ -223,7 +248,7 @@ def test_get_flow_runs(monkeypatch):
         assert post.call_args[1]["json"]["query"].split() == query.split()
 
 
-def test_get_flow_runs_populated(monkeypatch):
+def test_get_flow_runs_cloud(monkeypatch, cloud_api):
     post = MagicMock(
         return_value=MagicMock(
             json=MagicMock(return_value=dict(data=dict(flow_run=[])))
@@ -233,9 +258,49 @@ def test_get_flow_runs_populated(monkeypatch):
     session.return_value.post = post
     monkeypatch.setattr("requests.Session", session)
 
-    with set_temporary_config(
-        {"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
-    ):
+    with set_temporary_config({"cloud.auth_token": "secret_token"}):
+        runner = CliRunner()
+        result = runner.invoke(get, ["flow-runs"])
+        assert result.exit_code == 0
+        assert (
+            "NAME" in result.output
+            and "FLOW NAME" in result.output
+            and "STATE" in result.output
+            and "AGE" in result.output
+            and "START TIME" in result.output
+            and "DURATION" in result.output
+        )
+
+        query = """
+        query {
+            flow_run(where: { flow: { _and: { name: { _eq: null }, project: { name: { _eq: null } } } } }, limit: 10, order_by: { created: desc }) {
+                flow {
+                    name
+                }
+                created
+                state
+                name
+                duration
+                start_time
+            }
+        }
+        """
+
+        assert post.called
+        assert post.call_args[1]["json"]["query"].split() == query.split()
+
+
+def test_get_flow_runs_populated(monkeypatch, cloud_api):
+    post = MagicMock(
+        return_value=MagicMock(
+            json=MagicMock(return_value=dict(data=dict(flow_run=[])))
+        )
+    )
+    session = MagicMock()
+    session.return_value.post = post
+    monkeypatch.setattr("requests.Session", session)
+
+    with set_temporary_config({"cloud.auth_token": "secret_token"}):
         runner = CliRunner()
         result = runner.invoke(
             get,
@@ -271,7 +336,46 @@ def test_get_flow_runs_populated(monkeypatch):
         assert post.call_args[1]["json"]["query"].split() == query.split()
 
 
-def test_get_tasks(monkeypatch):
+def test_get_tasks_server(monkeypatch, server_api):
+    post = MagicMock(
+        return_value=MagicMock(json=MagicMock(return_value=dict(data=dict(task=[]))))
+    )
+    session = MagicMock()
+    session.return_value.post = post
+    monkeypatch.setattr("requests.Session", session)
+
+    runner = CliRunner()
+    result = runner.invoke(get, ["tasks"])
+    assert result.exit_code == 0
+    assert (
+        "NAME" in result.output
+        and "FLOW NAME" in result.output
+        and "FLOW VERSION" in result.output
+        and "AGE" in result.output
+        and "MAPPED" in result.output
+        and "TYPE" in result.output
+    )
+
+    query = """
+    query {
+        task(where: { _and: { name: { _eq: null }, flow: { name: { _eq: null }, version: { _eq: null } } } }, limit: 10, order_by: { created: desc }) {
+            name
+            created
+            flow {
+                name
+                version
+            }
+            mapped
+            type
+        }
+    }
+    """
+
+    assert post.called
+    assert post.call_args[1]["json"]["query"].split() == query.split()
+
+
+def test_get_tasks_cloud(monkeypatch, cloud_api):
     post = MagicMock(
         return_value=MagicMock(json=MagicMock(return_value=dict(data=dict(task=[]))))
     )
@@ -279,9 +383,7 @@ def test_get_tasks(monkeypatch):
     session.return_value.post = post
     monkeypatch.setattr("requests.Session", session)
 
-    with set_temporary_config(
-        {"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
-    ):
+    with set_temporary_config({"cloud.auth_token": "secret_token"}):
         runner = CliRunner()
         result = runner.invoke(get, ["tasks"])
         assert result.exit_code == 0
@@ -296,7 +398,7 @@ def test_get_tasks(monkeypatch):
 
         query = """
         query {
-            task(where: { _and: { name: { _eq: null }, flow: { name: { _eq: null }, version: { _eq: null } } } }, limit: 10, order_by: { created: desc }) {
+            task(where: { _and: { name: { _eq: null }, flow: { name: { _eq: null }, version: { _eq: null }, project: { name: { _eq: null } } } } }, limit: 10, order_by: { created: desc }) {
                 name
                 created
                 flow {
@@ -313,7 +415,7 @@ def test_get_tasks(monkeypatch):
         assert post.call_args[1]["json"]["query"].split() == query.split()
 
 
-def test_get_tasks_populated(monkeypatch):
+def test_get_tasks_populated(monkeypatch, cloud_api):
     post = MagicMock(
         return_value=MagicMock(json=MagicMock(return_value=dict(data=dict(task=[]))))
     )
@@ -321,9 +423,7 @@ def test_get_tasks_populated(monkeypatch):
     session.return_value.post = post
     monkeypatch.setattr("requests.Session", session)
 
-    with set_temporary_config(
-        {"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
-    ):
+    with set_temporary_config({"cloud.auth_token": "secret_token"}):
         runner = CliRunner()
         result = runner.invoke(
             get,
@@ -362,7 +462,7 @@ def test_get_tasks_populated(monkeypatch):
         assert post.call_args[1]["json"]["query"].split() == query.split()
 
 
-def test_get_logs(monkeypatch):
+def test_get_logs(monkeypatch, cloud_api):
     post = MagicMock(
         return_value=MagicMock(
             json=MagicMock(
@@ -388,9 +488,7 @@ def test_get_logs(monkeypatch):
     session.return_value.post = post
     monkeypatch.setattr("requests.Session", session)
 
-    with set_temporary_config(
-        {"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
-    ):
+    with set_temporary_config({"cloud.auth_token": "secret_token"}):
         runner = CliRunner()
         result = runner.invoke(get, ["logs", "--name", "flow_run"])
         assert result.exit_code == 0
@@ -418,7 +516,7 @@ def test_get_logs(monkeypatch):
         assert post.call_args[1]["json"]["query"].split() == query.split()
 
 
-def test_get_logs_info(monkeypatch):
+def test_get_logs_info(monkeypatch, cloud_api):
     post = MagicMock(
         return_value=MagicMock(
             json=MagicMock(
@@ -430,9 +528,7 @@ def test_get_logs_info(monkeypatch):
     session.return_value.post = post
     monkeypatch.setattr("requests.Session", session)
 
-    with set_temporary_config(
-        {"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
-    ):
+    with set_temporary_config({"cloud.auth_token": "secret_token"}):
         runner = CliRunner()
         result = runner.invoke(get, ["logs", "--name", "flow_run", "--info"])
         assert result.exit_code == 0
@@ -454,7 +550,7 @@ def test_get_logs_info(monkeypatch):
         assert post.call_args[1]["json"]["query"].split() == query.split()
 
 
-def test_get_logs_fails(monkeypatch):
+def test_get_logs_fails(monkeypatch, cloud_api):
     post = MagicMock(
         return_value=MagicMock(
             json=MagicMock(return_value=dict(data=dict(flow_run=[])))
@@ -464,9 +560,7 @@ def test_get_logs_fails(monkeypatch):
     session.return_value.post = post
     monkeypatch.setattr("requests.Session", session)
 
-    with set_temporary_config(
-        {"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
-    ):
+    with set_temporary_config({"cloud.auth_token": "secret_token"}):
         runner = CliRunner()
         result = runner.invoke(get, ["logs", "--name", "flow_run"])
         assert result.exit_code == 0