From bd39d61399c9478cda0b1500884e6f147726ad83 Mon Sep 17 00:00:00 2001 From: Orne Brocaar Date: Fri, 6 Aug 2010 10:12:48 +0200 Subject: [PATCH] Some testcases added. --- MANIFEST.in | 1 + googletools/fixtures/analytics_codes.json | 18 +++++++ .../fixtures/site_verification_codes.json | 18 +++++++ googletools/fixtures/sites.json | 18 +++++++ .../googletools/tests/analytics_code_in.html | 1 + .../tests/analytics_code_out_a.html | 9 ++++ .../tests/analytics_code_out_b.html | 9 ++++ .../tests/site_verification_code_in.html | 1 + .../tests/site_verification_code_out_a.html | 1 + .../tests/site_verification_code_out_b.html | 1 + googletools/templatetags/googletools.py | 13 +++-- googletools/tests.py | 50 +++++++++++++++++++ 12 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 googletools/fixtures/analytics_codes.json create mode 100644 googletools/fixtures/site_verification_codes.json create mode 100644 googletools/fixtures/sites.json create mode 100644 googletools/templates/googletools/tests/analytics_code_in.html create mode 100644 googletools/templates/googletools/tests/analytics_code_out_a.html create mode 100644 googletools/templates/googletools/tests/analytics_code_out_b.html create mode 100644 googletools/templates/googletools/tests/site_verification_code_in.html create mode 100644 googletools/templates/googletools/tests/site_verification_code_out_a.html create mode 100644 googletools/templates/googletools/tests/site_verification_code_out_b.html create mode 100644 googletools/tests.py diff --git a/MANIFEST.in b/MANIFEST.in index ad8713c..e4efeed 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,3 +1,4 @@ include README.mdown recursive-include googletools/templates * +recursive-include googletools/fixtures * \ No newline at end of file diff --git a/googletools/fixtures/analytics_codes.json b/googletools/fixtures/analytics_codes.json new file mode 100644 index 0000000..ba109b5 --- /dev/null +++ b/googletools/fixtures/analytics_codes.json @@ -0,0 +1,18 @@ +[ + { + "pk": 1, + "model": "googletools.analyticscode", + "fields": { + "code": "analytics-a", + "site": 1 + } + }, + { + "pk": 2, + "model": "googletools.analyticscode", + "fields": { + "code": "analytics-b", + "site": 2 + } + } +] diff --git a/googletools/fixtures/site_verification_codes.json b/googletools/fixtures/site_verification_codes.json new file mode 100644 index 0000000..0b0f1dc --- /dev/null +++ b/googletools/fixtures/site_verification_codes.json @@ -0,0 +1,18 @@ +[ + { + "pk": 1, + "model": "googletools.siteverificationcode", + "fields": { + "code": "verification-a", + "site": 1 + } + }, + { + "pk": 2, + "model": "googletools.siteverificationcode", + "fields": { + "code": "verification-b", + "site": 2 + } + } +] diff --git a/googletools/fixtures/sites.json b/googletools/fixtures/sites.json new file mode 100644 index 0000000..dbbafeb --- /dev/null +++ b/googletools/fixtures/sites.json @@ -0,0 +1,18 @@ +[ + { + "pk": 1, + "model": "sites.site", + "fields": { + "domain": "www.domaina.com", + "name": "Site A" + } + }, + { + "pk": 2, + "model": "sites.site", + "fields": { + "domain": "www.domainb.com", + "name": "Site B" + } + } +] diff --git a/googletools/templates/googletools/tests/analytics_code_in.html b/googletools/templates/googletools/tests/analytics_code_in.html new file mode 100644 index 0000000..50fe94f --- /dev/null +++ b/googletools/templates/googletools/tests/analytics_code_in.html @@ -0,0 +1 @@ +{% load googletools %}{% analytics_code %} \ No newline at end of file diff --git a/googletools/templates/googletools/tests/analytics_code_out_a.html b/googletools/templates/googletools/tests/analytics_code_out_a.html new file mode 100644 index 0000000..3fc38b9 --- /dev/null +++ b/googletools/templates/googletools/tests/analytics_code_out_a.html @@ -0,0 +1,9 @@ + + \ No newline at end of file diff --git a/googletools/templates/googletools/tests/analytics_code_out_b.html b/googletools/templates/googletools/tests/analytics_code_out_b.html new file mode 100644 index 0000000..b75368f --- /dev/null +++ b/googletools/templates/googletools/tests/analytics_code_out_b.html @@ -0,0 +1,9 @@ + + \ No newline at end of file diff --git a/googletools/templates/googletools/tests/site_verification_code_in.html b/googletools/templates/googletools/tests/site_verification_code_in.html new file mode 100644 index 0000000..46cc8cf --- /dev/null +++ b/googletools/templates/googletools/tests/site_verification_code_in.html @@ -0,0 +1 @@ +{% load googletools %}{% site_verification_code %} \ No newline at end of file diff --git a/googletools/templates/googletools/tests/site_verification_code_out_a.html b/googletools/templates/googletools/tests/site_verification_code_out_a.html new file mode 100644 index 0000000..5f1a6c3 --- /dev/null +++ b/googletools/templates/googletools/tests/site_verification_code_out_a.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/googletools/templates/googletools/tests/site_verification_code_out_b.html b/googletools/templates/googletools/tests/site_verification_code_out_b.html new file mode 100644 index 0000000..7feffc1 --- /dev/null +++ b/googletools/templates/googletools/tests/site_verification_code_out_b.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/googletools/templatetags/googletools.py b/googletools/templatetags/googletools.py index 4710b4d..1a77ad2 100644 --- a/googletools/templatetags/googletools.py +++ b/googletools/templatetags/googletools.py @@ -11,8 +11,8 @@ register = template.Library() class AnalyticsCodeNode(template.Node): - def __init__(self): - self.site = Site.objects.get_current() + def __init__(self, site): + self.site = site self.template = 'googletools/analytics_code.html' try: self.code = AnalyticsCode.objects.get(site=self.site) @@ -31,8 +31,8 @@ class AnalyticsCodeNode(template.Node): class SiteVerificationCodeNode(template.Node): - def __init__(self): - self.site = Site.objects.get_current() + def __init__(self, site): + self.site = site self.template = 'googletools/site_verification_code.html' try: self.code = SiteVerificationCode.objects.get(site=self.site) @@ -62,8 +62,7 @@ def analytics_code(parser, token): tag_name = token.split_contents() except ValueError: raise template.TemplateSyntaxError, '%r does not take any arguments' % token.contents.split()[0] - return AnalyticsCodeNode() - + return AnalyticsCodeNode(site=Site.objects.get_current()) @register.tag def site_verification_code(parser, token): @@ -78,4 +77,4 @@ def site_verification_code(parser, token): tag_name = token.split_contents() except ValueError: raise template.TemplateSyntaxError, '%r does not take any arguments' % token.contents.split()[0] - return SiteVerificationCodeNode() + return SiteVerificationCodeNode(site=Site.objects.get_current()) diff --git a/googletools/tests.py b/googletools/tests.py new file mode 100644 index 0000000..8d7d6f6 --- /dev/null +++ b/googletools/tests.py @@ -0,0 +1,50 @@ +from django.contrib.sites.models import Site +from django.template.loader import render_to_string +from django.test import TestCase + +from googletools.templatetags.googletools import AnalyticsCodeNode, \ + SiteVerificationCodeNode + + +class AnalyticsCodeTestCase(TestCase): + fixtures = ['sites', 'analytics_codes'] + + def setUp(self): + self.site_a = Site.objects.get(pk=1) + self.site_b = Site.objects.get(pk=2) + self.correct_out_a = render_to_string('googletools/tests/analytics_code_out_a.html', {}) + self.correct_out_b = render_to_string('googletools/tests/analytics_code_out_b.html', {}) + + def test_templatetag(self): + code_in = render_to_string('googletools/tests/analytics_code_in.html', {}) + self.assertEqual(code_in, self.correct_out_a) + + def test_node_site_a(self): + node = AnalyticsCodeNode(site=self.site_a) + self.assertEqual(node.render(None), self.correct_out_a) + + def test_node_site_b(self): + node = AnalyticsCodeNode(site=self.site_b) + self.assertEqual(node.render(None), self.correct_out_b) + + +class SiteVerificationCodeTestCase(TestCase): + fixtures = ['sites', 'site_verification_codes'] + + def setUp(self): + self.site_a = Site.objects.get(pk=1) + self.site_b = Site.objects.get(pk=2) + self.correct_out_a = render_to_string('googletools/tests/site_verification_code_out_a.html', {}) + self.correct_out_b = render_to_string('googletools/tests/site_verification_code_out_b.html', {}) + + def test_templatetag(self): + code_in = render_to_string('googletools/tests/site_verification_code_in.html', {}) + self.assertEqual(code_in, self.correct_out_a) + + def test_node_site_a(self): + node = SiteVerificationCodeNode(site=self.site_a) + self.assertEqual(node.render(None), self.correct_out_a) + + def test_node_site_b(self): + node = SiteVerificationCodeNode(site=self.site_b) + self.assertEqual(node.render(None), self.correct_out_b) -- 2.39.5