def test_lazy_watch_with_unprepared_relation(self, get_model, class_prepared): from observer.watchers.base import do_pending_lookups from observer.tests.models import User # emulate the situlation that User has not prepared yet get_model.return_value = None self.watcher._attr = 'author' self.watcher.watch = MagicMock() self.watcher.get_field().rel.to = 'observer.ObserverTestUser' kwargs = dict( foo=MagicMock(), bar=MagicMock(), hoge=MagicMock(), ) self.watcher.lazy_watch(**kwargs) # the rel.to have not ready yet thus watch should not be called yet self.assertFalse(self.watcher.watch.called) # emulate class_prepared signal # Note: # rel.to assignment is proceeded by other function thus it is # required to do manually, not like model assignment self.watcher.get_field().rel.to = User do_pending_lookups(User) # User has ready thus watch should be called automatically self.watcher.watch.assert_called_once_with(**kwargs)
def test_lazy_watch_call_watch(self): self.watcher.watch = MagicMock() kwargs = dict( foo=MagicMock(), bar=MagicMock(), hoge=MagicMock(), ) self.watcher.lazy_watch(**kwargs) self.watcher.watch.assert_called_once_with(**kwargs)
def setUp(self): self.model = Article self.attr = 'foobar' self.callback = MagicMock() self.Investigator = MagicMock(wraps=Investigator) self.investigator = self.Investigator(self.model) self.investigator._object_cached[1] = ArticleFactory() self.investigator._object_cached[2] = ArticleFactory() self.investigator._object_cached[3] = ArticleFactory()
def setUp(self): self.model = Article self.attr = 'title' self.callback = MagicMock() self.Watcher = MagicMock(wraps=ValueWatcher) self.watcher = self.Watcher(self.model, self.attr, self.callback) self.addCleanup(self.watcher.unwatch)
def test_call_call_callback(self): """call should call callback""" obj = MagicMock() self.watcher.call(obj) self.callback.assert_called_once_with(sender=self.watcher, obj=obj, attr=self.attr)
def test_prepare_not_update_cache(self): """prepare should not update cache if instance does not have pk""" pk = None new_instance = MagicMock(pk=pk) raw_instance = MagicMock(pk=pk) old_instance = MagicMock(pk=pk) # prepare cache manually / patch method self.investigator._object_cached[pk] = old_instance self.investigator.get_object = MagicMock(return_value=raw_instance) # make sure that get_cached(pk) return correct value self.assertEqual(self.investigator.get_cached(pk), old_instance) # call prepare with new instance self.investigator.prepare(new_instance) self.assertEqual(self.investigator.get_cached(pk), old_instance) self.assertNotEqual(self.investigator.get_cached(pk), new_instance) self.assertNotEqual(self.investigator.get_cached(pk), raw_instance)
def setUp(self): self.model = Tag self.attr = 'content_object' self.callback = MagicMock() self.watcher = GenericRelatedWatcher(self.model, self.attr, self.callback) self.addCleanup(self.watcher.unwatch)
def test_lazy_watch_with_unprepared_model(self, get_model, class_prepared): from observer.watchers.base import do_pending_lookups # emulate the situlation that Article has not prepared yet get_model.return_value = None field = WatcherBase('observer.ObserverTestArticle', self.attr, self.callback) field.watch = MagicMock() kwargs = dict( foo=MagicMock(), bar=MagicMock(), hoge=MagicMock(), ) field.lazy_watch(**kwargs) # the model have not ready yet thus watch should not be called yet self.assertFalse(field.watch.called) # emulate class_prepared signal do_pending_lookups(Article) # Article has ready thus watch should be called automatically field.watch.assert_called_once_with(**kwargs)
def setUp(self): self.hyperlinks = ( HyperlinkFactory(), HyperlinkFactory(), HyperlinkFactory(), HyperlinkFactory(), HyperlinkFactory(), ) self.model = Article self.attr = 'hyperlinks' self.callback = MagicMock() self.watcher = ManyRelatedWatcher(self.model, self.attr, self.callback) self.addCleanup(self.watcher.unwatch)
def setUp(self): self.users = ( UserFactory(), UserFactory(), UserFactory(), UserFactory(), UserFactory(), ) self.model = Article self.attr = 'collaborators' self.callback = MagicMock() self.watcher = ManyRelatedWatcher(self.model, self.attr, self.callback) self.addCleanup(self.watcher.unwatch)
def setUp(self): self.projects = ( ProjectFactory(), ProjectFactory(), ProjectFactory(), ProjectFactory(), ProjectFactory(), ) self.model = Article self.attr = 'projects' self.callback = MagicMock() self.watcher = ManyRelatedWatcher(self.model, self.attr, self.callback) self.addCleanup(self.watcher.unwatch)
def setUp(self): self.tags = ( TagFactory(), TagFactory(), TagFactory(), TagFactory(), TagFactory(), ) self.model = Article self.attr = 'tags' self.callback = MagicMock() self.watcher = GenericRelatedWatcher(self.model, self.attr, self.callback) self.addCleanup(self.watcher.unwatch)
def setUp(self): self.model = Article self.attr = 'title' self.callback = MagicMock() self.watcher = WatcherBase(self.model, self.attr, self.callback)
def setUp(self): self.model = Article self.attr = 'author' self.callback = MagicMock() self.watcher = RelatedWatcherBase(self.model, self.attr, self.callback) self.addCleanup(self.watcher.unwatch)
def setUp(self): self.model = Article self.attr = 'revision' self.callback = MagicMock() self.watcher = RelatedWatcher(self.model, self.attr, self.callback) self.addCleanup(self.watcher.unwatch)