~awuerl/blitzortung-python/master

« back to all changes in this revision

Viewing changes to tests/test_config.py

  • Committer: Andreas Wuerl
  • Date: 2019-06-24 19:32:25 UTC
  • mto: This revision was merged to the branch mainline in revision 394.
  • Revision ID: git-v1:29d4be282c05035bdff8d4828573537ed6d8ceea
update tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
"""
20
20
 
21
21
import sys
22
 
import unittest
23
 
from hamcrest import assert_that, is_, equal_to, instance_of, contains, same_instance, has_item
 
22
 
 
23
from assertpy import assert_that
24
24
from mock import Mock, call, patch
25
25
 
26
26
import blitzortung
32
32
    import ConfigParser as configparser
33
33
 
34
34
 
35
 
class TestConfig(unittest.TestCase):
36
 
    def setUp(self):
 
35
class TestConfig(object):
 
36
    def setup_method(self):
37
37
        self.config_parser = Mock()
38
38
        self.config = blitzortung.config.Config(self.config_parser)
39
39
 
40
40
    def test_get_username(self):
41
41
        self.config_parser.get.return_value = '<username>'
42
 
        assert_that(self.config.get_username(), is_(equal_to('<username>')))
43
 
        assert_that(self.config_parser.mock_calls, contains(call.get('auth', 'username')))
 
42
        assert_that(self.config.get_username()).is_equal_to('<username>')
 
43
        assert_that(self.config_parser.mock_calls).contains(call.get('auth', 'username'))
44
44
 
45
45
    def test_get_password(self):
46
46
        self.config_parser.get.return_value = '<password>'
47
 
        assert_that(self.config.get_password(), is_(equal_to('<password>')))
48
 
        assert_that(self.config_parser.mock_calls, contains(call.get('auth', 'password')))
 
47
        assert_that(self.config.get_password()).is_equal_to('<password>')
 
48
        assert_that(self.config_parser.mock_calls).contains(call.get('auth', 'password'))
49
49
 
50
50
    def test_get_raw_path(self):
51
51
        self.config_parser.get.return_value = '<raw_path>'
52
 
        assert_that(self.config.get_raw_path(), is_(equal_to('<raw_path>')))
53
 
        assert_that(self.config_parser.mock_calls, contains(call.get('path', 'raw')))
 
52
        assert_that(self.config.get_raw_path()).is_equal_to('<raw_path>')
 
53
        assert_that(self.config_parser.mock_calls).contains(call.get('path', 'raw'))
54
54
 
55
55
    def test_get_archive_path(self):
56
56
        self.config_parser.get.return_value = '<archive_path>'
57
 
        assert_that(self.config.get_archive_path(), is_(equal_to('<archive_path>')))
58
 
        assert_that(self.config_parser.mock_calls, contains(call.get('path', 'archive')))
 
57
        assert_that(self.config.get_archive_path()).is_equal_to('<archive_path>')
 
58
        assert_that(self.config_parser.mock_calls).contains(call.get('path', 'archive'))
59
59
 
60
60
    def test_get_db_connection_string(self):
61
61
        self.config_parser.get.side_effect = lambda *x: {
64
64
            ('db', 'username'): '<username>',
65
65
            ('db', 'password'): '<password>'}[x]
66
66
 
67
 
        assert_that(self.config.get_db_connection_string(),
68
 
                    is_(equal_to("host='<host>' dbname='<dbname>' user='<username>' password='<password>'")))
 
67
        assert_that(self.config.get_db_connection_string()) \
 
68
            .is_equal_to("host='<host>' dbname='<dbname>' user='<username>' password='<password>'")
69
69
 
70
 
        assert_that(self.config_parser.mock_calls, contains(
 
70
        assert_that(self.config_parser.mock_calls).contains(
71
71
            call.get('db', 'host'),
72
72
            call.get('db', 'dbname'),
73
73
            call.get('db', 'username'),
74
 
            call.get('db', 'password')))
 
74
            call.get('db', 'password'))
75
75
 
76
76
    def test_get_webservice_port(self):
77
77
        self.config_parser.get.return_value = 1234
78
 
        assert_that(self.config.get_webservice_port(), is_(equal_to(1234)))
79
 
        assert_that(self.config_parser.mock_calls, contains(call.get('webservice', 'port')))
 
78
        assert_that(self.config.get_webservice_port()).is_equal_to(1234)
 
79
        assert_that(self.config_parser.mock_calls).contains(call.get('webservice', 'port'))
80
80
 
81
81
    def test_string_representation(self):
82
82
        self.config_parser.get.side_effect = lambda *x: {
83
83
            ('auth', 'username'): '<username>',
84
84
            ('auth', 'password'): '<password>'}[x]
85
85
 
86
 
        assert_that(str(self.config), is_(equal_to("Config(user: <username>, pass: **********)")))
 
86
        assert_that(str(self.config)).is_equal_to("Config(user: <username>, pass: **********)")
87
87
 
88
 
        assert_that(self.config_parser.mock_calls, contains(
 
88
        assert_that(self.config_parser.mock_calls).contains(
89
89
            call.get('auth', 'username'),
90
 
            call.get('auth', 'password')))
91
 
 
92
 
 
93
 
class TestConfigModule(unittest.TestCase):
94
 
 
95
 
    def setUp(self):
 
90
            call.get('auth', 'password'))
 
91
 
 
92
 
 
93
class TestConfigModule(object):
 
94
 
 
95
    def setup_method(self):
96
96
        self.config_module = blitzortung.config.ConfigModule()
97
97
 
98
98
    @patch(config_parser_module + '.ConfigParser')
99
99
    def test_provide_config_parser(self, config_parser_class_mock):
100
100
        config_parser = self.config_module.provide_config_parser()
101
101
 
102
 
        assert_that(config_parser, is_(config_parser_class_mock.return_value))
103
 
        assert_that(config_parser_class_mock.mock_calls, has_item(call()))
104
 
        assert_that(config_parser.mock_calls, has_item(call.read('/etc/blitzortung.conf')))
 
102
        assert_that(config_parser).is_equal_to(config_parser_class_mock.return_value)
 
103
        assert_that(config_parser_class_mock.mock_calls).contains(call())
 
104
        assert_that(config_parser.mock_calls).contains(call.read('/etc/blitzortung.conf'))
105
105
 
106
106
    @patch('blitzortung.INJECTOR')
107
107
    def test_get_config(self, injector_class_mock):
108
108
        config = Mock()
109
109
        injector_class_mock.get.return_value = config
110
110
 
111
 
        assert_that(blitzortung.config.config(), is_(equal_to(config)))
 
 
b'\\ No newline at end of file'
 
111
        assert_that(blitzortung.config.config()).is_equal_to(config)