From: J. Cliff Dyer Date: Thu, 3 Dec 2009 20:01:21 +0000 (-0500) Subject: Added tests to check rendering of nodes. X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=8954df3b5eab2f0170c97ed747e8cf74c26a5b41;p=django-google-tools.git Added tests to check rendering of nodes. --- diff --git a/google_analytics/fixtures/analytics_test.json b/google_analytics/fixtures/analytics_test.json new file mode 100644 index 0000000..6697392 --- /dev/null +++ b/google_analytics/fixtures/analytics_test.json @@ -0,0 +1,18 @@ +[ + { + "pk": 1, + "model": "sites.site", + "fields": { + "domain": "example.com", + "name": "example.com" + } + }, + { + "pk": 1, + "model": "google_analytics.analytics", + "fields": { + "analytics_code": "UA-777777-3", + "site": 1 + } + } +] diff --git a/google_analytics/fixtures/test.json b/google_analytics/fixtures/test.json deleted file mode 100644 index 6697392..0000000 --- a/google_analytics/fixtures/test.json +++ /dev/null @@ -1,18 +0,0 @@ -[ - { - "pk": 1, - "model": "sites.site", - "fields": { - "domain": "example.com", - "name": "example.com" - } - }, - { - "pk": 1, - "model": "google_analytics.analytics", - "fields": { - "analytics_code": "UA-777777-3", - "site": 1 - } - } -] diff --git a/google_analytics/templates/google_analytics/test_template.html b/google_analytics/templates/google_analytics/test_template.html new file mode 100644 index 0000000..1013d7d --- /dev/null +++ b/google_analytics/templates/google_analytics/test_template.html @@ -0,0 +1 @@ +Tracking code: {{ analytics_code }} diff --git a/google_analytics/tests/test_templatetags.py b/google_analytics/tests/test_templatetags.py index 6dc850d..d440abc 100644 --- a/google_analytics/tests/test_templatetags.py +++ b/google_analytics/tests/test_templatetags.py @@ -1,11 +1,17 @@ from django.test import TestCase -from django import template +from django import template from django.contrib.sites.models import Site - +from google_analytics.models import Analytics from google_analytics.templatetags import analytics +code7 = 'UA-777777-3' # for fixture-based codes +code9 = 'UA-999999-1' # for explicit codes + class ParserTest(TestCase): + """Test parsing of template tokens""" + + fixtures = ['analytics_test'] def setUp(self): self.parser = "unused" @@ -15,13 +21,10 @@ class ParserTest(TestCase): ################################# self.token_noarg = template.Token(template.TOKEN_BLOCK, "test") - self.token_onearg = template.Token(template.TOKEN_BLOCK, "test 'UA-888888-1'") - self.token_twoarg = template.Token(template.TOKEN_BLOCK, "test 'UA-888888-1' 'UA-999999-2'") + self.token_onearg = template.Token(template.TOKEN_BLOCK, "test '%s'" % code9) + self.token_twoarg = template.Token(template.TOKEN_BLOCK, "test '%s' '%s'" % (code9, code7)) self.site = Site.objects.get_current() - def test_setup(self): - self.assert_(True) - def test_basic_return(self): node = analytics.do_get_analytics(self.parser, self.token_noarg) self.assertTrue(isinstance(node, template.Node)) @@ -51,29 +54,68 @@ class ParserTest(TestCase): node = analytics.do_get_analytics(self.parser, self.token_onearg) self.assertEqual(node.site, None) + class NodeTest(TestCase): + """Test set-up and rendering of AnalyticsNodes""" + + fixtures = ['analytics_test'] + def setUp(self): + self.site = Site.objects.get_current() self.node_noarg = analytics.AnalyticsNode() - self.node_code = analytics.AnalyticsNode(code='UA-999999-1') - self.node_explicit_template = analytics.AnalyticsNode(code='UA-999999-1', template_name='google_analytics/test_template.html') - self.node_site = analytics.AnalyticsNode(site=None, template_name='google_analytics/test_template.html') - self.node_code_and_site = analytics.AnalyticsNode(site=None, code='UA-999999-1', template_name='google_analytics/test_template.html') + self.node_code = analytics.AnalyticsNode(code=code9) + self.node_explicit_template = analytics.AnalyticsNode(code=code9, template_name='google_analytics/test_template.html') + self.node_site = analytics.AnalyticsNode(site=self.site, template_name='google_analytics/test_template.html') + self.node_code_and_site = analytics.AnalyticsNode(site=self.site, code=code9, template_name='google_analytics/test_template.html') - def test_default_template_name(self): - self.assertEqual(self.node_noarg.template_name, 'google_analytics/analytics_template.html') + def test_fixture(self): + """Fixtures have been loaded""" + self.assertNotEqual(Analytics.objects.count(), 0) - def test_explicit_template_name(self): - self.assertEqual(self.node_explicit_template.template_name, 'google_analytics/test_template.html') - - def test_noarg_code_name(self): - self.assertEqual(self.node_noarg.code, None) + def test_default_template_name(self): + self.assertEqual( + self.node_code.template_name, + 'google_analytics/analytics_template.html' + ) def test_explicit_code_name(self): - self.assertEqual(self.node_code.code, 'UA-999999-1') + self.assertEqual(self.node_code.code, code9) + self.assertTrue(code9 in self.node_code.render(template.Context())) - def _pending_test_site_code_name(self): - """This test needs more set-up, not yet implented""" + def test_noarg_code_name(self): + """If the node is constructed with no code and no site, it will return + an empty string""" + self.assertEqual(self.node_noarg.code, None) + self.assertEqual(self.node_noarg.render(template.Context()), "") - def _pending_test_explicit_code_overrides_site(self): - """This test needs more set-up, not yet implented""" + def test_explicit_template_name(self): + self.assertEqual( + self.node_explicit_template.template_name, + 'google_analytics/test_template.html' + ) + self.assertEqual( + self.node_explicit_template.render(template.Context()).strip(), + 'Tracking code: %s' % code9 + ) + + def test_defined_site(self): + self.assertEqual(self.node_site.site, self.site) + self.assertEqual(self.node_site.code, None) + self.assertEqual( + self.node_site.render(template.Context()).strip(), + 'Tracking code: %s' % code7 + ) + + def test_site_overrides_explicit_code(self): + """If both code and site are set, the site code will override the + explicitly set code. This is contrary to how the tag works, but + the parser never passes this combination of arguments.""" + + self.assertEqual(self.node_code_and_site.code, code9) + self.assertEqual(self.node_code_and_site.site, self.site) + self.assertEqual( + self.node_code_and_site.render(template.Context()).strip(), + 'Tracking code: %s' % code7 + ) +