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

2024-04-25 Thread via GitHub


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-unsubscr...@airflow.apache.org

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



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

2024-04-25 Thread via GitHub


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-unsubscr...@airflow.apache.org

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



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

2024-04-25 Thread via GitHub


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-unsubscr...@airflow.apache.org

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



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

2024-04-25 Thread via GitHub


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-unsubscr...@airflow.apache.org

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



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

2024-04-25 Thread via GitHub


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):
+

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

2024-04-24 Thread via GitHub


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-unsubscr...@airflow.apache.org

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