From 27174b13cdc803e3d0b085924504b1128ad0cf2f Mon Sep 17 00:00:00 2001 From: Jiri Podivin Date: Fri, 19 Mar 2021 11:19:29 +0100 Subject: [PATCH] Tests for validation.py module and helper classes Also groundwork for tests of Validation class. Closes-bug: #1922726 Signed-off-by: Jiri Podivin Change-Id: Ic66b67a8376ace4dd7d03c4dc6ff2a7e8ec164f6 --- test-requirements.txt | 1 + validations_common/tests/test_validation.py | 126 ++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 validations_common/tests/test_validation.py diff --git a/test-requirements.txt b/test-requirements.txt index 66526ab..b4c85b4 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -12,3 +12,4 @@ testtools>=2.2.0 # MIT reno>=2.5.0 # Apache-2.0 netaddr>=0.7.18 # BSD pre-commit # MIT +validations-libs>=1.0.4 # Apache-2.0 diff --git a/validations_common/tests/test_validation.py b/validations_common/tests/test_validation.py new file mode 100644 index 0000000..7f784a6 --- /dev/null +++ b/validations_common/tests/test_validation.py @@ -0,0 +1,126 @@ +# -*- coding: utf-8 -*- + +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +""" +test_validation +---------------------------------- + +Tests for `validation` sub module. + +""" +import argparse + +from unittest import mock + +from validations_common import validation +from validations_common.tests import base + + +class TestValidationModule(base.TestCase): + + def setUp(self): + self.module = mock.MagicMock() + + super(TestValidationModule, self).setUp() + + def test_module_init(self): + """Check for presence of required module attributes. + Including the color constants needed for output formatting. + """ + required_attributes = [ + 'DESCRIPTION', + 'EPILOG', + 'RED', + 'GREEN', + 'CYAN', + 'RESET' + ] + + module_attributes = set(dir(validation)) + + self.assertTrue(set(required_attributes).issubset(module_attributes)) + + +class TestCommaListGroupAction(base.TestCase): + + def setUp(self): + super(TestCommaListGroupAction, self).setUp() + + @mock.patch('validations_common.validation.setattr') + def test_comma_list_group_action(self, mock_setattr): + """As there is not much to test in this class + the assertions are made on attributes and inheritance. + """ + action = validation._CommaListGroupAction( + dest='foo', + option_strings='') + + action('foo', 'bar', 'fizz,buzz') + + self.assertEqual(type(action).__mro__[1], argparse.Action) + self.assertEqual(action.dest, 'foo') + self.assertEqual(action.option_strings, '') + mock_setattr.assert_called_once_with('bar', 'foo', ['fizz', 'buzz']) + + +class TestCommaListAction(base.TestCase): + + def setUp(self): + super(TestCommaListAction, self).setUp() + + @mock.patch('validations_common.validation.setattr') + def test_comma_list_action(self, mock_setattr): + """As there is not much to test in this class + the assertions are made on attributes and inheritance. + """ + action = validation._CommaListAction( + dest='foo', + option_strings='') + + action('foo', 'bar', 'fizz,buzz') + + self.assertEqual(type(action).__mro__[1], argparse.Action) + self.assertEqual(action.dest, 'foo') + self.assertEqual(action.option_strings, '') + mock_setattr.assert_called_once_with('bar', 'foo', ['fizz', 'buzz']) + + +class TestValidation(base.TestCase): + + def setUp(self): + super(TestValidation, self).setUp() + + def test_validation_init(self): + pass + + def test_validation_parser(self): + pass + + def test_validation_print_dict_table(self): + pass + + def test_validation_print_tuple_table(self): + pass + + def test_validation_write_output(self): + pass + + def test_validation_take_action_run(self): + pass + + def test_validation_take_action_list(self): + pass + + def test_validation_take_action_show(self): + pass