134 lines
No EOL
2.9 KiB
Python
Executable file
134 lines
No EOL
2.9 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
import pytest
|
|
from textwrap import dedent
|
|
from unittest.mock import mock_open, patch
|
|
from configparser import ConfigParser
|
|
from pathlib import Path
|
|
|
|
@pytest.fixture
|
|
def module():
|
|
|
|
from httpaste import backend
|
|
|
|
return backend
|
|
|
|
|
|
class Test_get_backend_config():
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def setup(self, module):
|
|
|
|
self.func = module.get_backend_config
|
|
|
|
def test_default_file(self, module):
|
|
|
|
data = dedent("""
|
|
[backend]
|
|
type = file
|
|
|
|
[backend.file]
|
|
base_dirname = 'sample_data'
|
|
""")
|
|
|
|
path = Path('/foo')
|
|
|
|
configIni = ConfigParser()
|
|
|
|
with patch('builtins.open', mock_open(read_data=data)):
|
|
|
|
configIni.read(str(path))
|
|
|
|
config = self.func(configIni, path)
|
|
|
|
assert isinstance(config, module.Config)
|
|
assert issubclass(config.interface, module.BackendInterface)
|
|
assert str(config.config.base_dirname) == '/foo/sample_data'
|
|
|
|
def test_sqlite(self, module):
|
|
|
|
data = dedent("""
|
|
[backend]
|
|
type = sqlite
|
|
|
|
[backend.sqlite]
|
|
uri = 'foobar.db'
|
|
""")
|
|
|
|
configIni = ConfigParser()
|
|
|
|
with patch('builtins.open', mock_open(read_data=data)):
|
|
|
|
configIni.read('void')
|
|
|
|
config = self.func(configIni, Path('/foo'))
|
|
|
|
assert str(config.config.uri) == '/foo/foobar.db'
|
|
|
|
def test_mysql(self, module):
|
|
|
|
data = dedent("""
|
|
[backend]
|
|
type = mysql
|
|
|
|
[backend.mysql]
|
|
user = 'foo'
|
|
password = bar
|
|
host = manana
|
|
database = test
|
|
""")
|
|
|
|
configIni = ConfigParser()
|
|
|
|
with patch('builtins.open', mock_open(read_data=data)):
|
|
|
|
configIni.read('void')
|
|
|
|
config = self.func(configIni, Path('/foo'))
|
|
|
|
assert config.config.user == 'foo'
|
|
assert config.config.password == 'bar'
|
|
assert config.config.host == 'manana'
|
|
assert config.config.database == 'test'
|
|
|
|
|
|
|
|
#class Test_load():
|
|
#
|
|
# @pytest.fixture(autouse=True)
|
|
# def setup(self, module):
|
|
#
|
|
# self.func = module.load
|
|
#
|
|
# def test_missing_parameter(self, module):
|
|
#
|
|
# config = module.Config()
|
|
# config.name = 'file'
|
|
# config.parameters = {}
|
|
#
|
|
# with pytest.raises(module.BackendError):
|
|
# self.func(config)
|
|
#
|
|
# def test_unknown_parameter(self, module):
|
|
#
|
|
# config = module.Config()
|
|
# config.name = 'file'
|
|
# config.parameters = {
|
|
# 'base_dirname': 'foofoo',
|
|
# 'foo': 'bar'
|
|
# }
|
|
#
|
|
# with pytest.raises(module.BackendError):
|
|
# self.func(config)
|
|
#
|
|
# def test_file(self, module):
|
|
#
|
|
# config = module.Config()
|
|
# config.name = 'file'
|
|
# config.parameters = {
|
|
# 'base_dirname': 'foofoo',
|
|
# 'user_dirnamea': 'test'
|
|
# }
|
|
#
|
|
# backend = self.func(config)
|
|
#
|
|
# assert isinstance(backend, module.BackendInterface) |