|
| 1 | +# Copyright (c) 2015 Intel Corporation |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | +# Borrowed from Ironic |
| 15 | + |
| 16 | +""" |
| 17 | +Tests for the versions constants and methods. |
| 18 | +""" |
| 19 | + |
| 20 | +import re |
| 21 | + |
| 22 | +from esi_leap.api.controllers.v1 import versions |
| 23 | +from esi_leap.tests import base |
| 24 | + |
| 25 | + |
| 26 | +class TestVersionConstants(base.TestCase): |
| 27 | + def setUp(self): |
| 28 | + super(TestVersionConstants, self).setUp() |
| 29 | + |
| 30 | + # Get all of our named constants. They all begin with r'MINOR_[0-9]' |
| 31 | + self.minor_consts = [x for x in dir(versions) if re.search(r"^MINOR_[0-9]", x)] |
| 32 | + |
| 33 | + # Sort key needs to be an integer |
| 34 | + def minor_key(x): |
| 35 | + return int(x.split("_", 2)[1]) |
| 36 | + |
| 37 | + self.minor_consts.sort(key=minor_key) |
| 38 | + |
| 39 | + def test_max_ver_str(self): |
| 40 | + # Test to make sure _MAX_VERSION_STRING corresponds with the largest |
| 41 | + # MINOR_ constant |
| 42 | + |
| 43 | + max_ver = "1.{}".format(getattr(versions, self.minor_consts[-1])) |
| 44 | + self.assertEqual(max_ver, versions._MAX_VERSION_STRING) |
| 45 | + |
| 46 | + def test_min_ver_str(self): |
| 47 | + # Try to make sure someone doesn't change the _MIN_VERSION_STRING by |
| 48 | + # accident and make sure it exists |
| 49 | + self.assertEqual("1.0", versions._MIN_VERSION_STRING) |
| 50 | + |
| 51 | + def test_name_value_match(self): |
| 52 | + # Test to make sure variable name matches the value. For example |
| 53 | + # MINOR_99_FOO should equal 99 |
| 54 | + |
| 55 | + for var_name in self.minor_consts: |
| 56 | + version = int(var_name.split("_", 2)[1]) |
| 57 | + self.assertEqual( |
| 58 | + version, |
| 59 | + getattr(versions, var_name), |
| 60 | + 'Constant "{}" does not equal {}'.format(var_name, version), |
| 61 | + ) |
| 62 | + |
| 63 | + def test_duplicates(self): |
| 64 | + # Test to make sure no duplicates values |
| 65 | + |
| 66 | + seen_values = set() |
| 67 | + for var_name in self.minor_consts: |
| 68 | + value = getattr(versions, var_name) |
| 69 | + self.assertNotIn( |
| 70 | + value, |
| 71 | + seen_values, |
| 72 | + "The value {} has been used more than once".format(value), |
| 73 | + ) |
| 74 | + seen_values.add(value) |
| 75 | + |
| 76 | + |
| 77 | +class TestMaxVersionString(base.TestCase): |
| 78 | + def test_max_version_not_pinned(self): |
| 79 | + self.assertEqual(versions._MAX_VERSION_STRING, versions.max_version_string()) |
0 commit comments