-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
59 lines (44 loc) · 1.6 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import pytest
from airflow import settings
from airflow.models import DagBag
from airflow.models.connection import Connection
from airflow.models.dag import DAG
# This will test the DAG is valid and the tasks are properly set up
def get_dag(dag_bag: DagBag, dag_id: str) -> DAG:
dag = dag_bag.get_dag(dag_id)
assert dag is not None
return dag
# Returns the result of the task execution
def get_result(results, task_id):
ti = results.get_task_instance(task_id=task_id)
return ti.xcom_pull(task_ids=task_id, key="return_value")
@pytest.fixture(scope="session")
def dag_bag() -> DagBag:
dag_bag = DagBag(include_examples=False, dag_folder="dags")
assert dag_bag.import_errors == {}
return dag_bag
@pytest.fixture(scope="session")
def httpserver_listen_address():
return ("127.0.0.1", 8000)
def stub_http_request(httpserver, endpoint, response):
httpserver.expect_request(endpoint).respond_with_json(response)
# Here we setup stub connections for airflow
@pytest.fixture(scope="session", autouse=True)
def setup_module():
session = settings.Session()
clean_connections(session)
data_api = Connection(
conn_id="data_api",
conn_type="http",
host="http://127.0.0.1:8000/",
)
session.add(data_api)
session.commit()
# Avoid issues caching connections, we clean the connections before each test
def clean_connections(session):
existing_connections = (
session.query(Connection).filter(Connection.conn_id == "data_api").all()
)
for connection in existing_connections:
session.delete(connection)
session.commit()