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

# This file is part of daf_base 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (http://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 <http://www.gnu.org/licenses/>. 

 

"""Test YAML serialization""" 

 

import os 

import unittest 

 

try: 

import yaml 

except ImportError: 

yaml = None 

 

import lsst.daf.base 

 

TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

 

 

class YAMLTestCase(unittest.TestCase): 

 

@unittest.skipIf(yaml is None, "yaml module not installed") 

def setUp(self): 

# In pyyaml >= 5.1 we prefer to use FullLoader 

try: 

self.yamlLoader = yaml.SafeLoader 

except AttributeError: 

self.yamlLoader = yaml.Loader 

 

def testYamlPS(self): 

ps = lsst.daf.base.PropertySet() 

ps.setBool("bool", True) 

ps.setShort("short", 42) 

ps.setInt("int", 2008) 

ps.setLongLong("int64_t", 0xfeeddeadbeef) 

ps.setFloat("float", 3.14159) 

ps.setDouble("double", 2.718281828459045) 

ps.set("char*", "foo") 

ps.setString("string", "bar") 

ps.set("char*", u"foo") 

ps.setString("string", u"bar") 

ps.set("int2", 2009) 

ps.set("dt", lsst.daf.base.DateTime("20090402T072639.314159265Z", lsst.daf.base.DateTime.UTC)) 

ps.set("blank", "") 

ps.set("undef", None) 

 

ps2 = yaml.load(yaml.dump(ps), Loader=self.yamlLoader) 

self.assertIsInstance(ps2, lsst.daf.base.PropertySet) 

self.assertEqual(ps, ps2) 

 

def testYamlPL(self): 

apl = lsst.daf.base.PropertyList() 

apl.setBool("bool", True) 

apl.setShort("short", 42) 

apl.setInt("int", 2008) 

apl.setLongLong("int64_t", 0xfeeddeadbeef) 

apl.setFloat("float", 3.14159) 

apl.setDouble("double", 2.718281828459045) 

apl.set("char*", "foo") 

apl.setString("string", "bar") 

apl.set("int2", 2009) 

apl.set("dt", lsst.daf.base.DateTime("20090402T072639.314159265Z", lsst.daf.base.DateTime.UTC)) 

apl.set("undef", None) 

 

apl2 = yaml.load(yaml.dump(apl), Loader=self.yamlLoader) 

self.assertIsInstance(apl2, lsst.daf.base.PropertyList) 

self.assertEqual(apl, apl2) 

 

def testYamlNest(self): 

"""Test nested property sets""" 

ps = lsst.daf.base.PropertySet() 

ps.setBool("bool", True) 

ps.setShort("short", 42) 

ps.setInt("int", 2008) 

 

ps2 = lsst.daf.base.PropertySet() 

ps2.setString("string", "foo") 

ps2.setString("string2", "bar") 

 

ps.setPropertySet("ps", ps2) 

 

ps3 = yaml.load(yaml.dump(ps), Loader=self.yamlLoader) 

self.assertEqual(ps3, ps) 

self.assertEqual(ps3.getPropertySet("ps"), ps.getPropertySet("ps")) 

 

# Now for a PropertyList 

apl = lsst.daf.base.PropertyList() 

apl.setBool("bool", True) 

apl.setShort("short", 42) 

apl.setInt("int", 2008) 

apl.add("withcom", "string", "a comment") 

apl.setPropertySet("ps", ps3) 

 

apl2 = yaml.load(yaml.dump(apl), Loader=self.yamlLoader) 

self.assertEqual(apl2, apl) 

 

# Add the PropertyList to the PropertySet to ensure that the 

# correct type is returned and no loss of comments 

ps.setPropertySet("newpl", apl) 

apl3 = yaml.load(yaml.dump(ps), Loader=self.yamlLoader) 

self.assertEqual(apl3, ps) 

 

def testYamlDateTime(self): 

ts = lsst.daf.base.DateTime("2004-03-01T12:39:45.1Z", lsst.daf.base.DateTime.UTC) 

ts2 = yaml.load(yaml.dump(ts), Loader=self.yamlLoader) 

self.assertIsInstance(ts2, lsst.daf.base.DateTime) 

self.assertEqual(ts, ts2) 

 

def testLoader(self): 

"""Test loading of reference YAML files""" 

# Old and new serialization of a propertyList 

with open(os.path.join(TESTDIR, "data", "fitsheader-tuple.yaml")) as fd: 

old = yaml.load(fd, Loader=yaml.Loader) 

with open(os.path.join(TESTDIR, "data", "fitsheader.yaml")) as fd: 

new = yaml.load(fd, Loader=self.yamlLoader) 

self.assertIsInstance(new, lsst.daf.base.PropertyList) 

self.assertIsInstance(old, lsst.daf.base.PropertyList) 

self.assertEqual(old, new) 

 

 

138 ↛ 139line 138 didn't jump to line 139, because the condition on line 138 was never trueif __name__ == '__main__': 

unittest.main()