blob: 0eecbf4c268d90a2e45261d69023f748e4394b6c [file] [log] [blame]
Max Rasskazova8db7a92015-05-30 02:23:43 +03001#-*- coding: utf-8 -*-
2
Max Rasskazova8db7a92015-05-30 02:23:43 +03003import unittest
4import yaml
5
Max Rasskazov64f77bf2015-06-02 15:22:48 +03006import rsync_url
Max Rasskazovb1b09a62015-06-03 15:21:35 +03007import utils
Max Rasskazov64f77bf2015-06-02 15:22:48 +03008
9
Max Rasskazovb1b09a62015-06-03 15:21:35 +030010logger = utils.logger.getChild('TestRsyncUrl')
Max Rasskazov64f77bf2015-06-02 15:22:48 +030011
Max Rasskazova8db7a92015-05-30 02:23:43 +030012
13class TestRsyncUrl(unittest.TestCase):
14
Max Rasskazov64f77bf2015-06-02 15:22:48 +030015 def log_locals(self, url):
16 if url.match:
Max Rasskazov84dd8672015-06-05 17:52:43 +030017 logger.info('Type: {}, RE: "{}"'
18 ''.format(url.url_type, url.match.pattern))
Max Rasskazov64f77bf2015-06-02 15:22:48 +030019 logger.info('user "{}", host "{}", port "{}", module "{}", '
20 'path "{}"'.format(url.user, url.host, url.port,
21 url.module, url.path))
22
Max Rasskazova8db7a92015-05-30 02:23:43 +030023 def exact_match_num(self, remote, expected_result):
Max Rasskazov84dd8672015-06-05 17:52:43 +030024 logger.info('For "{}" should be {}'.format(remote, expected_result))
Max Rasskazova8db7a92015-05-30 02:23:43 +030025 url = rsync_url.RsyncUrl(remote)
Max Rasskazov64f77bf2015-06-02 15:22:48 +030026 self.log_locals(url)
Max Rasskazova8db7a92015-05-30 02:23:43 +030027 matching_regexps = url._get_all_matching_regexps()
28 self.assertEqual(len(matching_regexps), expected_result)
29
30 def classed(self, remote, expected_result):
Max Rasskazov84dd8672015-06-05 17:52:43 +030031 logger.info('For "{}" should be {}'.format(remote, expected_result))
Max Rasskazova8db7a92015-05-30 02:23:43 +030032 url = rsync_url.RsyncUrl(remote)
Max Rasskazov64f77bf2015-06-02 15:22:48 +030033 self.log_locals(url)
Max Rasskazova8db7a92015-05-30 02:23:43 +030034 self.assertEqual(url.url_type, expected_result)
35
36 def parsed(self, remote, expected_result):
Max Rasskazov84dd8672015-06-05 17:52:43 +030037 logger.info('For "{}" should be {}'.format(remote, expected_result))
Max Rasskazova8db7a92015-05-30 02:23:43 +030038 url = rsync_url.RsyncUrl(remote)
Max Rasskazov64f77bf2015-06-02 15:22:48 +030039 self.log_locals(url)
Max Rasskazova8db7a92015-05-30 02:23:43 +030040 self.assertEqual(
Max Rasskazov64f77bf2015-06-02 15:22:48 +030041 [url.user, url.host, url.path],
Max Rasskazova8db7a92015-05-30 02:23:43 +030042 expected_result
43 )
44
Max Rasskazov64f77bf2015-06-02 15:22:48 +030045 def parsed_rsync(self, remote, expected_result):
Max Rasskazov84dd8672015-06-05 17:52:43 +030046 logger.info('For "{}" should be {}'.format(remote, expected_result))
Max Rasskazov64f77bf2015-06-02 15:22:48 +030047 url = rsync_url.RsyncUrl(remote)
48 self.log_locals(url)
49 self.assertEqual(
50 [url.user, url.host, url.port, url.module, url.path],
51 expected_result
52 )
53
54 def valid(self, remote, expected_result):
Max Rasskazov84dd8672015-06-05 17:52:43 +030055 logger.info('For "{}" should be {}'.format(remote, expected_result))
Max Rasskazov64f77bf2015-06-02 15:22:48 +030056 url = rsync_url.RsyncUrl(remote)
57 self.log_locals(url)
58 self.assertEqual(url.is_valid, expected_result)
59
60 def url(self, remote, expected_result):
Max Rasskazov84dd8672015-06-05 17:52:43 +030061 logger.info('For "{}" should be {}'.format(remote, expected_result))
Max Rasskazov64f77bf2015-06-02 15:22:48 +030062 url = rsync_url.RsyncUrl(remote)
63 self.log_locals(url)
64 self.assertEqual(url.url, expected_result)
Max Rasskazova8db7a92015-05-30 02:23:43 +030065
Max Rasskazovba34b142015-06-04 17:12:27 +030066 def urljoin(self, remote, expected_result):
Max Rasskazov84dd8672015-06-05 17:52:43 +030067 logger.info('For "{}" should be {}'.format(remote, expected_result))
Max Rasskazovba34b142015-06-04 17:12:27 +030068 url = rsync_url.RsyncUrl(remote)
69 self.log_locals(url)
70 for par, er in expected_result.items():
71 logger.info('par = "{}", er = "{}"'.format(par, er))
72 self.assertEqual(url.urljoin(par), er)
73
Max Rasskazov46cc0732015-06-05 19:23:24 +030074 def a_dir(self, remote, expected_result):
Max Rasskazov84dd8672015-06-05 17:52:43 +030075 logger.info('For "{}" should be {}'.format(remote, expected_result))
Max Rasskazovba34b142015-06-04 17:12:27 +030076 url = rsync_url.RsyncUrl(remote)
77 self.log_locals(url)
78 for par, er in expected_result.items():
79 logger.info('par = "{}", er = "{}"'.format(par, er))
Max Rasskazov46cc0732015-06-05 19:23:24 +030080 self.assertEqual(url.a_dir(par), er)
Max Rasskazovba34b142015-06-04 17:12:27 +030081
Max Rasskazov2f93dd22015-06-04 14:20:44 +030082 def url_in(self, remote, expected_result):
Max Rasskazov84dd8672015-06-05 17:52:43 +030083 logger.info('For "{}" should be {}'.format(remote, expected_result))
Max Rasskazov2f93dd22015-06-04 14:20:44 +030084 url = rsync_url.RsyncUrl(remote)
85 self.log_locals(url)
86 for par, er in expected_result.items():
Max Rasskazovba34b142015-06-04 17:12:27 +030087 logger.info('par = "{}", er = "{}"'.format(par, er))
Max Rasskazov2f93dd22015-06-04 14:20:44 +030088 self.assertEqual(url.url_in(par), er)
89
Max Rasskazov46cc0732015-06-05 19:23:24 +030090 def a_file(self, remote, expected_result):
Max Rasskazov84dd8672015-06-05 17:52:43 +030091 logger.info('For "{}" should be {}'.format(remote, expected_result))
Max Rasskazovba34b142015-06-04 17:12:27 +030092 url = rsync_url.RsyncUrl(remote)
93 self.log_locals(url)
94 for par, er in expected_result.items():
95 logger.info('par = "{}", er = "{}"'.format(par, er))
Max Rasskazov46cc0732015-06-05 19:23:24 +030096 self.assertEqual(url.a_file(par), er)
Max Rasskazovba34b142015-06-04 17:12:27 +030097
Max Rasskazov2f93dd22015-06-04 14:20:44 +030098 def url_is(self, remote, expected_result):
Max Rasskazov84dd8672015-06-05 17:52:43 +030099 logger.info('For "{}" should be {}'.format(remote, expected_result))
Max Rasskazov2f93dd22015-06-04 14:20:44 +0300100 url = rsync_url.RsyncUrl(remote)
101 self.log_locals(url)
102 for par, er in expected_result.items():
Max Rasskazovba34b142015-06-04 17:12:27 +0300103 logger.info('par = "{}", er = "{}"'.format(par, er))
Max Rasskazov2f93dd22015-06-04 14:20:44 +0300104 self.assertEqual(url.url_is(par), er)
105
Max Rasskazova8db7a92015-05-30 02:23:43 +0300106testdata = yaml.load(open('test_rsync_url.yaml'))
107
108index = 1
109for remote, tests in testdata.items():
110
111 for test, expected in tests.items():
112
Max Rasskazova8db7a92015-05-30 02:23:43 +0300113 def test_function(self, test=test,
114 remote=remote, expected_result=expected):
115 getattr(self, test)(remote, expected_result)
116
117 test_function.__name__ = \
Max Rasskazov64f77bf2015-06-02 15:22:48 +0300118 'test_rsync_url_{}_{}_{}'.format(index, tests['classed'], test)
Max Rasskazova8db7a92015-05-30 02:23:43 +0300119 test_function.__doc__ = test_function.__name__
120 setattr(TestRsyncUrl, test_function.__name__, test_function)
121 del test_function
122
123 index += 1
124
125
126if __name__ == '__main__':
127 unittest.main()