@@ -405,6 +405,28 @@ def test_timestamp(self):
405405 self .assertEqual (dct , rtdct )
406406 self .assertEqual ('{"ts": {"$timestamp": {"t": 4, "i": 13}}}' , res )
407407
408+ def test_timestamp_with_invalid_fields (self ):
409+ invalid_values = [
410+ '{"t": 4, "i": 13, "extra": 1}' ,
411+ '{"t": 4, "unexpected": 13}' ,
412+ ]
413+ for value in invalid_values :
414+ with self .subTest (value = value ):
415+ with self .assertRaisesRegex (
416+ TypeError , r'\$timestamp must include exactly "t" and "i" components'
417+ ):
418+ json_util .loads (f'{{"ts": {{"$timestamp": { value } }}}}' )
419+
420+ def test_timestamp_with_extra_wrapper_fields (self ):
421+ with self .assertRaisesRegex (TypeError , r"Bad \$timestamp, extra field\(s\)" ):
422+ json_util .loads ('{"ts": {"$timestamp": {"t": 4, "i": 13}, "extra": 1}}' )
423+
424+ def test_timestamp_with_non_document_value (self ):
425+ for value in ('["t", "i"]' , '"ti"' , "5" ):
426+ with self .subTest (value = value ):
427+ with self .assertRaisesRegex (TypeError , r"\$timestamp value must be a document" ):
428+ json_util .loads (f'{{"ts": {{"$timestamp": { value } }}}}' )
429+
408430 def test_uuid_default (self ):
409431 # Cannot directly encode native UUIDs with the default
410432 # uuid_representation.
0 commit comments