Skip to content

Commit

Permalink
bug fix with wildcard and compressions
Browse files Browse the repository at this point in the history
  • Loading branch information
havok2063 committed Jul 7, 2020
1 parent a4894ff commit 1903d8c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
3 changes: 2 additions & 1 deletion python/sdss_access/path/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,8 @@ def _check_compression(self, template):
for c in alternates if re.search(self._comp_regex, c)]))
if suffixes:
assert len(suffixes) == 1, 'should only be one suffix per file template '
template = template + suffixes[0]
if not template.endswith(suffixes[0]):
template = template + suffixes[0]

return template

Expand Down
17 changes: 16 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# @Last Modified time: 2019-08-07 12:30:00

from __future__ import print_function, division, absolute_import
import glob
import gzip
import os
import pytest
Expand Down Expand Up @@ -195,7 +196,6 @@ def monkeysas(tmpdir, monkeypatch, path):
path.replant_tree()



@pytest.fixture()
def copydata(tmpdir, request):
''' fixture to copy a file into a temporary directory '''
Expand All @@ -210,6 +210,21 @@ def copydata(tmpdir, request):
yield destpath


@pytest.fixture()
def copymulti(tmpdir, request):
''' Fixture to copy multiple files into a temporary directory '''
srcpath = os.path.join(os.getenv("SAS_BASE_DIR"), request.param)
files = glob.glob(srcpath)
if not files:
pytest.skip('Files do not exist, cannot copy')
for item in files:
loc = item.split(os.getenv("SAS_BASE_DIR") + '/')[-1]
sasdir = tmpdir / 'sas'
destpath = sasdir / loc
os.makedirs(os.path.dirname(destpath), exist_ok=True)
shutil.copy(item, destpath)


@contextlib.contextmanager
def gzuncompress(filename):
''' Context manager than gunzips a file temporarily. '''
Expand Down
19 changes: 18 additions & 1 deletion tests/path/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,11 @@ def test_refine(self, path):
def test_uncompress(self, copydata, monkeysas, path):
''' test to find unzipped files with zipped path templates '''
assert path.templates['mangacube'].endswith('.gz')
assert path.templates['mangacube'].count('.gz') == 1
with gzuncompress(copydata) as f:
full = path.full('mangacube', drpver='v2_4_3', plate=8485, ifu=1901, wave='LOG')
assert not full.endswith('.gz')
assert full.count('.gz') == 0
assert full.endswith('.fits')

@pytest.mark.parametrize('copydata',
Expand All @@ -163,16 +165,31 @@ def test_uncompress(self, copydata, monkeysas, path):
def test_compress(self, copydata, monkeysas, path):
''' test to find zipped files with non-zipped path templates '''
assert not path.templates['mangaimage'].endswith('.gz')
assert path.templates['mangaimage'].count('.gz') == 0
with gzcompress(copydata) as f:
full = path.full('mangaimage', drpver='v2_5_3', plate=8485, ifu=1901)
assert not full.endswith('.png')
assert full.endswith('.gz')
assert full.count('.gz') == 1

def test_uncompress_nofileexists(self, monkeysas, path):
''' test if no file exists, full returns original template path '''
assert path.templates['mangacube'].endswith('.gz')
full = path.full('mangacube', drpver='v2_4_3', plate=8485, ifu=1901, wave='LOG')
full = path.full('mangacube', drpver='v2_4_3', plate=8888, ifu=12345, wave='LOG')
assert full.endswith('.gz')
assert full.count('.gz') == 1

@pytest.mark.parametrize('copymulti',
[('mangawork/manga/spectro/redux/v2_4_3/8485/stack/manga-8485-*-LOGCUBE.fits.gz')],
indirect=True, ids=['data'])
@pytest.mark.parametrize('plate, ifu', [(8888, '*'), (8888, 12345),
(8485, 1901), (8485, '*')],
ids=['nodata-wild', 'nodata', 'glob', 'glob-wild'])
def test_compression_wildcards(self, copymulti, monkeysas, path, plate, ifu):
assert path.templates['mangacube'].endswith('.gz')
full = path.full('mangacube', drpver='v2_4_3', plate=plate, ifu=ifu, wave='LOG')
assert full.endswith('.gz')
assert full.count('.gz') == 1

@pytest.mark.parametrize('mirror', [(True), (False)])
def test_netloc(self, mirror):
Expand Down

0 comments on commit 1903d8c

Please sign in to comment.