|
20 | 20 | import unittest |
21 | 21 | from datetime import datetime |
22 | 22 | from unittest import expectedFailure |
| 23 | +import json |
23 | 24 |
|
24 | 25 | from ssvc import selection |
25 | 26 | from ssvc.selection import MinimalDecisionPointValue, SelectionList |
@@ -219,6 +220,32 @@ def test_reference_model(self): |
219 | 220 | self.assertIn(uri, str(ref.uri)) |
220 | 221 | self.assertEqual(ref.summary, "Test description") |
221 | 222 |
|
| 223 | + def test_model_dump_removes_empty_values(self): |
| 224 | + """model_dump() should remove None or empty values.""" |
| 225 | + result_clean = self.selections.model_dump(exclude_none=True) |
| 226 | + result_bloat = self.selections.model_dump() |
| 227 | + self.assertNotEqual(result_clean, result_bloat) |
| 228 | + self.assertIn("selections", result_clean) |
| 229 | + self.assertNotIn("metadata", result_clean) |
| 230 | + |
| 231 | + def test_model_dump_json_respects_indent(self): |
| 232 | + """model_dump_json() should apply JSON indentation and pruning.""" |
| 233 | + json_text = self.selections.model_dump_json(indent=4) |
| 234 | + data = json.loads(json_text) |
| 235 | + self.assertIn("selections", data) |
| 236 | + self.assertNotIn("metadata", data) |
| 237 | + self.assertIn("\n \"selections\":", json_text) |
| 238 | + |
| 239 | + def test_model_dump_json_excludes_none(self): |
| 240 | + """exclude_none=True should work with post-processing.""" |
| 241 | + json_text_clean = self.selections.model_dump_json(exclude_none=True) |
| 242 | + json_text_bloat = self.selections.model_dump_json() |
| 243 | + self.assertNotEqual(json_text_clean, json_text_bloat) |
| 244 | + data = json.loads(json_text_clean) |
| 245 | + self.assertIn("selections", data) |
| 246 | + self.assertNotIn("metadata", data) |
| 247 | + |
| 248 | + |
222 | 249 | @expectedFailure |
223 | 250 | def test_reference_model_without_summary(self): |
224 | 251 | """Test the Reference model.""" |
@@ -381,6 +408,17 @@ def test_selection_list_minimum_selections(self): |
381 | 408 | timestamp=datetime.now(), |
382 | 409 | ) |
383 | 410 |
|
| 411 | + def test_model_dump_removes_required_field(self): |
| 412 | + """ Test if a selections is dumped and breaks when items removed """ |
| 413 | + s = SelectionList( |
| 414 | + selections=[self.s1], |
| 415 | + timestamp=datetime.now(), |
| 416 | + ) |
| 417 | + dumped = s.model_dump() |
| 418 | + with self.assertRaises(Exception): |
| 419 | + del dumped['values'] |
| 420 | + |
| 421 | + |
384 | 422 |
|
385 | 423 | if __name__ == "__main__": |
386 | 424 | unittest.main() |
0 commit comments