Example #1
0
    def test_to_mongo_no_compression(self):
        field = JSONDictField(use_header=True)

        result = field.to_mongo(MOCK_DATA_DICT)
        self.assertTrue(isinstance(result, bytes))

        split = result.split(b":", 2)
        self.assertEqual(split[0], JSONDictFieldCompressionAlgorithmEnum.NONE.value)
        self.assertEqual(split[1], JSONDictFieldSerializationFormatEnum.ORJSON.value)
        self.assertEqual(orjson.loads(split[2]), MOCK_DATA_DICT)

        parsed_value = field.parse_field_value(result)
        self.assertEqual(parsed_value, MOCK_DATA_DICT)
Example #2
0
    def test_to_mongo_zstandard_compression(self):
        field = JSONDictField(use_header=True, compression_algorithm="zstandard")

        result = field.to_mongo(MOCK_DATA_DICT)
        self.assertTrue(isinstance(result, bytes))

        split = result.split(b":", 2)
        self.assertEqual(
            split[0], JSONDictFieldCompressionAlgorithmEnum.ZSTANDARD.value
        )
        self.assertEqual(split[1], JSONDictFieldSerializationFormatEnum.ORJSON.value)
        self.assertEqual(
            orjson.loads(zstandard.ZstdDecompressor().decompress(split[2])),
            MOCK_DATA_DICT,
        )

        parsed_value = field.parse_field_value(result)
        self.assertEqual(parsed_value, MOCK_DATA_DICT)
Example #3
0
    def test_parse_field_value(self):
        # 1. Value not provided, should use default one
        field = JSONDictField(use_header=False, default={})
        result = field.parse_field_value(b"")
        self.assertEqual(result, {})

        result = field.parse_field_value(None)
        self.assertEqual(result, {})

        field = JSONDictField(use_header=False, default={"foo": "bar"})
        result = field.parse_field_value(b"")
        self.assertEqual(result, {"foo": "bar"})

        result = field.parse_field_value(None)
        self.assertEqual(result, {"foo": "bar"})

        # Value should be deserialized
        result = field.parse_field_value(b'{"a": "b"}')
        self.assertEqual(result, {"a": "b"})

        # Already a dict
        result = field.parse_field_value({"c": "d"})
        self.assertEqual(result, {"c": "d"})