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: |
| 17 | logger.info('RE: "{}"'.format(url.match.pattern)) |
| 18 | logger.info('user "{}", host "{}", port "{}", module "{}", ' |
| 19 | 'path "{}"'.format(url.user, url.host, url.port, |
| 20 | url.module, url.path)) |
| 21 | |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 22 | def exact_match_num(self, remote, expected_result): |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 23 | logger.info('"{}" - {}'.format(remote, expected_result)) |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 24 | url = rsync_url.RsyncUrl(remote) |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 25 | self.log_locals(url) |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 26 | matching_regexps = url._get_all_matching_regexps() |
| 27 | self.assertEqual(len(matching_regexps), expected_result) |
| 28 | |
| 29 | def classed(self, remote, expected_result): |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 30 | logger.info('"{}" - {}'.format(remote, expected_result)) |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 31 | url = rsync_url.RsyncUrl(remote) |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 32 | self.log_locals(url) |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 33 | self.assertEqual(url.url_type, expected_result) |
| 34 | |
| 35 | def parsed(self, remote, expected_result): |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 36 | logger.info('"{}" - {}'.format(remote, expected_result)) |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 37 | url = rsync_url.RsyncUrl(remote) |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 38 | self.log_locals(url) |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 39 | self.assertEqual( |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 40 | [url.user, url.host, url.path], |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 41 | expected_result |
| 42 | ) |
| 43 | |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 44 | def parsed_rsync(self, remote, expected_result): |
| 45 | logger.info('"{}" - {}'.format(remote, expected_result)) |
| 46 | url = rsync_url.RsyncUrl(remote) |
| 47 | self.log_locals(url) |
| 48 | self.assertEqual( |
| 49 | [url.user, url.host, url.port, url.module, url.path], |
| 50 | expected_result |
| 51 | ) |
| 52 | |
| 53 | def valid(self, remote, expected_result): |
| 54 | logger.info('"{}" - {}'.format(remote, expected_result)) |
| 55 | url = rsync_url.RsyncUrl(remote) |
| 56 | self.log_locals(url) |
| 57 | self.assertEqual(url.is_valid, expected_result) |
| 58 | |
| 59 | def url(self, remote, expected_result): |
| 60 | logger.info('"{}" - {}'.format(remote, expected_result)) |
| 61 | url = rsync_url.RsyncUrl(remote) |
| 62 | self.log_locals(url) |
| 63 | self.assertEqual(url.url, expected_result) |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 64 | |
| 65 | testdata = yaml.load(open('test_rsync_url.yaml')) |
| 66 | |
| 67 | index = 1 |
| 68 | for remote, tests in testdata.items(): |
| 69 | |
| 70 | for test, expected in tests.items(): |
| 71 | |
| 72 | print index, test, expected |
| 73 | |
| 74 | def test_function(self, test=test, |
| 75 | remote=remote, expected_result=expected): |
| 76 | getattr(self, test)(remote, expected_result) |
| 77 | |
| 78 | test_function.__name__ = \ |
Max Rasskazov | 64f77bf | 2015-06-02 15:22:48 +0300 | [diff] [blame] | 79 | 'test_rsync_url_{}_{}_{}'.format(index, tests['classed'], test) |
Max Rasskazov | a8db7a9 | 2015-05-30 02:23:43 +0300 | [diff] [blame] | 80 | test_function.__doc__ = test_function.__name__ |
| 81 | setattr(TestRsyncUrl, test_function.__name__, test_function) |
| 82 | del test_function |
| 83 | |
| 84 | index += 1 |
| 85 | |
| 86 | |
| 87 | if __name__ == '__main__': |
| 88 | unittest.main() |