diff --git a/tests/httpaste/backend/__init__.py b/tests/httpaste/backend/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/httpaste/backend/test__init__.py b/tests/httpaste/backend/test__init__.py new file mode 100755 index 0000000..b10af79 --- /dev/null +++ b/tests/httpaste/backend/test__init__.py @@ -0,0 +1,134 @@ +#!/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) \ No newline at end of file diff --git a/tests/httpaste/helper/__init__.py b/tests/httpaste/helper/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/httpaste/helper/test_config.py b/tests/httpaste/helper/test_config.py new file mode 100644 index 0000000..d36355e --- /dev/null +++ b/tests/httpaste/helper/test_config.py @@ -0,0 +1,149 @@ +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' \ No newline at end of file diff --git a/tests/httpaste/model/test_paste.py b/tests/httpaste/model/test_paste.py new file mode 100755 index 0000000..aafd415 --- /dev/null +++ b/tests/httpaste/model/test_paste.py @@ -0,0 +1,79 @@ +#!/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.model import paste + + return paste + + +class Test_get_paste_model_config(): + + @pytest.fixture(autouse=True) + def setup(self, module): + + self.func = module.get_paste_model_config + + def test_default(self, module): + + data = '' + + configIni = ConfigParser() + + with patch('builtins.open', mock_open(read_data=data)): + + configIni.read('void') + + result = self.func(configIni) + + assert isinstance(result, module._Config) + assert isinstance(result.id_size, int), result.id_size + assert isinstance(result.key_size, int), result.key_size + + +#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) \ No newline at end of file