[AIRFLOW-7038] Cassandra sensors tests#7689
Conversation
f55472b to
40d62df
Compare
Codecov Report
@@ Coverage Diff @@
## master #7689 +/- ##
==========================================
- Coverage 86.98% 86.82% -0.17%
==========================================
Files 904 904
Lines 43736 43736
==========================================
- Hits 38045 37972 -73
- Misses 5691 5764 +73
Continue to review full report at Codecov.
|
| 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) |
There was a problem hiding this comment.
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.
As far as the sensor uses the hook methods, what's your idea to mock the hook methods? For example, mocking record_exists in here:
def poke(self, context: Dict[str, str]) -> bool:
self.log.info('Sensor check existence of record: %s', self.keys)
hook = CassandraHook(self.cassandra_conn_id)
return hook.record_exists(self.table, self.keys)
There was a problem hiding this comment.
I would mock CassandraHook and:
- check if it was initialized with required parameter
- the
record_existsof initializedhookwas called with expected parameters - check if the expected value was returned (mocking it)
- make sure that all methods I mock are already covered by tests. In this case, I would check tests for
CassandraHook
At 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
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Issue link: AIRFLOW-7038
Make sure to mark the boxes below before creating PR: [x]
[AIRFLOW-NNNN]. AIRFLOW-NNNN = JIRA ID** For document-only changes commit message can start with
[AIRFLOW-XXXX].In case of fundamental code change, Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in UPDATING.md.
Read the Pull Request Guidelines for more information.