from os import listdir
from pathlib import Path
from tempfile import TemporaryDirectory

from airflow_config import load_config

RENDERED_DAG = """# Generated by airflow-config
from airflow.models import DAG
from airflow.providers.ssh.hooks.ssh import SSHHook
from airflow.providers.ssh.operators.ssh import SSHOperator
with DAG('example_dag') as dag:
    task_1 = SSHOperator(ssh_hook=SSHHook(remote_host='test', username='test', port=22, cmd_timeout=10, keepalive_interval=30, banner_timeout=30.0), ssh_conn_id='test', command='test', task_id='task_1', dag=dag)
"""


def test_render():
    conf = load_config("config", "test")
    assert conf.dags["example_dag"].render() == RENDERED_DAG


def test_generate():
    conf = load_config("config", "test")
    with TemporaryDirectory() as tmp_dir:
        conf.generate(tmp_dir)
        assert sorted(listdir(tmp_dir)) == ["example_dag.py", "example_dag2.py"]
        (Path(tmp_dir) / "example_dag.py").read_text() == RENDERED_DAG
