def test_to_python(self): field = JSONDictField(use_header=False) data = orjson.dumps(MOCK_DATA_DICT) result = field.to_python(data) self.assertTrue(isinstance(result, dict)) self.assertEqual(result, MOCK_DATA_DICT)
def test_to_python_no_compression(self): field = JSONDictField(use_header=True) serialized_data = field.to_mongo(MOCK_DATA_DICT) self.assertTrue(isinstance(serialized_data, bytes)) split = serialized_data.split(b":", 2) self.assertEqual(split[0], JSONDictFieldCompressionAlgorithmEnum.NONE.value) self.assertEqual(split[1], JSONDictFieldSerializationFormatEnum.ORJSON.value) desserialized_data = field.to_python(serialized_data) self.assertEqual(desserialized_data, MOCK_DATA_DICT)
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)
class LiveActionDB_JSONFieldWithHeader(LiveActionDB): result = JSONDictField(default={}, use_header=True, compression_algorithm="none") field1 = JSONDictField(default={}, use_header=True, compression_algorithm="none") field2 = JSONDictField(default={}, use_header=True, compression_algorithm="none") field3 = JSONDictField(default={}, use_header=True, compression_algorithm="none")
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)
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"})
def test_roundtrip(self): field = JSONDictField(use_header=False) result_to_mongo = field.to_mongo(MOCK_DATA_DICT) result_to_python = field.to_python(result_to_mongo) self.assertEqual(result_to_python, MOCK_DATA_DICT) # sets get serialized to a list input_dict = {"a": 1, "set": {1, 2, 3, 4, 4, 4, 5, 5}} result = {"a": 1, "set": [1, 2, 3, 4, 5]} field = JSONDictField(use_header=False) result_to_mongo = field.to_mongo(input_dict) result_to_python = field.to_python(result_to_mongo) self.assertEqual(result_to_python, result)
def test_roundtrip(self): field = JSONDictField(use_header=False) result_to_mongo = field.to_mongo(MOCK_DATA_DICT) result_to_python = field.to_python(result_to_mongo) self.assertEqual(result_to_python, MOCK_DATA_DICT)
def test_to_mongo(self): field = JSONDictField(use_header=False) result = field.to_mongo(MOCK_DATA_DICT) self.assertTrue(isinstance(result, bytes)) self.assertEqual(result, orjson.dumps(MOCK_DATA_DICT))
class ModelWithJSONDictFieldDB(stormbase.StormFoundationDB): result = JSONDictField(default={}, use_header=False) counter = me.IntField(default=0) meta = {"collection": "model_result_test"}
class LiveActionDB_JSONField(LiveActionDB): result = JSONDictField(default={}, use_header=False) field1 = JSONDictField(default={}, use_header=False) field2 = JSONDictField(default={}, use_header=False) field3 = JSONDictField(default={}, use_header=False)
def test_header_set_to_mongo(self): field = JSONDictField(use_header=True) result = field.to_mongo({"test": {1, 2}}) self.assertTrue(isinstance(result, bytes))