def test_get_subscription_by_period(self): subscriptions = [ make_subscription(id="yearly_sub", period="yearly"), make_subscription(id="monthly_sub", period="monthly"), ] listing = make_listing(period="monthly") subscription = get_subscription_by_period(subscriptions, listing) self.assertEqual(subscription.id, "monthly_sub")
def test_get_subscription_by_period_with_listing_without_period(self): subscriptions = [ make_subscription(id="yearly_sub", period="yearly"), make_subscription(id="monthly_sub", period="monthly"), ] listing = make_listing() listing.period = None subscription = get_subscription_by_period(subscriptions, listing) self.assertEqual(subscription, None)
def test_get_pending_purchases(self): subscriptions = [ make_subscription(pending_purchases=[ "pAaBbCcDdEeFfGg", "pAaBbCcDdEeFfGg2", ]), make_subscription(pending_purchases=[ "pAaBbCcDdEeFfGg3", ]), ] pending_purchase = get_pending_purchases(subscriptions=subscriptions) expected_pending_purchases = [ "pAaBbCcDdEeFfGg", "pAaBbCcDdEeFfGg2", "pAaBbCcDdEeFfGg3", ] self.assertEqual(pending_purchase, expected_pending_purchases)
def test_is_user_subscription_cancelled(self): listing = make_listing(id="lAaBbCcDd") subscriptions = [ make_subscription( period="monthly", items=[make_subscription_item(product_listing_id="randomID")], ) ] is_cancelled = is_user_subscription_cancelled(listing, subscriptions) self.assertEqual(is_cancelled, True)
def test_set_listings_trial_status_to_false_due_to_listings(self): subscriptions = [ make_subscription(started_with_trial=False, status="deactivated") ] listings = { "lAbcABC": make_listing(trial_days=0), "lBcdBCD": make_listing(trial_days=0), } set_listings_trial_status(listings, subscriptions) for listing in listings.values(): self.assertEqual(listing.can_be_trialled, False)
def test_set_listings_trial_status_to_false_because_of_subscriptions(self): subscriptions = [make_subscription(started_with_trial=True)] listings = { "lAbcABC": make_listing(trial_days=30), "lBcdBCD": make_listing(trial_days=1), } set_listings_trial_status(listings, subscriptions) for listing in listings.values(): self.assertEqual(listing.can_be_trialled, False) subscriptions = [make_subscription()] listings = { "lAbcABC": make_listing(trial_days=30), "lBcdBCD": make_listing(trial_days=1), } set_listings_trial_status(listings, subscriptions) for listing in listings.values(): self.assertEqual(listing.can_be_trialled, False)
def test_extract_last_purchase_ids(self): subscriptions = [ make_subscription(period="monthly", last_purchase_id="pABC1"), make_subscription(period="yearly", last_purchase_id="pABC2"), ] last_purchase_ids = extract_last_purchase_ids(subscriptions) expectation = { "monthly": "pABC1", "yearly": "pABC2", } self.assertEqual(last_purchase_ids, expectation) subscriptions = [ make_subscription(period="monthly", last_purchase_id="pABC1"), ] last_purchase_ids = extract_last_purchase_ids(subscriptions) expectation = { "monthly": "pABC1", "yearly": "", } self.assertEqual(last_purchase_ids, expectation) last_purchase_ids = extract_last_purchase_ids([]) expectation = { "monthly": "", "yearly": "", } self.assertEqual(last_purchase_ids, expectation)
def test_get_user_subscription_statuses(self): freeze_datetime = "2020-09-01T00:00:00Z" scenarios = { "test_free_user_subscription": { "parameters": { "type": "free", "end_date": "2020-08-31T23:59:59Z", "subscriptions": None, "listing": None, }, "expectations": { "is_upsizeable": False, "is_downsizeable": False, "is_cancellable": False, "is_cancelled": False, "is_expiring": False, "is_in_grace_period": False, "is_expired": False, "is_trialled": False, "is_renewable": False, "is_renewal_actionable": False, "has_pending_purchases": False, }, }, "test_yearly_user_subscription": { "parameters": { "type": "yearly", "end_date": "2020-08-31T23:59:59Z", "subscriptions": None, "listing": None, }, "expectations": { "is_upsizeable": True, "is_downsizeable": False, "is_cancellable": False, "is_cancelled": False, "is_expiring": False, "is_in_grace_period": True, "is_expired": False, "is_trialled": False, "is_renewable": False, "is_renewal_actionable": False, "has_pending_purchases": False, }, }, "test_monthly_user_subscription": { "parameters": { "type": "monthly", "end_date": "2020-09-01T00:00:00Z", "subscriptions": [ make_subscription(items=[ make_subscription_item( product_listing_id="listing-id") ]) ], "listing": make_listing(id="listing-id"), }, "expectations": { "is_upsizeable": True, "is_downsizeable": True, "is_cancellable": True, "is_cancelled": False, "is_expiring": True, "is_in_grace_period": False, "is_expired": False, "is_trialled": False, "is_renewable": False, "is_renewal_actionable": False, "has_pending_purchases": False, }, }, "test_cancelled_user_subscription": { "parameters": { "type": "monthly", "end_date": "2020-09-05T00:00:00Z", "subscriptions": [ make_subscription(items=[ make_subscription_item( product_listing_id="random-id") ]) ], "listing": make_listing(id="listing-id"), }, "expectations": { "is_upsizeable": False, "is_downsizeable": False, "is_cancellable": False, "is_cancelled": True, "is_expiring": True, "is_in_grace_period": False, "is_expired": False, "is_trialled": False, "is_renewable": False, "is_renewal_actionable": False, "has_pending_purchases": False, }, }, "test_expired_user_subscription": { "parameters": { "type": "monthly", "end_date": "2020-08-01T00:00:00Z", "subscriptions": None, "listing": None, }, "expectations": { "is_upsizeable": False, "is_downsizeable": False, "is_cancellable": False, "is_cancelled": False, "is_expiring": False, "is_in_grace_period": False, "is_expired": True, "is_trialled": False, "is_renewable": False, "is_renewal_actionable": False, "has_pending_purchases": False, }, }, "test_trial_user_subscription": { "parameters": { "type": "trial", "end_date": "2020-08-18T00:00:00Z", "subscriptions": None, "listing": None, }, "expectations": { "is_upsizeable": False, "is_downsizeable": False, "is_cancellable": False, "is_cancelled": False, "is_expiring": False, "is_in_grace_period": True, "is_expired": False, "is_trialled": True, "is_renewable": False, "is_renewal_actionable": False, "has_pending_purchases": False, }, }, "test_pending_purchases_user_subscription": { "parameters": { "type": "trial", "end_date": "2020-09-30T23:59:59Z", "subscriptions": [make_subscription(pending_purchases=["pAaBbCcDdEeFfgG"])], "listing": None, }, "expectations": { "is_upsizeable": False, "is_downsizeable": False, "is_cancellable": False, "is_cancelled": False, "is_expiring": True, "is_in_grace_period": False, "is_expired": False, "is_trialled": False, "is_renewable": False, "is_renewal_actionable": False, "has_pending_purchases": True, }, }, "test_legacy_user_subscription": { "parameters": { "type": "legacy", "end_date": "2020-10-01T10:00:00Z", "renewal": make_renewal( actionable=True, status="pending", start_date="2020-08-01T10:00:00Z", end_date="2020-10-01T10:00:00Z", ), }, "expectations": { "is_upsizeable": False, "is_downsizeable": False, "is_cancellable": False, "is_cancelled": False, "is_expiring": False, "is_in_grace_period": False, "is_expired": False, "is_trialled": False, "is_renewable": True, "is_renewal_actionable": True, "has_pending_purchases": False, }, }, "test_non_actionable_legacy_user_subscription": { "parameters": { "type": "legacy", "end_date": "2020-10-01T10:00:00Z", "renewal": make_renewal(actionable=False), }, "expectations": { "is_upsizeable": False, "is_downsizeable": False, "is_cancellable": False, "is_cancelled": False, "is_expiring": False, "is_in_grace_period": False, "is_expired": False, "is_trialled": False, "is_renewable": True, "is_renewal_actionable": True, "has_pending_purchases": False, }, }, } with freeze_time(freeze_datetime): for case, scenario in scenarios.items(): with self.subTest(msg=f"{case}", scenario=scenario): parameters = scenario["parameters"] statuses = get_user_subscription_statuses( type=parameters.get("type"), end_date=parameters.get("end_date"), renewal=parameters.get("renewal"), subscriptions=parameters.get("subscriptions"), listing=parameters.get("listing"), ) self.assertEqual(statuses, scenario["expectations"])
def test_has_no_pending_purchases(self): subscriptions = [make_subscription()] self.assertEqual(has_pending_purchases(subscriptions), False)
def test_has_pending_purchases(self): subscriptions = [ make_subscription(pending_purchases=["pAaBbCcDdEeFfGg"]), ] self.assertEqual(has_pending_purchases(subscriptions), True)
def test_get_no_pending_purchases(self): subscriptions = [make_subscription(), make_subscription()] pending_purchase = get_pending_purchases(subscriptions=subscriptions) self.assertEqual(pending_purchase, [])