Example #1
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)
Example #2
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)
Example #3
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)
 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)
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)
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)
Example #9
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)
Example #10
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)