Ejemplo n.º 1
0
 def setUp(self):
     self.model = Article
     self.attr = 'author'
     self.callback = MagicMock()
     self.watcher = RelatedWatcher(self.model,
                                   self.attr,
                                   self.callback)
     self.addCleanup(self.watcher.unwatch)
Ejemplo n.º 2
0
class ObserverWatchersRelatedWatcherTestCaseOneToManyRel(TestCase):
    def setUp(self):
        self.model = Article
        self.attr = 'author'
        self.callback = MagicMock()
        self.watcher = RelatedWatcher(self.model,
                                      self.attr,
                                      self.callback)
        self.addCleanup(self.watcher.unwatch)

    def test_callback_not_called_on_create_without_watch(self):
        ArticleFactory()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_create_with_watch(self):
        self.watcher.watch()
        new_instance = ArticleFactory()
        # callback should be called with newly created instance
        self.callback.assert_called_once_with(
            obj=new_instance, attr=self.attr, sender=self.watcher)

    def test_callback_not_called_on_create_without_call_on_created(self):
        self.watcher.watch(call_on_created=False)
        ArticleFactory()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_not_called_on_modification_without_watch(self):
        new_instance = ArticleFactory()
        new_instance.author = UserFactory()
        new_instance.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()
        new_instance.author = UserFactory()
        new_instance.save()
        # callback should be called with instance modification
        self.callback.assert_called_once_with(
            obj=new_instance, attr=self.attr, sender=self.watcher)

    def test_callback_not_called_on_modification_with_non_interest_attr(self):
        new_instance = ArticleFactory()
        new_instance.content = 'modified'
        new_instance.save()
        # content is not watched thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_not_called_on_related_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_related_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)
Ejemplo n.º 3
0
class ObserverWatchersRelatedWatcherTestCaseRevOneToOneRel(TestCase):
    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)

    def test_callback_not_called_on_create_without_watch(self):
        ArticleFactory()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_create_with_watch(self):
        self.watcher.watch()
        new_instance = ArticleFactory()
        # callback should be called with newly created instance
        self.callback.assert_called_once_with(
            obj=new_instance, attr=self.attr, sender=self.watcher)

    def test_callback_not_called_on_create_without_call_on_created(self):
        self.watcher.watch(call_on_created=False)
        ArticleFactory()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_not_called_on_modification_without_watch(self):
        new_instance = ArticleFactory()
        new_instance.revision = RevisionFactory()
        new_instance.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(revision=None)
        new_revision = RevisionFactory()
        self.watcher.watch()
        # reverse assignment does not work (it is django's definition)
        new_instance.revision = new_revision
        new_instance.save()
        self.assertRaises(
            ObjectDoesNotExist,
            lambda: Article.objects.get(pk=new_instance.pk).revision)
        # thus assign directly to new_revision
        new_revision.article = new_instance
        new_revision.save()
        # callback should be called with instance modification
        self.callback.assert_called_once_with(
            obj=new_instance, attr=self.attr, sender=self.watcher)

    def test_callback_not_called_on_modification_with_non_interest_attr(self):
        new_instance = ArticleFactory()
        new_instance.content = 'modified'
        new_instance.save()
        # content is not watched thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_not_called_on_related_modification_without_watch(self):
        new_instance = ArticleFactory()
        revision = new_instance.revision
        revision.label = 'modified'
        revision.save()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_related_modification_with_watch(self):
        new_instance = ArticleFactory()
        self.watcher.watch()
        revision = new_instance.revision
        revision.label = 'modified'
        revision.save()
        # callback should be called with instance modification
        self.callback.assert_called_once_with(
            obj=new_instance, attr=self.attr, sender=self.watcher)
Ejemplo n.º 4
0
 def setUp(self):
     self.model = Article
     self.attr = 'author'
     self.callback = MagicMock()
     self.watcher = RelatedWatcher(self.model, self.attr, self.callback)
     self.addCleanup(self.watcher.unwatch)
Ejemplo n.º 5
0
class ObserverWatchersRelatedWatcherTestCaseOneToManyRel(TestCase):
    def setUp(self):
        self.model = Article
        self.attr = 'author'
        self.callback = MagicMock()
        self.watcher = RelatedWatcher(self.model, self.attr, self.callback)
        self.addCleanup(self.watcher.unwatch)

    def test_callback_not_called_on_create_without_watch(self):
        ArticleFactory()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_create_with_watch(self):
        self.watcher.watch()
        new_instance = ArticleFactory()
        # callback should be called with newly created instance
        self.callback.assert_called_once_with(obj=new_instance,
                                              attr=self.attr,
                                              sender=self.watcher)

    def test_callback_not_called_on_create_without_call_on_created(self):
        self.watcher.watch(call_on_created=False)
        ArticleFactory()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_not_called_on_modification_without_watch(self):
        new_instance = ArticleFactory()
        new_instance.author = UserFactory()
        new_instance.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()
        new_instance.author = UserFactory()
        new_instance.save()
        # callback should be called with instance modification
        self.callback.assert_called_once_with(obj=new_instance,
                                              attr=self.attr,
                                              sender=self.watcher)

    def test_callback_not_called_on_modification_with_non_interest_attr(self):
        new_instance = ArticleFactory()
        new_instance.content = 'modified'
        new_instance.save()
        # content is not watched thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_not_called_on_related_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_related_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)
Ejemplo n.º 6
0
class ObserverWatchersRelatedWatcherTestCaseRevOneToOneRel(TestCase):
    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)

    def test_callback_not_called_on_create_without_watch(self):
        ArticleFactory()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_create_with_watch(self):
        self.watcher.watch()
        new_instance = ArticleFactory()
        # callback should be called with newly created instance
        self.callback.assert_called_once_with(obj=new_instance,
                                              attr=self.attr,
                                              sender=self.watcher)

    def test_callback_not_called_on_create_without_call_on_created(self):
        self.watcher.watch(call_on_created=False)
        ArticleFactory()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_not_called_on_modification_without_watch(self):
        new_instance = ArticleFactory()
        new_instance.revision = RevisionFactory()
        new_instance.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(revision=None)
        new_revision = RevisionFactory()
        self.watcher.watch()
        # reverse assignment does not work (it is django's definition)
        new_instance.revision = new_revision
        new_instance.save()
        self.assertRaises(
            ObjectDoesNotExist,
            lambda: Article.objects.get(pk=new_instance.pk).revision)
        # thus assign directly to new_revision
        new_revision.article = new_instance
        new_revision.save()
        # callback should be called with instance modification
        self.callback.assert_called_once_with(obj=new_instance,
                                              attr=self.attr,
                                              sender=self.watcher)

    def test_callback_not_called_on_modification_with_non_interest_attr(self):
        new_instance = ArticleFactory()
        new_instance.content = 'modified'
        new_instance.save()
        # content is not watched thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_not_called_on_related_modification_without_watch(self):
        new_instance = ArticleFactory()
        revision = new_instance.revision
        revision.label = 'modified'
        revision.save()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_related_modification_with_watch(self):
        new_instance = ArticleFactory()
        self.watcher.watch()
        revision = new_instance.revision
        revision.label = 'modified'
        revision.save()
        # callback should be called with instance modification
        self.callback.assert_called_once_with(obj=new_instance,
                                              attr=self.attr,
                                              sender=self.watcher)