149 lines
No EOL
2.9 KiB
Python
149 lines
No EOL
2.9 KiB
Python
import pytest
|
|
from typing import NamedTuple
|
|
from unittest.mock import mock_open, patch
|
|
from textwrap import dedent
|
|
from pathlib import Path
|
|
from configparser import ConfigParser
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
def module():
|
|
|
|
from httpaste.helper import config
|
|
|
|
return config
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_aclass():
|
|
|
|
class Foobar(NamedTuple):
|
|
foo: int
|
|
bar: str = 'test'
|
|
|
|
return Foobar
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_aclass_special():
|
|
|
|
class Foobar(NamedTuple):
|
|
foobar: Path
|
|
|
|
return Foobar
|
|
|
|
|
|
class Test_typecast():
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def setup(self, module, mock_aclass):
|
|
|
|
self.func = module.typecast
|
|
|
|
self.mock_aclass = mock_aclass
|
|
|
|
def test_default(self, module):
|
|
|
|
foobar = {
|
|
'foo': '45'
|
|
}
|
|
|
|
result = self.func(foobar, self.mock_aclass)
|
|
|
|
assert isinstance(result, dict)
|
|
|
|
assert result['foo'] == 45
|
|
assert result.get('bar') is None
|
|
|
|
|
|
def test_type_mismatch(self, module):
|
|
|
|
foobar = {
|
|
'foo': 'foobar'
|
|
}
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
self.func(foobar, self.mock_aclass)
|
|
|
|
|
|
def test_unknown_key(self, module):
|
|
|
|
foobar = {
|
|
'foo': '45',
|
|
'foobar': 'foobar'
|
|
}
|
|
|
|
with pytest.raises(KeyError):
|
|
|
|
self.func(foobar, self.mock_aclass)
|
|
|
|
|
|
class Test_get_config():
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def setup(self, module, mock_aclass):
|
|
|
|
self.func = module.get_config
|
|
|
|
self.mock_aclass = mock_aclass
|
|
|
|
def test_default(self):
|
|
|
|
data = dedent("""
|
|
[foobar]
|
|
foo = 45
|
|
""")
|
|
|
|
configIni = ConfigParser()
|
|
|
|
with patch('builtins.open', mock_open(read_data=data)):
|
|
|
|
configIni.read(str('void'))
|
|
|
|
result = self.func(configIni, 'foobar', self.mock_aclass)
|
|
|
|
assert isinstance(result, self.mock_aclass)
|
|
|
|
assert result.foo == 45
|
|
|
|
def test_relative_path(self, mock_aclass_special):
|
|
|
|
data = dedent("""
|
|
[foobar]
|
|
foobar = 'bar/foo'
|
|
""")
|
|
|
|
dirname = Path('/foo/bar')
|
|
|
|
configIni = ConfigParser()
|
|
|
|
with patch('builtins.open', mock_open(read_data=data)):
|
|
|
|
configIni.read(str('void'))
|
|
|
|
result = self.func(configIni, 'foobar', mock_aclass_special, dirname)
|
|
|
|
assert isinstance(result, mock_aclass_special)
|
|
assert isinstance(result.foobar, Path)
|
|
assert str(result.foobar) == '/foo/bar/bar/foo'
|
|
|
|
def test_absolute_path(self, mock_aclass_special):
|
|
|
|
data = dedent("""
|
|
[foobar]
|
|
foobar = '/bar/foo'
|
|
""")
|
|
|
|
configIni = ConfigParser()
|
|
|
|
with patch('builtins.open', mock_open(read_data=data)):
|
|
|
|
configIni.read(str('void'))
|
|
|
|
result = self.func(configIni, 'foobar', mock_aclass_special)
|
|
|
|
assert isinstance(result, mock_aclass_special)
|
|
assert isinstance(result.foobar, Path)
|
|
assert str(result.foobar) == '/bar/foo' |