aboutsummaryrefslogtreecommitdiff
path: root/catapult/common/py_utils/py_utils/modules_util_unittest.py
blob: aa056740a2ce138f80f06bad234422290035a1c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Copyright 2019 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import unittest

from py_utils import modules_util


class FakeModule(object):
  def __init__(self, name, version):
    self.__name__ = name
    self.__version__ = version


class ModulesUitlTest(unittest.TestCase):
  def testRequireVersion_valid(self):
    numpy = FakeModule('numpy', '2.3')
    try:
      modules_util.RequireVersion(numpy, '1.0')
    except ImportError:
      self.fail('ImportError raised unexpectedly')

  def testRequireVersion_versionTooLow(self):
    numpy = FakeModule('numpy', '2.3')
    with self.assertRaises(ImportError) as error:
      modules_util.RequireVersion(numpy, '2.5')
    self.assertEqual(
        str(error.exception),
        'numpy has version 2.3, but version 2.5 or higher is required')

  def testRequireVersion_versionTooHigh(self):
    numpy = FakeModule('numpy', '2.3')
    with self.assertRaises(ImportError) as error:
      modules_util.RequireVersion(numpy, '1.0', '2.0')
    self.assertEqual(
        str(error.exception), 'numpy has version 2.3, but version'
        ' at or above 1.0 and below 2.0 is required')


if __name__ == '__main__':
  unittest.main()