def setup(self, user, create_article, create_comment, create_category): self.article = create_article() self.comment = create_comment() self.user = user self.cat1 = create_category(alias='cat1', creator=self.user, note='some_note') self.cat2 = create_category(alias='cat2', creator=self.user) self.cat3 = create_category(alias='cat3') self.cat11 = create_category(parent=self.cat1, note='subnote') self.cat111 = create_category(parent=self.cat1) self.cat22 = create_category(parent=self.cat2) self.cat222 = create_category(parent=self.cat2) self.cat2222 = create_category(parent=self.cat2) self.article.add_to_category(self.cat1, self.user) self.article.add_to_category(self.cat11, self.user) self.article.add_to_category(self.cat3, self.user) self.comment.add_to_category(self.cat1, self.user) self.comment.add_to_category(self.cat111, self.user) self.comment.add_to_category(self.cat2, self.user) self.comment.add_to_category(self.cat3, self.user) self.cl = CategoryList() self.cl.set_obj(self.article) self.cl_cat1 = CategoryList('cat1', show_title=True, show_links=False, cat_html_class='somecss') self.cl_cat1.set_obj(self.article)
def setup(self, user, create_category): self.user = user self.cat1 = create_category(alias='cat1', creator=self.user, note='some_note') self.cl = CategoryList() self.cl_cat1 = CategoryList('cat1', show_title=True, show_links=False, cat_html_class='somecss')
def setUp(self): self.user = create_user() self.cat1 = create_category(alias='cat1', creator=self.user, note='some_note') self.cat2 = create_category(alias='cat2', creator=self.user) self.cat3 = create_category(alias='cat3') self.cat11 = create_category(parent=self.cat1, note='subnote') self.cat111 = create_category(parent=self.cat1) self.cl = CategoryList() self.cl_cat1 = CategoryList('cat1', show_title=True, show_links=False, cat_html_class='somecss')
class TestCategoryListWithObj: @pytest.fixture def setup(self, user, create_article, create_comment, create_category): self.article = create_article() self.comment = create_comment() self.user = user self.cat1 = create_category(alias='cat1', creator=self.user, note='some_note') self.cat2 = create_category(alias='cat2', creator=self.user) self.cat3 = create_category(alias='cat3') self.cat11 = create_category(parent=self.cat1, note='subnote') self.cat111 = create_category(parent=self.cat1) self.cat22 = create_category(parent=self.cat2) self.cat222 = create_category(parent=self.cat2) self.cat2222 = create_category(parent=self.cat2) self.article.add_to_category(self.cat1, self.user) self.article.add_to_category(self.cat11, self.user) self.article.add_to_category(self.cat3, self.user) self.comment.add_to_category(self.cat1, self.user) self.comment.add_to_category(self.cat111, self.user) self.comment.add_to_category(self.cat2, self.user) self.comment.add_to_category(self.cat3, self.user) self.cl = CategoryList() self.cl.set_obj(self.article) self.cl_cat1 = CategoryList('cat1', show_title=True, show_links=False, cat_html_class='somecss') self.cl_cat1.set_obj(self.article) def test_all(self, setup): # test_get_choices choices = self.cl.get_choices() assert len(choices) == 3 assert self.cat1 in choices assert self.cat2 in choices assert self.cat3 in choices choices = self.cl_cat1.get_choices() assert len(choices) == 2 assert self.cat11 in choices assert self.cat111 in choices # test_get_categories cats = self.cl.get_categories() assert len(cats) == 2 assert self.cat1 in cats assert self.cat2 not in cats assert self.cat3 in cats cats = self.cl_cat1.get_categories() assert len(cats) == 1 assert self.cat11 in cats
def setup(self, user, create_article, create_category): self.user = user self.cat1 = create_category(alias='cat1') self.cl_cat1 = CategoryList('cat1') self.cat2 = create_category(alias='cat2') self.cl_cat2 = CategoryList('cat2', show_links=lambda category: category.id) self.cat3 = create_category(alias='cat3') self.cat31 = create_category(alias='cat31', parent=self.cat3) self.cl_cat3 = CategoryList('cat3') self.art1 = create_article()
class TestCategoryListWithObj(object): @pytest.fixture def setup(self, user, create_article, create_comment, create_category): self.article = create_article() self.comment = create_comment() self.user = user self.cat1 = create_category(alias='cat1', creator=self.user, note='some_note') self.cat2 = create_category(alias='cat2', creator=self.user) self.cat3 = create_category(alias='cat3') self.cat11 = create_category(parent=self.cat1, note='subnote') self.cat111 = create_category(parent=self.cat1) self.cat22 = create_category(parent=self.cat2) self.cat222 = create_category(parent=self.cat2) self.cat2222 = create_category(parent=self.cat2) self.article.add_to_category(self.cat1, self.user) self.article.add_to_category(self.cat11, self.user) self.article.add_to_category(self.cat3, self.user) self.comment.add_to_category(self.cat1, self.user) self.comment.add_to_category(self.cat111, self.user) self.comment.add_to_category(self.cat2, self.user) self.comment.add_to_category(self.cat3, self.user) self.cl = CategoryList() self.cl.set_obj(self.article) self.cl_cat1 = CategoryList('cat1', show_title=True, show_links=False, cat_html_class='somecss') self.cl_cat1.set_obj(self.article) def test_all(self, setup): # test_get_choices choices = self.cl.get_choices() assert len(choices) == 3 assert self.cat1 in choices assert self.cat2 in choices assert self.cat3 in choices choices = self.cl_cat1.get_choices() assert len(choices) == 2 assert self.cat11 in choices assert self.cat111 in choices # test_get_categories cats = self.cl.get_categories() assert len(cats) == 2 assert self.cat1 in cats assert self.cat2 not in cats assert self.cat3 in cats cats = self.cl_cat1.get_categories() assert len(cats) == 1 assert self.cat11 in cats
class TestCategoryListNoObj: @pytest.fixture def setup(self, user, create_category): self.user = user self.cat1 = create_category(alias='cat1', creator=self.user, note='some_note') self.cat2 = create_category(alias='cat2', creator=self.user) self.cat3 = create_category(alias='cat3') self.cat11 = create_category(parent=self.cat1, note='subnote') self.cat111 = create_category(parent=self.cat1) self.cl = CategoryList() self.cl_cat1 = CategoryList('cat1', show_title=True, show_links=False, cat_html_class='somecss') def test_all(self, setup): # test_get_choices choices = self.cl.get_choices() assert len(choices) == 3 assert self.cat1 in choices assert self.cat2 in choices assert self.cat3 in choices choices = self.cl_cat1.get_choices() assert len(choices) == 2 assert self.cat11 in choices assert self.cat111 in choices # test_get_categories cats = self.cl.get_categories() assert len(cats) == 3 assert self.cat1 in cats assert self.cat2 in cats assert self.cat3 in cats cats = self.cl_cat1.get_categories() assert len(cats) == 2 assert self.cat11 in cats assert self.cat111 in cats
class CategoryListWithObjTest(TestCase): def setUp(self): self.article = create_article() self.comment = create_comment() self.user = create_user() self.cat1 = create_category(alias='cat1', creator=self.user, note='some_note') self.cat2 = create_category(alias='cat2', creator=self.user) self.cat3 = create_category(alias='cat3') self.cat11 = create_category(parent=self.cat1, note='subnote') self.cat111 = create_category(parent=self.cat1) self.cat22 = create_category(parent=self.cat2) self.cat222 = create_category(parent=self.cat2) self.cat2222 = create_category(parent=self.cat2) self.article.add_to_category(self.cat1, self.user) self.article.add_to_category(self.cat11, self.user) self.article.add_to_category(self.cat3, self.user) self.comment.add_to_category(self.cat1, self.user) self.comment.add_to_category(self.cat111, self.user) self.comment.add_to_category(self.cat2, self.user) self.comment.add_to_category(self.cat3, self.user) self.cl = CategoryList() self.cl.set_obj(self.article) self.cl_cat1 = CategoryList('cat1', show_title=True, show_links=False, cat_html_class='somecss') self.cl_cat1.set_obj(self.article) def test_get_choices(self): choices = self.cl.get_choices() self.assertEqual(len(choices), 3) self.assertIn(self.cat1, choices) self.assertIn(self.cat2, choices) self.assertIn(self.cat3, choices) choices = self.cl_cat1.get_choices() self.assertEqual(len(choices), 2) self.assertIn(self.cat11, choices) self.assertIn(self.cat111, choices) def test_get_categories(self): cats = self.cl.get_categories() self.assertEqual(len(cats), 2) self.assertIn(self.cat1, cats) self.assertNotIn(self.cat2, cats) self.assertIn(self.cat3, cats) cats = self.cl_cat1.get_categories() self.assertEqual(len(cats), 1) self.assertIn(self.cat11, cats)
class TestCategoryListBasic: @pytest.fixture def setup(self, user, create_category): self.user = user self.cat1 = create_category(alias='cat1', creator=self.user, note='some_note') self.cl = CategoryList() self.cl_cat1 = CategoryList('cat1', show_title=True, show_links=False, cat_html_class='somecss') def test_all(self, setup): assert str(self.cl) == '' assert str(self.cl_cat1) == 'cat1' # test_cat_html_class assert self.cl.cat_html_class == '' assert self.cl_cat1.cat_html_class == 'somecss' # test_show_links assert self.cl.show_links assert self.cl_cat1.show_links == False # test_show_title assert not self.cl.show_title assert self.cl_cat1.show_title == True # test_alias assert self.cl.alias is None assert self.cl_cat1.alias == 'cat1' # test_get_category_model assert self.cl.get_category_model() is None assert self.cl_cat1.get_category_model() == self.cat1 # test_get_title assert self.cl.get_title() == 'Categories' assert self.cl_cat1.get_title() == self.cat1.title # test_get_id assert self.cl.get_id() is None assert self.cl_cat1.get_id() == self.cat1.id # test_get_note assert self.cl.get_note() == '' assert self.cl_cat1.get_note() == self.cat1.note
class TestCategoryListBasic(object): @pytest.fixture def setup(self, user, create_category): self.user = user self.cat1 = create_category(alias='cat1', creator=self.user, note='some_note') self.cl = CategoryList() self.cl_cat1 = CategoryList('cat1', show_title=True, show_links=False, cat_html_class='somecss') def test_all(self, setup): assert str(self.cl) == '' assert str(self.cl_cat1) == 'cat1' # test_cat_html_class assert self.cl.cat_html_class == '' assert self.cl_cat1.cat_html_class == 'somecss' # test_show_links assert self.cl.show_links assert self.cl_cat1.show_links == False # test_show_title assert not self.cl.show_title assert self.cl_cat1.show_title == True # test_alias assert self.cl.alias is None assert self.cl_cat1.alias == 'cat1' # test_get_category_model assert self.cl.get_category_model() is None assert self.cl_cat1.get_category_model() == self.cat1 # test_get_title assert self.cl.get_title() == 'Categories' assert self.cl_cat1.get_title() == self.cat1.title # test_get_id assert self.cl.get_id() is None assert self.cl_cat1.get_id() == self.cat1.id # test_get_note assert self.cl.get_note() == '' assert self.cl_cat1.get_note() == self.cat1.note
class CategoryListBasicTest(TestCase): def setUp(self): self.user = create_user() self.cat1 = create_category(alias='cat1', creator=self.user, note='some_note') self.cl = CategoryList() self.cl_cat1 = CategoryList('cat1', show_title=True, show_links=False, cat_html_class='somecss') def test_str(self): self.assertEqual(str(self.cl), '') self.assertEqual(str(self.cl_cat1), 'cat1') def test_cat_html_class(self): self.assertEqual(self.cl.cat_html_class, '') self.assertEqual(self.cl_cat1.cat_html_class, 'somecss') def test_show_links(self): self.assertTrue(self.cl.show_links) self.assertEqual(self.cl_cat1.show_links, False) def test_show_title(self): self.assertFalse(self.cl.show_title) self.assertEqual(self.cl_cat1.show_title, True) def test_alias(self): self.assertIsNone(self.cl.alias) self.assertEqual(self.cl_cat1.alias, 'cat1') def test_get_category_model(self): self.assertIsNone(self.cl.get_category_model()) self.assertEqual(self.cl_cat1.get_category_model(), self.cat1) def test_get_title(self): self.assertEqual(self.cl.get_title(), 'Categories') self.assertEqual(self.cl_cat1.get_title(), self.cat1.title) def test_get_id(self): self.assertIsNone(self.cl.get_id()) self.assertEqual(self.cl_cat1.get_id(), self.cat1.id) def test_get_note(self): self.assertEqual(self.cl.get_note(), '') self.assertEqual(self.cl_cat1.get_note(), self.cat1.note)
class TestTemplateTags(object): @pytest.fixture def setup(self, user, create_article, create_category): self.user = user self.cat1 = create_category(alias='cat1') self.cl_cat1 = CategoryList('cat1') self.cat2 = create_category(alias='cat2') self.cl_cat2 = CategoryList('cat2', show_links=lambda category: category.id) self.cat3 = create_category(alias='cat3') self.cat31 = create_category(alias='cat31', parent=self.cat3) self.cl_cat3 = CategoryList('cat3') self.art1 = create_article() def test_sitecats_url(self, setup, template_render_tag, template_context): with pytest.raises(TemplateSyntaxError): template_render_tag('sitecats', 'sitecats_url') # testing UNRESOLVED_URL_MARKER context = template_context({'my_category': self.cat1, 'my_list': self.cl_cat1}) result = template_render_tag('sitecats', 'sitecats_url for my_category using my_list', context) assert result == UNRESOLVED_URL_MARKER # testing ModelWithCategory.get_category_absolute_url() self.cl_cat1.set_obj(self.art1) expected_url = '%s/%s' % (self.cat1.id, self.art1.title) assert template_render_tag('sitecats', 'sitecats_url for my_category using my_list', context) == expected_url template_render_tag('sitecats', 'sitecats_url for my_category using my_list as someurl', context) assert context.get('someurl') == expected_url # testing CategoryList.show_links context = template_context({'my_category': self.cat2, 'my_list': self.cl_cat2}) result = template_render_tag('sitecats', 'sitecats_url for my_category using my_list', context) assert result == str(self.cat2.id) def test_sitecats_categories(self, setup, template_render_tag, template_context, settings): with pytest.raises(TemplateSyntaxError): template_render_tag('sitecats', 'sitecats_categories') # testing CategoryList passed into `from` clause context = template_context({'my_categories_list': self.cl_cat3}) result = template_render_tag('sitecats', 'sitecats_categories from my_categories_list', context) print(result) assert ('data-catid="%s"' % self.cat3.id) in result assert ('data-catid="%s"' % self.cat31.id) in result # testing list of CategoryList passed into `from` clause context = template_context({'my_categories_list': [self.cl_cat3]}) result = template_render_tag('sitecats', 'sitecats_categories from my_categories_list', context) assert ('data-catid="%s"' % self.cat3.id) in result assert ('data-catid="%s"' % self.cat31.id) in result # testing unknown type passed into `from` clause context = template_context({'my_categories_list': object()}) result = template_render_tag('sitecats', 'sitecats_categories from my_categories_list', context) assert result == '' settings.DEBUG = True with pytest.raises(SitecatsConfigurationError): context = template_context({'my_categories_list': object()}) template_render_tag('sitecats', 'sitecats_categories from my_categories_list', context)
class TemplateTagsTest(TestCase): def setUp(self): self.user = create_user() self.cat1 = create_category(alias='cat1') self.cl_cat1 = CategoryList('cat1') self.cat2 = create_category(alias='cat2') self.cl_cat2 = CategoryList('cat2', show_links=lambda category: category.id) self.cat3 = create_category(alias='cat3') self.cat31 = create_category(alias='cat31', parent=self.cat3) self.cl_cat3 = CategoryList('cat3') self.art1 = create_article() def test_sitecats_url(self): tpl = '{% load sitecats %}{% sitecats_url %}' self.assertRaises(TemplateSyntaxError, render_string, tpl) # testing UNRESOLVED_URL_MARKER context = Context({'my_category': self.cat1, 'my_list': self.cl_cat1}) tpl = '{% load sitecats %}{% sitecats_url for my_category using my_list %}' result = render_string(tpl, context) self.assertEqual(result, UNRESOLVED_URL_MARKER) # testing ModelWithCategory.get_category_absolute_url() self.cl_cat1.set_obj(self.art1) expected_url = '%s/%s' % (self.cat1.id, self.art1.title) tpl = '{% load sitecats %}{% sitecats_url for my_category using my_list %}' result = render_string(tpl, context) self.assertEqual(result, expected_url) tpl = '{% load sitecats %}{% sitecats_url for my_category using my_list as someurl %}' render_string(tpl, context) self.assertEqual(context.get('someurl'), expected_url) # testing CategoryList.show_links tpl = '{% load sitecats %}{% sitecats_url for my_category using my_list %}' result = render_string(tpl, Context({'my_category': self.cat2, 'my_list': self.cl_cat2})) self.assertEqual(result, str(self.cat2.id)) def test_sitecats_categories(self): tpl = '{% load sitecats %}{% sitecats_categories %}' self.assertRaises(TemplateSyntaxError, render_string, tpl) # testing CategoryList passed into `from` clause context = Context({'my_categories_list': self.cl_cat3}) tpl = '{% load sitecats %}{% sitecats_categories from my_categories_list %}' result = render_string(tpl, context) self.assertIn('data-catid="%s"' % self.cat3.id, result) self.assertIn('data-catid="%s"' % self.cat31.id, result) # testing list of CategoryList passed into `from` clause context = Context({'my_categories_list': [self.cl_cat3]}) tpl = '{% load sitecats %}{% sitecats_categories from my_categories_list %}' result = render_string(tpl, context) self.assertIn('data-catid="%s"' % self.cat3.id, result) self.assertIn('data-catid="%s"' % self.cat31.id, result) # testing unknown type passed into `from` clause context = Context({'my_categories_list': object()}) tpl = '{% load sitecats %}{% sitecats_categories from my_categories_list %}' result = render_string(tpl, context) self.assertEqual(result, '') with override_settings(DEBUG=True): context = Context({'my_categories_list': object()}) tpl = '{% load sitecats %}{% sitecats_categories from my_categories_list %}' self.assertRaises(SitecatsConfigurationError, render_string, tpl, context)
class TestTemplateTags: @pytest.fixture def setup(self, user, create_article, create_category): self.user = user self.cat1 = create_category(alias='cat1') self.cl_cat1 = CategoryList('cat1') self.cat2 = create_category(alias='cat2') self.cl_cat2 = CategoryList('cat2', show_links=lambda category: category.id) self.cat3 = create_category(alias='cat3') self.cat31 = create_category(alias='cat31', parent=self.cat3) self.cl_cat3 = CategoryList('cat3') self.art1 = create_article() def test_sitecats_url(self, setup, template_render_tag, template_context): with pytest.raises(TemplateSyntaxError): template_render_tag('sitecats', 'sitecats_url') # testing UNRESOLVED_URL_MARKER context = template_context({ 'my_category': self.cat1, 'my_list': self.cl_cat1 }) result = template_render_tag( 'sitecats', 'sitecats_url for my_category using my_list', context) assert result == UNRESOLVED_URL_MARKER # testing ModelWithCategory.get_category_absolute_url() self.cl_cat1.set_obj(self.art1) expected_url = '%s/%s' % (self.cat1.id, self.art1.title) assert template_render_tag( 'sitecats', 'sitecats_url for my_category using my_list', context) == expected_url template_render_tag( 'sitecats', 'sitecats_url for my_category using my_list as someurl', context) assert context.get('someurl') == expected_url # testing CategoryList.show_links context = template_context({ 'my_category': self.cat2, 'my_list': self.cl_cat2 }) result = template_render_tag( 'sitecats', 'sitecats_url for my_category using my_list', context) assert result == str(self.cat2.id) def test_sitecats_categories(self, setup, template_render_tag, template_context, settings): with pytest.raises(TemplateSyntaxError): template_render_tag('sitecats', 'sitecats_categories') # testing CategoryList passed into `from` clause context = template_context({'my_categories_list': self.cl_cat3}) result = template_render_tag( 'sitecats', 'sitecats_categories from my_categories_list', context) print(result) assert ('data-catid="%s"' % self.cat3.id) in result assert ('data-catid="%s"' % self.cat31.id) in result # testing list of CategoryList passed into `from` clause context = template_context({'my_categories_list': [self.cl_cat3]}) result = template_render_tag( 'sitecats', 'sitecats_categories from my_categories_list', context) assert ('data-catid="%s"' % self.cat3.id) in result assert ('data-catid="%s"' % self.cat31.id) in result # testing unknown type passed into `from` clause context = template_context({'my_categories_list': object()}) result = template_render_tag( 'sitecats', 'sitecats_categories from my_categories_list', context) assert result == '' settings.DEBUG = True with pytest.raises(SitecatsConfigurationError): context = template_context({'my_categories_list': object()}) template_render_tag('sitecats', 'sitecats_categories from my_categories_list', context)