Skip to content

Commit

Permalink
Merge pull request pyfa-org#7 from Regner/tests-for-sourcemanager
Browse files Browse the repository at this point in the history
Tests for SourceManager and fix for the circular dependency in SourceManager
  • Loading branch information
regner authored Oct 7, 2016
2 parents f30de89 + f8e04da commit c339314
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 7 deletions.
6 changes: 3 additions & 3 deletions eos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
# ===============================================================================


# This should NOT be above the imports but there is a circular import to be solved first
__version__ = '0.0.0.dev8' # NOQA

from .const.eos import State, Restriction
from .data.cache_handler.exception import TypeFetchError
from .data.source import SourceManager
Expand All @@ -35,6 +32,9 @@
ModuleHigh, ModuleMed, ModuleLow, Rig, Ship, Skill, Stance, Subsystem
)


__version__ = '0.0.0.dev8'

__all__ = [
'State',
'Restriction',
Expand Down
19 changes: 17 additions & 2 deletions eos/data/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from logging import getLogger
from collections import namedtuple

from eos import __version__ as eos_version
from eos.util.repr import make_repr_str
from .cache_customizer import CacheCustomizer
from .cache_generator import CacheGenerator
Expand Down Expand Up @@ -70,10 +69,12 @@ def add(cls, alias, data_handler, cache_handler, make_default=False):
logger.info('adding source with alias "{}"'.format(alias))
if alias in cls._sources:
raise ExistingSourceError(alias)

# Compare fingerprints from data and cache
cache_fp = cache_handler.get_fingerprint()
data_version = data_handler.get_version()
current_fp = '{}_{}'.format(data_version, eos_version)
current_fp = cls.format_fingerprint(data_version)

# If data version is corrupt or fingerprints mismatch, update cache
if data_version is None or cache_fp != current_fp:
if data_version is None:
Expand All @@ -82,10 +83,12 @@ def add(cls, alias, data_handler, cache_handler, make_default=False):
msg = 'fingerprint mismatch: cache "{}", data "{}", updating cache'.format(
cache_fp, current_fp)
logger.info(msg)

# Generate cache, apply customizations and write it
cache_data = CacheGenerator().run(data_handler)
CacheCustomizer().run_builtin(cache_data)
cache_handler.update_cache(cache_data, current_fp)

# Finally, add record to list of sources
source = Source(alias=alias, cache_handler=cache_handler)
cls._sources[alias] = source
Expand Down Expand Up @@ -132,3 +135,15 @@ def list(cls):
def __repr__(cls):
spec = [['sources', '_sources']]
return make_repr_str(cls, spec)

@staticmethod
def format_fingerprint(data_version):
"""
Required arguments:
data_version -- version from the data handler
"""
# We import here to avoid a circular dependency
from eos import __version__ as eos_version

return '{}_{}'.format(data_version, eos_version)
2 changes: 2 additions & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pytest==3.0.3
pytest-mock==1.2
pytest-cov==2.3.1
pytest-capturelog==0.7
coverage==4.2
coveralls==1.1
19 changes: 19 additions & 0 deletions tests/data/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# ===============================================================================
# Copyright (C) 2011 Diego Duclos
# Copyright (C) 2011-2015 Anton Vorobyov
#
# This file is part of Eos.
#
# Eos is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Eos is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Eos. If not, see <http://www.gnu.org/licenses/>.
# ===============================================================================
127 changes: 127 additions & 0 deletions tests/data/test_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# ===============================================================================
# Copyright (C) 2011 Diego Duclos
# Copyright (C) 2011-2015 Anton Vorobyov
#
# This file is part of Eos.
#
# Eos is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Eos is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Eos. If not, see <http://www.gnu.org/licenses/>.
# ===============================================================================


import pytest

from eos import SourceManager
from eos.data.source import Source
from eos.data.exception import ExistingSourceError, UnknownSourceError
from unittest.mock import MagicMock, Mock


@pytest.fixture
def mock_data_handler():
data_handler = MagicMock()
return data_handler


@pytest.fixture
def mock_cache_handler():
cache_handler = MagicMock()
return cache_handler


def setup_function():
SourceManager._sources = {}
SourceManager.default = None


def teardown_function():
SourceManager._sources = {}
SourceManager.default = None


def test_add_existing_source_error(mock_data_handler, mock_cache_handler):
SourceManager.add('test', mock_data_handler, mock_cache_handler, True)

with pytest.raises(ExistingSourceError):
SourceManager.add('test', mock_data_handler, mock_cache_handler, True)


def test_add_sets_sources(mock_data_handler, mock_cache_handler):
alias = 'test'
source = Source(alias=alias, cache_handler=mock_cache_handler)

SourceManager.add(alias, mock_data_handler, mock_cache_handler, True)

assert SourceManager._sources == {alias: source}
assert SourceManager.default == source


def test_add_does_not_set_default(mock_data_handler, mock_cache_handler):
SourceManager.add('test', mock_data_handler, mock_cache_handler)

assert SourceManager.default is None


def test_add_processes_with_data_version_none(mock_data_handler, mock_cache_handler, caplog):
mock_data_handler.get_version = Mock(return_value=None)
SourceManager.add('test', mock_data_handler, mock_cache_handler)

assert mock_cache_handler.update_cache.called
assert 'data version is None, updating cache' in caplog.text()


def test_add_fingerprint_mismatch(mock_data_handler, mock_cache_handler, caplog):
mock_cache_handler.get_fingerprint = Mock(return_value='cache_fingerprint')
mock_data_handler.get_version = Mock(return_value='dh_version')
SourceManager.add('test', mock_data_handler, mock_cache_handler)

log_msg = 'fingerprint mismatch: cache "cache_fingerprint", data "dh_version_0.0.0.dev8", updating cache'

assert log_msg in caplog.text()


def test_removing_known_source(mock_data_handler, mock_cache_handler):
SourceManager.add('test', mock_data_handler, mock_cache_handler)
SourceManager.remove('test')

assert 'test' not in SourceManager._sources


def test_removing_unknown_source(mock_data_handler, mock_cache_handler):
with pytest.raises(UnknownSourceError):
SourceManager.remove('test')


def test_get_real_source(mock_data_handler, mock_cache_handler):
alias = 'test'
source = Source(alias=alias, cache_handler=mock_cache_handler)

SourceManager.add(alias, mock_data_handler, mock_cache_handler)
result = SourceManager.get(alias)

assert result == source


def test_get_unkown_source(mock_data_handler, mock_cache_handler):
with pytest.raises(UnknownSourceError):
SourceManager.get('test')


def test_list_sources_correctly(mock_data_handler, mock_cache_handler):
SourceManager.add('source one', mock_data_handler, mock_cache_handler)
SourceManager.add('source two', mock_data_handler, mock_cache_handler)
SourceManager.add('source three', mock_data_handler, mock_cache_handler)

sources = SourceManager.list()

assert sorted(sources) == sorted(['source one', 'source two', 'source three'])
7 changes: 5 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ skipsdist = True
deps =
-rrequirements.txt
-rrequirements_test.txt
basepython = python3
commands = py.test -vv --cov eos tests/

[testenv:pep8]
deps = flake8
basepython = python3
commands = flake8 --exclude=.svn,CVS,.bzr,.hg,.git,__pycache__,venv,tests,.tox,build,dist --max-line-length=120
commands = flake8 --exclude=.svn,CVS,.bzr,.hg,.git,__pycache__,venv,tests,.tox,build,dist --max-line-length=120

[testenv:single]
commands = py.test -vv eos {posargs}

0 comments on commit c339314

Please sign in to comment.