Пример #1
0
 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)
Пример #2
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)
Пример #3
0
 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)
Пример #4
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)
Пример #5
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)
Пример #6
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)
Пример #7
0
 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)
Пример #8
0
 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()
Пример #9
0
 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)
Пример #10
0
 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)
Пример #11
0
 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)
Пример #12
0
 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)
Пример #13
0
class ObserverWatchersValueWatcherTestCase(TestCase):
    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_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.title = 'modified'
        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.title = 'modified'
        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)
Пример #14
0
 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)
Пример #15
0
 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)
Пример #16
0
 def setUp(self):
     self.model = Article
     self.attr = 'title'
     self.callback = MagicMock()
     self.watcher = WatcherBase(self.model,
                                self.attr,
                                self.callback)
Пример #17
0
 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)
Пример #18
0
 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)
Пример #19
0
 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)
Пример #20
0
 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)
Пример #21
0
    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)
Пример #22
0
 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)
Пример #23
0
 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)
Пример #24
0
 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)
Пример #25
0
 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)
Пример #26
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)
Пример #27
0
class ObserverWatchersWatcherBaseTestCase(TestCase):
    def setUp(self):
        self.model = Article
        self.attr = 'title'
        self.callback = MagicMock()
        self.watcher = WatcherBase(self.model, self.attr, self.callback)

    def test_watch_raise_exception(self):
        """watch should raise NotImplementedError"""
        self.assertRaises(NotImplementedError, self.watcher.watch)

    def test_unwatch_raise_exception(self):
        """unwatch should raise NotImplementedError"""
        self.assertRaises(NotImplementedError, self.watcher.watch)

    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_get_field_return_field(self):
        """get_field should return field instance"""
        self.assertTrue(isinstance(self.watcher.get_field(), models.CharField))

    def test_get_field_return_field_with_attr(self):
        """get_field should return field instance"""
        self.assertTrue(
            isinstance(self.watcher.get_field('author'), models.ForeignKey))

    def test_construct_with_string_relation(self):
        field = WatcherBase('observer.ObserverTestArticle', self.attr,
                            self.callback)
        self.assertEqual(field.model, Article)

    @patch.multiple('observer.watchers.base',
                    get_model=DEFAULT,
                    class_prepared=DEFAULT)
    def test_construct_with_string_relation_lazy_relation(
            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)
        # Article haven't ready yet (get_model return None)
        self.assertEqual(field.model, 'observer.ObserverTestArticle')
        # emulate class_prepared signal
        do_pending_lookups(Article)
        # Article had ready (class_prepared signal call do_pending_lookups)
        self.assertEqual(field.model, Article)

    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)

    @patch.multiple('observer.watchers.base',
                    get_model=DEFAULT,
                    class_prepared=DEFAULT)
    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)

    @patch.multiple('observer.watchers.base',
                    get_model=DEFAULT,
                    class_prepared=DEFAULT)
    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)
Пример #28
0
class ObserverWatchersManyRelatedWatcherTestCaseRevManyToManyRel(TestCase):
    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 test_callback_not_called_on_add_without_watch(self):
        new_instance = ArticleFactory(hyperlinks=self.hyperlinks)
        new_instance.hyperlinks.add(HyperlinkFactory())
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_add_with_watch(self):
        new_instance = ArticleFactory(hyperlinks=self.hyperlinks)
        self.watcher.watch()
        new_instance.hyperlinks.add(HyperlinkFactory())
        # 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_remove_without_watch(self):
        new_instance = ArticleFactory(hyperlinks=self.hyperlinks)
        new_instance.hyperlinks.get(pk=1).delete()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_remove_with_watch(self):
        new_instance = ArticleFactory(hyperlinks=self.hyperlinks)
        self.watcher.watch()
        new_instance.hyperlinks.remove(self.hyperlinks[0])
        # 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_reverse_add_without_watch(self):
        new_instance = HyperlinkFactory()
        add_instance = ArticleFactory()
        new_instance.articles.add(add_instance)
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_reverse_add_with_watch(self):
        new_instance = HyperlinkFactory()
        add_instance = ArticleFactory()
        self.watcher.watch()
        new_instance.articles.add(add_instance)
        # callback should be called with instance modification
        self.callback.assert_called_once_with(obj=add_instance,
                                              attr=self.attr,
                                              sender=self.watcher)

    def test_callback_not_called_on_reverse_remove_without_watch(self):
        add_instance = ArticleFactory(hyperlinks=self.hyperlinks)
        self.hyperlinks[0].articles.remove(add_instance)
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_reverse_remove_with_watch(self):
        add_instance = ArticleFactory(hyperlinks=self.hyperlinks)
        self.watcher.watch()
        self.hyperlinks[0].articles.remove(add_instance)
        # callback should be called with instance modification
        self.callback.assert_called_once_with(obj=add_instance,
                                              attr=self.attr,
                                              sender=self.watcher)

    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_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_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)
Пример #29
0
 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)
Пример #30
0
class ObserverWatchersGenericRelatedWatcherTestCase(TestCase):
    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 test_callback_not_called_on_add_without_watch(self):
        new_instance = ArticleFactory(tags=self.tags)
        new_instance.tags.add(TagFactory())
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_add_with_watch(self):
        new_instance = ArticleFactory(tags=self.tags)
        self.watcher.watch()
        new_instance.tags.add(TagFactory())
        # 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_remove_without_watch(self):
        new_instance = ArticleFactory(tags=self.tags)
        new_instance.tags.remove(self.tags[0])
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    @skip("TODO: Fixme")
    def test_callback_called_on_remove_with_watch(self):
        new_instance = ArticleFactory(tags=self.tags)
        self.watcher.watch()
        new_instance.tags.remove(self.tags[0])
        self.assertFalse(self.tags in new_instance.tags.all())
        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_reverse_add_without_watch(self):
        new_instance = TagFactory()
        add_instance = ArticleFactory()
        new_instance.content_object = add_instance
        new_instance.save()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_reverse_add_with_watch(self):
        new_instance = TagFactory()
        add_instance = ArticleFactory()
        self.watcher.watch()
        new_instance.content_object = add_instance
        new_instance.save()
        # callback should be called with instance modification
        self.callback.assert_called_once_with(obj=add_instance,
                                              attr=self.attr,
                                              sender=self.watcher)

    def test_callback_not_called_on_reverse_remove_without_watch(self):
        ArticleFactory(tags=self.tags)
        self.tags[0].article = None
        self.tags[0].save()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    @skip("TODO: Fixme")
    def test_callback_called_on_reverse_remove_with_watch(self):
        new_instance = ArticleFactory(tags=self.tags)
        self.watcher.watch()
        self.tags[0].article = None
        self.tags[0].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_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_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_modification_without_watch(self):
        new_instance = ArticleFactory(tags=self.tags)
        tag = new_instance.tags.get(pk=1)
        tag.label = 'modified'
        tag.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(tags=self.tags)
        self.watcher.watch()
        tag = new_instance.tags.get(pk=1)
        tag.label = 'modified'
        tag.save()
        # callback should be called with instance modification
        self.callback.assert_called_once_with(obj=new_instance,
                                              attr=self.attr,
                                              sender=self.watcher)
Пример #31
0
class ObserverWatchersWatcherBaseTestCase(TestCase):
    def setUp(self):
        self.model = Article
        self.attr = 'title'
        self.callback = MagicMock()
        self.watcher = WatcherBase(self.model,
                                   self.attr,
                                   self.callback)

    def test_watch_raise_exception(self):
        """watch should raise NotImplementedError"""
        self.assertRaises(NotImplementedError,
                          self.watcher.watch)

    def test_unwatch_raise_exception(self):
        """unwatch should raise NotImplementedError"""
        self.assertRaises(NotImplementedError,
                          self.watcher.watch)

    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_get_field_return_field(self):
        """get_field should return field instance"""
        self.assertTrue(isinstance(self.watcher.get_field(),
                                   models.CharField))

    def test_get_field_return_field_with_attr(self):
        """get_field should return field instance"""
        self.assertTrue(isinstance(self.watcher.get_field('author'),
                                   models.ForeignKey))

    def test_construct_with_string_relation(self):
        field = WatcherBase('observer.ObserverTestArticle',
                            self.attr, self.callback)
        self.assertEqual(field.model, Article)

    @patch.multiple('observer.watchers.base',
                    get_model=DEFAULT, class_prepared=DEFAULT)
    def test_construct_with_string_relation_lazy_relation(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)
        # Article haven't ready yet (get_model return None)
        self.assertEqual(field.model, 'observer.ObserverTestArticle')
        # emulate class_prepared signal
        do_pending_lookups(Article)
        # Article had ready (class_prepared signal call do_pending_lookups)
        self.assertEqual(field.model, Article)

    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)

    @patch.multiple('observer.watchers.base',
                    get_model=DEFAULT, class_prepared=DEFAULT)
    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)

    @patch.multiple('observer.watchers.base',
                    get_model=DEFAULT, class_prepared=DEFAULT)
    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)
Пример #32
0
class ObserverWatchersGenericRelatedWatcherTestCaseRev(TestCase):
    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_callback_not_called_on_create_without_watch(self):
        TagFactory()
        # 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 = TagFactory()
        # 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)
        TagFactory()
        # 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 = TagFactory()
        new_instance.content_object = ArticleFactory()
        new_instance.save()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    @skip("TODO: Fix me")
    def test_callback_called_on_modification_with_watch(self):
        new_instance = TagFactory()
        self.watcher.watch()
        new_instance.content_object = ArticleFactory()
        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 = TagFactory()
        new_instance.label = '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 = TagFactory(content_object=UserFactory())
        user = new_instance.content_object
        user.label = 'modified'
        user.save()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    @skip("TODO: Fix me")
    def test_callback_called_on_related_modification_with_watch(self):
        new_instance = TagFactory(content_object=UserFactory())
        user = new_instance.content_object
        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)
Пример #33
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)
Пример #34
0
class ObserverWatchersManyRelatedWatcherTestCaseRevManyToOneRel(TestCase):
    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 test_callback_not_called_on_add_without_watch(self):
        new_instance = ArticleFactory(projects=self.projects)
        new_instance.projects.add(ProjectFactory())
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_add_with_watch(self):
        new_instance = ArticleFactory(projects=self.projects)
        self.watcher.watch()
        new_instance.projects.add(ProjectFactory())
        # 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_remove_without_watch(self):
        new_instance = ArticleFactory(projects=self.projects)
        new_instance.projects.remove(self.projects[0])
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_remove_with_watch(self):
        new_instance = ArticleFactory(projects=self.projects)
        self.watcher.watch()
        new_instance.projects.remove(self.projects[0])
        # 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_reverse_add_without_watch(self):
        new_instance = ProjectFactory()
        add_instance = ArticleFactory()
        new_instance.article = add_instance
        new_instance.save()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_reverse_add_with_watch(self):
        new_instance = ProjectFactory()
        add_instance = ArticleFactory()
        self.watcher.watch()
        new_instance.article = add_instance
        new_instance.save()
        # callback should be called with instance modification
        self.callback.assert_called_once_with(
            obj=add_instance, attr=self.attr, sender=self.watcher)

    def test_callback_not_called_on_reverse_remove_without_watch(self):
        ArticleFactory(projects=self.projects)
        self.projects[0].article = None
        self.projects[0].save()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_reverse_remove_with_watch(self):
        new_instance = ArticleFactory(projects=self.projects)
        self.watcher.watch()
        self.projects[0].article = None
        self.projects[0].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_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_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_modification_without_watch(self):
        new_instance = ArticleFactory(projects=self.projects)
        project = new_instance.projects.get(pk=1)
        project.label = 'modified'
        project.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(projects=self.projects)
        self.watcher.watch()
        project = new_instance.projects.get(pk=1)
        project.label = 'modified'
        project.save()
        # callback should be called with instance modification
        self.callback.assert_called_once_with(
            obj=new_instance, attr=self.attr, sender=self.watcher)
Пример #35
0
 def setUp(self):
     self.model = Article
     self.attr = 'title'
     self.callback = MagicMock()
     self.watcher = WatcherBase(self.model, self.attr, self.callback)
Пример #36
0
class ObserverWatchersManyRelatedWatcherTestCaseRevManyToOneRel(TestCase):
    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 test_callback_not_called_on_add_without_watch(self):
        new_instance = ArticleFactory(projects=self.projects)
        new_instance.projects.add(ProjectFactory())
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_add_with_watch(self):
        new_instance = ArticleFactory(projects=self.projects)
        self.watcher.watch()
        new_instance.projects.add(ProjectFactory())
        # 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_remove_without_watch(self):
        new_instance = ArticleFactory(projects=self.projects)
        new_instance.projects.remove(self.projects[0])
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_remove_with_watch(self):
        new_instance = ArticleFactory(projects=self.projects)
        self.watcher.watch()
        new_instance.projects.remove(self.projects[0])
        # 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_reverse_add_without_watch(self):
        new_instance = ProjectFactory()
        add_instance = ArticleFactory()
        new_instance.article = add_instance
        new_instance.save()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_reverse_add_with_watch(self):
        new_instance = ProjectFactory()
        add_instance = ArticleFactory()
        self.watcher.watch()
        new_instance.article = add_instance
        new_instance.save()
        # callback should be called with instance modification
        self.callback.assert_called_once_with(obj=add_instance,
                                              attr=self.attr,
                                              sender=self.watcher)

    def test_callback_not_called_on_reverse_remove_without_watch(self):
        ArticleFactory(projects=self.projects)
        self.projects[0].article = None
        self.projects[0].save()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_reverse_remove_with_watch(self):
        new_instance = ArticleFactory(projects=self.projects)
        self.watcher.watch()
        self.projects[0].article = None
        self.projects[0].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_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_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_modification_without_watch(self):
        new_instance = ArticleFactory(projects=self.projects)
        project = new_instance.projects.get(pk=1)
        project.label = 'modified'
        project.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(projects=self.projects)
        self.watcher.watch()
        project = new_instance.projects.get(pk=1)
        project.label = 'modified'
        project.save()
        # callback should be called with instance modification
        self.callback.assert_called_once_with(obj=new_instance,
                                              attr=self.attr,
                                              sender=self.watcher)
Пример #37
0
class ObserverWatchersGenericRelatedWatcherTestCaseRev(TestCase):
    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_callback_not_called_on_create_without_watch(self):
        TagFactory()
        # 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 = TagFactory()
        # 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)
        TagFactory()
        # 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 = TagFactory()
        new_instance.content_object = ArticleFactory()
        new_instance.save()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    @skip("TODO: Fix me")
    def test_callback_called_on_modification_with_watch(self):
        new_instance = TagFactory()
        self.watcher.watch()
        new_instance.content_object = ArticleFactory()
        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 = TagFactory()
        new_instance.label = '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 = TagFactory(content_object=UserFactory())
        user = new_instance.content_object
        user.label = 'modified'
        user.save()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    @skip("TODO: Fix me")
    def test_callback_called_on_related_modification_with_watch(self):
        new_instance = TagFactory(content_object=UserFactory())
        user = new_instance.content_object
        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)
Пример #38
0
class ObserverWatchersGenericRelatedWatcherTestCase(TestCase):
    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 test_callback_not_called_on_add_without_watch(self):
        new_instance = ArticleFactory(tags=self.tags)
        new_instance.tags.add(TagFactory())
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_add_with_watch(self):
        new_instance = ArticleFactory(tags=self.tags)
        self.watcher.watch()
        new_instance.tags.add(TagFactory())
        # 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_remove_without_watch(self):
        new_instance = ArticleFactory(tags=self.tags)
        new_instance.tags.remove(self.tags[0])
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    @skip("TODO: Fixme")
    def test_callback_called_on_remove_with_watch(self):
        new_instance = ArticleFactory(tags=self.tags)
        self.watcher.watch()
        new_instance.tags.remove(self.tags[0])
        self.assertFalse(self.tags in new_instance.tags.all())
        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_reverse_add_without_watch(self):
        new_instance = TagFactory()
        add_instance = ArticleFactory()
        new_instance.content_object = add_instance
        new_instance.save()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_reverse_add_with_watch(self):
        new_instance = TagFactory()
        add_instance = ArticleFactory()
        self.watcher.watch()
        new_instance.content_object = add_instance
        new_instance.save()
        # callback should be called with instance modification
        self.callback.assert_called_once_with(
            obj=add_instance, attr=self.attr, sender=self.watcher)

    def test_callback_not_called_on_reverse_remove_without_watch(self):
        ArticleFactory(tags=self.tags)
        self.tags[0].article = None
        self.tags[0].save()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    @skip("TODO: Fixme")
    def test_callback_called_on_reverse_remove_with_watch(self):
        new_instance = ArticleFactory(tags=self.tags)
        self.watcher.watch()
        self.tags[0].article = None
        self.tags[0].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_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_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_modification_without_watch(self):
        new_instance = ArticleFactory(tags=self.tags)
        tag = new_instance.tags.get(pk=1)
        tag.label = 'modified'
        tag.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(tags=self.tags)
        self.watcher.watch()
        tag = new_instance.tags.get(pk=1)
        tag.label = 'modified'
        tag.save()
        # callback should be called with instance modification
        self.callback.assert_called_once_with(
            obj=new_instance, attr=self.attr, sender=self.watcher)
Пример #39
0
class ObserverWatchersManyRelatedWatcherTestCaseRevManyToManyRel(TestCase):
    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 test_callback_not_called_on_add_without_watch(self):
        new_instance = ArticleFactory(hyperlinks=self.hyperlinks)
        new_instance.hyperlinks.add(HyperlinkFactory())
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_add_with_watch(self):
        new_instance = ArticleFactory(hyperlinks=self.hyperlinks)
        self.watcher.watch()
        new_instance.hyperlinks.add(HyperlinkFactory())
        # 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_remove_without_watch(self):
        new_instance = ArticleFactory(hyperlinks=self.hyperlinks)
        new_instance.hyperlinks.get(pk=1).delete()
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_remove_with_watch(self):
        new_instance = ArticleFactory(hyperlinks=self.hyperlinks)
        self.watcher.watch()
        new_instance.hyperlinks.remove(self.hyperlinks[0])
        # 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_reverse_add_without_watch(self):
        new_instance = HyperlinkFactory()
        add_instance = ArticleFactory()
        new_instance.articles.add(add_instance)
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_reverse_add_with_watch(self):
        new_instance = HyperlinkFactory()
        add_instance = ArticleFactory()
        self.watcher.watch()
        new_instance.articles.add(add_instance)
        # callback should be called with instance modification
        self.callback.assert_called_once_with(
            obj=add_instance, attr=self.attr, sender=self.watcher)

    def test_callback_not_called_on_reverse_remove_without_watch(self):
        add_instance = ArticleFactory(hyperlinks=self.hyperlinks)
        self.hyperlinks[0].articles.remove(add_instance)
        # have not watched, thus callback should not be called
        self.assertFalse(self.callback.called)

    def test_callback_called_on_reverse_remove_with_watch(self):
        add_instance = ArticleFactory(hyperlinks=self.hyperlinks)
        self.watcher.watch()
        self.hyperlinks[0].articles.remove(add_instance)
        # callback should be called with instance modification
        self.callback.assert_called_once_with(
            obj=add_instance, attr=self.attr, sender=self.watcher)

    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_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_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)
Пример #40
0
 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)