def _merge_nodes(self, source, replacement): assert source.graph is replacement.graph for k, v in source.attrs.items(): if k in replacement.attrs: old_val = replacement.attrs[k] if v == old_val: continue field = replacement.model._meta.get_field(k) if isinstance(field, DateTimeField): new_val = max(pendulum.parse(v), pendulum.parse(old_val)).isoformat() else: # use the longer value, or the first alphabetically if they're the same length new_val = sorted([v, old_val], key=lambda x: (-len(str(x)), x))[0] else: new_val = source.attrs[k] replacement.attrs[k] = new_val from share.models import Person if replacement.model == Person: replacement.attrs['name'] = '' Person.normalize(replacement, replacement.graph) source.graph.replace(source, replacement)
def test_many_versions(self, change_ids): p = Person(given_name='John', family_name='Doe', change_id=change_ids.get()) p.save() names = ['Jane', 'John', 'Jone', 'Jane', 'James', 'Joe', 'Jim', 'Jack', 'Jacklynn'] for name in names: p.given_name = name p.change_id = change_ids.get() p.save() p.refresh_from_db() assert p.versions.first() == p.version for i, name in enumerate(reversed(['John'] + names)): assert p.versions.all()[i].given_name == name
def test_simple(self, change_ids): p = Person(given_name='John', family_name='Doe', change_id=change_ids.get()) p.save() p.given_name = 'Jane' p.change_id = change_ids.get() p.save() assert p.versions.all()[0].given_name == 'Jane' assert p.versions.all()[1].given_name == 'John'
def test_timestamping(share_source, change_ids): p = Person(given_name='John', family_name='Doe', change_id=change_ids.get()) p.save() created, modified = p.date_created, p.date_modified assert (p.date_created - p.date_modified).total_seconds() < 1 p.given_name = 'Jane' p.change_id = change_ids.get() p.save() assert modified < p.date_modified assert created == p.date_created
def test_creates_version(self, change_ids): p = Person(given_name='John', family_name='Doe', change_id=change_ids.get()) p.save() p.refresh_from_db() assert isinstance(p.version, Person.VersionModel)
def test_creates_version(share_source, change_ids): p = Person(given_name='John', family_name='Doe', change_id=change_ids.get()) p.save() p.refresh_from_db() assert isinstance(p.version, Person.VersionModel)