|
| 1 | +## |
| 2 | +# Copyright 2012-2021 Ghent University |
| 3 | +# |
| 4 | +# This file is part of EasyBuild, |
| 5 | +# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en), |
| 6 | +# with support of Ghent University (http://ugent.be/hpc), |
| 7 | +# the Flemish Supercomputer Centre (VSC) (https://www.vscentrum.be), |
| 8 | +# Flemish Research Foundation (FWO) (http://www.fwo.be/en) |
| 9 | +# and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en). |
| 10 | +# |
| 11 | +# https://github.com/easybuilders/easybuild |
| 12 | +# |
| 13 | +# EasyBuild is free software: you can redistribute it and/or modify |
| 14 | +# it under the terms of the GNU General Public License as published by |
| 15 | +# the Free Software Foundation v2. |
| 16 | +# |
| 17 | +# EasyBuild is distributed in the hope that it will be useful, |
| 18 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | +# GNU General Public License for more details. |
| 21 | +# |
| 22 | +# You should have received a copy of the GNU General Public License |
| 23 | +# along with EasyBuild. If not, see <http://www.gnu.org/licenses/>. |
| 24 | +## |
| 25 | +""" |
| 26 | +Unit tests for utilities.py |
| 27 | +
|
| 28 | +@author: Jens Timmerman (Ghent University) |
| 29 | +@author: Kenneth Hoste (Ghent University) |
| 30 | +@author: Alexander Grund (TU Dresden) |
| 31 | +""" |
| 32 | +import os |
| 33 | +import random |
| 34 | +import sys |
| 35 | +import tempfile |
| 36 | +from datetime import datetime |
| 37 | +from unittest import TextTestRunner |
| 38 | + |
| 39 | +from test.framework.utilities import EnhancedTestCase, TestLoaderFiltered |
| 40 | +from easybuild.tools.build_log import EasyBuildError |
| 41 | +from easybuild.tools.utilities import time2str, natural_keys |
| 42 | + |
| 43 | + |
| 44 | +class UtilitiesTest(EnhancedTestCase): |
| 45 | + """Class for utilities testcases """ |
| 46 | + |
| 47 | + def setUp(self): |
| 48 | + """ setup """ |
| 49 | + super(UtilitiesTest, self).setUp() |
| 50 | + |
| 51 | + self.test_tmp_logdir = tempfile.mkdtemp() |
| 52 | + os.environ['EASYBUILD_TMP_LOGDIR'] = self.test_tmp_logdir |
| 53 | + |
| 54 | + def test_time2str(self): |
| 55 | + """Test time2str function.""" |
| 56 | + |
| 57 | + start = datetime(2019, 7, 30, 5, 14, 23) |
| 58 | + |
| 59 | + test_cases = [ |
| 60 | + (start, "0 sec"), |
| 61 | + (datetime(2019, 7, 30, 5, 14, 37), "14 sec"), |
| 62 | + (datetime(2019, 7, 30, 5, 15, 22), "59 sec"), |
| 63 | + (datetime(2019, 7, 30, 5, 15, 23), "1 min 0 sec"), |
| 64 | + (datetime(2019, 7, 30, 5, 16, 22), "1 min 59 sec"), |
| 65 | + (datetime(2019, 7, 30, 5, 37, 26), "23 min 3 sec"), |
| 66 | + (datetime(2019, 7, 30, 6, 14, 22), "59 min 59 sec"), |
| 67 | + (datetime(2019, 7, 30, 6, 14, 23), "1 hour 0 min 0 sec"), |
| 68 | + (datetime(2019, 7, 30, 6, 49, 14), "1 hour 34 min 51 sec"), |
| 69 | + (datetime(2019, 7, 30, 7, 14, 23), "2 hours 0 min 0 sec"), |
| 70 | + (datetime(2019, 7, 30, 8, 35, 59), "3 hours 21 min 36 sec"), |
| 71 | + (datetime(2019, 7, 30, 16, 29, 24), "11 hours 15 min 1 sec"), |
| 72 | + (datetime(2019, 7, 31, 5, 14, 22), "23 hours 59 min 59 sec"), |
| 73 | + (datetime(2019, 7, 31, 5, 14, 23), "24 hours 0 min 0 sec"), |
| 74 | + (datetime(2019, 8, 5, 20, 39, 44), "159 hours 25 min 21 sec"), |
| 75 | + ] |
| 76 | + for end, expected in test_cases: |
| 77 | + self.assertEqual(time2str(end - start), expected) |
| 78 | + |
| 79 | + error_pattern = "Incorrect value type provided to time2str, should be datetime.timedelta: <.* 'int'>" |
| 80 | + self.assertErrorRegex(EasyBuildError, error_pattern, time2str, 123) |
| 81 | + |
| 82 | + def test_natural_keys(self): |
| 83 | + """Test the natural_keys function""" |
| 84 | + sorted_items = [ |
| 85 | + 'ACoolSw-1.0', |
| 86 | + 'ACoolSw-2.1', |
| 87 | + 'ACoolSw-11.0', |
| 88 | + 'ACoolSw-23.0', |
| 89 | + 'ACoolSw-30.0', |
| 90 | + 'ACoolSw-30.1', |
| 91 | + 'BigNumber-1234567890', |
| 92 | + 'BigNumber-1234567891', |
| 93 | + 'NoNumbers', |
| 94 | + 'VeryLastEntry-10' |
| 95 | + ] |
| 96 | + shuffled_items = sorted_items[:] |
| 97 | + random.shuffle(shuffled_items) |
| 98 | + shuffled_items.sort(key=natural_keys) |
| 99 | + self.assertEqual(shuffled_items, sorted_items) |
| 100 | + |
| 101 | + |
| 102 | +def suite(): |
| 103 | + """ return all the tests in this file """ |
| 104 | + return TestLoaderFiltered().loadTestsFromTestCase(UtilitiesTest, sys.argv[1:]) |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == '__main__': |
| 108 | + res = TextTestRunner(verbosity=1).run(suite()) |
| 109 | + sys.exit(len(res.failures)) |
0 commit comments