Skip to content

Commit

Permalink
Add tests for folder reading and > 1 MTL files (#11)
Browse files Browse the repository at this point in the history
Test reading from a folder and check if an exception is raised when
multiple MTL files are present.
  • Loading branch information
leouieda authored Dec 5, 2022
1 parent 3f74ee4 commit 12bfbc3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
3 changes: 3 additions & 0 deletions xlandsat/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Copyright (c) 2022 The xlandsat developers.
# Distributed under the terms of the MIT License.
# SPDX-License-Identifier: MIT
55 changes: 53 additions & 2 deletions xlandsat/tests/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,67 @@
"""
Test the main IO functionality
"""
import pathlib
import shutil
import tempfile

import pooch
import pytest

from .._read import load_scene


def test_load_scene_minimal():
"Minimal check that things don't crash."
def test_load_scene_archive():
"Check basic things about loading a scene from a tar archive"
path = pooch.retrieve(
"doi:10.6084/m9.figshare.21665630.v1/cropped-after.tar.gz",
known_hash="md5:4ae61a2d7a8b853c727c0c433680cece",
)
scene = load_scene(path)
assert scene.attrs["title"] == "Landsat 8 scene from 2019-01-30 (path/row=218/74)"
assert set(scene.data_vars) == set(
["red", "green", "blue", "nir", "swir1", "swir2"]
)
assert scene.red.shape == (300, 400)


def test_load_scene_folder():
"Check basic things about loading a scene from a folder"
paths = pooch.retrieve(
"doi:10.6084/m9.figshare.21665630.v1/cropped-after.tar.gz",
known_hash="md5:4ae61a2d7a8b853c727c0c433680cece",
processor=pooch.Untar(),
)
# Unpack the archive and get the folder name
path = pathlib.Path(paths[0]).parent
assert path.is_dir()
scene = load_scene(path)
assert scene.attrs["title"] == "Landsat 8 scene from 2019-01-30 (path/row=218/74)"
assert set(scene.data_vars) == set(
["red", "green", "blue", "nir", "swir1", "swir2"]
)
assert scene.red.shape == (300, 400)


def test_load_scene_fail_multiple_mtl_files():
"Check that loading fails when there are multiple MTL.txt files"
paths = pooch.retrieve(
"doi:10.6084/m9.figshare.21665630.v1/cropped-after.tar.gz",
known_hash="md5:4ae61a2d7a8b853c727c0c433680cece",
processor=pooch.Untar(),
)
# Unpack the archive and get the folder name
path = pathlib.Path(paths[0]).parent
assert path.is_dir()
# Find the name of the MTL file
mtl = list(path.glob("*_MTL.txt"))
assert len(mtl) == 1
mtl = mtl[0].name
# Duplicate the scene into a temporary folder and add an extra file
with tempfile.TemporaryDirectory() as tmpdir:
duplicate = pathlib.Path(tmpdir) / "duplicate_scene"
shutil.copytree(path, duplicate)
shutil.copy(pathlib.Path(path) / mtl, duplicate / ("copy_" + mtl))
with pytest.raises(ValueError) as error:
load_scene(duplicate)
assert "Found 2" in str(error)

0 comments on commit 12bfbc3

Please sign in to comment.