From 342f67dfa25d470ccdaf08b0d1cb3c95d09e5b9f Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Wed, 28 Jun 2023 15:27:11 -0700 Subject: [PATCH] Protocol.load bug fix, leave new/changed None if we don't know for sure eg if local is False --- models.py | 2 ++ protocol.py | 11 +++++++---- tests/test_protocol.py | 9 +++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/models.py b/models.py index ab0fcd8..44cf27f 100644 --- a/models.py +++ b/models.py @@ -374,6 +374,8 @@ class Object(StringIdModel): # Protocol and subclasses set these in fetch if this Object is new or if its # contents have changed from what was originally loaded from the datastore. + # If either one is None, that means we don't know whether this Object is + # new/changed. new = None changed = None diff --git a/protocol.py b/protocol.py index 7c65187..475997d 100644 --- a/protocol.py +++ b/protocol.py @@ -634,6 +634,9 @@ class Protocol: def load(cls, id, remote=None, local=True, **kwargs): """Loads and returns an Object from memory cache, datastore, or HTTP fetch. + Sets the :attr:`new` and :attr:`changed` attributes if we know either + one for the loaded object, ie local is True and remote is True or None. + Note that :meth:`Object._post_put_hook` updates the cache. Args: @@ -693,14 +696,14 @@ class Protocol: obj.clear() obj.new = False else: + obj = Object(id=id) if local: logger.info(' not in datastore') - obj = Object(id=id) - obj.new = True - obj.changed = False + obj.new = True + obj.changed = False cls.fetch(obj, **kwargs) - if not obj.new: + if obj.new is False: if orig_as1 and obj.as1: obj.changed = as1.activity_changed(orig_as1, obj.as1) else: diff --git a/tests/test_protocol.py b/tests/test_protocol.py index 4645a17..98d33e2 100644 --- a/tests/test_protocol.py +++ b/tests/test_protocol.py @@ -250,6 +250,15 @@ class ProtocolTest(TestCase): self.assertFalse(loaded.new) self.assertEqual(['foo'], Fake.fetched) + def test_load_remote_true_local_false(self): + Fake.fetchable['foo'] = our_as1={'x': 'y'} + + loaded = Fake.load('foo', local=False, remote=True) + self.assertEqual({'x': 'y'}, loaded.as1) + self.assertIsNone(loaded.changed) + self.assertIsNone(loaded.new) + self.assertEqual(['foo'], Fake.fetched) + def test_load_remote_true_changed(self): self.store_object(id='foo', our_as1={'content': 'stored'}) Fake.fetchable['foo'] = {'content': 'new'}