mik-laj commented on a change in pull request #9170:
URL: https://github.com/apache/airflow/pull/9170#discussion_r441955670



##########
File path: tests/api_connexion/endpoints/test_xcom_endpoint.py
##########
@@ -15,62 +15,254 @@
 # specific language governing permissions and limitations
 # under the License.
 import unittest
+from urllib import parse
 
 import pytest
+from parameterized import parameterized
 
+from airflow.models import DagRun as DR, XCom
+from airflow.utils.dates import parse_execution_date
+from airflow.utils.session import create_session, provide_session
+from airflow.utils.types import DagRunType
 from airflow.www import app
 
 
-class TesXComEndpoint(unittest.TestCase):
+class TestXComEndpoint(unittest.TestCase):
     @classmethod
     def setUpClass(cls) -> None:
         super().setUpClass()
         cls.app = app.create_app(testing=True)  # type:ignore
 
     def setUp(self) -> None:
+        """
+        Setup For XCom endpoint TC
+        """
         self.client = self.app.test_client()  # type:ignore
+        # clear existing xcoms
+        with create_session() as session:
+            session.query(XCom).delete()
+            session.query(DR).delete()
 
+    def tearDown(self) -> None:
+        """
+        Clear Hanging XComs
+        """
+        with create_session() as session:
+            session.query(XCom).delete()
+            session.query(DR).delete()
 
-class TestDeleteXComEntry(TesXComEndpoint):
+
+class TestDeleteXComEntry(TestXComEndpoint):
     @pytest.mark.skip(reason="Not implemented yet")
     def test_should_response_200(self):
         response = self.client.delete(
-            
"/dags/TEST_DAG_ID}/taskInstances/TEST_TASK_ID/2005-04-02T21:37:42Z/xcomEntries/XCOM_KEY"
+            
"/dags/TEST_DAG_ID/taskInstances/TEST_TASK_ID/2005-04-02T21:37:42Z/xcomEntries/XCOM_KEY"
         )
         assert response.status_code == 204
 
 
-class TestGetXComEntry(TesXComEndpoint):
-    @pytest.mark.skip(reason="Not implemented yet")
-    def test_should_response_200(self):
+class TestGetXComEntry(TestXComEndpoint):
+
+    @provide_session
+    def test_should_response_200(self, session):
+        dag_id = 'test-dag-id'
+        task_id = 'test-task-id'
+        execution_date = '2005-04-02T21:37:42+00:00'
+        xcom_key = 'test-xcom-key'
+        execution_date_parsed = parse_execution_date(execution_date)
+        xcom_model = XCom(
+            key=xcom_key,
+            execution_date=execution_date_parsed,
+            task_id=task_id,
+            dag_id=dag_id,
+            timestamp=execution_date_parsed,
+        )
+        dag_run_id = DR.generate_run_id(DagRunType.MANUAL, 
execution_date_parsed)
+        dagrun = DR(
+            dag_id=dag_id,
+            run_id=dag_run_id,
+            execution_date=execution_date_parsed,
+            start_date=execution_date_parsed,
+            run_type=DagRunType.MANUAL.value,
+        )
+        session.add(xcom_model)
+        session.add(dagrun)
+        session.commit()
         response = self.client.get(
-            
"/dags/TEST_DAG_ID}/taskInstances/TEST_TASK_ID/2005-04-02T21:37:42Z/xcomEntries/XCOM_KEY"
+            
f"/api/v1/dags/{dag_id}/dagRuns/{dag_run_id}/taskInstances/{task_id}/xcomEntries/{xcom_key}"
+        )
+        self.assertEqual(200, response.status_code)
+        self.assertEqual(
+            response.json,
+            {
+                'dag_id': dag_id,
+                'execution_date': execution_date,
+                'key': xcom_key,
+                'task_id': task_id,
+                'timestamp': execution_date
+            }
         )
-        assert response.status_code == 200
 
 
-class TestGetXComEntries(TesXComEndpoint):
-    @pytest.mark.skip(reason="Not implemented yet")
-    def test_should_response_200(self):
+class TestGetXComEntries(TestXComEndpoint):
+    @provide_session
+    def test_should_response_200(self, session):
+        dag_id = 'test-dag-id'
+        task_id = 'test-task-id'
+        execution_date = '2005-04-02T21:37:42+00:00'
+        execution_date_parsed = parse_execution_date(execution_date)
+        xcom_model_1 = XCom(
+            key='test-xcom-key-1',
+            execution_date=execution_date_parsed,
+            task_id=task_id,
+            dag_id=dag_id,
+            timestamp=execution_date_parsed,
+        )
+        xcom_model_2 = XCom(
+            key='test-xcom-key-2',
+            execution_date=execution_date_parsed,
+            task_id=task_id,
+            dag_id=dag_id,
+            timestamp=execution_date_parsed,
+        )
+        dag_run_id = DR.generate_run_id(DagRunType.MANUAL, 
execution_date_parsed)
+        dagrun = DR(
+            dag_id=dag_id,
+            run_id=dag_run_id,
+            execution_date=execution_date_parsed,
+            start_date=execution_date_parsed,
+            run_type=DagRunType.MANUAL.value,
+        )
+        xcom_models = [xcom_model_1, xcom_model_2]
+        session.add_all(xcom_models)
+        session.add(dagrun)
+        session.commit()
         response = self.client.get(
-            
"/dags/TEST_DAG_ID}/taskInstances/TEST_TASK_ID/2005-04-02T21:37:42Z/xcomEntries/"
+            
f"/api/v1/dags/{dag_id}/dagRuns/{dag_run_id}/taskInstances/{task_id}/xcomEntries"
+        )
+        self.assertEqual(200, response.status_code)
+        self.assertEqual(
+            response.json,
+            {
+                'xcom_entries': [
+                    {
+                        'dag_id': dag_id,
+                        'execution_date': execution_date,
+                        'key': 'test-xcom-key-1',
+                        'task_id': task_id,
+                        'timestamp': execution_date
+                    },
+                    {
+                        'dag_id': dag_id,
+                        'execution_date': execution_date,
+                        'key': 'test-xcom-key-2',
+                        'task_id': task_id,
+                        'timestamp': execution_date
+                    }
+                ],
+                'total_entries': 2,
+            }
+        )
+
+
+class TestPaginationGetXComEntries(TestXComEndpoint):
+
+    def setUp(self):
+        super().setUp()
+        self.dag_id = 'test-dag-id'
+        self.task_id = 'test-task-id'
+        self.execution_date = '2005-04-02T21:37:42+00:00'
+        self.execution_date_parsed = parse_execution_date(self.execution_date)
+        self.dag_run_id = DR.generate_run_id(DagRunType.MANUAL, 
self.execution_date_parsed)
+        self.expected_xcom_ids = sorted([
+            'TEST_XCOM_KEY1',
+            'TEST_XCOM_KEY2',
+            'TEST_XCOM_KEY3',
+            'TEST_XCOM_KEY4',
+            'TEST_XCOM_KEY5',
+            'TEST_XCOM_KEY6',
+            'TEST_XCOM_KEY7',
+            'TEST_XCOM_KEY8',
+            'TEST_XCOM_KEY9',
+            'TEST_XCOM_KEY10',
+        ])
+
+    @parameterized.expand(
+        [
+            (
+                "limit=1"
+            ),
+            (
+                "limit=2"
+            ),
+            (
+                "offset=5"
+            ),
+            (
+                "offset=0"
+            ),
+            (
+                "limit=1&offset=5"
+            ),
+            (
+                "limit=1&offset=1"
+            ),
+            (
+                "limit=2&offset=2"
+            ),
+        ]
+    )
+    @provide_session
+    def test_handle_limit_offset(self, query_params, session):
+        url = 
"/api/v1/dags/{dag_id}/dagRuns/{dag_run_id}/taskInstances/{task_id}/xcomEntries?{query_params}"
+        url = url.format(dag_id=self.dag_id,
+                         dag_run_id=self.dag_run_id,
+                         task_id=self.task_id,
+                         query_params=query_params)
+        parsed = dict(parse.parse_qsl(parse.urlsplit(url).query))
+        limit = int(parsed.get('limit', 100))
+        offset = int(parsed.get('offset', 0))
+        expected_xcom_ids = self.expected_xcom_ids[0 + offset: offset + limit]

Review comment:
       This should be a constant value. Whenever possible, the tests should 
only create variables, call other functions, do assertions. They should not 
calculate anything because you could have made a mistake in two places. 
   
   
https://testing.googleblog.com/2019/12/testing-on-toilet-tests-too-dry-make.html




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

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


Reply via email to