Skip to content

Commit

Permalink
updating test fixture usage
Browse files Browse the repository at this point in the history
  • Loading branch information
kylemann16 committed May 13, 2024
1 parent e24dc66 commit 207c3d9
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 50 deletions.
34 changes: 20 additions & 14 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dask
import pdal
import copy
from shutil import rmtree

from datetime import datetime
from typing import Generator
Expand All @@ -29,10 +30,8 @@ def threaded_dask() -> None:
dask.config.set(scheduler="threads")

@pytest.fixture(scope='function')
def tdb_filepath(tmp_path_factory) -> Generator[str, None, None]:
path = tmp_path_factory.mktemp("test_tdb")
p = os.path.abspath(path)
yield p
def tdb_filepath(storage_config) -> Generator[str, None, None]:
yield storage_config.tdb_dir

@pytest.fixture(scope='function')
def app_config(tdb_filepath, debug=True) -> Generator[ApplicationConfig, None, None]:
Expand All @@ -42,26 +41,32 @@ def app_config(tdb_filepath, debug=True) -> Generator[ApplicationConfig, None, N
yield app

@pytest.fixture(scope='function')
def storage_config(tdb_filepath, bounds, resolution, crs, attrs, metrics) -> Generator[StorageConfig, None, None]:
def storage_config(tmp_path_factory, bounds, resolution, crs, attrs, metrics) -> Generator[StorageConfig, None, None]:
path = tmp_path_factory.mktemp("test_tdb")
p = os.path.abspath(path)
log = Log('DEBUG')
yield StorageConfig(tdb_dir = tdb_filepath,

sc = StorageConfig(tdb_dir = p,
log = log,
crs = crs,
root = bounds,
resolution = resolution,
attrs = attrs,
metrics = metrics,
version = svversion)
Storage.create(sc)
yield sc
rmtree(path)

@pytest.fixture(scope="function")
def storage(storage_config) -> Generator[Storage, None, None]:
yield Storage.create(storage_config)
@pytest.fixture(scope='function')
def storage(storage_config):
yield Storage.from_db(storage_config.tdb_dir)

@pytest.fixture(scope='function')
def shatter_config(tdb_filepath, copc_filepath, storage_config, bounds,
app_config, storage, date) -> Generator[ShatterConfig, None, None]:
def shatter_config(copc_filepath, storage_config, bounds, date
) -> Generator[ShatterConfig, None, None]:
log = Log('INFO') # INFO
s = ShatterConfig(tdb_dir = tdb_filepath,
s = ShatterConfig(tdb_dir = storage_config.tdb_dir,
log = log,
filename = copc_filepath,
attrs = storage_config.attrs,
Expand All @@ -73,11 +78,12 @@ def shatter_config(tdb_filepath, copc_filepath, storage_config, bounds,
yield s

@pytest.fixture(scope='function')
def extract_config(tdb_filepath, tif_filepath, metrics, shatter_config, extract_attrs, storage):
def extract_config(tif_filepath, metrics, shatter_config, extract_attrs):
from silvimetric.commands import shatter
tdb_dir = shatter_config.tdb_dir
shatter.shatter(shatter_config)
log = Log(20)
c = ExtractConfig(tdb_dir = tdb_filepath,
c = ExtractConfig(tdb_dir = tdb_dir,
log = log,
out_dir = tif_filepath,
attrs = extract_attrs,
Expand Down
5 changes: 3 additions & 2 deletions tests/fixtures/extract_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def extract_attrs(dims)->Generator[list[str], None, None]:


@pytest.fixture(scope='function')
def multivalue_config(tdb_filepath, tif_filepath, metric_shatter_config):
def multivalue_config(tif_filepath, metric_shatter_config):

shatter(metric_shatter_config)

Expand All @@ -32,7 +32,8 @@ def multivalue_config(tdb_filepath, tif_filepath, metric_shatter_config):

shatter(second_config)
log = Log(20)
c = ExtractConfig(tdb_dir = tdb_filepath,
tdb_dir = metric_shatter_config.tdb_dir
c = ExtractConfig(tdb_dir = tdb_dir,
log = log,
out_dir = tif_filepath)
yield c
10 changes: 6 additions & 4 deletions tests/fixtures/metric_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
from silvimetric import __version__ as svversion

@pytest.fixture(scope='function')
def metric_shatter_config(tdb_filepath, copc_filepath, attrs, metrics, bounds,
def metric_shatter_config(tmp_path_factory, copc_filepath, attrs, metrics, bounds,
date, crs, resolution) -> Generator[pd.Series, None, None]:

log = Log(10)
path = tmp_path_factory.mktemp("test_tdb")
p = os.path.abspath(path)
log = Log('DEBUG')

def dummy_fn(df: pd.DataFrame) -> pd.DataFrame:
assert isinstance(df, pd.DataFrame)
Expand All @@ -22,7 +24,7 @@ def dummy_fn(df: pd.DataFrame) -> pd.DataFrame:
metrics[0].attributes=attrs

"""Make output"""
st_config=StorageConfig(tdb_dir=tdb_filepath,
st_config=StorageConfig(tdb_dir=p,
log=log,
crs=crs,
root=bounds,
Expand All @@ -32,7 +34,7 @@ def dummy_fn(df: pd.DataFrame) -> pd.DataFrame:
version=svversion)

s = Storage.create(st_config)
sh_config = ShatterConfig(tdb_dir=tdb_filepath,
sh_config = ShatterConfig(tdb_dir=p,
log=log,
filename=copc_filepath,
bounds=bounds,
Expand Down
31 changes: 21 additions & 10 deletions tests/fixtures/shatter_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from typing import Generator
from uuid import uuid4
import os

from silvimetric import __version__ as svversion
from silvimetric import StorageConfig, ShatterConfig, Storage, Log, Bounds
Expand Down Expand Up @@ -32,9 +33,13 @@ def s3_shatter_config(s3_storage, copc_filepath, attrs, metrics, date) -> Genera
debug=True, tdb_dir=config.tdb_dir, date=date)

@pytest.fixture(scope='function')
def uneven_storage_config(tdb_filepath, bounds, crs, attrs, metrics) -> Generator[StorageConfig, None, None]:
def uneven_storage_config(tmp_path_factory, bounds, crs, attrs, metrics) -> Generator[StorageConfig, None, None]:
log = Log('INFO')
sc = StorageConfig(tdb_dir = tdb_filepath,
path = tmp_path_factory.mktemp("test_tdb")
p = os.path.abspath(path)

log = Log('DEBUG')
sc = StorageConfig(tdb_dir = p,
log = log,
crs = crs,
root = bounds,
Expand All @@ -46,9 +51,11 @@ def uneven_storage_config(tdb_filepath, bounds, crs, attrs, metrics) -> Generato
yield sc

@pytest.fixture(scope='function')
def uneven_shatter_config(tdb_filepath, copc_filepath, uneven_storage_config, date) -> Generator[ShatterConfig, None, None]:
log = Log('INFO') # INFO
s = ShatterConfig(tdb_dir = tdb_filepath,
def uneven_shatter_config(copc_filepath, uneven_storage_config, date) -> Generator[ShatterConfig, None, None]:
tdb_dir = uneven_storage_config.tdb_dir
log = uneven_storage_config.log

s = ShatterConfig(tdb_dir = tdb_dir,
log = log,
filename = copc_filepath,
attrs = uneven_storage_config.attrs,
Expand All @@ -58,10 +65,13 @@ def uneven_shatter_config(tdb_filepath, copc_filepath, uneven_storage_config, da
yield s

@pytest.fixture(scope='function')
def partial_storage_config(tdb_filepath, crs, attrs, metrics) -> Generator[StorageConfig, None, None]:
log = Log('INFO')
def partial_storage_config(tmp_path_factory, crs, attrs, metrics) -> Generator[StorageConfig, None, None]:
path = tmp_path_factory.mktemp("test_tdb")
p = os.path.abspath(path)
log = Log('DEBUG')

bounds = Bounds(300,300,450,450)
sc = StorageConfig(tdb_dir = tdb_filepath,
sc = StorageConfig(tdb_dir = p,
log = log,
crs = crs,
root = bounds,
Expand All @@ -73,10 +83,11 @@ def partial_storage_config(tdb_filepath, crs, attrs, metrics) -> Generator[Stora
yield sc

@pytest.fixture(scope='function')
def partial_shatter_config(tdb_filepath, copc_filepath, date, partial_storage_config) -> Generator[ShatterConfig, None, None]:
def partial_shatter_config(copc_filepath, date, partial_storage_config) -> Generator[ShatterConfig, None, None]:
tdb_dir = partial_storage_config.tdb_dir
psc: StorageConfig = partial_storage_config
log = Log('INFO') # INFO
yield ShatterConfig(tdb_dir=tdb_filepath,
yield ShatterConfig(tdb_dir=tdb_dir,
log=log,
attrs=psc.attrs,
metrics=psc.metrics,
Expand Down
16 changes: 10 additions & 6 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import numpy as np
import os

from silvimetric.cli import cli
from silvimetric.commands import shatter, info
from silvimetric import ShatterConfig

class TestCli(object):
def test_cli_init(self, tdb_filepath, runner, bounds):
res = runner.invoke(cli.cli, args=["-d", tdb_filepath, "--debug",
def test_cli_init(self, tmp_path_factory, runner, bounds):
path = tmp_path_factory.mktemp("test_tdb")
p = os.path.abspath(path)
res = runner.invoke(cli.cli, args=["-d", p, "--debug",
"--scheduler", "single-threaded", "initialize", "--resolution",
'10', '--crs', 'EPSG:3857', '--bounds', str(bounds)],
catch_exceptions=False)
assert res.exit_code == 0

def test_cli_shatter(self, runner, storage, maxy, date, tdb_filepath,
copc_filepath):
def test_cli_shatter(self, runner, maxy, date, tdb_filepath,
copc_filepath, storage):

res = runner.invoke(cli.cli, args=["-d", tdb_filepath,
"--scheduler", "single-threaded",
Expand All @@ -37,8 +40,9 @@ def test_cli_shatter(self, runner, storage, maxy, date, tdb_filepath,
# if oob error, it's not this test's fault
assert bool(np.all( a[xi, yi]['Z'][0] == ((maxy/storage.config.resolution) - (yi + 1)) ))

def test_cli_scan(self, tdb_filepath, runner, copc_filepath, storage):
res = runner.invoke(cli.cli, args=['-d', tdb_filepath, '--scheduler', 'single-threaded', 'scan', copc_filepath])
def test_cli_scan(self, runner, copc_filepath, storage_config):
tdb_dir = storage_config.tdb_dir
res = runner.invoke(cli.cli, args=['-d', tdb_dir, '--scheduler', 'single-threaded', 'scan', copc_filepath])
print(res.output)
assert res.exit_code == 0

Expand Down
16 changes: 8 additions & 8 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@

class Test_Data(object):

def test_filepath(self, no_cell_line_path, storage: Storage, no_cell_line_pc, bounds):
def test_filepath(self, no_cell_line_path, storage_config, no_cell_line_pc, bounds):
"""Check open a COPC file"""
data = Data(no_cell_line_path, storage.config)
data = Data(no_cell_line_path, storage_config)
assert data.is_pipeline() == False
data.execute()
assert len(data.array) == no_cell_line_pc
assert data.estimate_count(bounds) == no_cell_line_pc

def test_pipeline(self, no_cell_line_pipeline, bounds, storage: Storage, no_cell_line_pc):
def test_pipeline(self, no_cell_line_pipeline, bounds, storage_config, no_cell_line_pc):
"""Check open a pipeline"""
data = Data(no_cell_line_pipeline, storage.config)
data = Data(no_cell_line_pipeline, storage_config)
assert data.is_pipeline() == True
data.execute()
assert len(data.array) == no_cell_line_pc
assert data.estimate_count(bounds) == no_cell_line_pc

def test_pipeline_bounds(self, no_cell_line_pipeline, bounds, storage: Storage, no_cell_line_pc):
def test_pipeline_bounds(self, no_cell_line_pipeline, bounds, storage_config, no_cell_line_pc):
"""Check open a pipeline with our own bounds"""
ll = list(bounds.bisect())[0]

#data will be collared upon execution, extra data will be grabbed
minx, miny, maxx, maxy = ll.get()
collared = Bounds(minx - 30, miny - 30, maxx + 30, maxy + 30)

data = Data(no_cell_line_pipeline, storage.config, bounds = ll)
data = Data(no_cell_line_pipeline, storage_config, bounds = ll)
assert data.is_pipeline() == True
data.execute()

Expand All @@ -38,9 +38,9 @@ def test_pipeline_bounds(self, no_cell_line_pipeline, bounds, storage: Storage,

class Test_Autzen(object):

def test_filepath(self, autzen_filepath, storage: Storage):
def test_filepath(self, autzen_filepath, storage_config):
"""Check open Autzen """
data = Data(autzen_filepath, storage.config)
data = Data(autzen_filepath, storage_config)
assert data.is_pipeline() == False
data.execute()
assert len(data.array) == 577637
Expand Down
23 changes: 17 additions & 6 deletions tests/test_storage.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import tiledb
import numpy as np
import pytest
import os
import copy

from silvimetric import Storage, Metrics, Attribute, Attributes
from silvimetric.commands import info
from silvimetric import Storage, Metrics, Attribute, Attributes, StorageConfig, Log
from silvimetric import __version__ as svversion

class Test_Storage(object):
Expand Down Expand Up @@ -43,17 +44,27 @@ def test_config(self, storage: Storage):
assert config.crs == storage.config.crs
assert storage.config.version == svversion

def test_metric_dependencies(self, storage_config):
ms = storage_config.metrics
def test_metric_dependencies(self, tmp_path_factory, metrics, crs, resolution, attrs, bounds):
ms = copy.deepcopy(metrics)

path = tmp_path_factory.mktemp("test_tdb")
p = os.path.abspath(path)
log = Log('DEBUG')

ms[0].dependencies = [Attributes['HeightAboveGround']]
sc = StorageConfig(tdb_dir=p, crs=crs, resolution=resolution,
attrs=attrs, metrics=ms, root=bounds)

with pytest.raises(ValueError) as e:
Storage.create(storage_config)
Storage.create(sc)
assert str(e.value) == 'Missing required dependency, HeightAboveGround.'

ms[0].dependencies = [Attributes['NumberOfReturns']]
s = Storage.create(storage_config)
s = Storage.create(sc)
assert isinstance(s, Storage)

ms[0].dependencies = []

def test_metrics(self, storage: Storage):
m_list = storage.getMetrics()
a_list = storage.getAttributes()
Expand Down

0 comments on commit 207c3d9

Please sign in to comment.