blob: 6eab3eb64b94e16baa2cb6f876b4f03c8a77abf3 [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 Rasskazov72929692015-06-16 17:39:07 +030066 def root(self, remote, expected_result):
67 logger.info('For "{}" should be {}'.format(remote, expected_result))
68 url = rsync_url.RsyncUrl(remote)
69 self.log_locals(url)
70 self.assertEqual(url.root, expected_result)
71
Max Rasskazovba34b142015-06-04 17:12:27 +030072 def urljoin(self, remote, expected_result):
Max Rasskazov84dd8672015-06-05 17:52:43 +030073 logger.info('For "{}" should be {}'.format(remote, expected_result))
Max Rasskazovba34b142015-06-04 17:12:27 +030074 url = rsync_url.RsyncUrl(remote)
75 self.log_locals(url)
76 for par, er in expected_result.items():
77 logger.info('par = "{}", er = "{}"'.format(par, er))
78 self.assertEqual(url.urljoin(par), er)
79
Max Rasskazov72929692015-06-16 17:39:07 +030080 def join(self, remote, expected_result):
81 logger.info('For "{}" should be {}'.format(remote, expected_result))
82 url = rsync_url.RsyncUrl(remote)
83 self.log_locals(url)
84 for par, er in expected_result.items():
85 logger.info('par = "{}", er = "{}"'.format(par, er))
86 self.assertEqual(url.join(par), er)
87
Max Rasskazov46cc0732015-06-05 19:23:24 +030088 def a_dir(self, remote, expected_result):
Max Rasskazov84dd8672015-06-05 17:52:43 +030089 logger.info('For "{}" should be {}'.format(remote, expected_result))
Max Rasskazovba34b142015-06-04 17:12:27 +030090 url = rsync_url.RsyncUrl(remote)
91 self.log_locals(url)
92 for par, er in expected_result.items():
93 logger.info('par = "{}", er = "{}"'.format(par, er))
Max Rasskazov46cc0732015-06-05 19:23:24 +030094 self.assertEqual(url.a_dir(par), er)
Max Rasskazovba34b142015-06-04 17:12:27 +030095
Max Rasskazovbe9a2772015-06-05 20:15:11 +030096 def url_dir(self, remote, expected_result):
Max Rasskazov84dd8672015-06-05 17:52:43 +030097 logger.info('For "{}" should be {}'.format(remote, expected_result))
Max Rasskazov2f93dd22015-06-04 14:20:44 +030098 url = rsync_url.RsyncUrl(remote)
99 self.log_locals(url)
100 for par, er in expected_result.items():
Max Rasskazovba34b142015-06-04 17:12:27 +0300101 logger.info('par = "{}", er = "{}"'.format(par, er))
Max Rasskazovbe9a2772015-06-05 20:15:11 +0300102 self.assertEqual(url.url_dir(par), er)
Max Rasskazov2f93dd22015-06-04 14:20:44 +0300103
Max Rasskazov46cc0732015-06-05 19:23:24 +0300104 def a_file(self, remote, expected_result):
Max Rasskazov84dd8672015-06-05 17:52:43 +0300105 logger.info('For "{}" should be {}'.format(remote, expected_result))
Max Rasskazovba34b142015-06-04 17:12:27 +0300106 url = rsync_url.RsyncUrl(remote)
107 self.log_locals(url)
108 for par, er in expected_result.items():
109 logger.info('par = "{}", er = "{}"'.format(par, er))
Max Rasskazov46cc0732015-06-05 19:23:24 +0300110 self.assertEqual(url.a_file(par), er)
Max Rasskazovba34b142015-06-04 17:12:27 +0300111
Max Rasskazovbe9a2772015-06-05 20:15:11 +0300112 def url_file(self, remote, expected_result):
Max Rasskazov84dd8672015-06-05 17:52:43 +0300113 logger.info('For "{}" should be {}'.format(remote, expected_result))
Max Rasskazov2f93dd22015-06-04 14:20:44 +0300114 url = rsync_url.RsyncUrl(remote)
115 self.log_locals(url)
116 for par, er in expected_result.items():
Max Rasskazovba34b142015-06-04 17:12:27 +0300117 logger.info('par = "{}", er = "{}"'.format(par, er))
Max Rasskazovbe9a2772015-06-05 20:15:11 +0300118 self.assertEqual(url.url_file(par), er)
Max Rasskazov2f93dd22015-06-04 14:20:44 +0300119
Max Rasskazova8db7a92015-05-30 02:23:43 +0300120testdata = yaml.load(open('test_rsync_url.yaml'))
121
122index = 1
123for remote, tests in testdata.items():
124
125 for test, expected in tests.items():
126
Max Rasskazova8db7a92015-05-30 02:23:43 +0300127 def test_function(self, test=test,
128 remote=remote, expected_result=expected):
129 getattr(self, test)(remote, expected_result)
130
131 test_function.__name__ = \
Max Rasskazov64f77bf2015-06-02 15:22:48 +0300132 'test_rsync_url_{}_{}_{}'.format(index, tests['classed'], test)
Max Rasskazova8db7a92015-05-30 02:23:43 +0300133 test_function.__doc__ = test_function.__name__
134 setattr(TestRsyncUrl, test_function.__name__, test_function)
135 del test_function
136
137 index += 1
138
139
140if __name__ == '__main__':
141 unittest.main()