@@ -122,41 +122,146 @@ def test_validation(self) -> None:
122122 with self .assertRaises (ValidationError ):
123123 Column (** units_data )
124124
125- def test_description (self ) -> None :
126- """Test Pydantic validation of the ``description`` attribute."""
125+ def test_description_unchecked (self ) -> None :
126+ """Test that the ``description`` attribute is optional when the
127+ ``check_description`` flag is disabled (the default).
128+ """
129+ # A missing description should be allowed.
130+ col = Column .model_validate (
131+ {
132+ "name" : "testColumn" ,
133+ "@id" : "#test_col_id" ,
134+ "datatype" : "string" ,
135+ "length" : 256 ,
136+ }
137+ )
138+ self .assertIsNone (col .description )
139+
140+ # An explicit 'None' description should be allowed.
141+ col = Column .model_validate (
142+ {
143+ "name" : "testColumn" ,
144+ "@id" : "#test_col_id" ,
145+ "datatype" : "string" ,
146+ "length" : 256 ,
147+ "description" : None ,
148+ }
149+ )
150+ self .assertIsNone (col .description )
151+
152+ # An empty description should be allowed.
153+ col = Column .model_validate (
154+ {
155+ "name" : "testColumn" ,
156+ "@id" : "#test_col_id" ,
157+ "datatype" : "string" ,
158+ "length" : 256 ,
159+ "description" : "" ,
160+ }
161+ )
162+ self .assertEqual (col .description , "" )
163+
164+ # A description with only whitespace should be allowed.
165+ col = Column .model_validate (
166+ {
167+ "name" : "testColumn" ,
168+ "@id" : "#test_col_id" ,
169+ "datatype" : "string" ,
170+ "length" : 256 ,
171+ "description" : " " ,
172+ }
173+ )
174+
175+ # Pydantic will strip this automatically.
176+ self .assertEqual (col .description , "" )
177+
178+ # A description with one or more non-whitespace characters should be
179+ # allowed.
180+ col = Column .model_validate (
181+ {
182+ "name" : "testColumn" ,
183+ "@id" : "#test_col_id" ,
184+ "datatype" : "string" ,
185+ "length" : 256 ,
186+ "description" : "x" ,
187+ }
188+ )
189+ self .assertEqual (col .description , "x" )
190+
191+ def test_description_checked (self ) -> None :
192+ """Test Pydantic validation of the ``description`` attribute when the
193+ ``check_description`` flag is enabled.
194+ """
195+ cxt = {"check_description" : True }
196+
127197 # Creating a column with a description of 'None' should throw.
128- with self .assertRaises (ValueError ):
129- Column (
130- ** {
198+ with self .assertRaises (ValidationError ):
199+ Column . model_validate (
200+ {
131201 "name" : "testColumn" ,
132202 "@id" : "#test_col_id" ,
133203 "datatype" : "string" ,
204+ "length" : 256 ,
134205 "description" : None ,
135- }
206+ },
207+ context = cxt ,
136208 )
137209
138210 # Creating a column with an empty description should throw.
139- with self .assertRaises (ValueError ):
140- Column (
141- ** {
211+ with self .assertRaises (ValidationError ):
212+ Column . model_validate (
213+ {
142214 "name" : "testColumn" ,
143215 "@id" : "#test_col_id" ,
144216 "datatype" : "string" ,
217+ "length" : 256 ,
145218 "description" : "" ,
146- }
219+ },
220+ context = cxt ,
147221 )
148222
149- # Creating a column with a description that is too short should throw.
223+ # Creating a column with a whitespace-only description should throw,
224+ # since whitespace is stripped before validation.
150225 with self .assertRaises (ValidationError ):
151- Column (
152- ** {
226+ Column . model_validate (
227+ {
153228 "name" : "testColumn" ,
154229 "@id" : "#test_col_id" ,
155230 "datatype" : "string" ,
156- "description" : "xy" ,
157- }
231+ "length" : 256 ,
232+ "description" : " " ,
233+ },
234+ context = cxt ,
158235 )
159236
237+ # Creating a column with a single non-whitespace character in the
238+ # description should not throw.
239+ col = Column .model_validate (
240+ {
241+ "name" : "testColumn" ,
242+ "@id" : "#test_col_id" ,
243+ "datatype" : "string" ,
244+ "length" : 256 ,
245+ "description" : "x" ,
246+ },
247+ context = cxt ,
248+ )
249+ self .assertEqual (col .description , "x" )
250+
251+ # Creating a column with more than one non-whitespace character in the
252+ # description should not throw.
253+ col = Column .model_validate (
254+ {
255+ "name" : "testColumn" ,
256+ "@id" : "#test_col_id" ,
257+ "datatype" : "string" ,
258+ "length" : 256 ,
259+ "description" : "test description" ,
260+ },
261+ context = cxt ,
262+ )
263+ self .assertEqual (col .description , "test description" )
264+
160265 def test_values (self ) -> None :
161266 """Test Pydantic validation of the ``value`` attribute."""
162267
0 commit comments