Skip to content

Commit

Permalink
add bank index versions tests (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-chambers committed Nov 15, 2019
1 parent 61129fb commit f7ac7e4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 22 deletions.
31 changes: 22 additions & 9 deletions tests/test_bank/test_eventbank.py
Expand Up @@ -82,20 +82,19 @@ def ebank_with_bad_files(tmpdir):

class TestBankBasics:
expected_attrs = ["update_index", "read_index"]
low_version_str: str = "0.0.-1"

@pytest.fixture
def ebank_low_version(self, ebank):
def ebank_low_version(self, ebank, monkeypatch):
""" return the default bank with a negative version number. """
# monkey patch obsplus version
negative_version = "0.0.-1"
version = obsplus.__version__
obsplus.__version__ = negative_version
# monkey patch obsplus version so that a low version is saved to disk
monkeypatch.setattr(obsplus, "__version__", self.low_version_str)
# write index with negative version
os.remove(ebank.index_path)
ebank.update_index()
assert ebank._index_version == negative_version
# restore correct version
obsplus.__version__ = version
monkeypatch.undo()
assert ebank._index_version == self.low_version_str
assert obsplus.__version__ != self.low_version_str
return ebank

@pytest.fixture(scope="class")
Expand Down Expand Up @@ -172,7 +171,7 @@ def test_get_events_empty_bank(self, tmp_path):
cat2_dict = {str(x.resource_id): x for x in cat2}
assert cat2_dict == cat1_dict

def test_min_version_recreates_index(self, ebank_low_version):
def test_update_index_recreates_index(self, ebank_low_version):
"""
If the min version of the event bank is not met the index should
be deleted and re-created. A warning should be issued.
Expand All @@ -187,6 +186,20 @@ def test_min_version_recreates_index(self, ebank_low_version):
# ensure the index was deleted and rewritten
assert mtime1 < mtime2

def test_get_events_recreates_index(self, ebank_low_version):
"""
Not just updating the index but also initing a new bank and using it.
"""
ebank = ebank_low_version
# The index should not yet have been updated
assert ebank._index_version == self.low_version_str
with pytest.warns(UserWarning):
ebank2 = EventBank(ebank.bank_path)
_ = ebank2.get_events(limit=1)
# but after creating a new bank it should
assert ebank._index_version == obsplus.__version__
assert ebank2._index_version == obsplus.__version__

def test_limit_keyword(self, ebank):
""" Test that the limit keyword limits results (see #19) """
limit = 2
Expand Down
41 changes: 28 additions & 13 deletions tests/test_bank/test_wavebank.py
Expand Up @@ -117,6 +117,8 @@ def empty_bank():
class TestBankBasics:
""" basic tests for Bank class """

low_version_str = "0.0.-1"

# fixtures
@pytest.fixture(scope="function")
def ta_bank_no_index(self, ta_bank):
Expand All @@ -127,18 +129,18 @@ def ta_bank_no_index(self, ta_bank):
return sbank.WaveBank(ta_bank.bank_path)

@pytest.fixture
def default_bank_low_version(self, default_wbank):
def default_bank_low_version(self, default_wbank, monkeypatch):
""" return the default bank with a negative version number. """
# monkey patch obsplus version
negative_version = "0.0.-1"
version = obsplus.__version__
obsplus.__version__ = negative_version
monkeypatch.setattr(obsplus, "__version__", self.low_version_str)
# write index with negative version
os.remove(default_wbank.index_path)
default_wbank.update_index()
assert default_wbank._index_version == negative_version
assert default_wbank._index_version == self.low_version_str
# restore correct version
obsplus.__version__ = version
monkeypatch.undo()
assert obsplus.__version__ != self.low_version_str
assert Path(default_wbank.bank_path).exists()
return default_wbank

# tests
Expand Down Expand Up @@ -217,15 +219,28 @@ def test_min_version_recreates_index(self, default_bank_low_version):
If the min version is not met the index should be deleted and re-created.
A warning should be issued.
"""
# TODO start here
bank = default_bank_low_version
ipath = Path(bank.index_path)
mtime1 = ipath.stat().st_mtime
with pytest.warns(UserWarning) as w:
with pytest.warns(UserWarning):
bank.update_index()
assert len(w) # a warning should have been raised
mtime2 = ipath.stat().st_mtime
# ensure the index was deleted and rewritten
assert mtime1 < mtime2
assert bank._index_version == obsplus.__version__
assert Path(bank.index_path).exists()

def test_min_version_new_bank_recreates_index(self, default_bank_low_version):
"""
A new bank should delete the old index and getting data from the bank
should recreate it.
"""
bank = default_bank_low_version
assert bank._index_version == self.low_version_str
# initing a new bank should warn and delete the old index
with pytest.warns(UserWarning):
bank2 = WaveBank(bank.bank_path)
assert not Path(bank2.index_path).exists()
bank2.get_waveforms()
assert bank2._index_version != self.low_version_str
assert bank2._index_version == obsplus.__version__
assert Path(bank2.index_path).exists()

def test_empty_bank_raises(self, tmpdir):
"""
Expand Down

0 comments on commit f7ac7e4

Please sign in to comment.