Skip to content

Commit

Permalink
wrote test display referrer by sharer
Browse files Browse the repository at this point in the history
  • Loading branch information
ruixia committed Feb 5, 2012
1 parent 5d64924 commit e919ad6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
15 changes: 15 additions & 0 deletions test_shit.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
from wordout.models import *
from urlparse import urlparse
from django.db import connection

def test_shit():
customer = Customer.objects.get(pk=1)
sharer = Sharer.objects.get(customer=customer, customer_sharer_identifier='abc')
clicks = Click.objects.filter(sharer=sharer).values('referrer__host__host_name','referrer__path').annotate(click_total=Count('id')).order_by('click_total')
return clicks

def test_shit2():
customer = Customer.objects.get(pk=1)
for sharer in customer.sharer_set.all():
data = customer.get_referrers_for_sharer(sharer.customer_sharer_identifier)
print data
if data != []:
for referrer_and_click in data:
if referrer_and_click.get('referrer', ''):
url = urlparse(referrer_and_click['referrer'])
host_name, path = url.scheme + '://' + url.netloc, url.path
print str(host_name) + '|||' + str(path) + '|||' + sharer.customer_sharer_identifier + '|||' + str(referrer_and_click['clicks']) + '|||||'+ str(Click.objects.filter(sharer__customer=customer, referrer__host__host_name=host_name, referrer__path=path, sharer__customer_sharer_identifier = sharer.customer_sharer_identifier).count())
else:
print str('') + '|||' + str('') + '|||' + sharer.customer_sharer_identifier + '|||' + str(referrer_and_click['clicks']) + '|||||'+ str(Click.objects.filter(sharer__customer=customer, referrer = None, sharer__customer_sharer_identifier = sharer.customer_sharer_identifier).count())
2 changes: 1 addition & 1 deletion wordout/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def get_referrers_for_sharer(self, customer_sharer_identifier):
except Sharer.DoesNotExist:
return []
#Now we get the clicks, grouped by the referrers
clicks = Click.objects.filter(sharer=sharer).values('referrer__host__host_name','referrer__path').annotate(click_total=Count('id')).order_by('click_total') #We select related so we can get the referrer link
clicks = Click.objects.filter(sharer=sharer).values('referrer__host__host_name','referrer__path').annotate(click_total=Count('id')).order_by('-click_total') #We select related so we can get the referrer link
#haven't gotten the official way to serialization models. need replace the code below in the future
data = []
if clicks:
Expand Down
35 changes: 34 additions & 1 deletion wordout/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from urlparse import urlparse
from django.test import TestCase
from django.db import IntegrityError # Exception raised when the relational integrity of the database is affected, e.g. a foreign key check fails, duplicate key, etc.
from wordout.models import *
Expand All @@ -17,7 +18,39 @@ class Test_Display_Sharers(TestCase):
pass

class Test_Display_Referrer_By_Sharer(TestCase):
pass
fixtures = ['test_data.json']

# requirement 1. invalidate customer sharer identifier should return empty ls
# requirement 2. when the referrer is not None. the referrer has to be one of the full link and the total clicks should be right.
# requirement 3. when the referrer is None. the total clicks have to match.
def setUp(self):
self.customer = Customer.objects.get(pk=1)
self.clicks = Click.objects.filter(sharer__customer=self.customer)

def test_invalid_sharer_identifier(self):

# invalid sharer identifier will return None
invalid_sharer_ident_ls = ['aaa', 'bbb', 'ccc', 'ddd', 'eee123', 'fff', 'ggg0983', '9751']
sharer_ident_ls = [sharer['customer_sharer_identifier'] for sharer in Sharer.objects.filter(customer=self.customer).values('customer_sharer_identifier')]
for invalid_sharer_ident in invalid_sharer_ident_ls:
if invalid_sharer_ident not in sharer_ident_ls:
self.assertEqual(self.customer.get_referrers_for_sharer(invalid_sharer_ident), list())

# loop through the user's sharers. send the referrer and customer sharer id back to the click model. check whether the click total matches.

def test_get_referrers_for_sharer(self):
for sharer in self.customer.sharer_set.all():
data = self.customer.get_referrers_for_sharer(sharer.customer_sharer_identifier)
if data != []:
for referrer_and_click in data:
if referrer_and_click.get('referrer', ''):
url = urlparse(referrer_and_click['referrer'])
host_name, path = url.scheme + '://' + url.netloc, url.path
# referrer is not empty and has host_name and path
self.assertEqual(referrer_and_click['clicks'], Click.objects.filter(sharer__customer=self.customer, referrer__host__host_name=host_name, referrer__path=path, sharer__customer_sharer_identifier = sharer.customer_sharer_identifier).count())
else:
# referrer is empty
self.assertEqual(referrer_and_click['clicks'], Click.objects.filter(sharer__customer=self.customer, referrer=None, sharer__customer_sharer_identifier = sharer.customer_sharer_identifier).count())


class Test_Create_Sharer(TestCase):
Expand Down

0 comments on commit e919ad6

Please sign in to comment.