예제 #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))
예제 #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)