Add simple tests for SqliteOperator (#8307)
Improve code coverage by adding a few simple tests for the SqliteOperator. Signed-off-by: Remy Suen <remy.suen@gmail.com>
This commit is contained in:
Родитель
968c96f154
Коммит
3a98032b25
|
@ -0,0 +1,17 @@
|
|||
#
|
||||
# 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.
|
|
@ -0,0 +1,79 @@
|
|||
#
|
||||
# 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.models.dag import DAG
|
||||
from airflow.providers.sqlite.operators.sqlite import SqliteOperator
|
||||
from airflow.utils import timezone
|
||||
|
||||
DEFAULT_DATE = timezone.datetime(2015, 1, 1)
|
||||
DEFAULT_DATE_ISO = DEFAULT_DATE.isoformat()
|
||||
DEFAULT_DATE_DS = DEFAULT_DATE_ISO[:10]
|
||||
TEST_DAG_ID = 'unit_test_dag'
|
||||
|
||||
|
||||
@pytest.mark.backend("sqlite")
|
||||
class TestSqliteOperator(unittest.TestCase):
|
||||
def setUp(self):
|
||||
args = {'owner': 'airflow', 'start_date': DEFAULT_DATE}
|
||||
dag = DAG(TEST_DAG_ID, default_args=args)
|
||||
self.dag = dag
|
||||
|
||||
def tearDown(self):
|
||||
tables_to_drop = ['test_airflow', 'test_airflow2']
|
||||
from airflow.providers.sqlite.hooks.sqlite import SqliteHook
|
||||
with SqliteHook().get_conn() as conn:
|
||||
cur = conn.cursor()
|
||||
for table in tables_to_drop:
|
||||
cur.execute(f"DROP TABLE IF EXISTS {table}")
|
||||
|
||||
def test_sqlite_operator_with_one_statement(self):
|
||||
sql = """
|
||||
CREATE TABLE IF NOT EXISTS test_airflow (
|
||||
dummy VARCHAR(50)
|
||||
);
|
||||
"""
|
||||
op = SqliteOperator(task_id='basic_sqlite', sql=sql, dag=self.dag)
|
||||
op.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True)
|
||||
|
||||
def test_sqlite_operator_with_multiple_statements(self):
|
||||
sql = [
|
||||
"CREATE TABLE IF NOT EXISTS test_airflow (dummy VARCHAR(50))",
|
||||
"INSERT INTO test_airflow VALUES ('X')",
|
||||
]
|
||||
op = SqliteOperator(
|
||||
task_id='sqlite_operator_with_multiple_statements', sql=sql, dag=self.dag)
|
||||
op.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True)
|
||||
|
||||
def test_sqlite_operator_with_invalid_sql(self):
|
||||
sql = [
|
||||
"CREATE TABLE IF NOT EXISTS test_airflow (dummy VARCHAR(50))",
|
||||
"INSERT INTO test_airflow2 VALUES ('X')",
|
||||
]
|
||||
|
||||
from sqlite3 import OperationalError
|
||||
try:
|
||||
op = SqliteOperator(
|
||||
task_id='sqlite_operator_with_multiple_statements', sql=sql, dag=self.dag)
|
||||
op.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True)
|
||||
pytest.fail("An exception should have been thrown")
|
||||
except OperationalError as e:
|
||||
assert 'no such table: test_airflow2' in str(e)
|
|
@ -46,8 +46,7 @@ MISSING_TEST_FILES = {
|
|||
'tests/providers/microsoft/mssql/hooks/test_mssql.py',
|
||||
'tests/providers/oracle/operators/test_oracle.py',
|
||||
'tests/providers/qubole/hooks/test_qubole.py',
|
||||
'tests/providers/samba/hooks/test_samba.py',
|
||||
'tests/providers/sqlite/operators/test_sqlite.py'
|
||||
'tests/providers/samba/hooks/test_samba.py'
|
||||
}
|
||||
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче