Author: Anonymous Language: python
Description: No description Timestamp: 2010-02-01 04:33:21 -0500
View raw paste Reply
  1. import unittest
  2. import random
  3. import sys
  4. if sys.version_info < (2, 6):
  5.     from django.utils import simplejson as json
  6. else:
  7.     import json
  8.  
  9. from django.db import IntegrityError, DatabaseError
  10. from django.test.client import Client
  11. from django.http import HttpResponse
  12. from django.core.handlers.wsgi import WSGIRequest
  13.  
  14. from models import BillingUser, Rental, Wallet, Currency, Package, PriceCategory
  15. from cms_sample.cms_models import Item, Media_biz
  16. from exceptions import BillExcep_rental, BillExcep_noWallet, BillExcep_notEnoughMoney
  17. from utils import rent, is_rented, time_from_end, tokenize, untokenize, is_subscribed, subscribe
  18. from webmx_views import rent_item
  19.  
  20. # in order to run these tests, run: "python manage.py test billing.Funtionality"
  21. # test functions must start with 'test'. For example testMyPlan
  22. class Funtionality(unittest.TestCase):
  23.  
  24.     item = Item.objects.all()[0]
  25.     item2 = Item.objects.all()[1]
  26.  
  27.     def setUp(self):
  28.         pass
  29.  
  30.     def tearDown(self):
  31.         pass
  32.         #self.user.delete()
  33.  
  34.     def test_Rental_Item(self):
  35.         user = BillingUser(username='AtestsUser76212', pin='1111', sbox_id='7777777')
  36.         user.save()
  37.         currency = Currency.objects.all()[0]
  38.         wallet = Wallet(user=user, currency=currency)
  39.         wallet.save()
  40.         rent = Rental(wallet=wallet, item=self.item, value=10.0, index=1)
  41.         rent.save()
  42.         self.assertTrue(rent.id)
  43.  
  44.         # Test that rental objects can not re-save
  45.         try:
  46.             rent.save()
  47.         except BillExcep_rental:
  48.             self.assertTrue(1)
  49.  
  50.         # Test that the user can not rent same item twice with the same index
  51.         rent2 = Rental(wallet=wallet, item=self.item, value=10.0, index=1)
  52.         try:
  53.             rent2.save()
  54.         except IntegrityError:
  55.             self.assertTrue(1)
  56.  
  57.     def test_Rental_AntherItem(self):
  58.         user = BillingUser(username='AtestsUser812228', pin='1111', sbox_id='7777777')
  59.         user.save()
  60.         currency = Currency.objects.all()[0]
  61.         wallet = Wallet(user=user, currency=currency)
  62.         wallet.save()
  63.         rent = Rental(wallet=wallet, item=self.item, value=10.0, index=1)
  64.         rent.save()
  65.         self.assertTrue(rent.id)
  66.         # Test that anther item can be rent by the user
  67.         rent2 = Rental(wallet=wallet, item=self.item2, value=10.0, index=1)
  68.         rent2.save()
  69.         self.assertTrue(rent2.id)
  70.  
  71.     def test_Rent(self):
  72.         user = BillingUser(username='AtestsUser812229', pin='1111', sbox_id='7777777')
  73.         user.save()
  74.         try:
  75.             rent(self.item, user)
  76.         except BillExcep_noWallet:
  77.             self.assertTrue(1) # Make sure that rent will not work without wallet
  78.         currency = Currency.objects.all()[2]
  79.         if not Wallet.objects.filter(user=user):
  80.             wallet = Wallet(user=user, currency=currency)
  81.             wallet.save()
  82.         self.assertFalse(is_rented(self.item, user))
  83.         try:
  84.             rent(self.item, user)
  85.         except BillExcep_notEnoughMoney:
  86.             self.assertTrue(1) # Can't rent with no money
  87.         user.wallet.amount = 12.0
  88.         user.wallet.save()
  89.         self.assertTrue(rent(self.item, user))
  90.         self.assertTrue(is_rented(self.item, user))
  91.         try:
  92.             rent(self.item2, user)
  93.         except BillExcep_notEnoughMoney:
  94.             self.assertTrue(1) # Can rent with no money
  95.         user.wallet.amount = 0
  96.         #self.assertFalse(is_rented(self.item2, user), 'Wallet suppose to be empty now')
  97.  
  98.     def test_tokenize(self):
  99.         print BillingUser.objects.all()
  100.         user = BillingUser(username='AtestsUser812230', pin='1111', sbox_id='7777777')
  101.         user.save()
  102.         try:
  103.             rent(self.item, user)
  104.         except BillExcep_noWallet:
  105.             self.assertTrue(1, 'Wallet.DoesNotExist') # Make sure that rent will not work without wallet
  106.         currency = Currency.objects.all()[0]
  107.         wallet = Wallet(user=user, currency=currency)
  108.         wallet.save()
  109.  
  110.         token = tokenize(username, self.item.id)
  111.         tmp_username, tmp_item_id = untokenize(token)
  112.         self.assertTrue(tmp_username == user.username, tmp_username + '==' + user.username)
  113.         self.assertTrue(int(tmp_item_id) == self.item.id)
  114.  
  115.     def test_subscription(self):
  116.         username = 'AtestsUser_4123109_subscription'
  117.         sid = '7777777'
  118.         user = BillingUser(username=username, pin='1111', sbox_id=sid, parental_code='1111')
  119.         user.save()
  120.         self.assertFalse(is_subscribed(self.item, user), 'User not suppose to be subscribed to random item')
  121.         currency = Currency.objects.all()[0]
  122.         price_cat = PriceCategory(name = 'Price Cat', price = 100.0, currency = currency )
  123.         price_cat.save()
  124.         package = Package(name = 'test package', pricecategory = price_cat)
  125.         package.save()
  126.         package.items.add(self.item)
  127.         subscrition = subscribe(package, user)
  128.         self.assertTrue(is_subscribed(self.item, user), 'User suppose to be subscribed item')
  129.         try:
  130.             subscribe(package, user)
  131.         except IntegrityError: # Second attempt must raise Exception
  132.             self.assertTrue(1)
  133.         child_package = Package(name = 'test package 2', pricecategory = price_cat)
  134.         child_package.save()
  135.         self.assertFalse(is_subscribed(self.item2, user), 'User not suppose to be subscribed item')
  136.         child_package.parent.add(package)
  137.         package.items.add(self.item2)
  138.         self.assertTrue(is_subscribed(self.item2, user), 'User suppose to be subscribed to item within sub-package')
  139.         subscrition.delete()
  140.         self.assertFalse(is_subscribed(self.item2, user), 'User not suppose to be subscribed to item')
  141.         item_subscrition = subscribe(self.item2, user)
  142.         self.assertTrue(is_subscribed(self.item2, user), 'User suppose to be subscribed to item')
  143.  
  144.  
  145.  
  146. #in order to run these tests, run: "python manage.py test billing.WebMX_view"
  147. #class WebMX_view(unittest.TestCase):
  148.     #def setUp(self):
  149.         #self.client = Client()
  150.  
  151.     #def test_rent(self):
  152.         #username = 'AtestsUser52825_92'
  153.         #sid = '7777777'
  154.         #pin_code = '1234'
  155.         #user = BillingUser(username=username, pin=pin_code, sbox_id=sid)
  156.         #user.save()
  157.         #currency = Currency.objects.all()[0]
  158.         #wallet = Wallet(user=user, currency=currency)
  159.         #wallet.save()
  160.         #item_id = '225'
  161.         ## Purche transaction (1/2)
  162.         #response = self.client.get('/billing/purchase/' + item_id, {'user': username, 'sid': '7777777'})
  163.         #self.assertEquals(response.status_code, 200)
  164.         #j = json.loads(response.content)
  165.         #self.assertTrue('section' in j.keys(), 'No "section" in the WebMX response')
  166.         #self.assertTrue('items' in j['section'].keys(), 'No "items" in the WebMX response')
  167.         #self.assertTrue('link' in j['section']['items'][0].keys(), 'No "items" in the WebMX response')
  168.         #link = j['section']['items'][0]['link']
  169.         ## Rent transaction (2/2)
  170.         #path = '/' + link
  171.         #response2 = self.client.get(path, {'user': username, 'sid': '7777777', 'code': pin_code})
  172.         #print response2
  173.         #jj = json.loads(response2.content)
  174.         #print j['section']['items']
  175.         #self.assertTrue('section' in jj.keys(), 'No "section" in the WebMX response')
  176.         #self.assertTrue('items' in j['section'].keys(), 'No "items" in the WebMX response')
  177.         #self.assertTrue('link' in j['section']['items'][0].keys(), 'No "items" in the WebMX response')
  178.  
View raw paste Reply