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

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

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

# This file is part of verify. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

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

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

# for details of code ownership. 

# 

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

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

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

# (at your option) any later version. 

# 

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

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

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

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

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

import unittest 

 

import astropy.units as u 

import yaml 

 

from lsst.verify import Datum 

 

 

class DatumTestCase(unittest.TestCase): 

"""Test Datum functionality""" 

 

def test_properties(self): 

"""Validate basic setters and getters.""" 

d = Datum(5., 'mmag', label='millimag', description='Hello world') 

 

self.assertIsInstance(d.quantity, u.Quantity) 

 

self.assertEqual(d.quantity.value, 5.) 

 

d.quantity = 7 * u.mmag 

self.assertEqual(d.quantity.value, 7) 

 

self.assertEqual(d.unit_str, 'mmag') 

self.assertEqual(d.unit, u.mmag) 

 

# change units 

d.quantity = 5 * u.mag 

self.assertEqual(d.unit, u.mag) 

 

self.assertEqual(d.label, 'millimag') 

d.label = 'magnitudes' 

self.assertEqual(d.label, 'magnitudes') 

 

self.assertEqual(d.description, 'Hello world') 

d.description = 'Updated description.' 

self.assertEqual(d.description, 'Updated description.') 

 

def test_bad_unit(self): 

"""Ensure that units are being validated by astropy.""" 

with self.assertRaises(ValueError): 

Datum(5., 'millimag') 

 

def test_no_units(self): 

"""Ensure an exception is raised if not units are provided to Datum. 

""" 

with self.assertRaises(ValueError): 

Datum(5.) 

 

def test_init_with_quantity(self): 

"""Ensure a Datum can be build from a Quantity.""" 

d = Datum(5 * u.mag) 

 

self.assertEqual(d.quantity.value, 5.) 

self.assertEqual(d.unit_str, 'mag') 

 

def test_quantity_update(self): 

"""Verify that when a quantity is updated the unit attributes 

are updated. 

""" 

d = Datum(5 * u.mag) 

self.assertEqual(d.quantity.value, 5.) 

self.assertEqual(d.unit_str, 'mag') 

 

d.quantity = 100. * u.mmag 

self.assertEqual(d.quantity.value, 100.) 

self.assertEqual(d.unit_str, 'mmag') 

 

def _assertDatumsEqual(self, datum1, datum2): 

"""Test that two Datums are equal without calling ``__eq__``. 

""" 

self.assertEqual(datum1.quantity, datum2.quantity) 

self.assertEqual(datum1.unit, datum2.unit) 

self.assertEqual(datum1.label, datum2.label) 

self.assertEqual(datum1.description, datum2.description) 

 

def _checkRoundTrip(self, d): 

"""Test that a Datum can be serialized and restored. 

""" 

json_data = d.json 

d2 = Datum.deserialize(**json_data) 

self._assertDatumsEqual(d, d2) 

 

yaml_data = yaml.dump(d) 

d3 = yaml.safe_load(yaml_data) 

self._assertDatumsEqual(d, d3) 

 

def test_unitless(self): 

"""Ensure that Datums can be unitless too.""" 

d = Datum(5., '') 

self.assertEqual(d.unit_str, '') 

self.assertEqual(d.unit, u.dimensionless_unscaled) 

 

self._checkRoundTrip(d) 

 

def test_str_quantity(self): 

"""Quantity as a string.""" 

d = Datum('Hello world', label='Test string', 

description='Test description.') 

self.assertEqual(d.quantity, 'Hello world') 

self.assertIsNone(d.unit) 

self.assertEqual(d.unit_str, '') 

self.assertEqual(d.label, 'Test string') 

self.assertEqual(d.description, 'Test description.') 

 

self._checkRoundTrip(d) 

 

def test_bool_quantity(self): 

"""Quantity as a boolean.""" 

d = Datum(True, label='Test boolean', 

description='Test description.') 

self.assertTrue(d.quantity) 

self.assertIsNone(d.unit) 

self.assertEqual(d.unit_str, '') 

self.assertEqual(d.label, 'Test boolean') 

self.assertEqual(d.description, 'Test description.') 

 

self._checkRoundTrip(d) 

 

def test_int_quantity(self): 

"""Quantity as a unitless int.""" 

d = Datum(5, label='Test int', 

description='Test description.') 

self.assertEqual(d.quantity, 5) 

self.assertIsNone(d.unit) 

self.assertEqual(d.unit_str, '') 

self.assertEqual(d.label, 'Test int') 

self.assertEqual(d.description, 'Test description.') 

 

self._checkRoundTrip(d) 

 

def test_none(self): 

"""Quantity as None.""" 

d = Datum(None, label='Test None', 

description='Test description.') 

self.assertIsNone(d.quantity) 

self.assertIsNone(d.unit) 

self.assertEqual(d.unit_str, '') 

self.assertEqual(d.label, 'Test None') 

self.assertEqual(d.description, 'Test description.') 

 

self._checkRoundTrip(d) 

 

def test_json_output(self): 

"""Verify content from json property and deserialization.""" 

d = Datum(5., 'mmag', label='millimag', description='Hello world') 

dj = d.json 

 

self.assertEqual(d.quantity.value, dj['value']) 

self.assertEqual(d.unit_str, dj['unit']) 

self.assertEqual(d.label, dj['label']) 

self.assertEqual(d.description, dj['description']) 

 

new_datum = Datum.deserialize(**dj) 

self.assertEqual(d, new_datum) 

 

 

177 ↛ 178line 177 didn't jump to line 178, because the condition on line 177 was never trueif __name__ == "__main__": 

unittest.main()