Commit b9bdbc35 authored by Kenzo-Hugo Hillion's avatar Kenzo-Hugo Hillion
Browse files

finish udpate code for bulklistserializer

parent 0a35a932
Pipeline #14145 failed with stages
in 1 minute and 42 seconds
......@@ -20,6 +20,19 @@ class BulkListSerializer(serializers.ListSerializer):
db_index_keys.append(field_name)
return db_index_keys
def _get_all_key_fields(self, validated_data):
"""
Retrieve all keys that are within the payload.
:return: list of keys found in payload.
:rtype: List
"""
all_keys = []
for validated_data_element in validated_data:
for key in validated_data_element.keys():
all_keys.append(key)
return list(set(all_keys))
def create(self, validated_data):
ModelClass = self.Meta.model
instances = ModelClass.objects.bulk_create(
......@@ -31,10 +44,14 @@ class BulkListSerializer(serializers.ListSerializer):
ModelClass = self.Meta.model
info = model_meta.get_field_info(ModelClass)
db_index_fields = self._get_db_index_fields(info)
id_list = [data.pop(db_index_fields[0]) for data in validated_data] # noqa
[data.pop(db_index_fields[0]) for data in validated_data] # remove db_index fields
for instance, validated_data_element in zip(instances, validated_data):
for key, value in validated_data_element.items():
setattr(instance, key, value)
updated_keys = self._get_all_key_fields(validated_data)
instances = ModelClass.objects.bulk_update(
instances,
['name']
updated_keys
)
return instances
......
......@@ -64,6 +64,14 @@ class TestExtractManyToMany(BaseTestBulkListSerializerMethods):
class TestGetDbIndexFields(BaseTestBulkListSerializerMethods):
def test_get_db_index_fields(self):
expected_keys = ['field1', 'field2']
tested_keys = self.bulk_list_serializer._get_all_key_fields(self.data)
self.assertListEqual(tested_keys, expected_keys)
class TestGetKeyFields(BaseTestBulkListSerializerMethods):
def test_get_all_key_fields(self):
self.info.fields = {
'field1': Mock(db_index=True),
'field2': Mock(db_index=False)
......@@ -72,7 +80,7 @@ class TestGetDbIndexFields(BaseTestBulkListSerializerMethods):
tested_keys = self.bulk_list_serializer._get_db_index_fields(self.info)
self.assertListEqual(tested_keys, expected_keys)
def test_get_db_index_fields_no_keys(self):
def test_get_all_key_fields_no_keys(self):
self.info.fields = {
'field1': Mock(db_index=False),
'field2': Mock(db_index=False)
......@@ -82,7 +90,7 @@ class TestGetDbIndexFields(BaseTestBulkListSerializerMethods):
self.assertListEqual(tested_keys, expected_keys)
class TestCreateBulk(APITestCase):
class TestOperationsBulk(APITestCase):
"""
We are going to use an Serializer based on a real model since there are interactions with the DB.
We are using the FunctionListSerializer for the moment (through FunctionSerializer(many=True)).
......@@ -123,7 +131,10 @@ class TestCreateBulk(APITestCase):
"name": "Test 2"
}
]
for data in validated_data:
self.assertNotEqual(self.function_api.get(data['function_id']), data)
serializer = FunctionSerializer(many=True)
serializer.update(functions, validated_data)
serializer.update(functions, deepcopy(validated_data))
self.assertEqual(self.function_api.get_all()['count'], 2)
print(self.function_api.get_all())
for data in validated_data:
self.assertDictEqual(self.function_api.get(data['function_id']), data)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment