tests.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. from django.test import TestCase
  2. from django.test.client import Client
  3. from django.core.handlers.wsgi import WSGIRequest
  4. from django.core.urlresolvers import reverse
  5. from adzone.models import *
  6. from django.contrib.auth.models import User
  7. def datenow():
  8. from datetime import datetime
  9. return datetime.now()
  10. class RequestFactory(Client):
  11. """
  12. Class that lets you create mock Request objects for use in testing.
  13. Usage:
  14. rf = RequestFactory()
  15. get_request = rf.get('/hello/')
  16. post_request = rf.post('/submit/', {'foo': 'bar'})
  17. This class re-uses the django.test.client.Client interface, docs here:
  18. http://www.djangoproject.com/documentation/testing/#the-test-client
  19. Once you have a request object you can pass it to any view function,
  20. just as if that view had been hooked up using a URLconf.
  21. """
  22. def request(self, **request):
  23. """
  24. Similar to parent class, but returns the request object as soon as it
  25. has created it.
  26. """
  27. environ = {
  28. 'HTTP_COOKIE': self.cookies,
  29. 'PATH_INFO': '/',
  30. 'QUERY_STRING': '',
  31. 'REQUEST_METHOD': 'GET',
  32. 'SCRIPT_NAME': '',
  33. 'SERVER_NAME': 'testserver',
  34. 'SERVER_PORT': 80,
  35. 'SERVER_PROTOCOL': 'HTTP/1.1',
  36. 'REMOTE_ADDR': '127.0.0.1',
  37. }
  38. environ.update(self.defaults)
  39. environ.update(request)
  40. return WSGIRequest(environ)
  41. class AdvertisingTestCase(TestCase):
  42. def setUp(self):
  43. self.request = RequestFactory().request()
  44. self.user = User.objects.create_user('test', 'test@example.com', 'testpass')
  45. self.advertiser = Advertiser.objects.create(
  46. company_name = 'teh_node Web Development',
  47. website = 'http://andre.smoenux.webfactional.com/',
  48. user = self.user)
  49. # Categories setup
  50. self.category = AdCategory.objects.create(
  51. title = 'Internet Services',
  52. slug = 'internet-services',
  53. description = 'All internet based services')
  54. self.category2 = AdCategory.objects.create(
  55. title = 'Category Two',
  56. slug = 'categorytwo',
  57. description = 'A Second Category')
  58. # Zones setup
  59. self.adzone = AdZone.objects.create(
  60. title = 'Sidebar',
  61. slug = 'sidebar',
  62. description = 'Side Bar Ads')
  63. self.adzone2 = AdZone.objects.create(
  64. title = 'Content Banner',
  65. slug = 'contentbanner',
  66. description = 'Content Adverts')
  67. # Ad setup
  68. self.ad = TextAd.objects.create(
  69. title = 'First Ad',
  70. content = 'For all your web design and development needs, at competitive rates.',
  71. url = 'http://www.teh-node.co.za/',
  72. enabled = True,
  73. advertiser = self.advertiser,
  74. category = self.category,
  75. zone = self.adzone)
  76. self.ad2 = TextAd.objects.create(
  77. title = 'Second Ad',
  78. content = 'A second advert.',
  79. url = 'http://www.teh-node.co.za/',
  80. enabled = True,
  81. advertiser = self.advertiser,
  82. category = self.category2,
  83. zone = self.adzone2)
  84. self.ad3 = TextAd.objects.create(
  85. title = 'A Third Ad',
  86. content = 'A third advert.',
  87. url = 'http://www.teh-node.co.za/',
  88. enabled = True,
  89. advertiser = self.advertiser,
  90. category = self.category2,
  91. zone = self.adzone2)
  92. # AdImpression Setup
  93. self.impression1 = AdImpression.objects.create(
  94. impression_date=datenow(),
  95. source_ip='127.0.0.2',
  96. ad=self.ad)
  97. self.impression2 = AdImpression.objects.create(
  98. impression_date=datenow(),
  99. source_ip='127.0.0.3',
  100. ad=self.ad2)
  101. # Clicks Setup
  102. self.adclick1 = AdClick.objects.create(
  103. click_date=datenow(),
  104. source_ip='127.0.0.1',
  105. ad=self.ad)
  106. class AdvertiserTestCase(AdvertisingTestCase):
  107. def testAdvertiser(self):
  108. self.assertEquals(self.advertiser.get_website_url(), 'http://andre.smoenux.webfactional.com/')
  109. class CategoryTestCase(AdvertisingTestCase):
  110. def testAdCategory(self):
  111. self.assertEquals(self.category.__unicode__(), 'Internet Services')
  112. class ZoneTestCase(AdvertisingTestCase):
  113. def testAdZone(self):
  114. self.assertEquals(self.adzone.__unicode__(), 'Sidebar')
  115. def testAdinZone(self):
  116. ads = TextAd.objects.filter(zone__slug='sidebar')
  117. self.assertEquals(len(ads), 1)
  118. class AdvertTestCase(AdvertisingTestCase):
  119. def testAd(self):
  120. self.assertEquals(reverse(
  121. 'adzone_ad_view',
  122. args=['1']
  123. )[-8:], '/view/1/')
  124. adimpressions = AdImpression.objects.filter(ad=self.ad)
  125. self.assertEquals(len(adimpressions), 1)
  126. self.assertEquals(adimpressions[0].source_ip, '127.0.0.2')
  127. def testAdAdvertiser(self):
  128. self.assertEquals(self.ad.advertiser.__unicode__(), 'teh_node Web Development')
  129. self.assertEquals(self.ad.advertiser.company_name, 'teh_node Web Development')
  130. def testAddsInCategory(self):
  131. ads = TextAd.objects.filter(category__slug='internet-services')
  132. self.assertEquals(len(ads), 1)
  133. self.assertEquals(ads[0].title, 'First Ad')
  134. def testRandomAd(self):
  135. ad = AdBase.objects.get_random_ad(
  136. ad_category='internet-services',
  137. ad_zone='sidebar'
  138. )
  139. self.assertEquals(ad.title, 'First Ad')
  140. class ImpressionTestCase(AdvertisingTestCase):
  141. def testImpression(self):
  142. impressions = AdImpression.objects.all()
  143. self.assertEquals(len(impressions), 2)
  144. class ClickTestCase(AdvertisingTestCase):
  145. def testClicks(self):
  146. clicks = AdClick.objects.all()
  147. self.assertEquals(len(clicks), 1)
  148. def testClickOnAds(self):
  149. c = Client(REMOTE_ADDR='127.0.0.1')
  150. response = c.get(reverse(
  151. 'adzone_ad_view',
  152. args=['1']
  153. ))
  154. self.assertEquals(len(AdClick.objects.all()), 2)
  155. click = AdClick.objects.all()[1]
  156. self.assertEquals(click.source_ip, '127.0.0.1')
  157. def testInvalidAdURL(self):
  158. c = Client(REMOTE_ADDR='127.0.0.1')
  159. response = c.get('/te/10')
  160. self.assertEquals(len(AdClick.objects.all()), 1)