Skip to content

Commit

Permalink
Merge pull request #45 from jhamman/coverage
Browse files Browse the repository at this point in the history
100% test coverage
  • Loading branch information
Joe Hamman authored Nov 19, 2021
2 parents 007bd22 + 828318d commit cdf0bad
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 33 deletions.
17 changes: 8 additions & 9 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ on:
- cron: "0 0 * * *"

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
- uses: actions/[email protected]
- uses: pre-commit/[email protected]

test:
name: ${{ matrix.python-version }}-build
runs-on: ubuntu-latest
Expand All @@ -41,7 +34,13 @@ jobs:
python -m pip list
- name: Running Tests
run: |
python -m pytest --verbose
py.test --verbose --cov=. --cov-report=xml
- name: Upload coverage to Codecov
uses: codecov/[email protected]
if: ${{ matrix.python-version }} == 3.9
with:
file: ./coverage.xml
fail_ci_if_error: false

test-upstream:
name: ${{ matrix.python-version }}-dev-build
Expand Down Expand Up @@ -71,4 +70,4 @@ jobs:
python -m pip list
- name: Running Tests
run: |
python -m pytest --verbose
py.test --verbose --cov=.
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
ci:
autoupdate_schedule: quarterly
autofix_prs: false

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
Expand Down
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pytest
coverage
pytest-cov
adlfs
-r requirements.txt
7 changes: 0 additions & 7 deletions xbatcher/features.py

This file was deleted.

61 changes: 45 additions & 16 deletions xbatcher/tests/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,29 @@ def sample_ds_1d():
return ds


@pytest.fixture(scope='module')
def sample_ds_3d():
shape = (10, 50, 100)
ds = xr.Dataset(
{
'foo': (['time', 'y', 'x'], np.random.rand(*shape)),
'bar': (['time', 'y', 'x'], np.random.randint(0, 10, shape)),
},
{
'x': (['x'], np.arange(shape[-1])),
'y': (['y'], np.arange(shape[-2])),
},
)
return ds


def test_constructor_coerces_to_dataset():
da = xr.DataArray(np.random.rand(10), dims='x', name='foo')
bg = BatchGenerator(da, input_dims={'x': 2})
assert isinstance(bg.ds, xr.Dataset)
assert bg.ds.equals(da.to_dataset())


# TODO: decide how to handle bsizes like 15 that don't evenly divide the dimension
# Should we enforce that each batch size always has to be the same
@pytest.mark.parametrize('bsize', [5, 10])
Expand Down Expand Up @@ -86,22 +109,6 @@ def test_batch_1d_overlap(sample_ds_1d, olap):
assert ds_batch.equals(ds_batch_expected)


@pytest.fixture(scope='module')
def sample_ds_3d():
shape = (10, 50, 100)
ds = xr.Dataset(
{
'foo': (['time', 'y', 'x'], np.random.rand(*shape)),
'bar': (['time', 'y', 'x'], np.random.randint(0, 10, shape)),
},
{
'x': (['x'], np.arange(shape[-1])),
'y': (['y'], np.arange(shape[-2])),
},
)
return ds


@pytest.mark.parametrize('bsize', [5, 10])
def test_batch_3d_1d_input(sample_ds_3d, bsize):

Expand Down Expand Up @@ -160,3 +167,25 @@ def test_batch_3d_2d_input_concat(sample_ds_3d, bsize):
* (sample_ds_3d.dims['y'] // bsize)
* sample_ds_3d.dims['time']
)


def test_preload_batch_false(sample_ds_1d):
sample_ds_1d_dask = sample_ds_1d.chunk({'x': 2})
bg = BatchGenerator(
sample_ds_1d_dask, input_dims={'x': 2}, preload_batch=False
)
assert bg.preload_batch is False
for ds_batch in bg:
assert isinstance(ds_batch, xr.Dataset)
assert ds_batch.chunks


def test_preload_batch_true(sample_ds_1d):
sample_ds_1d_dask = sample_ds_1d.chunk({'x': 2})
bg = BatchGenerator(
sample_ds_1d_dask, input_dims={'x': 2}, preload_batch=True
)
assert bg.preload_batch is True
for ds_batch in bg:
assert isinstance(ds_batch, xr.Dataset)
assert not ds_batch.chunks

0 comments on commit cdf0bad

Please sign in to comment.