Skip to content

Commit

Permalink
Don't update equivalent entities in import_data
Browse files Browse the repository at this point in the history
Fixes #314
  • Loading branch information
manuelma committed Nov 21, 2023
1 parent 90d4854 commit 32a23a8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
11 changes: 9 additions & 2 deletions spinedb_api/db_mapping_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,8 @@ class MappedItemBase(dict):
"""A dictionary that represents a db item."""

fields = {}
"""A dictionary mapping keys to a tuple of (type, value description)"""
"""A dictionary mapping keys to a another dict mapping "type" to a Python type,
"value" to a description of the value for the key, and "optional" to a bool."""
_defaults = {}
"""A dictionary mapping keys to their default values"""
_unique_keys = ()
Expand All @@ -597,6 +598,7 @@ class MappedItemBase(dict):
source key.
"""
_private_fields = set()
"""A set with fields that should be ignored in validations."""

def __init__(self, db_map, item_type, **kwargs):
"""
Expand Down Expand Up @@ -741,7 +743,12 @@ def _something_to_update(self, other):
def _convert(x):
return tuple(x) if isinstance(x, list) else x

return not all(_convert(self.get(key)) == _convert(value) for key, value in other.items())
return not all(
_convert(self.get(key)) == _convert(value)
for key, value in other.items()
if value is not None
or self.fields.get(key, {}).get("optional", False) # Ignore mandatory fields that are None
)

def first_invalid_key(self):
"""Goes through the ``_references`` class attribute and returns the key of the first reference
Expand Down
21 changes: 21 additions & 0 deletions tests/test_import_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,27 @@ def test_import_existing_relationship_class_parameter(self):
db_map.close()


class TestImportEntity(unittest.TestCase):
def test_import_multi_d_entity_twice(self):
db_map = DatabaseMapping("sqlite://", create=True)
import_data(
db_map,
entity_classes=(
("object_class1",),
("object_class2",),
("relationship_class", ("object_class1", "object_class2")),
),
entities=(
("object_class1", "object1"),
("object_class2", "object2"),
("relationship_class", ("object1", "object2")),
),
)
count, errors = import_data(db_map, entities=(("relationship_class", ("object1", "object2")),))
self.assertEqual(count, 0)
self.assertEqual(errors, [])


class TestImportRelationship(unittest.TestCase):
@staticmethod
def populate(db_map):
Expand Down

0 comments on commit 32a23a8

Please sign in to comment.