Skip to content

Commit

Permalink
Get all tests working again
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed Aug 8, 2016
1 parent c963da0 commit 7643076
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
2 changes: 2 additions & 0 deletions ocelot/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ def check_cache_directory(data_path):
Returns a boolean."""
assert os.path.isdir(data_path), "Invalid path for ``data_path``: {}".format(data_path)
cache_fp = get_cache_filepath_for_data_path(data_path)
if not os.path.exists(cache_fp):
return False
cache_time = os.stat(cache_fp).st_mtime
source_time = os.stat(data_path).st_mtime
return cache_time > source_time
Expand Down
3 changes: 3 additions & 0 deletions ocelot/model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from .configuration import default_configuration
from .filesystem import (
cache_data,
check_cache_directory,
get_from_cache,
OutputDir,
Expand Down Expand Up @@ -72,8 +73,10 @@ def system_model(data_path, config=None, show=False, use_cache=True):
config = config or default_configuration
if use_cache and check_cache_directory(data_path):
data = get_from_cache(data_path)
print("Using cached ecospold2 data")
else:
data = extract_directory(data_path)
cache_data(data, data_path)
output_manager = OutputDir()
counter = itertools.count()
logfile_path = create_log(output_manager.directory)
Expand Down
4 changes: 3 additions & 1 deletion tests/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,25 @@ def __call__(self):

@pytest.fixture
def fake_output_dir(monkeypatch):
tempdir.reset()
monkeypatch.setattr(
'ocelot.filesystem.get_base_directory',
tempdir
)


def test_fake_output_dir_fixture(fake_output_dir):
tempdir.reset()
assert "Ocelot" not in get_cache_directory()

def test_cache_loading(fake_output_dir):
tempdir.reset()
random_data = {k: random.randint(0, 10) for k in "abcdefghijklmnop"}
fp = os.path.abspath(__file__)
cache_data(random_data, fp)
assert random_data == get_from_cache(fp)

def test_cache_expiration(fake_output_dir):
tempdir.reset()
new_dp = tempfile.mkdtemp()
new_file = os.path.join(new_dp, "test.file")
with open(new_file, "w") as f:
Expand Down
27 changes: 14 additions & 13 deletions tests/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,31 @@
import uuid


class MockOutputDir(object):
"""Test mock that uses a tempfile directory, and deletes it after test runs"""
def __init__(self, *args, **kwargs):
self.report_id = uuid.uuid4().hex
self.directory = tempfile.mkdtemp()
print("Created:")
print(self.directory)
print(os.path.exists(self.directory))

def do_nothing(*args, **kwargs):
"""Mock for `HTMLReport` that doesn't do anything"""
pass


def passthrough(obj):
"""Mock for `extract_directory` that doesn't do anything"""
"""Mock for ``extract_directory`` that returns the initial input"""
return obj


@pytest.fixture
@pytest.fixture(scope="function")
def fake_report(monkeypatch):
tempdir = tempfile.mkdtemp()
get_fake_directory = lambda : tempdir
monkeypatch.setattr(
'ocelot.filesystem.get_base_directory',
get_fake_directory
)
monkeypatch.setattr(
'ocelot.model.check_cache_directory',
lambda x: False
)
monkeypatch.setattr(
'ocelot.model.OutputDir',
MockOutputDir
'ocelot.model.cache_data',
lambda x, y: None
)
monkeypatch.setattr(
'ocelot.model.extract_directory',
Expand Down

0 comments on commit 7643076

Please sign in to comment.