Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from __future__ import with_statement 

2import unittest 

3import numpy as np 

4import lsst.utils.tests 

5from lsst.sims.utils.CodeUtilities import _validate_inputs 

6 

7 

8def setup_module(module): 

9 lsst.utils.tests.init() 

10 

11 

12class CodeUtilsTest(unittest.TestCase): 

13 

14 def test_validate_inputs(self): 

15 """ 

16 test that _validate_inputs returns expected values 

17 """ 

18 

19 # test that it correctly identifies numpy array inputs 

20 self.assertTrue(_validate_inputs([np.array([1, 2]), 

21 np.array([3, 4])], 

22 ['a', 'b'], 

23 'dummy_method')) 

24 

25 # test that it correctly identifies inputs that are numbers 

26 self.assertFalse(_validate_inputs([1, 2, 3], ['a', 'b', 'c'], 'dummy')) 

27 

28 # test that the correct exception is raised when you pass in 

29 # a number a numpy array 

30 with self.assertRaises(RuntimeError) as ee: 

31 _validate_inputs([1, np.array([2, 3])], ['a', 'b'], 'dummy') 

32 

33 self.assertIn("and the same type", ee.exception.args[0]) 

34 

35 # test that the correct exception is raised when you pass in 

36 # numpy arrays of different length 

37 with self.assertRaises(RuntimeError) as ee: 

38 _validate_inputs([np.array([1, 2]), np.array([1, 2, 3])], 

39 ['a', 'b'], 'dummy') 

40 

41 self.assertIn("same length", ee.exception.args[0]) 

42 

43 # test that an exception is raised if lists (rather than numpy 

44 # arrays) are passed in 

45 with self.assertRaises(RuntimeError) as ee: 

46 _validate_inputs([[1, 2], [3, 4]], ['a', 'b'], 'dummy') 

47 

48 self.assertIn("either a number or a numpy array", 

49 ee.exception.args[0]) 

50 

51 

52class MemoryTestClass(lsst.utils.tests.MemoryTestCase): 

53 pass 

54 

55if __name__ == "__main__": 55 ↛ 56line 55 didn't jump to line 56, because the condition on line 55 was never true

56 lsst.utils.tests.init() 

57 unittest.main()