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 | ba34b14 | 2015-06-04 17:12:27 +0300 | [diff] [blame] | 66 | def urljoin(self, remote, expected_result): |
Max Rasskazov | 84dd867 | 2015-06-05 17:52:43 +0300 | [diff] [blame] | 67 | logger.info('For "{}" should be {}'.format(remote, expected_result)) |
Max Rasskazov | ba34b14 | 2015-06-04 17:12:27 +0300 | [diff] [blame] | 68 | 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 Rasskazov | 46cc073 | 2015-06-05 19:23:24 +0300 | [diff] [blame^] | 74 | def a_dir(self, remote, expected_result): |
Max Rasskazov | 84dd867 | 2015-06-05 17:52:43 +0300 | [diff] [blame] | 75 | logger.info('For "{}" should be {}'.format(remote, expected_result)) |
Max Rasskazov | ba34b14 | 2015-06-04 17:12:27 +0300 | [diff] [blame] | 76 | 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 Rasskazov | 46cc073 | 2015-06-05 19:23:24 +0300 | [diff] [blame^] | 80 | self.assertEqual(url.a_dir(par), er) |
Max Rasskazov | ba34b14 | 2015-06-04 17:12:27 +0300 | [diff] [blame] | 81 | |
Max Rasskazov | 2f93dd2 | 2015-06-04 14:20:44 +0300 | [diff] [blame] | 82 | def url_in(self, remote, expected_result): |
Max Rasskazov | 84dd867 | 2015-06-05 17:52:43 +0300 | [diff] [blame] | 83 | logger.info('For "{}" should be {}'.format(remote, expected_result)) |
Max Rasskazov | 2f93dd2 | 2015-06-04 14:20:44 +0300 | [diff] [blame] | 84 | url = rsync_url.RsyncUrl(remote) |
| 85 | self.log_locals(url) |
| 86 | for par, er in expected_result.items(): |
Max Rasskazov | ba34b14 | 2015-06-04 17:12:27 +0300 | [diff] [blame] | 87 | logger.info('par = "{}", er = "{}"'.format(par, er)) |
Max Rasskazov | 2f93dd2 | 2015-06-04 14:20:44 +0300 | [diff] [blame] | 88 | self.assertEqual(url.url_in(par), er) |
| 89 | |
Max Rasskazov | 46cc073 | 2015-06-05 19:23:24 +0300 | [diff] [blame^] | 90 | def a_file(self, remote, expected_result): |
Max Rasskazov | 84dd867 | 2015-06-05 17:52:43 +0300 | [diff] [blame] | 91 | logger.info('For "{}" should be {}'.format(remote, expected_result)) |
Max Rasskazov | ba34b14 | 2015-06-04 17:12:27 +0300 | [diff] [blame] | 92 | 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 Rasskazov | 46cc073 | 2015-06-05 19:23:24 +0300 | [diff] [blame^] | 96 | self.assertEqual(url.a_file(par), er) |
Max Rasskazov | ba34b14 | 2015-06-04 17:12:27 +0300 | [diff] [blame] | 97 | |
Max Rasskazov | 2f93dd2 | 2015-06-04 14:20:44 +0300 | [diff] [blame] | 98 | def url_is(self, remote, expected_result): |
Max Rasskazov | 84dd867 | 2015-06-05 17:52:43 +0300 | [diff] [blame] | 99 | logger.info('For "{}" should be {}'.format(remote, expected_result)) |
Max Rasskazov | 2f93dd2 | 2015-06-04 14:20:44 +0300 | [diff] [blame] | 100 | url = rsync_url.RsyncUrl(remote) |
| 101 | self.log_locals(url) |
| 102 | for par, er in expected_result.items(): |
Max Rasskazov | ba34b14 | 2015-06-04 17:12:27 +0300 | [diff] [blame] | 103 | logger.info('par = "{}", er = "{}"'.format(par, er)) |
Max Rasskazov | 2f93dd2 | 2015-06-04 14:20:44 +0300 | [diff] [blame] | 104 | self.assertEqual(url.url_is(par), er) |
| 105 | |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 106 | testdata = yaml.load(open('test_rsync_url.yaml')) |
| 107 | |
| 108 | index = 1 |
| 109 | for remote, tests in testdata.items(): |
| 110 | |
| 111 | for test, expected in tests.items(): |
| 112 | |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 113 | 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 Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 118 | 'test_rsync_url_{}_{}_{}'.format(index, tests['classed'], test) |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 119 | test_function.__doc__ = test_function.__name__ |
| 120 | setattr(TestRsyncUrl, test_function.__name__, test_function) |
| 121 | del test_function |
| 122 | |
| 123 | index += 1 |
| 124 | |
| 125 | |
| 126 | if __name__ == '__main__': |
| 127 | unittest.main() |