Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 1 | #-*- coding: utf-8 -*- |
| 2 | |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 3 | import unittest |
| 4 | import yaml |
| 5 | |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 6 | import rsync_url |
Max Rasskazov | b1b09a6 | 2015-06-03 15:21:35 +0300 | [diff] [blame] | 7 | import utils |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 8 | |
| 9 | |
Max Rasskazov | b1b09a6 | 2015-06-03 15:21:35 +0300 | [diff] [blame] | 10 | logger = utils.logger.getChild('TestRsyncUrl') |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 11 | |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 12 | |
| 13 | class TestRsyncUrl(unittest.TestCase): |
| 14 | |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 15 | def log_locals(self, url): |
| 16 | if url.match: |
Max Rasskazov | 84dd867 | 2015-06-05 17:52:43 +0300 | [diff] [blame] | 17 | logger.info('Type: {}, RE: "{}"' |
| 18 | ''.format(url.url_type, url.match.pattern)) |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 19 | logger.info('user "{}", host "{}", port "{}", module "{}", ' |
| 20 | 'path "{}"'.format(url.user, url.host, url.port, |
| 21 | url.module, url.path)) |
| 22 | |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 23 | def exact_match_num(self, remote, expected_result): |
Max Rasskazov | 84dd867 | 2015-06-05 17:52:43 +0300 | [diff] [blame] | 24 | logger.info('For "{}" should be {}'.format(remote, expected_result)) |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 25 | url = rsync_url.RsyncUrl(remote) |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 26 | self.log_locals(url) |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 27 | matching_regexps = url._get_all_matching_regexps() |
| 28 | self.assertEqual(len(matching_regexps), expected_result) |
| 29 | |
| 30 | def classed(self, remote, expected_result): |
Max Rasskazov | 84dd867 | 2015-06-05 17:52:43 +0300 | [diff] [blame] | 31 | logger.info('For "{}" should be {}'.format(remote, expected_result)) |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 32 | url = rsync_url.RsyncUrl(remote) |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 33 | self.log_locals(url) |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 34 | self.assertEqual(url.url_type, expected_result) |
| 35 | |
| 36 | def parsed(self, remote, expected_result): |
Max Rasskazov | 84dd867 | 2015-06-05 17:52:43 +0300 | [diff] [blame] | 37 | logger.info('For "{}" should be {}'.format(remote, expected_result)) |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 38 | url = rsync_url.RsyncUrl(remote) |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 39 | self.log_locals(url) |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 40 | self.assertEqual( |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 41 | [url.user, url.host, url.path], |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 42 | expected_result |
| 43 | ) |
| 44 | |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 45 | def parsed_rsync(self, remote, expected_result): |
Max Rasskazov | 84dd867 | 2015-06-05 17:52:43 +0300 | [diff] [blame] | 46 | logger.info('For "{}" should be {}'.format(remote, expected_result)) |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 47 | 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 Rasskazov | 84dd867 | 2015-06-05 17:52:43 +0300 | [diff] [blame] | 55 | logger.info('For "{}" should be {}'.format(remote, expected_result)) |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 56 | 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 Rasskazov | 84dd867 | 2015-06-05 17:52:43 +0300 | [diff] [blame] | 61 | logger.info('For "{}" should be {}'.format(remote, expected_result)) |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 62 | url = rsync_url.RsyncUrl(remote) |
| 63 | self.log_locals(url) |
| 64 | self.assertEqual(url.url, expected_result) |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 65 | |
Max Rasskazov | 7292969 | 2015-06-16 17:39:07 +0300 | [diff] [blame] | 66 | 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 Rasskazov | ba34b14 | 2015-06-04 17:12:27 +0300 | [diff] [blame] | 72 | def urljoin(self, remote, expected_result): |
Max Rasskazov | 84dd867 | 2015-06-05 17:52:43 +0300 | [diff] [blame] | 73 | logger.info('For "{}" should be {}'.format(remote, expected_result)) |
Max Rasskazov | ba34b14 | 2015-06-04 17:12:27 +0300 | [diff] [blame] | 74 | 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 Rasskazov | 7292969 | 2015-06-16 17:39:07 +0300 | [diff] [blame] | 80 | 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 Rasskazov | 46cc073 | 2015-06-05 19:23:24 +0300 | [diff] [blame] | 88 | def a_dir(self, remote, expected_result): |
Max Rasskazov | 84dd867 | 2015-06-05 17:52:43 +0300 | [diff] [blame] | 89 | logger.info('For "{}" should be {}'.format(remote, expected_result)) |
Max Rasskazov | ba34b14 | 2015-06-04 17:12:27 +0300 | [diff] [blame] | 90 | 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 Rasskazov | 46cc073 | 2015-06-05 19:23:24 +0300 | [diff] [blame] | 94 | self.assertEqual(url.a_dir(par), er) |
Max Rasskazov | ba34b14 | 2015-06-04 17:12:27 +0300 | [diff] [blame] | 95 | |
Max Rasskazov | be9a277 | 2015-06-05 20:15:11 +0300 | [diff] [blame] | 96 | def url_dir(self, remote, expected_result): |
Max Rasskazov | 84dd867 | 2015-06-05 17:52:43 +0300 | [diff] [blame] | 97 | logger.info('For "{}" should be {}'.format(remote, expected_result)) |
Max Rasskazov | 2f93dd2 | 2015-06-04 14:20:44 +0300 | [diff] [blame] | 98 | url = rsync_url.RsyncUrl(remote) |
| 99 | self.log_locals(url) |
| 100 | for par, er in expected_result.items(): |
Max Rasskazov | ba34b14 | 2015-06-04 17:12:27 +0300 | [diff] [blame] | 101 | logger.info('par = "{}", er = "{}"'.format(par, er)) |
Max Rasskazov | be9a277 | 2015-06-05 20:15:11 +0300 | [diff] [blame] | 102 | self.assertEqual(url.url_dir(par), er) |
Max Rasskazov | 2f93dd2 | 2015-06-04 14:20:44 +0300 | [diff] [blame] | 103 | |
Max Rasskazov | 46cc073 | 2015-06-05 19:23:24 +0300 | [diff] [blame] | 104 | def a_file(self, remote, expected_result): |
Max Rasskazov | 84dd867 | 2015-06-05 17:52:43 +0300 | [diff] [blame] | 105 | logger.info('For "{}" should be {}'.format(remote, expected_result)) |
Max Rasskazov | ba34b14 | 2015-06-04 17:12:27 +0300 | [diff] [blame] | 106 | 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 Rasskazov | 46cc073 | 2015-06-05 19:23:24 +0300 | [diff] [blame] | 110 | self.assertEqual(url.a_file(par), er) |
Max Rasskazov | ba34b14 | 2015-06-04 17:12:27 +0300 | [diff] [blame] | 111 | |
Max Rasskazov | be9a277 | 2015-06-05 20:15:11 +0300 | [diff] [blame] | 112 | def url_file(self, remote, expected_result): |
Max Rasskazov | 84dd867 | 2015-06-05 17:52:43 +0300 | [diff] [blame] | 113 | logger.info('For "{}" should be {}'.format(remote, expected_result)) |
Max Rasskazov | 2f93dd2 | 2015-06-04 14:20:44 +0300 | [diff] [blame] | 114 | url = rsync_url.RsyncUrl(remote) |
| 115 | self.log_locals(url) |
| 116 | for par, er in expected_result.items(): |
Max Rasskazov | ba34b14 | 2015-06-04 17:12:27 +0300 | [diff] [blame] | 117 | logger.info('par = "{}", er = "{}"'.format(par, er)) |
Max Rasskazov | be9a277 | 2015-06-05 20:15:11 +0300 | [diff] [blame] | 118 | self.assertEqual(url.url_file(par), er) |
Max Rasskazov | 2f93dd2 | 2015-06-04 14:20:44 +0300 | [diff] [blame] | 119 | |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 120 | testdata = yaml.load(open('test_rsync_url.yaml')) |
| 121 | |
| 122 | index = 1 |
| 123 | for remote, tests in testdata.items(): |
| 124 | |
| 125 | for test, expected in tests.items(): |
| 126 | |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 127 | 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 Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 132 | 'test_rsync_url_{}_{}_{}'.format(index, tests['classed'], test) |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 133 | test_function.__doc__ = test_function.__name__ |
| 134 | setattr(TestRsyncUrl, test_function.__name__, test_function) |
| 135 | del test_function |
| 136 | |
| 137 | index += 1 |
| 138 | |
| 139 | |
| 140 | if __name__ == '__main__': |
| 141 | unittest.main() |