Coverage for tests/test_blob.py: 26%

58 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-15 02:05 -0800

1# This file is part of verify. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21 

22import unittest 

23import astropy.units as u 

24import yaml 

25 

26from lsst.verify.blob import Blob 

27from lsst.verify.datum import Datum 

28 

29 

30class BlobTestCase(unittest.TestCase): 

31 """Test Blob functionality.""" 

32 

33 def setUp(self): 

34 self.mag1 = Datum( 

35 quantity=5 * u.mag, 

36 label='mag1', 

37 description='Magnitude') 

38 

39 self.mag2 = Datum( 

40 quantity=10 * u.mag, 

41 label='mag2', 

42 description='Magnitude') 

43 

44 self.blob = Blob('demo', mag1=self.mag1, mag2=self.mag2) 

45 

46 def test_access(self): 

47 self.assertEqual(self.blob['mag1'], self.mag1) 

48 self.assertEqual(self.blob['mag2'], self.mag2) 

49 

50 with self.assertRaises(KeyError): 

51 self.blob['magX'] 

52 

53 def test_len(self): 

54 self.assertEqual(len(self.blob), 2) 

55 

56 def test_contains(self): 

57 self.assertTrue('mag1' in self.blob) 

58 self.assertTrue('mag2' in self.blob) 

59 self.assertFalse('magX' in self.blob) 

60 

61 def test_iter(self): 

62 keys = set([k for k in self.blob]) 

63 self.assertEqual(keys, set(['mag1', 'mag2'])) 

64 

65 def test_name(self): 

66 self.assertEqual(self.blob.name, 'demo') 

67 

68 def test_identifier(self): 

69 new_blob = Blob('demo') 

70 self.assertNotEqual(new_blob.identifier, self.blob.identifier) 

71 

72 def test_json(self): 

73 j = self.blob.json 

74 

75 self.assertIn('identifier', j) 

76 self.assertEqual(self.blob.name, 'demo') 

77 self.assertEqual(j['data']['mag1']['value'], 5) 

78 self.assertEqual(j['data']['mag1']['unit'], 'mag') 

79 self.assertEqual(j['data']['mag1']['label'], 'mag1') 

80 self.assertEqual(j['data']['mag1']['description'], 'Magnitude') 

81 

82 # Rebuild from blob 

83 b2 = Blob.deserialize(**j) 

84 self.assertEqual(self.blob, b2) 

85 

86 def test_yaml(self): 

87 yaml_form = yaml.dump(self.blob) 

88 blob2 = yaml.safe_load(yaml_form) 

89 self.assertEqual(self.blob, blob2) 

90 

91 def test_mutation(self): 

92 blob = Blob('mutable') 

93 self.assertEqual(len(blob), 0) 

94 

95 blob['test'] = Datum(quantity=1 * u.arcsec) 

96 self.assertEqual(len(blob), 1) 

97 

98 with self.assertRaises(TypeError): 

99 blob['fails'] = 10 

100 self.assertEqual(len(blob), 1) 

101 

102 with self.assertRaises(KeyError): 

103 blob[10] = Datum(quantity=1 * u.arcsec) 

104 self.assertEqual(len(blob), 1) 

105 

106 del blob['test'] 

107 self.assertEqual(len(blob), 0) 

108 

109 

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

111 unittest.main()