You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by po...@apache.org on 2024/04/15 17:13:11 UTC
(airflow) branch main updated: Add four unit tests for aws/utils (#38820)
This is an automated email from the ASF dual-hosted git repository.
potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new 10d5654c9b Add four unit tests for aws/utils (#38820)
10d5654c9b is described below
commit 10d5654c9b0ce46a74de60abaab2b067a2a77482
Author: Steven Blake <91...@users.noreply.github.com>
AuthorDate: Mon Apr 15 10:13:01 2024 -0700
Add four unit tests for aws/utils (#38820)
---
tests/always/test_project_structure.py | 2 -
tests/providers/amazon/aws/utils/test_sqs.py | 137 ++++++++++++++++++++++++++
tests/providers/amazon/aws/utils/test_tags.py | 50 ++++++++++
3 files changed, 187 insertions(+), 2 deletions(-)
diff --git a/tests/always/test_project_structure.py b/tests/always/test_project_structure.py
index 1b965ec998..cd8e68594c 100644
--- a/tests/always/test_project_structure.py
+++ b/tests/always/test_project_structure.py
@@ -82,8 +82,6 @@ class TestProjectStructure:
"tests/providers/amazon/aws/triggers/test_step_function.py",
"tests/providers/amazon/aws/utils/test_rds.py",
"tests/providers/amazon/aws/utils/test_sagemaker.py",
- "tests/providers/amazon/aws/utils/test_sqs.py",
- "tests/providers/amazon/aws/utils/test_tags.py",
"tests/providers/amazon/aws/waiters/test_base_waiter.py",
"tests/providers/apache/cassandra/hooks/test_cassandra.py",
"tests/providers/apache/drill/operators/test_drill.py",
diff --git a/tests/providers/amazon/aws/utils/test_sqs.py b/tests/providers/amazon/aws/utils/test_sqs.py
new file mode 100644
index 0000000000..74f9c309fb
--- /dev/null
+++ b/tests/providers/amazon/aws/utils/test_sqs.py
@@ -0,0 +1,137 @@
+# 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
+
+import jsonpath_ng
+import pytest
+
+from airflow.providers.amazon.aws.utils.sqs import (
+ filter_messages,
+ filter_messages_jsonpath,
+ filter_messages_literal,
+ process_response,
+)
+
+
+@pytest.fixture
+def response_json():
+ return {"Messages": [{"Body": "message1"}, {"Body": "message2"}]}
+
+
+@pytest.fixture
+def response_nested_dict():
+ return [{"Body": '{"key": "value1"}'}, {"Body": '{"key": "value2"}'}]
+
+
+@pytest.fixture
+def response_dict():
+ return [{"Body": "message1"}, {"Body": "message2"}, {"Body": "message3"}]
+
+
+def test_process_response_with_empty_response():
+ response_json = {}
+ processed_response = process_response(response_json)
+ assert processed_response == []
+
+
+def test_process_response_with_no_messages(response_json):
+ response_json["Messages"] = []
+ processed_response = process_response(response_json)
+ assert processed_response == []
+
+
+def test_process_response_with_messages_and_no_filtering(response_json):
+ processed_response = process_response(response_json)
+ assert processed_response == response_json["Messages"]
+
+
+def test_process_response_with_messages_and_literal_filtering(response_json):
+ processed_response = process_response(
+ response_json, message_filtering="literal", message_filtering_match_values=["message1"]
+ )
+ assert processed_response == [{"Body": "message1"}]
+
+
+def test_filter_messages_literal():
+ messages = [{"Body": "message1"}, {"Body": "message2"}]
+ filtered_messages = filter_messages(
+ messages,
+ message_filtering="literal",
+ message_filtering_match_values=["message1"],
+ message_filtering_config="",
+ )
+ assert filtered_messages == [{"Body": "message1"}]
+
+
+def test_filter_messages_jsonpath(response_nested_dict):
+ filtered_messages = filter_messages(
+ response_nested_dict,
+ message_filtering="jsonpath",
+ message_filtering_match_values=["value1"],
+ message_filtering_config="key",
+ )
+ assert filtered_messages == [{"Body": '{"key": "value1"}'}]
+
+
+def test_filter_messages_literal_with_matching_values(response_dict):
+ filtered_messages = filter_messages_literal(
+ response_dict, message_filtering_match_values=["message1", "message3"]
+ )
+ assert filtered_messages == [{"Body": "message1"}, {"Body": "message3"}]
+
+
+def test_filter_messages_literal_with_no_matching_values(response_dict):
+ filtered_messages = filter_messages_literal(response_dict, message_filtering_match_values=["message4"])
+ assert filtered_messages == []
+
+
+def test_filter_messages_literal_with_empty_messages():
+ messages = []
+ filtered_messages = filter_messages_literal(messages, message_filtering_match_values=["message1"])
+ assert filtered_messages == []
+
+
+def test_filter_messages_jsonpath_with_matching_values(response_nested_dict):
+ filtered_messages = filter_messages_jsonpath(
+ response_nested_dict,
+ message_filtering_match_values=["value1"],
+ message_filtering_config="key",
+ parse=jsonpath_ng.parse,
+ )
+ assert filtered_messages == [{"Body": '{"key": "value1"}'}]
+
+
+def test_filter_messages_jsonpath_with_no_matching_values(response_nested_dict):
+ filtered_messages = filter_messages_jsonpath(
+ response_nested_dict,
+ message_filtering_match_values=["value3"],
+ message_filtering_config="key",
+ parse=jsonpath_ng.parse,
+ )
+ assert filtered_messages == []
+
+
+def test_filter_messages_jsonpath_with_empty_messages():
+ messages = []
+ filtered_messages = filter_messages_jsonpath(
+ messages,
+ message_filtering_match_values=["value1"],
+ message_filtering_config="key",
+ parse=jsonpath_ng.parse,
+ )
+ assert filtered_messages == []
diff --git a/tests/providers/amazon/aws/utils/test_tags.py b/tests/providers/amazon/aws/utils/test_tags.py
new file mode 100644
index 0000000000..9f6ee15c59
--- /dev/null
+++ b/tests/providers/amazon/aws/utils/test_tags.py
@@ -0,0 +1,50 @@
+# 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
+
+import pytest
+
+from airflow.providers.amazon.aws.utils.tags import format_tags
+
+
+@pytest.fixture
+def source():
+ return {"key1": "value1", "key2": "value2"}
+
+
+def test_format_tags_with_dict(source):
+ formatted_tags = format_tags(source)
+ expected_result = [{"Key": "key1", "Value": "value1"}, {"Key": "key2", "Value": "value2"}]
+ assert formatted_tags == expected_result
+
+
+def test_format_tags_with_other_input():
+ source = [{"Key": "key1", "Value": "value1"}, {"Key": "key2", "Value": "value2"}]
+ formatted_tags = format_tags(source)
+ assert formatted_tags == source
+
+
+def test_format_tags_with_custom_labels(source):
+ formatted_tags = format_tags(source, key_label="TagKey", value_label="TagValue")
+ expected_result = [{"TagKey": "key1", "TagValue": "value1"}, {"TagKey": "key2", "TagValue": "value2"}]
+ assert formatted_tags == expected_result
+
+
+def test_format_tags_with_none_input():
+ formatted_tags = format_tags(None)
+ assert formatted_tags == []