Coverage for gpkit\tests\t_small.py: 0%

52 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 16:57 -0500

1"""Tests for small_classes.py and small_scripts.py""" 

2import unittest 

3from gpkit.small_classes import HashVector 

4from gpkit.repr_conventions import unitstr 

5import gpkit 

6 

7 

8class TestHashVector(unittest.TestCase): 

9 """TestCase for the HashVector class""" 

10 

11 def test_init(self): 

12 """Make sure HashVector acts like a dict""" 

13 # args and kwargs 

14 hv = HashVector([(2, 3), (1, 10)], dog='woof') 

15 self.assertTrue(isinstance(hv, dict)) 

16 self.assertEqual(hv, {2: 3, 1: 10, 'dog': 'woof'}) 

17 # no args 

18 self.assertEqual(HashVector(), {}) 

19 # creation from dict 

20 self.assertEqual(HashVector({'x': 7}), {'x': 7}) 

21 

22 def test_neg(self): 

23 """Test negation""" 

24 hv = HashVector(x=7, y=0, z=-1) 

25 self.assertEqual(-hv, {'x': -7, 'y': 0, 'z': 1}) 

26 

27 def test_pow(self): 

28 """Test exponentiation""" 

29 hv = HashVector(x=4, y=0, z=1) 

30 self.assertEqual(hv**0.5, {'x': 2, 'y': 0, 'z': 1}) 

31 with self.assertRaises(TypeError): 

32 _ = hv**hv 

33 with self.assertRaises(TypeError): 

34 _ = hv**"a" 

35 

36 def test_mul_add(self): 

37 """Test multiplication and addition""" 

38 a = HashVector(x=1, y=7) 

39 b = HashVector() 

40 c = HashVector(x=3, z=4) 

41 # nonsense multiplication 

42 with self.assertRaises(TypeError): 

43 _ = a * set() 

44 # multiplication and addition by scalars 

45 r = a*0 

46 self.assertEqual(r, HashVector(x=0, y=0)) 

47 self.assertTrue(isinstance(r, HashVector)) 

48 r = a - 2 

49 self.assertEqual(r, HashVector(x=-1, y=5)) 

50 self.assertTrue(isinstance(r, HashVector)) 

51 with self.assertRaises(TypeError): 

52 _ = r + "a" 

53 # multiplication and addition by dicts 

54 self.assertEqual(a + b, a) 

55 self.assertEqual(a + b + c, HashVector(x=4, y=7, z=4)) 

56 

57 

58class TestSmallScripts(unittest.TestCase): 

59 """TestCase for gpkit.small_scripts""" 

60 def test_unitstr(self): 

61 x = gpkit.Variable("x", "ft") 

62 # pint issue 356 

63 footstrings = ("ft", "foot") # backwards compatibility with pint 0.6 

64 if gpkit.units: 

65 self.assertEqual(unitstr(gpkit.Variable("n", "count")), "count") 

66 self.assertIn(unitstr(x), footstrings) 

67 self.assertIn(unitstr(x.key), footstrings) 

68 self.assertEqual(unitstr(gpkit.Variable("y"), dimless="---"), "---") 

69 self.assertEqual(unitstr(None, dimless="--"), "--") 

70 

71 def test_pint_366(self): 

72 # test for https://github.com/hgrecco/pint/issues/366 

73 if gpkit.units: 

74 self.assertIn(unitstr(gpkit.units("nautical_mile")), 

75 ("nmi", "nautical_mile")) 

76 self.assertEqual(gpkit.units("nautical_mile"), gpkit.units("nmi")) 

77 

78 

79TESTS = [TestHashVector, TestSmallScripts] 

80 

81 

82if __name__ == "__main__": # pragma: no cover 

83 # pylint: disable=wrong-import-position 

84 from gpkit.tests.helpers import run_tests 

85 run_tests(TESTS)