Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Add tests for promote refunds functions
Browse files Browse the repository at this point in the history
  • Loading branch information
zeantsoi committed May 4, 2016
1 parent 0e45cce commit 3d773f2
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
2 changes: 2 additions & 0 deletions r2/r2/lib/promote.py
Expand Up @@ -1153,6 +1153,8 @@ def get_total_run(thing):
campaigns = PromoCampaign._by_link(thing._id)
elif isinstance(thing, PromoCampaign):
campaigns = [thing]
else:
campaigns = []

earliest = None
latest = None
Expand Down
99 changes: 98 additions & 1 deletion r2/r2/tests/unit/lib/promote_test.py
@@ -1,16 +1,20 @@
import datetime
import unittest

from mock import MagicMock, patch
from mock import MagicMock, Mock, patch

from r2.lib.promote import (
get_nsfw_collections_srnames,
get_refund_amount,
refund_campaign,
srnames_from_site,
)
from r2.models import (
Account,
Collection,
FakeAccount,
Frontpage,
PromoCampaign,
Subreddit,
MultiReddit,
)
Expand Down Expand Up @@ -125,3 +129,96 @@ def test_remove_nsfw_collection_srnames_on_frontpage(self, get_nsfw_collections_
self.assertEqual(frontpage_srnames, {Frontpage.name, nice_srname})
self.assertTrue(len(frontpage_srnames & {questionably_nsfw}) == 0)


class TestPromoteRefunds(unittest.TestCase):
def setUp(self):
self.link = Mock()
self.campaign = MagicMock(spec=PromoCampaign)
self.campaign._id = 1
self.campaign.owner_id = 1
self.campaign.trans_id = 1
self.campaign.start_date = datetime.datetime.now()
self.campaign.end_date = (datetime.datetime.now() +
datetime.timedelta(days=1))
self.campaign.total_budget_dollars = 200.
self.refund_amount = 100.
self.billable_amount = 100.
self.billable_impressions = 1000

@patch('r2.lib.promote.authorize.refund_transaction')
@patch('r2.lib.promote.PromotionLog.add')
@patch('r2.lib.promote.queries.unset_underdelivered_campaigns')
@patch('r2.lib.promote.emailer.refunded_promo')
def test_refund_campaign_success(self, emailer_refunded_promo,
queries_unset, promotion_log_add, refund_transaction):
"""Assert return value and that correct calls are made on success."""
refund_transaction.return_value = (True, None)

success = refund_campaign(
link=self.link,
camp=self.campaign,
refund_amount=self.refund_amount,
billable_amount=self.billable_amount,
billable_impressions=self.billable_impressions,
)

self.assertTrue(refund_transaction.called)
self.assertTrue(promotion_log_add.called)
queries_unset.assert_called_once_with(self.campaign)
emailer_refunded_promo.assert_called_once_with(self.link)
self.assertTrue(success)

@patch('r2.lib.promote.authorize.refund_transaction')
@patch('r2.lib.promote.PromotionLog.add')
def test_refund_campaign_failed(self, promotion_log_add,
refund_transaction):
"""Assert return value and that correct calls are made on failure."""
refund_transaction.return_value = (False, None)

success = refund_campaign(
link=self.link,
camp=self.campaign,
refund_amount=self.refund_amount,
billable_amount=self.billable_amount,
billable_impressions=self.billable_impressions,
)

self.assertTrue(refund_transaction.called)
self.assertTrue(promotion_log_add.called)
self.assertFalse(success)

def test_get_refund_amount_when_zero(self):
"""
Assert that correct value is returned when existing refund_amount is
zero.
"""
campaign = Mock(spec=('total_budget_dollars',))
campaign.total_budget_dollars = 200.
refund_amount = get_refund_amount(campaign, self.billable_amount)
self.assertEquals(refund_amount,
campaign.total_budget_dollars - self.billable_amount)

def test_get_refund_amount_rounding(self):
"""Assert that inputs are correctly rounded up to the nearest penny."""
# If campaign.refund_amount is less than a fraction of a penny,
# the refund_amount should be campaign.total_budget_dollars.
self.campaign.refund_amount = 0.00000001
refund_amount = get_refund_amount(self.campaign, self.billable_amount)
self.assertEquals(refund_amount, self.billable_amount)

self.campaign.refund_amount = 0.00999999
refund_amount = get_refund_amount(self.campaign, self.billable_amount)
self.assertEquals(refund_amount, self.billable_amount)

# If campaign.refund_amount is just slightly more than a penny,
# the refund amount should be campaign.total_budget_dollars - 0.01.
self.campaign.refund_amount = 0.01000001
refund_amount = get_refund_amount(self.campaign, self.billable_amount)
self.assertEquals(refund_amount, self.billable_amount - 0.01)

# Even if campaign.refund_amount is just barely short of two pennies,
# the refund amount should be campaign.total_budget_dollars - 0.01.
self.campaign.refund_amount = 0.01999999
refund_amount = get_refund_amount(self.campaign, self.billable_amount)
self.assertEquals(refund_amount, self.billable_amount - 0.01)

0 comments on commit 3d773f2

Please sign in to comment.