-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #337 from compute-tooling/rm-unused-code
Clean up unused code after PR #335
- Loading branch information
Showing
10 changed files
with
42 additions
and
285 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,11 @@ | ||
from django.contrib import admin | ||
|
||
from .models import (Customer, Product, Plan, Subscription, SubscriptionItem, | ||
UsageRecord, Event) | ||
from .models import Customer, Product, Plan, Subscription, SubscriptionItem, Event | ||
|
||
# Register your models here. | ||
admin.site.register(Customer) | ||
admin.site.register(Product) | ||
admin.site.register(Plan) | ||
admin.site.register(Subscription) | ||
admin.site.register(SubscriptionItem) | ||
admin.site.register(UsageRecord) | ||
admin.site.register(Event) | ||
admin.site.register(Event) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Generated by Django 3.0.10 on 2020-09-11 14:33 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("billing", "0005_auto_20200306_1600"), | ||
] | ||
|
||
operations = [ | ||
migrations.DeleteModel(name="UsageRecord",), | ||
] |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,6 @@ | |
Product, | ||
Subscription, | ||
SubscriptionItem, | ||
UsageRecord, | ||
create_pro_billing_objects, | ||
UpdateStatus, | ||
) | ||
|
@@ -47,143 +46,20 @@ def test_construct_customer(self, stripe_customer, user): | |
assert not created | ||
assert not customer.livemode | ||
|
||
def test_construct(self): | ||
product = Product.objects.get(name="modeler/Used-for-testing") | ||
assert ( | ||
f"{product.project.owner.user.username}/{product.project.title}" | ||
== product.name | ||
) | ||
assert ( | ||
Plan.objects.filter(product__name="modeler/Used-for-testing").count() == 2 | ||
) | ||
|
||
def test_construct_subscription(self, basiccustomer, licensed_plan, metered_plan): | ||
stripe_subscription = Subscription.create_stripe_object( | ||
basiccustomer, [licensed_plan, metered_plan] | ||
) | ||
assert stripe_subscription | ||
|
||
subscription, created = Subscription.get_or_construct( | ||
stripe_subscription.id, | ||
customer=basiccustomer, | ||
plans=[licensed_plan, metered_plan], | ||
) | ||
|
||
for raw_si in stripe_subscription["items"]["data"]: | ||
stripe_si = SubscriptionItem.get_stripe_object(raw_si["id"]) | ||
plan, created = Plan.get_or_construct(raw_si["plan"]["id"]) | ||
assert not created | ||
si, created = SubscriptionItem.get_or_construct( | ||
stripe_si.id, plan, subscription | ||
) | ||
assert created | ||
assert si | ||
assert si.stripe_id == raw_si["id"] | ||
assert si.plan == plan | ||
assert si.subscription == subscription | ||
|
||
assert subscription | ||
assert created | ||
assert subscription.customer == basiccustomer | ||
qs = Subscription.objects.filter( | ||
plans__stripe_id=licensed_plan.stripe_id, | ||
customer__stripe_id=basiccustomer.stripe_id, | ||
) | ||
assert qs.count() == 1 | ||
assert subscription.subscription_items.count() == 2 | ||
|
||
subscription, created = Subscription.get_or_construct( | ||
stripe_subscription.stripe_id | ||
) | ||
|
||
assert subscription | ||
assert not created | ||
assert subscription.customer == basiccustomer | ||
assert subscription.subscription_items.count() == 2 | ||
|
||
def test_metered_subscription_item(self, subscription): | ||
assert subscription | ||
si = subscription.subscription_items.get(plan__usage_type="metered") | ||
assert si | ||
assert si.plan.usage_type == "metered" | ||
ts = math.floor(datetime.now().timestamp()) | ||
stripe_usage_record = UsageRecord.create_stripe_object( | ||
quantity=10, timestamp=ts, subscription_item=si, action="increment" | ||
) | ||
assert stripe_usage_record | ||
print("one", stripe_usage_record) | ||
usage_record = UsageRecord.construct(stripe_usage_record, si) | ||
|
||
assert usage_record | ||
assert usage_record.stripe_id == stripe_usage_record.id | ||
assert usage_record.livemode == False | ||
assert usage_record.action == "increment" | ||
assert usage_record.quantity == 10 | ||
assert usage_record.subscription_item == si | ||
|
||
def test_update_customer(self, customer): | ||
tok = "tok_bypassPending" | ||
prev_source = customer.default_source | ||
customer.update_source(tok) | ||
assert customer.default_source != prev_source | ||
|
||
def test_cancel_subscriptions(self, customer): | ||
def test_cancel_subscriptions(self, pro_profile): | ||
customer = pro_profile.user.customer | ||
for sub in customer.subscriptions.all(): | ||
assert not sub.cancel_at_period_end | ||
customer.cancel_subscriptions() | ||
for sub in customer.subscriptions.all(): | ||
assert sub.cancel_at_period_end | ||
|
||
def test_customer_sync_subscriptions(self, db, client): | ||
u = User.objects.create_user( | ||
username="synctest", email="[email protected]", password="syncer2222" | ||
) | ||
p = Profile.objects.create(user=u, is_active=True) | ||
stripe_customer = stripe.Customer.create( | ||
email=u.email, source="tok_bypassPending" | ||
) | ||
c, _ = Customer.get_or_construct(stripe_customer.id, u) | ||
assert c.subscriptions.count() == 1 | ||
assert c.current_plan(as_dict=False).plan == Plan.objects.get( | ||
nickname="Free Plan" | ||
) | ||
|
||
# test new customer gets all subscriptions. | ||
c.sync_subscriptions() | ||
primary_sub = c.subscriptions.get(subscription_type="primary") | ||
curr_plans = [plan.id for plan in primary_sub.plans.all()] | ||
assert set(curr_plans) == set( | ||
pp.id for pp in Plan.get_public_plans(usage_type="metered") | ||
) | ||
|
||
# create new product and test that customer gets subscribed to it. | ||
client.login(username=u.username, password="syncer2222") | ||
post_data = { | ||
"title": "New-Model", | ||
"oneliner": "oneliner", | ||
"description": "**Super** new!", | ||
"repo_url": "https://github.com/compute-tooling/compute-studio", | ||
"repo_tag": "dev", | ||
"cpu": 3, | ||
"memory": 9, | ||
"listed": True, | ||
} | ||
|
||
with mock_sync_projects(): | ||
resp = client.post("/apps/api/v1/", post_data) | ||
assert resp.status_code == 200 | ||
Project.objects.sync_products() | ||
Customer.objects.sync_subscriptions() | ||
|
||
c = Customer.objects.get(user__username="synctest") | ||
primary_sub = c.subscriptions.get(subscription_type="primary") | ||
curr_plans = [plan.id for plan in primary_sub.plans.all()] | ||
assert set(curr_plans) == set( | ||
pp.id for pp in Plan.get_public_plans(usage_type="metered") | ||
) | ||
|
||
Customer.objects.sync_subscriptions() | ||
|
||
def test_create_pro_billing_objects(self, db): | ||
# this function is called in conftest.py | ||
# create_pro_billing_objects() | ||
|
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
Oops, something went wrong.