-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also introduce the TestData class, which has a mechanism to download external data so that datasets too big for the repository can be hosted elsewhere and downloaded as needed during unit test running.
- Loading branch information
Showing
7 changed files
with
93 additions
and
11 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import os | ||
from tempfile import TemporaryDirectory | ||
|
||
import pystac | ||
|
||
from stactools.aster.commands import create_aster_command | ||
from tests.utils import (TestData, CliTestCase) | ||
|
||
EXTERNAL_DATA_PATH = 'aster/AST_L1T_00301012006003619_20150512141939_7778.hdf' | ||
|
||
|
||
class CreateItemTest(CliTestCase): | ||
def create_subcommand_functions(self): | ||
return [create_aster_command] | ||
|
||
def test_create_item(self): | ||
test_path = TestData.get_external_data(EXTERNAL_DATA_PATH) | ||
|
||
with TemporaryDirectory() as tmp_dir: | ||
cmd = ['aster', 'create-item', test_path, tmp_dir] | ||
self.run_command(cmd) | ||
|
||
item_path = os.path.join(tmp_dir, os.listdir(tmp_dir)[0]) | ||
item = pystac.read_file(item_path) | ||
|
||
self.assertEqual(set(item.assets.keys()), set(['hdf'])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# flake8: noqa | ||
|
||
from tests.utils.test_cases import TestCases | ||
from tests.utils.test_data import TestData | ||
from tests.utils.cli_test import CliTestCase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import os | ||
import shutil | ||
from tempfile import TemporaryDirectory | ||
from zipfile import ZipFile | ||
|
||
import requests | ||
|
||
EXTERNAL_DATA = { | ||
'aster/AST_L1T_00301012006003619_20150512141939_7778.hdf': { | ||
'url': | ||
('https://ai4epublictestdata.blob.core.windows.net/' | ||
'stactools/aster/AST_L1T_00301012006003619_20150512141939_7778.zip'), | ||
'compress': | ||
'zip' | ||
} | ||
} | ||
|
||
|
||
class TestData: | ||
@staticmethod | ||
def get_path(rel_path): | ||
return os.path.abspath( | ||
os.path.join(os.path.dirname(__file__), '..', rel_path)) | ||
|
||
@staticmethod | ||
def get_external_data(rel_path): | ||
path = TestData.get_path(os.path.join('data-files/external', rel_path)) | ||
if not os.path.exists(path): | ||
entry = EXTERNAL_DATA.get(rel_path) | ||
if entry is None: | ||
raise Exception('Path {} does not exist and there is no entry ' | ||
'for external test data {}.'.format( | ||
path, rel_path)) | ||
|
||
print('Downloading external test data {}...'.format(rel_path)) | ||
os.makedirs(os.path.dirname(path), exist_ok=True) | ||
|
||
resp = requests.get(entry['url']) | ||
if entry['compress'] == 'zip': | ||
with TemporaryDirectory() as tmp_dir: | ||
tmp_path = os.path.join(tmp_dir, 'file.zip') | ||
with open(tmp_path, 'wb') as f: | ||
f.write(resp.content) | ||
z = ZipFile(tmp_path) | ||
name = z.namelist()[0] | ||
extracted_path = z.extract(name) | ||
shutil.move(extracted_path, path) | ||
else: | ||
with open(path, 'wb') as f: | ||
f.write(resp.content) | ||
|
||
return path |