Created
February 24, 2012 18:35
-
-
Save juliomistral/1902834 to your computer and use it in GitHub Desktop.
Writing up a Proudct pricing unit test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytz | |
from flexmock import flexmock | |
from decimal import Decimal | |
from isodate import parse_duration | |
from datetime import datetime | |
from checkoutservice.tests.base import BaseTestCase | |
from yola.django.user import User | |
from checkoutservice.products.models import Product, get_payments_release_date | |
class ProductPricingTestCase(BaseTestCase): | |
# Date/time vars | |
NOW = datetime.now().replace(tzinfo=pytz.utc) | |
NOW_PLUS_P1Y = NOW + parse_duration("P1Y") | |
NOW_PLUS_P1M = NOW + parse_duration("P1M") | |
PAYMENTS_RELEASE = get_payments_release_date() | |
PAYMENTS_RELEASE_PLUS_P1Y = PAYMENTS_RELEASE + parse_duration("P1Y") | |
# Free Users have NO subs | |
FREE_USER_SUB = {} | |
# New subscriptions | |
ACTIVE_BRONZE_P1Y_SUB = { | |
'bronze' : { | |
"term": "P1Y", | |
"name": "Yola Bronze Bundle (P1Y)", | |
"current": True, | |
"purchase_date": NOW.isoformat(), | |
"expiration": NOW_PLUS_P1Y.isoformat(), | |
} | |
} | |
ACTIVE_SILVER_P1Y_SUB = { | |
'silver' : { | |
"term": "P1Y", | |
"name": "Yola Silver Bundle (P1Y)", | |
"current": True, | |
"purchase_date": NOW.isoformat(), | |
"expiration": NOW_PLUS_P1Y.isoformat(), | |
} | |
} | |
# Legacy subscriptions | |
ACTIVE_LEGACY_SILVER_P1Y_SUB = { | |
'silver' : { | |
"term": "P1Y", | |
"name": "Yola Silver Bundle (P1Y)", | |
"current": (PAYMENTS_RELEASE_PLUS_P1Y > NOW), | |
"purchase_date": PAYMENTS_RELEASE.isoformat(), | |
"expiration": PAYMENTS_RELEASE_PLUS_P1Y.isoformat(), | |
} | |
} | |
def setUp(self): | |
super(ProductPricingTestCase, self).setUp() | |
self.silver_product_P1Y = Product.objects.get(sku='40000035', availability_end=None) | |
self.silver_product_P1M = Product.objects.get(sku='40000101', availability_end=None) | |
self.bronze_product_P1Y = Product.objects.get(sku='40000104', availability_end=None) | |
self.bronze_product_P1M = Product.objects.get(sku='40000105', availability_end=None) | |
self.domain_product = Product.objects.get(sku='10000125', availability_end=None) | |
def tearDown(self): | |
super(ProductPricingTestCase, self).tearDown() | |
def _stub_user_class_subscription(self, subscription): | |
fake_user = flexmock( | |
id='12345', | |
subscriptions = subscription, | |
partner_id = 'YOLA', | |
) | |
flexmock(User).new_instances(fake_user) | |
def test_silver_P1Y_plus_domain_is_discounted(self): | |
self._stub_user_class_subscription(self.FREE_USER_SUB) | |
prices = self.silver_product_P1Y.get_prices('12345', [self.domain_product]) | |
expected_price = Decimal('88.00') | |
self.assertEquals( | |
prices['USD'], | |
expected_price, | |
"Silver P1Y + Domain wasn't disocunted. %s != %s" % (prices['USD'], expected_price), | |
) | |
def test_silver_P1Y_alone_is_full_price(self): | |
self._stub_user_class_subscription(self.FREE_USER_SUB) | |
prices = self.silver_product_P1Y.get_prices('12344') | |
expected_price = Decimal('99.95') | |
self.assertEquals( | |
prices['USD'], | |
expected_price, | |
"Silver P1Y was discounted! %s != %s" % (prices['USD'], expected_price), | |
) | |
def test_silver_P1M_plus_domain_is_full_price(self): | |
self._stub_user_class_subscription(self.FREE_USER_SUB) | |
prices = self.silver_product_P1M.get_prices('12344', [self.domain_product]) | |
expected_price = Decimal('9.95') | |
self.assertEquals( | |
prices['USD'], | |
expected_price, | |
"Silver P1M was discounted! %s != %s" % (prices['USD'], expected_price), | |
) | |
def test_bronze_P1Y_plus_domain_is_discounted(self): | |
self._stub_user_class_subscription(self.FREE_USER_SUB) | |
prices = self.bronze_product_P1Y.get_prices('12345', [self.domain_product]) | |
expected_price = Decimal('38.00') | |
self.assertEquals( | |
prices['USD'], | |
expected_price, | |
"Bronze P1Y + Domain wasn't disocunted. %s != %s" % (prices['USD'], expected_price), | |
) | |
def test_bronze_P1Y_alone_is_full_price(self): | |
self._stub_user_class_subscription(self.FREE_USER_SUB) | |
prices = self.bronze_product_P1Y.get_prices('12344') | |
expected_price = Decimal('49.95') | |
self.assertEquals( | |
prices['USD'], | |
expected_price, | |
"Bronze P1Y was discounted! %s != %s" % (prices['USD'], expected_price), | |
) | |
def test_silver_P1M_plus_domain_is_full_price(self): | |
self._stub_user_class_subscription(self.FREE_USER_SUB) | |
prices = self.bronze_product_P1M.get_prices('12344', [self.domain_product]) | |
expected_price = Decimal('4.95') | |
self.assertEquals( | |
prices['USD'], | |
expected_price, | |
"Bronze P1M was discounted! %s != %s" % (prices['USD'], expected_price), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment