"""
This is a stubbed out test file designed to be used with PyTest, but can 
easily be modified to support any testing framework.
"""
import json
import os
from pathlib import Path
import sys

from arcgis.geometry import Geometry
from arcgis.gis import GIS
from dotenv import find_dotenv, load_dotenv
import pandas as pd
import pytest

# load up the dotenv file
load_dotenv(find_dotenv())

# get paths to useful resources - notably where the src directory is
self_pth = Path(__file__)
dir_test = self_pth.parent
dir_prj = dir_test.parent
dir_src = dir_prj/'src'

dir_test_data = dir_test/'test_data'
block_group_csv = dir_test_data/'seattle_block_group.csv'
h3_l7_csv = dir_test_data/'seattle_h3_l7.csv'

# insert the src directory into the path and import the project package
sys.path.insert(0, str(dir_src))
from business_analyst import BusinessAnalyst
from business_analyst import Country


def read_spatial_csv(pth: Path, geom_col: str = 'SHAPE') -> pd.DataFrame:
    assert pth.exists()
    df = pd.read_csv(pth, index_col=0)
    assert geom_col in df.columns
    df[geom_col] = df[geom_col].apply(lambda geom: Geometry(json.loads(geom)))
    df.spatial.set_geometry(geom_col)
    return df


@pytest.fixture
def gis_agol():
    gis = GIS(os.getenv('AGOL_URL'), username=os.getenv('AGOL_USERNAME'), password=os.getenv('AGOL_PASSWORD'))
    return gis


block_group_df = read_spatial_csv(block_group_csv)
h3_l7_df = read_spatial_csv(h3_l7_csv)


def get_key_var_df(cntry):
    var_df = cntry.variables
    key_df = var_df[
        (var_df.data_collection.str.lower().str.contains('key'))
        & (var_df.name.str.lower().str.endswith('cy'))
    ]
    return key_df


def business_analyst_instantiation_test(src):
    ba = BusinessAnalyst(src)
    assert isinstance(ba, BusinessAnalyst)
    assert hasattr(ba, 'source')


def business_analyst_countries_test(src):
    cntry_df = BusinessAnalyst(src).countries
    assert isinstance(cntry_df, pd.DataFrame)


def business_analyst_get_country_test(src):
    cntry = BusinessAnalyst(src).get_country('US')  # deliberately using iso2
    assert isinstance(cntry, Country)
    assert cntry.iso3 == 'USA'


def variables_test(src):
    var_df = BusinessAnalyst(src).get_country('USA').variables
    assert isinstance(var_df, pd.DataFrame)


def enrich_apportionment_test(src):
    cntry = BusinessAnalyst(src).get_country('USA')
    key_df = get_key_var_df(cntry)
    enrich_df = cntry.enrich(geometries=h3_l7_df, variables=key_df)
    assert isinstance(enrich_df, pd.DataFrame)
    assert enrich_df.spatial.validate()


def enrich_geo_id_test(src):
    cntry = BusinessAnalyst(src).get_country('USA')
    key_df = get_key_var_df(cntry)
    enrich_df = cntry.enrich(geometries=block_group_df, geographic_id_column='FIPS', variables=key_df)
    assert isinstance(enrich_df, pd.DataFrame)
    assert enrich_df.spatial.validate()


def enrich_apportionment_test_no_geometry(src):
    cntry = BusinessAnalyst(src).get_country('USA')
    key_df = get_key_var_df(cntry)
    enrich_df = cntry.enrich(geometries=h3_l7_df, variables=key_df, return_geometry=False)
    assert isinstance(enrich_df, pd.DataFrame)
    assert enrich_df.spatial.validate() is False

"""
This is a stubbed out test file designed to be used with PyTest, but can 
easily be modified to support any testing framework.
"""
import json
import os
from pathlib import Path
import sys

from arcgis.geometry import Geometry
from arcgis.gis import GIS
from dotenv import find_dotenv, load_dotenv
import pandas as pd
import pytest

# load up the dotenv file
load_dotenv(find_dotenv())

# get paths to useful resources - notably where the src directory is
self_pth = Path(__file__)
dir_test = self_pth.parent
dir_prj = dir_test.parent
dir_src = dir_prj/'src'

dir_test_data = dir_test/'test_data'
block_group_csv = dir_test_data/'seattle_block_group.csv'
h3_l7_csv = dir_test_data/'seattle_h3_l7.csv'

# insert the src directory into the path and import the project package
sys.path.insert(0, str(dir_src))
from business_analyst import BusinessAnalyst
from business_analyst import Country


def read_spatial_csv(pth: Path, geom_col: str = 'SHAPE') -> pd.DataFrame:
    assert pth.exists()
    df = pd.read_csv(pth, index_col=0)
    assert geom_col in df.columns
    df[geom_col] = df[geom_col].apply(lambda geom: Geometry(json.loads(geom)))
    df.spatial.set_geometry(geom_col)
    return df


@pytest.fixture
def gis_agol():
    gis = GIS(os.getenv('AGOL_URL'), username=os.getenv('AGOL_USERNAME'), password=os.getenv('AGOL_PASSWORD'))
    return gis


block_group_df = read_spatial_csv(block_group_csv)
h3_l7_df = read_spatial_csv(h3_l7_csv)


def get_key_var_df(cntry):
    var_df = cntry.variables
    key_df = var_df[
        (var_df.data_collection.str.lower().str.contains('key'))
        & (var_df.name.str.lower().str.endswith('cy'))
    ]
    return key_df


def business_analyst_instantiation_test(src):
    ba = BusinessAnalyst(src)
    assert isinstance(ba, BusinessAnalyst)
    assert hasattr(ba, 'source')


def business_analyst_countries_test(src):
    cntry_df = BusinessAnalyst(src).countries
    assert isinstance(cntry_df, pd.DataFrame)


def business_analyst_get_country_test(src):
    cntry = BusinessAnalyst(src).get_country('US')  # deliberately using iso2
    assert isinstance(cntry, Country)
    assert cntry.iso3 == 'USA'


def variables_test(src):
    var_df = BusinessAnalyst(src).get_country('USA').variables
    assert isinstance(var_df, pd.DataFrame)


def enrich_apportionment_test(src):
    cntry = BusinessAnalyst(src).get_country('USA')
    key_df = get_key_var_df(cntry)
    enrich_df = cntry.enrich(geometries=h3_l7_df, variables=key_df)
    assert isinstance(enrich_df, pd.DataFrame)
    assert enrich_df.spatial.validate()


def enrich_geo_id_test(src):
    cntry = BusinessAnalyst(src).get_country('USA')
    key_df = get_key_var_df(cntry)
    enrich_df = cntry.enrich(geometries=block_group_df, geographic_id_column='FIPS', variables=key_df)
    assert isinstance(enrich_df, pd.DataFrame)
    assert enrich_df.spatial.validate()


def enrich_apportionment_test_no_geometry(src):
    cntry = BusinessAnalyst(src).get_country('USA')
    key_df = get_key_var_df(cntry)
    enrich_df = cntry.enrich(geometries=h3_l7_df, variables=key_df, return_geometry=False)
    assert isinstance(enrich_df, pd.DataFrame)
    assert enrich_df.spatial.validate() is False

This is a stubbed out test file designed to be used with PyTest, but can  easily be modified to support any testing framework. # load up the dotenv file # get paths to useful resources - notably where the src directory is # insert the src directory into the path and import the project package # deliberately using iso2