tests.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. )
  50. # Categories setup
  51. self.category = AdCategory.objects.create(
  52. title="Internet Services",
  53. slug="internet-services",
  54. description="All internet based services",
  55. )
  56. self.category2 = AdCategory.objects.create(
  57. title="Category Two", slug="categorytwo", description="A Second Category"
  58. )
  59. # Zones setup
  60. self.adzone = AdZone.objects.create(
  61. title="Sidebar", slug="sidebar", description="Side Bar Ads"
  62. )
  63. self.adzone2 = AdZone.objects.create(
  64. title="Content Banner", slug="contentbanner", description="Content Adverts"
  65. )
  66. # Ad setup
  67. self.ad = TextAd.objects.create(
  68. title="First Ad",
  69. content="For all your web design and development needs, at competitive rates.",
  70. url="http://www.teh-node.co.za/",
  71. enabled=True,
  72. advertiser=self.advertiser,
  73. category=self.category,
  74. zone=self.adzone,
  75. )
  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. )
  85. self.ad3 = TextAd.objects.create(
  86. title="A Third Ad",
  87. content="A third advert.",
  88. url="http://www.teh-node.co.za/",
  89. enabled=True,
  90. advertiser=self.advertiser,
  91. category=self.category2,
  92. zone=self.adzone2,
  93. )
  94. # AdImpression Setup
  95. self.impression1 = AdImpression.objects.create(
  96. impression_date=datenow(), source_ip="127.0.0.2", ad=self.ad
  97. )
  98. self.impression2 = AdImpression.objects.create(
  99. impression_date=datenow(), source_ip="127.0.0.3", ad=self.ad2
  100. )
  101. # Clicks Setup
  102. self.adclick1 = AdClick.objects.create(
  103. click_date=datenow(), source_ip="127.0.0.1", ad=self.ad
  104. )
  105. class AdvertiserTestCase(AdvertisingTestCase):
  106. def testAdvertiser(self):
  107. self.assertEquals(
  108. self.advertiser.get_website_url(), "http://andre.smoenux.webfactional.com/"
  109. )
  110. class CategoryTestCase(AdvertisingTestCase):
  111. def testAdCategory(self):
  112. self.assertEquals(self.category.__unicode__(), "Internet Services")
  113. class ZoneTestCase(AdvertisingTestCase):
  114. def testAdZone(self):
  115. self.assertEquals(self.adzone.__unicode__(), "Sidebar")
  116. def testAdinZone(self):
  117. ads = TextAd.objects.filter(zone__slug="sidebar")
  118. self.assertEquals(len(ads), 1)
  119. class AdvertTestCase(AdvertisingTestCase):
  120. def testAd(self):
  121. self.assertEquals(reverse("adzone_ad_view", args=["1"])[-8:], "/view/1/")
  122. adimpressions = AdImpression.objects.filter(ad=self.ad)
  123. self.assertEquals(len(adimpressions), 1)
  124. self.assertEquals(adimpressions[0].source_ip, "127.0.0.2")
  125. def testAdAdvertiser(self):
  126. self.assertEquals(self.ad.advertiser.__unicode__(), "teh_node Web Development")
  127. self.assertEquals(self.ad.advertiser.company_name, "teh_node Web Development")
  128. def testAddsInCategory(self):
  129. ads = TextAd.objects.filter(category__slug="internet-services")
  130. self.assertEquals(len(ads), 1)
  131. self.assertEquals(ads[0].title, "First Ad")
  132. def testRandomAd(self):
  133. ad = AdBase.objects.get_random_ad(
  134. ad_category="internet-services", ad_zone="sidebar"
  135. )
  136. self.assertEquals(ad.title, "First Ad")
  137. class ImpressionTestCase(AdvertisingTestCase):
  138. def testImpression(self):
  139. impressions = AdImpression.objects.all()
  140. self.assertEquals(len(impressions), 2)
  141. class ClickTestCase(AdvertisingTestCase):
  142. def testClicks(self):
  143. clicks = AdClick.objects.all()
  144. self.assertEquals(len(clicks), 1)
  145. def testClickOnAds(self):
  146. c = Client(REMOTE_ADDR="127.0.0.1")
  147. response = c.get(reverse("adzone_ad_view", args=["1"]))
  148. self.assertEquals(len(AdClick.objects.all()), 2)
  149. click = AdClick.objects.all()[1]
  150. self.assertEquals(click.source_ip, "127.0.0.1")
  151. def testInvalidAdURL(self):
  152. c = Client(REMOTE_ADDR="127.0.0.1")
  153. response = c.get("/te/10")
  154. self.assertEquals(len(AdClick.objects.all()), 1)