blag commented on code in PR #26358:
URL: https://github.com/apache/airflow/pull/26358#discussion_r972476169


##########
tests/www/views/test_views_dataset.py:
##########
@@ -0,0 +1,281 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import string
+
+import pendulum
+import pytest
+from dateutil.tz import UTC
+
+from airflow import Dataset
+from airflow.models.dataset import DatasetEvent, DatasetModel
+from airflow.operators.empty import EmptyOperator
+from airflow.utils import timezone
+from tests.test_utils.asserts import assert_queries_count
+from tests.test_utils.db import clear_db_datasets
+
+
+class TestDatasetEndpoint:
+
+    default_time = "2020-06-11T18:00:00+00:00"
+
+    @pytest.fixture(autouse=True)
+    def cleanup(self):
+        clear_db_datasets()
+        yield
+        # clear_db_datasets()
+
+
+class TestGetDatasets(TestDatasetEndpoint):
+    def test_should_respond_200(self, admin_client, session):
+        datasets = [
+            DatasetModel(
+                id=i,
+                uri=f"s3://bucket/key/{i}",
+                extra={"foo": "bar"},
+                created_at=timezone.parse(self.default_time),
+                updated_at=timezone.parse(self.default_time),
+            )
+            for i in [1, 2]
+        ]
+        session.add_all(datasets)
+        session.commit()
+        assert session.query(DatasetModel).count() == 2
+
+        with assert_queries_count(8):
+            response = admin_client.get("/object/list_datasets")
+
+        assert response.status_code == 200
+        response_data = response.json
+        assert response_data == {
+            "datasets": [
+                {
+                    "id": 1,
+                    "uri": "s3://bucket/key/1",
+                    "last_dataset_update": None,
+                    "total_updates": 0,
+                    "producing_task_count": 0,
+                    "consuming_dag_count": 0,
+                },
+                {
+                    "id": 2,
+                    "uri": "s3://bucket/key/2",
+                    "last_dataset_update": None,
+                    "total_updates": 0,
+                    "producing_task_count": 0,
+                    "consuming_dag_count": 0,
+                },
+            ],
+            "total_entries": 2,
+        }
+
+    def test_order_by_raises_400_for_invalid_attr(self, admin_client, session):
+        datasets = [
+            DatasetModel(
+                uri=f"s3://bucket/key/{i}",
+                extra={"foo": "bar"},
+                created_at=timezone.parse(self.default_time),
+                updated_at=timezone.parse(self.default_time),
+            )
+            for i in [1, 2]
+        ]
+        session.add_all(datasets)
+        session.commit()
+        assert session.query(DatasetModel).count() == 2
+
+        response = admin_client.get("/object/list_datasets?order_by=fake")
+
+        assert response.status_code == 400
+        msg = "Ordering with 'fake' is disallowed or the attribute does not 
exist on the model"
+        assert response.json['detail'] == msg
+
+    @pytest.mark.parametrize(
+        "order_by, ordered_dataset_ids",
+        [
+            ("uri", [1, 2, 3, 4]),
+            ("-uri", [4, 3, 2, 1]),
+            ("last_dataset_update", [3, 2, 4, 1]),
+            ("-last_dataset_update", [1, 4, 2, 3]),
+        ],
+    )
+    def test_order_by(self, admin_client, session, order_by, 
ordered_dataset_ids):
+        datasets = [
+            DatasetModel(
+                id=i,
+                uri=string.ascii_lowercase[i],
+                extra={"foo": "bar"},
+                created_at=timezone.parse(self.default_time),
+                updated_at=timezone.parse(self.default_time),
+            )
+            for i in range(1, len(ordered_dataset_ids) + 1)
+        ]
+        session.add_all(datasets)
+        dataset_events = [
+            DatasetEvent(
+                dataset_id=datasets[2].id,
+                timestamp=pendulum.today('UTC').add(days=-3),
+            ),
+            DatasetEvent(
+                dataset_id=datasets[1].id,
+                timestamp=pendulum.today('UTC').add(days=-2),
+            ),
+            DatasetEvent(
+                dataset_id=datasets[1].id,
+                timestamp=pendulum.today('UTC').add(days=-1),
+            ),
+        ]
+        session.add_all(dataset_events)

Review Comment:
   These three dataset events point to two different datasets (`datasets[2]` 
and `datasets[1]`), so a simple loop is not equivalent.
   
   And I figured a loop for the two dataset events for `datasets[1]` was 
probably overkill.



-- 
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