Skip to content

Commit 897060d

Browse files
test: add tests for schema_obj parameter functionality
1 parent 00ae6e6 commit 897060d

1 file changed

Lines changed: 132 additions & 0 deletions

File tree

tests/test_neutral_template.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,138 @@ def test_bif_cache_complete(self):
495495
# we give 2 second for cache reset to repeat tests because: {:^cache; /3/ >>
496496
time.sleep(2)
497497

498+
def test_initialization_with_schema_obj(self):
499+
"""Test initialization with Python dict schema."""
500+
501+
template = NeutralTemplate(TEMPLATE1, schema_obj={"data": {"__hello-nts": "From dict"}})
502+
contents = template.render()
503+
504+
self.assertEqual(contents, "From dict")
505+
self.assertEqual(template.has_error(), False)
506+
self.assertEqual(template.get_status_code(), "200")
507+
self.assertEqual(template.get_status_text(), "OK")
508+
self.assertEqual(template.get_status_param(), "")
509+
510+
def test_merge_schema_obj(self):
511+
"""Test merge_schema_obj with Python dict."""
512+
513+
template = NeutralTemplate()
514+
template.set_source("{:;key:}")
515+
template.merge_schema_obj({"data": {"key": "value from dict"}})
516+
contents = template.render()
517+
518+
self.assertEqual(contents, "value from dict")
519+
self.assertEqual(template.has_error(), False)
520+
521+
def test_merge_schema_obj_complex_types(self):
522+
"""Test merge_schema_obj with nested dicts, lists, tuples."""
523+
524+
template = NeutralTemplate()
525+
template.set_source("{:;nested->level:} {:;items->0:} {:;items->1:} {:;number:}")
526+
template.merge_schema_obj({
527+
"data": {
528+
"nested": {"level": "deep"},
529+
"items": ["first", "second", "third"],
530+
"number": 42
531+
}
532+
})
533+
contents = template.render()
534+
535+
self.assertEqual(contents, "deep first second 42")
536+
self.assertEqual(template.has_error(), False)
537+
538+
def test_merge_schema_obj_tuple(self):
539+
"""Test merge_schema_obj with tuple (should become array)."""
540+
541+
template = NeutralTemplate()
542+
template.set_source("{:;items->0:} {:;items->1:}")
543+
template.merge_schema_obj({"data": {"items": ("first", "second")}})
544+
contents = template.render()
545+
546+
self.assertEqual(contents, "first second")
547+
self.assertEqual(template.has_error(), False)
548+
549+
def test_merge_schema_obj_unsupported_type(self):
550+
"""Test merge_schema_obj with unsupported type raises error."""
551+
552+
template = NeutralTemplate()
553+
template.set_source("{:;key:}")
554+
with self.assertRaises(TypeError):
555+
template.merge_schema_obj({"data": {"key": object()}})
556+
557+
def test_merge_schema_obj_nan(self):
558+
"""Test merge_schema_obj with NaN raises error."""
559+
560+
template = NeutralTemplate()
561+
template.set_source("{:;key:}")
562+
with self.assertRaises(ValueError):
563+
template.merge_schema_obj({"data": {"key": float('nan')}})
564+
565+
def test_merge_schema_obj_infinity(self):
566+
"""Test merge_schema_obj with Infinity raises error."""
567+
568+
template = NeutralTemplate()
569+
template.set_source("{:;key:}")
570+
with self.assertRaises(ValueError):
571+
template.merge_schema_obj({"data": {"key": float('inf')}})
572+
573+
def test_constructor_multiple_schemas_str_and_obj(self):
574+
"""Test that providing schema_str and schema_obj raises ValueError."""
575+
576+
with self.assertRaises(ValueError) as context:
577+
NeutralTemplate(TEMPLATE1, schema_str='{"data": {}}', schema_obj={"data": {}})
578+
self.assertIn("use only one schema input", str(context.exception))
579+
580+
def test_constructor_multiple_schemas_str_and_msgpack(self):
581+
"""Test that providing schema_str and schema_msgpack raises ValueError."""
582+
583+
with self.assertRaises(ValueError) as context:
584+
NeutralTemplate(TEMPLATE1, schema_str='{"data": {}}', schema_msgpack=SCHEMA_MSGPACK)
585+
self.assertIn("use only one schema input", str(context.exception))
586+
587+
def test_constructor_multiple_schemas_obj_and_msgpack(self):
588+
"""Test that providing schema_obj and schema_msgpack raises ValueError."""
589+
590+
with self.assertRaises(ValueError) as context:
591+
NeutralTemplate(TEMPLATE1, schema_obj={"data": {}}, schema_msgpack=SCHEMA_MSGPACK)
592+
self.assertIn("use only one schema input", str(context.exception))
593+
594+
def test_merge_schema_obj_cumulative(self):
595+
"""Test that multiple merges accumulate correctly."""
596+
597+
template = NeutralTemplate()
598+
template.set_source("{:;key1:} {:;key2:}")
599+
template.merge_schema_obj({"data": {"key1": "first"}})
600+
template.merge_schema_obj({"data": {"key2": "second"}})
601+
contents = template.render()
602+
603+
self.assertEqual(contents, "first second")
604+
self.assertEqual(template.has_error(), False)
605+
606+
def test_merge_order_obj_then_json(self):
607+
"""Last merge must win, preserving call order (obj -> json)."""
608+
609+
template = NeutralTemplate()
610+
template.set_source("{:;key:}")
611+
template.merge_schema_obj({"data": {"key": "from obj"}})
612+
template.merge_schema('{"data": {"key": "from json"}}')
613+
contents = template.render()
614+
615+
self.assertEqual(contents, "from json")
616+
self.assertEqual(template.has_error(), False)
617+
618+
def test_merge_order_json_then_obj(self):
619+
"""Last merge must win, preserving call order (json -> obj)."""
620+
621+
template = NeutralTemplate()
622+
template.set_source("{:;key:}")
623+
template.merge_schema('{"data": {"key": "from json"}}')
624+
template.merge_schema_obj({"data": {"key": "from obj"}})
625+
contents = template.render()
626+
627+
self.assertEqual(contents, "from obj")
628+
self.assertEqual(template.has_error(), False)
629+
498630

499631
if __name__ == '__main__':
500632
unittest.main()

0 commit comments

Comments
 (0)