from csv import DictReader

import pytest

from digital_land.api import DigitalLandApi
from digital_land.specification import specification_path


@pytest.mark.skip("data needs updating")
@pytest.mark.parametrize(
    "dataset_name",
    [
        "listed-building-grade",
        "listed-building-outline",
        "locally-listed-building",
    ],
)
def test_package_dataset(
    # Parametrize args
    dataset_name,
    # Static runtime filesystem dependencies
    column_field_dir,
    dataset_resource_dir,
    organisation_path,
    # Runtime filesystem dependencies generated by previous steps
    transformed_dir,
    pipeline_dir,
    # Test assertion directories
    dataset_dir,
    # Pytest fixtures
    tmp_path,
):
    # Setup
    expected_csv_result = dataset_dir.joinpath(f"{dataset_name}.csv")

    input_paths = [
        str(transformed_path)
        for transformed_path in transformed_dir.joinpath(dataset_name).iterdir()
    ]

    output_dir = tmp_path.joinpath("dataset_output")
    output_dir.mkdir()
    sqlite_path = output_dir.joinpath(f"{dataset_name}.sqlite3")
    csv_path = output_dir.joinpath(f"{dataset_name}.csv")

    # Call
    api = DigitalLandApi(
        debug=False,
        dataset=dataset_name,
        pipeline_dir=pipeline_dir,
        specification_dir=str(specification_path),
    )
    api.dataset_create_cmd(input_paths, sqlite_path, organisation_path)
    api.dataset_dump_cmd(sqlite_path, csv_path)

    # Assert
    with csv_path.open() as actual, expected_csv_result.open() as expected:
        actual_dict_reader = DictReader(actual)
        expected_dict_reader = DictReader(expected)
        assert actual_dict_reader.fieldnames == expected_dict_reader.fieldnames
        assert list(actual_dict_reader) == list(expected_dict_reader)


@pytest.mark.skip("data needs updating")
@pytest.mark.parametrize(
    "dataset_name",
    [
        "listed-building-grade",
        "listed-building-outline",
        "locally-listed-building",
    ],
)
def test_package_dataset_hoisted(
    # Parametrize args
    dataset_name,
    # Runtime filesystem dependencies generated by previous steps
    pipeline_dir,
    # Test assertion directories
    dataset_dir,
    # Pytest fixtures
    tmp_path,
):
    # Setup
    expected_hoisted_csv_result = dataset_dir.joinpath(f"{dataset_name}-hoisted.csv")

    csv_path = dataset_dir.joinpath(f"{dataset_name}.csv")

    output_dir = tmp_path.joinpath("dataset_output")
    output_dir.mkdir()
    hoisted_csv_path = output_dir.joinpath(f"{dataset_name}-hoisted.csv")

    # Call
    api = DigitalLandApi(
        debug=False,
        dataset=dataset_name,
        pipeline_dir=pipeline_dir,
        specification_dir=str(specification_path),
    )
    api.dataset_dump_hoisted_cmd(csv_path, hoisted_csv_path)

    with hoisted_csv_path.open() as actual, expected_hoisted_csv_result.open() as expected:
        actual_dict_reader = DictReader(actual)
        expected_dict_reader = DictReader(expected)
        assert actual_dict_reader.fieldnames == expected_dict_reader.fieldnames
        assert list(actual_dict_reader) == list(expected_dict_reader)

from csv import DictReader

import pytest

from digital_land.api import DigitalLandApi
from digital_land.specification import specification_path


@pytest.mark.skip("data needs updating")
@pytest.mark.parametrize(
    "dataset_name",
    [
        "listed-building-grade",
        "listed-building-outline",
        "locally-listed-building",
    ],
)
def test_package_dataset(
    # Parametrize args
    dataset_name,
    # Static runtime filesystem dependencies
    column_field_dir,
    dataset_resource_dir,
    organisation_path,
    # Runtime filesystem dependencies generated by previous steps
    transformed_dir,
    pipeline_dir,
    # Test assertion directories
    dataset_dir,
    # Pytest fixtures
    tmp_path,
):
    # Setup
    expected_csv_result = dataset_dir.joinpath(f"{dataset_name}.csv")

    input_paths = [
        str(transformed_path)
        for transformed_path in transformed_dir.joinpath(dataset_name).iterdir()
    ]

    output_dir = tmp_path.joinpath("dataset_output")
    output_dir.mkdir()
    sqlite_path = output_dir.joinpath(f"{dataset_name}.sqlite3")
    csv_path = output_dir.joinpath(f"{dataset_name}.csv")

    # Call
    api = DigitalLandApi(
        debug=False,
        dataset=dataset_name,
        pipeline_dir=pipeline_dir,
        specification_dir=str(specification_path),
    )
    api.dataset_create_cmd(input_paths, sqlite_path, organisation_path)
    api.dataset_dump_cmd(sqlite_path, csv_path)

    # Assert
    with csv_path.open() as actual, expected_csv_result.open() as expected:
        actual_dict_reader = DictReader(actual)
        expected_dict_reader = DictReader(expected)
        assert actual_dict_reader.fieldnames == expected_dict_reader.fieldnames
        assert list(actual_dict_reader) == list(expected_dict_reader)


@pytest.mark.skip("data needs updating")
@pytest.mark.parametrize(
    "dataset_name",
    [
        "listed-building-grade",
        "listed-building-outline",
        "locally-listed-building",
    ],
)
def test_package_dataset_hoisted(
    # Parametrize args
    dataset_name,
    # Runtime filesystem dependencies generated by previous steps
    pipeline_dir,
    # Test assertion directories
    dataset_dir,
    # Pytest fixtures
    tmp_path,
):
    # Setup
    expected_hoisted_csv_result = dataset_dir.joinpath(f"{dataset_name}-hoisted.csv")

    csv_path = dataset_dir.joinpath(f"{dataset_name}.csv")

    output_dir = tmp_path.joinpath("dataset_output")
    output_dir.mkdir()
    hoisted_csv_path = output_dir.joinpath(f"{dataset_name}-hoisted.csv")

    # Call
    api = DigitalLandApi(
        debug=False,
        dataset=dataset_name,
        pipeline_dir=pipeline_dir,
        specification_dir=str(specification_path),
    )
    api.dataset_dump_hoisted_cmd(csv_path, hoisted_csv_path)

    with hoisted_csv_path.open() as actual, expected_hoisted_csv_result.open() as expected:
        actual_dict_reader = DictReader(actual)
        expected_dict_reader = DictReader(expected)
        assert actual_dict_reader.fieldnames == expected_dict_reader.fieldnames
        assert list(actual_dict_reader) == list(expected_dict_reader)

# Parametrize args # Static runtime filesystem dependencies # Runtime filesystem dependencies generated by previous steps # Test assertion directories # Pytest fixtures # Setup # Call # Assert # Parametrize args # Runtime filesystem dependencies generated by previous steps # Test assertion directories # Pytest fixtures # Setup # Call