You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "mateuslatrova (via GitHub)" <gi...@apache.org> on 2024/04/18 00:58:30 UTC

[PR] Add tests for EmrServerlessJobSensor and EmrServerlessApplicationSensor [airflow]

mateuslatrova opened a new pull request, #39099:
URL: https://github.com/apache/airflow/pull/39099

   <!--
    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.
    -->
   
   <!--
   Thank you for contributing! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   closes: #ISSUE
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   
   
   
   <!-- Please keep an empty line above the dashes. -->
   Related: #35442 
   
   This PR adds unit tests for both the **EmrServerlessJobSensor** and the **EmrServerlessApplicationSensor** from the Airflow AWS provider package.
   
   


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add tests for EmrServerlessJobSensor and EmrServerlessApplicationSensor [airflow]

Posted by "mateuslatrova (via GitHub)" <gi...@apache.org>.
mateuslatrova commented on PR #39099:
URL: https://github.com/apache/airflow/pull/39099#issuecomment-2063788519

   > The serverless tests are failing. Can you check and fix them?
   
   Sure! I will soon fix them and also add the tests for EmrServerlessApplicationSensor.


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add tests for EmrServerlessJobSensor and EmrServerlessApplicationSensor [airflow]

Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on PR #39099:
URL: https://github.com/apache/airflow/pull/39099#issuecomment-2079448091

   Love it! Thanks!


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add tests for EmrServerlessJobSensor [airflow]

Posted by "mateuslatrova (via GitHub)" <gi...@apache.org>.
mateuslatrova commented on code in PR #39099:
URL: https://github.com/apache/airflow/pull/39099#discussion_r1579734692


##########
tests/providers/amazon/aws/sensors/test_emr_serverless.py:
##########
@@ -0,0 +1,119 @@
+#
+# 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.
+from __future__ import annotations
+
+from unittest.mock import MagicMock
+
+import pytest
+
+from airflow.exceptions import AirflowException, AirflowSkipException
+from airflow.providers.amazon.aws.sensors.emr import EmrServerlessJobSensor
+
+
+class TestEmrServerlessJobSensor:
+    def setup_method(self):
+        self.app_id = "vzwemreks"
+        self.job_run_id = "job1234"
+        self.sensor = EmrServerlessJobSensor(
+            task_id="test_emrcontainer_sensor",
+            application_id=self.app_id,
+            job_run_id=self.job_run_id,
+            aws_conn_id="aws_default",
+        )
+
+    def set_get_job_run_return_value(self, return_value: dict[str, str]):
+        self.mock_hook = MagicMock()
+        self.mock_hook.conn.get_job_run.return_value = return_value
+        self.sensor.hook = self.mock_hook
+
+    def assert_get_job_run_was_called_once_with_app_and_run_id(self):
+        self.mock_hook.conn.get_job_run.assert_called_once_with(
+            applicationId=self.app_id, jobRunId=self.job_run_id
+        )
+
+
+class TestPokeReturnsFalse(TestEmrServerlessJobSensor):
+    def run_test(self, get_job_run_return_value: dict[str, str]):
+        self.set_get_job_run_return_value(get_job_run_return_value)
+        assert not self.sensor.poke(None)
+        self.assert_get_job_run_was_called_once_with_app_and_run_id()
+
+    def test_when_state_is_pending_poke_should_return_false(self):
+        self.run_test(get_job_run_return_value={"jobRun": {"state": "PENDING"}})
+
+    def test_when_state_is_running_poke_should_return_false(self):
+        self.run_test(get_job_run_return_value={"jobRun": {"state": "RUNNING"}})
+
+    def test_when_state_is_scheduled_poke_should_return_false(self):
+        self.run_test(get_job_run_return_value={"jobRun": {"state": "SCHEDULED"}})
+
+    def test_when_state_is_submitted_poke_should_return_false(self):
+        self.run_test(get_job_run_return_value={"jobRun": {"state": "SUBMITTED"}})

Review Comment:
   Oh, nice. I am more familiar with unittest features and never used `parametrize` before. But I'll take a look at it and do these changes until tomorrow. Thanks.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add tests for EmrServerlessJobSensor and EmrServerlessApplicationSensor [airflow]

Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck merged PR #39099:
URL: https://github.com/apache/airflow/pull/39099


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add tests for EmrServerlessJobSensor [airflow]

Posted by "mateuslatrova (via GitHub)" <gi...@apache.org>.
mateuslatrova commented on PR #39099:
URL: https://github.com/apache/airflow/pull/39099#issuecomment-2076129433

   Hi @dirrao !
   
   Just fixed the tests for EmrServelessJobSensor, whenever you can take a look.
   
   I decided to do a separate PR with the tests for the EmrServerlessApplicationSensor.
   
   Thanks in advance!


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add tests for EmrServerlessJobSensor [airflow]

Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on code in PR #39099:
URL: https://github.com/apache/airflow/pull/39099#discussion_r1579579796


##########
tests/providers/amazon/aws/sensors/test_emr_serverless.py:
##########
@@ -0,0 +1,119 @@
+#
+# 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.
+from __future__ import annotations
+
+from unittest.mock import MagicMock
+
+import pytest
+
+from airflow.exceptions import AirflowException, AirflowSkipException
+from airflow.providers.amazon.aws.sensors.emr import EmrServerlessJobSensor
+
+
+class TestEmrServerlessJobSensor:
+    def setup_method(self):
+        self.app_id = "vzwemreks"
+        self.job_run_id = "job1234"
+        self.sensor = EmrServerlessJobSensor(
+            task_id="test_emrcontainer_sensor",
+            application_id=self.app_id,
+            job_run_id=self.job_run_id,
+            aws_conn_id="aws_default",
+        )
+
+    def set_get_job_run_return_value(self, return_value: dict[str, str]):
+        self.mock_hook = MagicMock()
+        self.mock_hook.conn.get_job_run.return_value = return_value
+        self.sensor.hook = self.mock_hook
+
+    def assert_get_job_run_was_called_once_with_app_and_run_id(self):
+        self.mock_hook.conn.get_job_run.assert_called_once_with(
+            applicationId=self.app_id, jobRunId=self.job_run_id
+        )
+
+
+class TestPokeReturnsFalse(TestEmrServerlessJobSensor):
+    def run_test(self, get_job_run_return_value: dict[str, str]):
+        self.set_get_job_run_return_value(get_job_run_return_value)
+        assert not self.sensor.poke(None)
+        self.assert_get_job_run_was_called_once_with_app_and_run_id()
+
+    def test_when_state_is_pending_poke_should_return_false(self):
+        self.run_test(get_job_run_return_value={"jobRun": {"state": "PENDING"}})
+
+    def test_when_state_is_running_poke_should_return_false(self):
+        self.run_test(get_job_run_return_value={"jobRun": {"state": "RUNNING"}})
+
+    def test_when_state_is_scheduled_poke_should_return_false(self):
+        self.run_test(get_job_run_return_value={"jobRun": {"state": "SCHEDULED"}})
+
+    def test_when_state_is_submitted_poke_should_return_false(self):
+        self.run_test(get_job_run_return_value={"jobRun": {"state": "SUBMITTED"}})

Review Comment:
   These tests could be one test using [parametrize](https://docs.pytest.org/en/7.1.x/how-to/parametrize.html)



##########
tests/providers/amazon/aws/sensors/test_emr_serverless.py:
##########
@@ -0,0 +1,119 @@
+#
+# 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.
+from __future__ import annotations
+
+from unittest.mock import MagicMock
+
+import pytest
+
+from airflow.exceptions import AirflowException, AirflowSkipException
+from airflow.providers.amazon.aws.sensors.emr import EmrServerlessJobSensor
+
+
+class TestEmrServerlessJobSensor:
+    def setup_method(self):
+        self.app_id = "vzwemreks"
+        self.job_run_id = "job1234"
+        self.sensor = EmrServerlessJobSensor(
+            task_id="test_emrcontainer_sensor",
+            application_id=self.app_id,
+            job_run_id=self.job_run_id,
+            aws_conn_id="aws_default",
+        )
+
+    def set_get_job_run_return_value(self, return_value: dict[str, str]):
+        self.mock_hook = MagicMock()
+        self.mock_hook.conn.get_job_run.return_value = return_value
+        self.sensor.hook = self.mock_hook
+
+    def assert_get_job_run_was_called_once_with_app_and_run_id(self):
+        self.mock_hook.conn.get_job_run.assert_called_once_with(
+            applicationId=self.app_id, jobRunId=self.job_run_id
+        )
+
+
+class TestPokeReturnsFalse(TestEmrServerlessJobSensor):
+    def run_test(self, get_job_run_return_value: dict[str, str]):
+        self.set_get_job_run_return_value(get_job_run_return_value)
+        assert not self.sensor.poke(None)
+        self.assert_get_job_run_was_called_once_with_app_and_run_id()
+
+    def test_when_state_is_pending_poke_should_return_false(self):
+        self.run_test(get_job_run_return_value={"jobRun": {"state": "PENDING"}})
+
+    def test_when_state_is_running_poke_should_return_false(self):
+        self.run_test(get_job_run_return_value={"jobRun": {"state": "RUNNING"}})
+
+    def test_when_state_is_scheduled_poke_should_return_false(self):
+        self.run_test(get_job_run_return_value={"jobRun": {"state": "SCHEDULED"}})
+
+    def test_when_state_is_submitted_poke_should_return_false(self):
+        self.run_test(get_job_run_return_value={"jobRun": {"state": "SUBMITTED"}})
+
+
+class TestPokeReturnsTrue(TestEmrServerlessJobSensor):
+    def test_when_state_is_success_poke_should_return_true(self):
+        self.set_get_job_run_return_value({"jobRun": {"state": "SUCCESS"}})
+        assert self.sensor.poke(None)
+        self.assert_get_job_run_was_called_once_with_app_and_run_id()

Review Comment:
   You could include this test as part of the test above using [parametrize](https://docs.pytest.org/en/7.1.x/how-to/parametrize.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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add tests for EmrServerlessJobSensor [airflow]

Posted by "mateuslatrova (via GitHub)" <gi...@apache.org>.
mateuslatrova commented on code in PR #39099:
URL: https://github.com/apache/airflow/pull/39099#discussion_r1579735908


##########
tests/providers/amazon/aws/sensors/test_emr_serverless.py:
##########
@@ -0,0 +1,119 @@
+#
+# 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.
+from __future__ import annotations
+
+from unittest.mock import MagicMock
+
+import pytest
+
+from airflow.exceptions import AirflowException, AirflowSkipException
+from airflow.providers.amazon.aws.sensors.emr import EmrServerlessJobSensor
+
+
+class TestEmrServerlessJobSensor:
+    def setup_method(self):
+        self.app_id = "vzwemreks"
+        self.job_run_id = "job1234"
+        self.sensor = EmrServerlessJobSensor(
+            task_id="test_emrcontainer_sensor",
+            application_id=self.app_id,
+            job_run_id=self.job_run_id,
+            aws_conn_id="aws_default",
+        )
+
+    def set_get_job_run_return_value(self, return_value: dict[str, str]):
+        self.mock_hook = MagicMock()
+        self.mock_hook.conn.get_job_run.return_value = return_value
+        self.sensor.hook = self.mock_hook
+
+    def assert_get_job_run_was_called_once_with_app_and_run_id(self):
+        self.mock_hook.conn.get_job_run.assert_called_once_with(
+            applicationId=self.app_id, jobRunId=self.job_run_id
+        )
+
+
+class TestPokeReturnsFalse(TestEmrServerlessJobSensor):
+    def run_test(self, get_job_run_return_value: dict[str, str]):
+        self.set_get_job_run_return_value(get_job_run_return_value)
+        assert not self.sensor.poke(None)
+        self.assert_get_job_run_was_called_once_with_app_and_run_id()
+
+    def test_when_state_is_pending_poke_should_return_false(self):
+        self.run_test(get_job_run_return_value={"jobRun": {"state": "PENDING"}})
+
+    def test_when_state_is_running_poke_should_return_false(self):
+        self.run_test(get_job_run_return_value={"jobRun": {"state": "RUNNING"}})
+
+    def test_when_state_is_scheduled_poke_should_return_false(self):
+        self.run_test(get_job_run_return_value={"jobRun": {"state": "SCHEDULED"}})
+
+    def test_when_state_is_submitted_poke_should_return_false(self):
+        self.run_test(get_job_run_return_value={"jobRun": {"state": "SUBMITTED"}})
+
+
+class TestPokeReturnsTrue(TestEmrServerlessJobSensor):
+    def test_when_state_is_success_poke_should_return_true(self):
+        self.set_get_job_run_return_value({"jobRun": {"state": "SUCCESS"}})
+        assert self.sensor.poke(None)
+        self.assert_get_job_run_was_called_once_with_app_and_run_id()

Review Comment:
   Same answer as above.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add tests for EmrServerlessJobSensor and EmrServerlessApplicationSensor [airflow]

Posted by "mateuslatrova (via GitHub)" <gi...@apache.org>.
mateuslatrova commented on PR #39099:
URL: https://github.com/apache/airflow/pull/39099#issuecomment-2062799411

   Obs.: still working on the tests for **EmrServerlessApplicationSensor**


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add tests for EmrServerlessJobSensor [airflow]

Posted by "mateuslatrova (via GitHub)" <gi...@apache.org>.
mateuslatrova commented on PR #39099:
URL: https://github.com/apache/airflow/pull/39099#issuecomment-2078350187

   Hi @vincbeck ! Thanks for your suggestion, the code became much more cleaner. I have just uploaded the changes, whenever you can review it again. Thanks!


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add tests for EmrServerlessJobSensor [airflow]

Posted by "mateuslatrova (via GitHub)" <gi...@apache.org>.
mateuslatrova commented on PR #39099:
URL: https://github.com/apache/airflow/pull/39099#issuecomment-2078394365

   Since this MR is still not merged, and both sensors (EmrServerlessJobSensor and EmrServerlessApplicationSensor) are very related, I have just added here a commit with the tests for EmrServerlessApplicationSensor.


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org