From 9d1dc652f9bf342c2ba8582e35285d35d278c465 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Fri, 2 Jun 2023 21:53:44 -0700 Subject: [PATCH] Protocol.load: bug fix, handle Object.as1 going from non-None to None fixes https://console.cloud.google.com/errors/detail/CI3L3MzXjPza9gE;time=P30D?project=bridgy-federated --- protocol.py | 9 ++++++--- tests/test_protocol.py | 31 ++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/protocol.py b/protocol.py index d05ad14..358be8f 100644 --- a/protocol.py +++ b/protocol.py @@ -423,6 +423,7 @@ class Protocol: if obj: obj.clear() + obj.new = False else: logger.info(f' not in datastore') obj = Object(id=id) @@ -430,9 +431,11 @@ class Protocol: obj.changed = False cls.fetch(obj, **kwargs) - if orig_as1: - obj.new = False - obj.changed = as1.activity_changed(orig_as1, obj.as1) + if not obj.new: + if orig_as1 and obj.as1: + obj.changed = as1.activity_changed(orig_as1, obj.as1) + else: + obj.changed = bool(orig_as1) != bool(obj.as1) obj.source_protocol = cls.LABEL obj.put() diff --git a/tests/test_protocol.py b/tests/test_protocol.py index d5fd72e..4fddb27 100644 --- a/tests/test_protocol.py +++ b/tests/test_protocol.py @@ -76,7 +76,7 @@ class ProtocolTest(TestCase): self.assertIsNotNone(Object.get_by_id('foo')) self.assertEqual(['foo'], Fake.fetched) - def test_load_already_stored(self): + def test_load_existing(self): stored = Object(id='foo', our_as1={'x': 'y'}) stored.put() @@ -87,8 +87,7 @@ class ProtocolTest(TestCase): self.assertEqual([], Fake.fetched) - @patch('requests.get') - def test_load_empty_deleted(self, mock_get): + def test_load_existing_empty_deleted(self): stored = Object(id='foo', deleted=True) stored.put() @@ -99,8 +98,27 @@ class ProtocolTest(TestCase): self.assertEqual([], Fake.fetched) - @patch('requests.get') - def test_load_refresh_unchanged(self, mock_get): + def test_load_refresh_existing_empty(self): + Fake.objects['foo'] = {'x': 'y'} + Object(id='foo').put() + + loaded = Fake.load('foo', refresh=True) + self.assertEqual({'x': 'y'}, loaded.as1) + self.assertTrue(loaded.changed) + self.assertFalse(loaded.new) + self.assertEqual(['foo'], Fake.fetched) + + def test_load_refresh_new_empty(self): + Fake.objects['foo'] = None + Object(id='foo', our_as1={'x': 'y'}).put() + + loaded = Fake.load('foo', refresh=True) + self.assertIsNone(loaded.as1) + self.assertTrue(loaded.changed) + self.assertFalse(loaded.new) + self.assertEqual(['foo'], Fake.fetched) + + def test_load_refresh_unchanged(self): obj = Object(id='foo', our_as1={'x': 'stored'}) obj.put() Fake.objects['foo'] = {'x': 'stored'} @@ -111,8 +129,7 @@ class ProtocolTest(TestCase): self.assertFalse(obj.new) self.assertEqual(['foo'], Fake.fetched) - @patch('requests.get') - def test_load_refresh_changed(self, mock_get): + def test_load_refresh_changed(self): Object(id='foo', our_as1={'content': 'stored'}).put() Fake.objects['foo'] = {'content': 'new'}