Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright (c) 2016 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """Unit tests for frontend/afe/rpc_utils.py.""" |
| 8 | |
| 9 | |
| 10 | import unittest |
| 11 | |
| 12 | import common |
| 13 | from autotest_lib.client.common_lib import control_data |
| 14 | from autotest_lib.frontend import setup_django_environment |
| 15 | from autotest_lib.frontend.afe import frontend_test_utils |
| 16 | from autotest_lib.frontend.afe import rpc_utils |
| 17 | |
| 18 | |
| 19 | class RpcUtilsTest(unittest.TestCase, |
| 20 | frontend_test_utils.FrontendTestMixin): |
| 21 | """Unit tests for functions in rpc_utils.py.""" |
| 22 | def setUp(self): |
| 23 | self._frontend_common_setup() |
| 24 | |
| 25 | |
| 26 | def tearDown(self): |
| 27 | self._frontend_common_teardown() |
| 28 | |
| 29 | |
| 30 | def testCheckIsServer(self): |
| 31 | """Ensure that test type check is correct.""" |
| 32 | self.assertFalse(rpc_utils._check_is_server_test(None)) |
| 33 | self.assertFalse(rpc_utils._check_is_server_test( |
| 34 | control_data.CONTROL_TYPE.CLIENT)) |
| 35 | self.assertFalse(rpc_utils._check_is_server_test('Client')) |
| 36 | self.assertTrue(rpc_utils._check_is_server_test( |
| 37 | control_data.CONTROL_TYPE.SERVER)) |
| 38 | self.assertTrue(rpc_utils._check_is_server_test('Server')) |
| 39 | self.assertFalse(rpc_utils._check_is_server_test('InvalidType')) |
| 40 | |
| 41 | |
Allen Li | ab8d379 | 2016-12-12 18:00:31 -0800 | [diff] [blame] | 42 | class ConvertToKwargsOnlyTest(unittest.TestCase): |
| 43 | """Unit tests for _convert_to_kwargs_only().""" |
| 44 | |
| 45 | # pylint: disable=unused-argument,missing-docstring |
| 46 | |
| 47 | def test_no_kwargs_in_spec(self): |
| 48 | """Test with function without kwargs.""" |
| 49 | def func(a, b): |
| 50 | pass |
| 51 | got = rpc_utils._convert_to_kwargs_only(func, (1, 2), {}) |
| 52 | self.assertEquals(got, {'a': 1, 'b': 2}) |
| 53 | |
| 54 | def test_pass_by_keyword(self): |
| 55 | """Test passing required args by keyword.""" |
| 56 | def func(a, b): |
| 57 | pass |
| 58 | got = rpc_utils._convert_to_kwargs_only(func, (), {'a': 1, 'b': 2}) |
| 59 | self.assertEquals(got, {'a': 1, 'b': 2}) |
| 60 | |
| 61 | def test_with_kwargs(self): |
| 62 | """Test with custom keyword arg.""" |
| 63 | def func(a, b, **kwargs): |
| 64 | pass |
| 65 | got = rpc_utils._convert_to_kwargs_only(func, (1, 2), {'c': 3}) |
| 66 | self.assertEquals(got, {'a': 1, 'b': 2, 'c': 3}) |
| 67 | |
| 68 | def test_with_kwargs_pass_by_keyword(self): |
| 69 | """Test passing required parameter by keyword.""" |
| 70 | def func(a, b, **kwargs): |
| 71 | pass |
| 72 | got = rpc_utils._convert_to_kwargs_only(func, (1,), {'b': 2, 'c': 3}) |
| 73 | self.assertEquals(got, {'a': 1, 'b': 2, 'c': 3}) |
| 74 | |
| 75 | def test_empty_kwargs(self): |
| 76 | """Test without passing kwargs.""" |
| 77 | def func(a, b, **kwargs): |
| 78 | pass |
| 79 | got = rpc_utils._convert_to_kwargs_only(func, (1, 2), {}) |
| 80 | self.assertEquals(got, {'a': 1, 'b': 2}) |
| 81 | |
| 82 | def test_with_varargs(self): |
| 83 | """Test against vararg function.""" |
| 84 | def func(a, b, *args): |
| 85 | pass |
| 86 | got = rpc_utils._convert_to_kwargs_only(func, (1, 2, 3), {}) |
| 87 | self.assertEquals(got, {'a': 1, 'b': 2, 'args': (3,)}) |
| 88 | |
| 89 | |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame] | 90 | if __name__ == '__main__': |
| 91 | unittest.main() |