class ObserverWatchersRelatedWatcherBaseTestCaseOneToManyRel(TestCase):
    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 test_callback_not_called_on_modification_without_watch(self):
        new_instance = ArticleFactory()
        user = new_instance.author
        user.label = 'modified'
        user.save()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_modification_with_watch(self):
        new_instance = ArticleFactory()
        self.watcher.watch()
        user = new_instance.author
        user.label = 'modified'
        user.save()
        # callback should be called with instance modification
        self.callback.assert_called_once_with(
            obj=new_instance, attr=self.attr, sender=self.watcher)
Beispiel #2
0
class ObserverWatchersRelatedWatcherBaseTestCaseOneToManyRel(TestCase):
    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 test_callback_not_called_on_modification_without_watch(self):
        new_instance = ArticleFactory()
        user = new_instance.author
        user.label = 'modified'
        user.save()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_modification_with_watch(self):
        new_instance = ArticleFactory()
        self.watcher.watch()
        user = new_instance.author
        user.label = 'modified'
        user.save()
        # callback should be called with instance modification
        self.callback.assert_called_once_with(obj=new_instance,
                                              attr=self.attr,
                                              sender=self.watcher)
class ObserverWatchersRelatedWatcherBaseTestCaseRevManyToManyRel(TestCase):
    def setUp(self):
        self.hyperlinks = (
            HyperlinkFactory(),
            HyperlinkFactory(),
            HyperlinkFactory(),
            HyperlinkFactory(),
            HyperlinkFactory(),
        )
        self.model = Article
        self.attr = 'hyperlinks'
        self.callback = MagicMock()
        self.watcher = RelatedWatcherBase(self.model,
                                          self.attr,
                                          self.callback)
        self.addCleanup(self.watcher.unwatch)

    def test_callback_not_called_on_modification_without_watch(self):
        new_instance = ArticleFactory(hyperlinks=self.hyperlinks)
        hyperlink = new_instance.hyperlinks.get(pk=1)
        hyperlink.label = 'modified'
        hyperlink.save()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_modification_with_watch(self):
        new_instance = ArticleFactory(hyperlinks=self.hyperlinks)
        self.watcher.watch()
        hyperlink = new_instance.hyperlinks.get(pk=1)
        hyperlink.label = 'modified'
        hyperlink.save()
        # callback should be called with instance modification
        self.callback.assert_called_once_with(
            obj=new_instance, attr=self.attr, sender=self.watcher)
Beispiel #4
0
class ObserverWatchersRelatedWatcherBaseTestCaseRevManyToManyRel(TestCase):
    def setUp(self):
        self.hyperlinks = (
            HyperlinkFactory(),
            HyperlinkFactory(),
            HyperlinkFactory(),
            HyperlinkFactory(),
            HyperlinkFactory(),
        )
        self.model = Article
        self.attr = 'hyperlinks'
        self.callback = MagicMock()
        self.watcher = RelatedWatcherBase(self.model, self.attr, self.callback)
        self.addCleanup(self.watcher.unwatch)

    def test_callback_not_called_on_modification_without_watch(self):
        new_instance = ArticleFactory(hyperlinks=self.hyperlinks)
        hyperlink = new_instance.hyperlinks.get(pk=1)
        hyperlink.label = 'modified'
        hyperlink.save()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_modification_with_watch(self):
        new_instance = ArticleFactory(hyperlinks=self.hyperlinks)
        self.watcher.watch()
        hyperlink = new_instance.hyperlinks.get(pk=1)
        hyperlink.label = 'modified'
        hyperlink.save()
        # callback should be called with instance modification
        self.callback.assert_called_once_with(obj=new_instance,
                                              attr=self.attr,
                                              sender=self.watcher)