Coverage for tests/test_validation.py: 10%

42 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-14 09:10 +0000

1# This file is part of felis. 

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 

23 

24from pydantic import ValidationError 

25 

26from felis.datamodel import Schema 

27from felis.validation import RspColumn, RspSchema, RspTable, get_schema 

28 

29 

30class RSPSchemaTestCase(unittest.TestCase): 

31 """Test validation of RSP schema data.""" 

32 

33 def test_rsp_validation(self) -> None: 

34 # Creating an empty RSP column should throw an exception. 

35 with self.assertRaises(ValidationError): 

36 RspColumn() 

37 

38 # Missing column description should throw an exception. 

39 with self.assertRaises(ValidationError): 

40 RspColumn(name="testColumn", id="#test_col_id", datatype="string") 

41 

42 # A column description with only whitespace should throw an exception. 

43 with self.assertRaises(ValidationError): 

44 RspColumn(name="testColumn", id="#test_col_id", datatype="string", description=" ") 

45 

46 # A column description of `None` should throw an exception. 

47 with self.assertRaises(ValidationError): 

48 RspColumn(name="testColumn", id="#test_col_id", datatype="string", description=None) 

49 

50 # A column description which is not long enough should throw. 

51 with self.assertRaises(ValidationError): 

52 RspColumn(name="testColumn", id="#test_col_id", datatype="string", description="xy") 

53 

54 # Creating a valid RSP column should not throw an exception. 

55 col = RspColumn( 

56 **{ 

57 "name": "testColumn", 

58 "@id": "#test_col_id", 

59 "datatype": "string", 

60 "description": "test column", 

61 "length": 256, 

62 "tap:principal": 1, 

63 } 

64 ) 

65 

66 # Creating an empty RSP table should throw an exception. 

67 with self.assertRaises(ValidationError): 

68 RspTable() 

69 

70 # Missing table description should throw an exception. 

71 with self.assertRaises(ValidationError): 

72 RspTable(**{"name": "testTable", "@id": "#test_table_id", "tap:table_index": 1}, columns=[col]) 

73 

74 # A table description with only whitespace should throw an exception. 

75 with self.assertRaises(ValidationError): 

76 RspTable( 

77 **{"name": "testTable", "@id": "#test_table_id", "tap:table_index": 1, "description": " "}, 

78 columns=[col], 

79 ) 

80 

81 # A table description of `None` should throw an exception. 

82 with self.assertRaises(ValidationError): 

83 RspTable( 

84 **{"name": "testTable", "@id": "#test_table_id", "tap:table_index": 1, "description": None}, 

85 columns=[col], 

86 ) 

87 

88 # Missing TAP table index should throw an exception. 

89 with self.assertRaises(ValidationError): 

90 RspTable(name="testTable", id="#test_table_id", description="test table", columns=[col]) 

91 

92 # Missing at least one column flagged as TAP principal should throw an 

93 # exception. 

94 with self.assertRaises(ValidationError): 

95 RspTable( 

96 **{ 

97 "name": "testTable", 

98 "@id": "#test_table_id", 

99 "description": "test table", 

100 "tap:table_index": 1, 

101 "columns": [ 

102 RspColumn( 

103 **{ 

104 "name": "testColumn", 

105 "@id": "#test_col_id", 

106 "datatype": "string", 

107 "description": "test column", 

108 } 

109 ) 

110 ], 

111 } 

112 ) 

113 

114 # Creating a valid RSP table should not throw an exception. 

115 tbl = RspTable( 

116 **{ 

117 "name": "testTable", 

118 "@id": "#test_table_id", 

119 "description": "test table", 

120 "tap:table_index": 1, 

121 "columns": [col], 

122 } 

123 ) 

124 

125 # Creating an empty RSP table schema throw an exception. 

126 with self.assertRaises(ValidationError): 

127 RspSchema(tables=[tbl]) 

128 

129 # Creating a schema with duplicate TAP table indices should throw an 

130 # exception. 

131 with self.assertRaises(ValidationError): 

132 RspSchema( 

133 **{"name": "testSchema", "@id": "#test_schema_id", "description": "test schema"}, 

134 tables=[ 

135 RspTable( 

136 **{ 

137 "name": "testTable1", 

138 "@id": "#test_table1_id", 

139 "description": "test table", 

140 "tap:table_index": 1, 

141 "columns": [ 

142 RspColumn( 

143 **{ 

144 "name": "testColumn", 

145 "@id": "#test_col1_id", 

146 "datatype": "string", 

147 "description": "test column", 

148 "tap:principal": 1, 

149 } 

150 ) 

151 ], 

152 } 

153 ), 

154 RspTable( 

155 **{ 

156 "name": "testTable2", 

157 "@id": "#test_table2_id", 

158 "description": "test table", 

159 "tap:table_index": 1, 

160 "columns": [ 

161 RspColumn( 

162 **{ 

163 "name": "testColumn", 

164 "@id": "#test_col2_id", 

165 "datatype": "string", 

166 "description": "test column", 

167 "tap:principal": 1, 

168 } 

169 ) 

170 ], 

171 } 

172 ), 

173 ], 

174 ) 

175 

176 # Creating a valid schema with multiple tables having unique TAP table 

177 # indices should not throw a exception. 

178 RspSchema( 

179 **{"name": "testSchema", "@id": "#test_schema_id", "description": "test schema"}, 

180 tables=[ 

181 RspTable( 

182 **{ 

183 "name": "testTable", 

184 "@id": "#test_table_id", 

185 "description": "test table", 

186 "tap:table_index": 1, 

187 "columns": [ 

188 RspColumn( 

189 **{ 

190 "name": "testColumn", 

191 "@id": "#test_col1_id", 

192 "datatype": "string", 

193 "description": "test column", 

194 "tap:principal": 1, 

195 "length": 256, 

196 } 

197 ) 

198 ], 

199 } 

200 ), 

201 RspTable( 

202 **{ 

203 "name": "testTable2", 

204 "@id": "#test_table2_id", 

205 "description": "test table", 

206 "tap:table_index": 2, 

207 "columns": [ 

208 RspColumn( 

209 **{ 

210 "name": "testColumn", 

211 "@id": "#test_col2_id", 

212 "datatype": "string", 

213 "description": "test column", 

214 "tap:principal": 1, 

215 "length": 256, 

216 } 

217 ) 

218 ], 

219 } 

220 ), 

221 ], 

222 ) 

223 

224 def test_get_schema(self) -> None: 

225 """Test that get_schema() returns the correct schema types.""" 

226 rsp_schema: RspSchema = get_schema("RSP") 

227 self.assertEqual(rsp_schema.__name__, "RspSchema") 

228 

229 default_schema: Schema = get_schema("default") 

230 self.assertEqual(default_schema.__name__, "Schema") 

231 

232 with self.assertRaises(ValueError): 

233 get_schema("invalid_schema")