Ejemplo n.º 1
0
class FilledModelMetadataTests(TestCase):

    def setUp(self):
        self.site1 = create_site()
        self.site2 = create_site()

        self.article = create_article(language='ru', site=self.site1)
        self.forum = create_forum(language='ru', sites=[self.site1, self.site2])

        self.article_metadata = ModelMetadata(Article,
            language_field_name='language',
            sites_field_name='site',
        )
        self.article_metadata.enable()

        self.forum_metadata = ModelMetadata(Forum,
            language_field_name='language',
            sites_field_name='sites',
        )
        self.forum_metadata.enable()

    def tearDown(self):
        self.article_metadata.disable()
        self.forum_metadata.disable()

    def test_returns_object_language(self):
        with translation.override('en'):
            language = self.article_metadata.language(self.article)
        self.assertEqual(language, 'ru')

    def test_returns_object_site(self):
        sites = self.article_metadata.sites(self.article)
        self.assertEqual(len(sites), 1)
        self.assertIn(self.site1, sites)

    def test_returns_object_sites(self):
        sites = self.forum_metadata.sites(self.forum)
        self.assertEqual(len(sites), 2)
        self.assertIn(self.site1, sites)
        self.assertIn(self.site2, sites)

    def test_returns_empty_list_of_sites_if_field_name_is_wrong(self):
        self.forum_metadata.sites_field_name = 'abcdef'
        sites = self.forum_metadata.sites(self.forum)
        self.assertEqual(sites, [])

    def test_returns_empty_list_of_sites_if_field_type_is_wrong(self):
        self.forum_metadata.sites_field_name = 'name'
        sites = self.forum_metadata.sites(self.forum)
        self.assertEqual(sites, [])

    def test_returns_object_url_path(self):
        url_path = self.article_metadata.url_path(self.article)
        self.assertEqual(url_path, '/articles/{0}/'.format(self.article.pk))
Ejemplo n.º 2
0
class DefaultModelMetadataTests(TestCase):

    def setUp(self):
        self.default_site = Site.objects.get(id=1)
        self.custom_site = create_site()
        self.article = create_article(language='ru')
        self.article_metadata = ModelMetadata(Article)
        self.article_metadata.enable()

    def tearDown(self):
        self.article_metadata.disable()

    def test_returns_current_language(self):
        with translation.override('en'):
            language = self.article_metadata.language(self.article)
        self.assertEqual(language, 'en')

    def test_returns_all_sites(self):
        sites = self.article_metadata.sites(self.article)
        self.assertEqual(len(sites), 2)
        self.assertIn(self.default_site, sites)
        self.assertIn(self.custom_site, sites)
Ejemplo n.º 3
0
class ModelMetadataSignalHandlersTests(TestCase):

    def setUp(self):
        self.article_metadata = ModelMetadata(Article)
        self.article_metadata.enable()
        self.forum_metadata = ModelMetadata(Forum, sites_field_name='sites')
        self.forum_metadata.enable()

    def tearDown(self):
        self.article_metadata.disable()
        self.forum_metadata.disable()

    @patch.object(ModelMetadata, 'update_metadata')
    def test_metadata_created_on_object_creation(self, update_metadata):
        article = create_article()
        self.assertEqual(update_metadata.call_count, 1)
        self.assertEqual(update_metadata.call_args[0][0].content_object, article)

    def test_metadata_updated_on_object_update(self):
        article = create_article()
        metadata = Metadata.objects.get_for_content_object(article)
        with patch.object(ModelMetadata, 'update_metadata') as update_metadata:
            article.save()
            update_metadata.assert_called_once_with(metadata)

    def test_metadata_updated_if_sites_changed(self):
        forum = create_forum()
        metadata = Metadata.objects.get_for_content_object(forum)
        with patch.object(ModelMetadata, 'update_metadata') as update_metadata:
            forum.sites.add(create_site())
            update_metadata.assert_called_once_with(metadata)

    def test_metadata_deleted_on_object_deletion(self):
        article = create_article()
        metadata = Metadata.objects.get_for_content_object(article)
        with patch.object(ModelMetadata, 'delete_metadata') as delete_metadata:
            article.delete()
            delete_metadata.assert_called_once_with(metadata)