Object.get_or_create: populate .new and .changed

pull/582/head
Ryan Barrett 2023-07-01 18:45:18 -07:00
rodzic 3ceeae418f
commit 3f9ee02126
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
2 zmienionych plików z 39 dodań i 12 usunięć

Wyświetl plik

@ -487,9 +487,19 @@ class Object(StringIdModel):
Returns:
:class:`Object`
"""
obj = cls.get_by_id(id) or Object(id=id)
obj = cls.get_by_id(id)
if obj:
obj.new = False
orig_as1 = obj.as1
else:
obj = Object(id=id)
obj.new = True
obj.clear()
obj.populate(**{k: v for k, v in props.items() if v})
if not obj.new:
obj.changed = obj.activity_changed(orig_as1)
obj.put()
return obj

Wyświetl plik

@ -168,31 +168,48 @@ class ObjectTest(TestCase):
self.assertEqual(0, Object.query().count())
obj = Object.get_or_create('foo', status='failed', source_protocol='ui',
labels=['notification'])
obj = Object.get_or_create('foo', our_as1={'content': 'foo'},
source_protocol='ui', labels=['notification'])
check([obj], Object.query().fetch())
self.assertTrue(obj.new)
self.assertIsNone(obj.changed)
self.assertEqual('foo', obj.key.id())
self.assertEqual('failed', obj.status)
self.assertEqual({'content': 'foo'}, obj.as1)
self.assertEqual('ui', obj.source_protocol)
self.assertEqual(['notification'], obj.labels)
obj2 = Object.get_or_create('foo')
self.assertFalse(obj2.new)
self.assertFalse(obj2.changed)
check(obj, obj2)
check([obj2], Object.query().fetch())
# non-null **props should be populated
obj3 = Object.get_or_create('foo', status='complete', source_protocol=None,
labels=[])
obj3 = Object.get_or_create('foo', our_as1={'content': 'bar'},
source_protocol=None, labels=[])
self.assertEqual('foo', obj3.key.id())
self.assertEqual('complete', obj3.status)
self.assertEqual('ui', obj.source_protocol)
self.assertEqual(['notification'], obj.labels)
self.assertEqual({'content': 'bar'}, obj3.as1)
self.assertEqual('ui', obj3.source_protocol)
self.assertEqual(['notification'], obj3.labels)
self.assertFalse(obj3.new)
self.assertTrue(obj3.changed)
check([obj3], Object.query().fetch())
check(obj3, Object.get_by_id('foo'))
Object.get_or_create('bar')
Object.get_or_create('baz', labels=['feed'])
obj4 = Object.get_or_create('foo', our_as1={'content': 'bar'})
self.assertEqual({'content': 'bar'}, obj4.as1)
self.assertFalse(obj4.new)
self.assertFalse(obj4.changed)
check(obj4, Object.get_by_id('foo'))
obj5 = Object.get_or_create('bar')
self.assertTrue(obj5.new)
self.assertIsNone(obj5.changed)
obj6 = Object.get_or_create('baz', labels=['feed'])
self.assertTrue(obj6.new)
self.assertIsNone(obj6.changed)
self.assertEqual(3, Object.query().count())
def test_activity_changed(self):