Coverage for tests/test_validation.py: 10%
42 statements
« prev ^ index » next coverage.py v7.4.2, created at 2024-02-22 10:56 +0000
« prev ^ index » next coverage.py v7.4.2, created at 2024-02-22 10:56 +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/>.
22import unittest
24from pydantic import ValidationError
26from felis.datamodel import Schema
27from felis.validation import RspColumn, RspSchema, RspTable, get_schema
30class RSPSchemaTestCase(unittest.TestCase):
31 """Test validation of RSP schema data."""
33 def test_rsp_validation(self) -> None:
34 # Creating an empty RSP column should throw an exception.
35 with self.assertRaises(ValidationError):
36 RspColumn()
38 # Missing column description should throw an exception.
39 with self.assertRaises(ValidationError):
40 RspColumn(name="testColumn", id="#test_col_id", datatype="string")
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=" ")
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)
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")
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 "tap:principal": 1,
62 }
63 )
65 # Creating an empty RSP table should throw an exception.
66 with self.assertRaises(ValidationError):
67 RspTable()
69 # Missing table description should throw an exception.
70 with self.assertRaises(ValidationError):
71 RspTable(**{"name": "testTable", "@id": "#test_table_id", "tap:table_index": 1}, columns=[col])
73 # A table description with only whitespace should throw an exception.
74 with self.assertRaises(ValidationError):
75 RspTable(
76 **{"name": "testTable", "@id": "#test_table_id", "tap:table_index": 1, "description": " "},
77 columns=[col],
78 )
80 # A table description of `None` should throw an exception.
81 with self.assertRaises(ValidationError):
82 RspTable(
83 **{"name": "testTable", "@id": "#test_table_id", "tap:table_index": 1, "description": None},
84 columns=[col],
85 )
87 # Missing TAP table index should throw an exception.
88 with self.assertRaises(ValidationError):
89 RspTable(name="testTable", id="#test_table_id", description="test table", columns=[col])
91 # Missing at least one column flagged as TAP principal should throw an
92 # exception.
93 with self.assertRaises(ValidationError):
94 RspTable(
95 **{
96 "name": "testTable",
97 "@id": "#test_table_id",
98 "description": "test table",
99 "tap:table_index": 1,
100 "columns": [
101 RspColumn(
102 **{
103 "name": "testColumn",
104 "@id": "#test_col_id",
105 "datatype": "string",
106 "description": "test column",
107 }
108 )
109 ],
110 }
111 )
113 # Creating a valid RSP table should not throw an exception.
114 tbl = RspTable(
115 **{
116 "name": "testTable",
117 "@id": "#test_table_id",
118 "description": "test table",
119 "tap:table_index": 1,
120 "columns": [col],
121 }
122 )
124 # Creating an empty RSP table schema throw an exception.
125 with self.assertRaises(ValidationError):
126 RspSchema(tables=[tbl])
128 # Creating a schema with duplicate TAP table indices should throw an
129 # exception.
130 with self.assertRaises(ValidationError):
131 RspSchema(
132 **{"name": "testSchema", "@id": "#test_schema_id", "description": "test schema"},
133 tables=[
134 RspTable(
135 **{
136 "name": "testTable1",
137 "@id": "#test_table1_id",
138 "description": "test table",
139 "tap:table_index": 1,
140 "columns": [
141 RspColumn(
142 **{
143 "name": "testColumn",
144 "@id": "#test_col1_id",
145 "datatype": "string",
146 "description": "test column",
147 "tap:principal": 1,
148 }
149 )
150 ],
151 }
152 ),
153 RspTable(
154 **{
155 "name": "testTable2",
156 "@id": "#test_table2_id",
157 "description": "test table",
158 "tap:table_index": 1,
159 "columns": [
160 RspColumn(
161 **{
162 "name": "testColumn",
163 "@id": "#test_col2_id",
164 "datatype": "string",
165 "description": "test column",
166 "tap:principal": 1,
167 }
168 )
169 ],
170 }
171 ),
172 ],
173 )
175 # Creating a valid schema with multiple tables having unique TAP table
176 # indices should not throw a exception.
177 RspSchema(
178 **{"name": "testSchema", "@id": "#test_schema_id", "description": "test schema"},
179 tables=[
180 RspTable(
181 **{
182 "name": "testTable",
183 "@id": "#test_table_id",
184 "description": "test table",
185 "tap:table_index": 1,
186 "columns": [
187 RspColumn(
188 **{
189 "name": "testColumn",
190 "@id": "#test_col1_id",
191 "datatype": "string",
192 "description": "test column",
193 "tap:principal": 1,
194 }
195 )
196 ],
197 }
198 ),
199 RspTable(
200 **{
201 "name": "testTable2",
202 "@id": "#test_table2_id",
203 "description": "test table",
204 "tap:table_index": 2,
205 "columns": [
206 RspColumn(
207 **{
208 "name": "testColumn",
209 "@id": "#test_col2_id",
210 "datatype": "string",
211 "description": "test column",
212 "tap:principal": 1,
213 }
214 )
215 ],
216 }
217 ),
218 ],
219 )
221 def test_get_schema(self) -> None:
222 """Test that get_schema() returns the correct schema types."""
223 rsp_schema: RspSchema = get_schema("RSP")
224 self.assertEqual(rsp_schema.__name__, "RspSchema")
226 default_schema: Schema = get_schema("default")
227 self.assertEqual(default_schema.__name__, "Schema")
229 with self.assertRaises(ValueError):
230 get_schema("invalid_schema")