-
Notifications
You must be signed in to change notification settings - Fork 16.6k
[AIRFLOW-7038] Cassandra sensors tests #7689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # | ||
| # 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 unittest | ||
|
|
||
| import pytest | ||
|
|
||
| from airflow import DAG | ||
| from airflow.providers.apache.cassandra.hooks.cassandra import CassandraHook | ||
| from airflow.providers.apache.cassandra.sensors.record import CassandraRecordSensor | ||
| from airflow.utils import timezone | ||
|
|
||
| DEFAULT_DATE = timezone.datetime(2017, 1, 1) | ||
|
|
||
|
|
||
| @pytest.mark.integration("cassandra") | ||
| class TestCassandraRecordSensor(unittest.TestCase): | ||
| def setUp(self) -> None: | ||
| args = { | ||
| 'owner': 'airflow', | ||
| 'start_date': DEFAULT_DATE | ||
| } | ||
|
|
||
| self.dag = DAG('test_dag_id', default_args=args) | ||
|
|
||
| def test_poke(self): | ||
| hook = CassandraHook(cassandra_conn_id="cassandra_default") | ||
| session = hook.get_conn() | ||
| cqls = [ | ||
| "DROP TABLE IF EXISTS s.t", | ||
| "CREATE TABLE s.t (pk1 text, pk2 text, c text, PRIMARY KEY (pk1, pk2))", | ||
| "INSERT INTO s.t (pk1, pk2, c) VALUES ('foo', 'bar', 'baz')", | ||
| ] | ||
| for cql in cqls: | ||
| session.execute(cql) | ||
|
|
||
| true_sensor = CassandraRecordSensor( | ||
| dag=self.dag, | ||
| task_id="test_task", | ||
| cassandra_conn_id="cassandra_default", | ||
| table="s.t", | ||
| keys={"pk1": "foo", "pk2": "bar"} | ||
| ) | ||
| self.assertTrue(true_sensor.poke({})) | ||
|
|
||
| false_sensor = CassandraRecordSensor( | ||
| dag=self.dag, | ||
| task_id="test_task", | ||
| cassandra_conn_id="cassandra_default", | ||
| table="s.u", | ||
| keys={"pk1": "foo", "pk2": "baz"} | ||
| ) | ||
| self.assertFalse(false_sensor.poke({})) | ||
|
|
||
| session.shutdown() | ||
| hook.shutdown_cluster() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # | ||
| # 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 unittest | ||
|
|
||
| import pytest | ||
|
|
||
| from airflow import DAG | ||
| from airflow.providers.apache.cassandra.hooks.cassandra import CassandraHook | ||
| from airflow.providers.apache.cassandra.sensors.table import CassandraTableSensor | ||
| from airflow.utils import timezone | ||
|
|
||
| DEFAULT_DATE = timezone.datetime(2017, 1, 1) | ||
|
|
||
|
|
||
| @pytest.mark.integration("cassandra") | ||
| class TestCassandraHook(unittest.TestCase): | ||
| def setUp(self) -> None: | ||
| args = { | ||
| 'owner': 'airflow', | ||
| 'start_date': DEFAULT_DATE | ||
| } | ||
|
|
||
| self.dag = DAG('test_dag_id', default_args=args) | ||
|
|
||
| def test_poke(self): | ||
| hook = CassandraHook("cassandra_default_with_schema") | ||
| session = hook.get_conn() | ||
| cqls = [ | ||
| "DROP TABLE IF EXISTS s.t", | ||
| "CREATE TABLE s.t (pk1 text PRIMARY KEY)", | ||
| ] | ||
| for cql in cqls: | ||
| session.execute(cql) | ||
|
|
||
| true_sensor = CassandraTableSensor( | ||
| dag=self.dag, | ||
| task_id="test_task", | ||
| cassandra_conn_id="cassandra_default", | ||
| table="s.t" | ||
| ) | ||
| self.assertTrue(true_sensor.poke({})) | ||
|
|
||
| true_sensor = CassandraTableSensor( | ||
| dag=self.dag, | ||
| task_id="test_task", | ||
| cassandra_conn_id="cassandra_default", | ||
| table="s.u" | ||
| ) | ||
| self.assertFalse(true_sensor.poke({})) | ||
|
|
||
| session.shutdown() | ||
| hook.shutdown_cluster() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a pure unit test and it has side effects (the table is not dropped in tear down). I would suggest to mock connection with database (this will make the test faster) and add system tests (and example DAG). For example here's system test for PostgresToGCS:
https://github.com/apache/airflow/blob/master/tests/providers/google/cloud/operators/test_postgres_to_gcs_system.py
System tests are not run regularly but this hopefully will change soon as @potiuk is working on that :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as the sensor uses the hook methods, what's your idea to mock the hook methods? For example, mocking
record_existsin here:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would mock
CassandraHookand:record_existsof initializedhookwas called with expected parametersCassandraHookAt least that is the approach we use for GCP operators / hooks. Once you cover the hook with tests you can mock the hook in operator tests. In my opinion, it's a nice way to separate and highlight what exactly is tested.
Of course, mocking only asserts that arguments are passed as expected and some methods are called. To check integrity we should use system test (that runs example DAG). It's something still discussed but is easy to do with
SystemTest:https://github.com/apache/airflow/blob/master/TESTING.rst#id24