Skip to content

Commit 3eb5abe

Browse files
committed
better_tests_tenant_billing
1 parent 7cfef3f commit 3eb5abe

File tree

1 file changed

+60
-13
lines changed

1 file changed

+60
-13
lines changed
Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,68 @@
1-
from datetime import date
1+
from datetime import date, timedelta
2+
from random import randint
23

4+
import backoff
5+
import pytest
36

4-
def test_trigger_billing(master_api, custom_tenant):
5-
assert master_api.tenants.trigger_billing(custom_tenant, date.today().isoformat())
67

8+
# Some of the tests can fail randomly because of tenant not being 100% ready, be warned
79

8-
def test_trigger_billing_resource(custom_tenant):
9-
assert custom_tenant.trigger_billing(date.today().isoformat())
10+
@pytest.fixture()
11+
def next_day() -> str:
12+
return (date.today() + timedelta(days=1)).isoformat()
1013

1114

12-
def test_trigger_billing_account(master_api, custom_tenant):
13-
# this test can sometimes fail randomly because of tenant not being 100% ready
14-
account = custom_tenant.admin_api(wait=True).accounts.list()[0]
15-
assert master_api.tenants.trigger_billing_account(custom_tenant, account, date.today().isoformat())
15+
@pytest.fixture()
16+
def paid_account(service_params, account_params, account_plans_params, application_plan_params):
17+
def _paid_account(api):
18+
"""
19+
Creates new account with payed app plan on default service.
20+
No need for deletion as all is happening on temporary tenant.
21+
"""
22+
service = api.services.list()[0]
1623

24+
account_params.update(org_name=f"test-{randint(1,10000)}", username=f"test-{randint(1,10000)}")
25+
account = api.accounts.create(account_params)
1726

18-
def test_trigger_billing_account_resource(custom_tenant):
19-
# this test can sometimes fail randomly because of tenant not being 100% ready
20-
account = custom_tenant.admin_api(wait=True).accounts.list()[0]
21-
assert custom_tenant.trigger_billing_account(account, date.today().isoformat())
27+
application_plan_params.update(name=f"test-{randint(1,10000)}", setup_fee="5", cost_per_month="5")
28+
app_plan = service.app_plans.create(params=application_plan_params)
29+
30+
application_params = dict(name=f"test-{randint(1,10000)}", description="desc", plan_id=app_plan.entity_id)
31+
account.applications.create(params=application_params)
32+
33+
return account
34+
return _paid_account
35+
36+
37+
@backoff.on_predicate(backoff.fibo, lambda x: x == 0, max_tries=8, jitter=None)
38+
def count_invoice(api, account):
39+
# creating the invoice takes some time
40+
return len(api.invoices.list_by_account(account))
41+
42+
43+
def test_trigger_billing(master_api, custom_tenant, paid_account, next_day):
44+
api = custom_tenant.admin_api(wait=True)
45+
account = paid_account(api)
46+
assert master_api.tenants.trigger_billing(custom_tenant, next_day)
47+
assert count_invoice(api, account) == 1
48+
49+
50+
def test_trigger_billing_resource(custom_tenant, paid_account, next_day):
51+
api = custom_tenant.admin_api(wait=True)
52+
account = paid_account(api)
53+
assert custom_tenant.trigger_billing(next_day)
54+
assert count_invoice(api, account) == 1
55+
56+
57+
def test_trigger_billing_account(master_api, custom_tenant, paid_account, next_day):
58+
api = custom_tenant.admin_api(wait=True)
59+
account = paid_account(api)
60+
assert master_api.tenants.trigger_billing_account(custom_tenant, account, next_day)
61+
assert count_invoice(api, account) == 1
62+
63+
64+
def test_trigger_billing_account_resource(custom_tenant, paid_account, next_day):
65+
api = custom_tenant.admin_api(wait=True)
66+
account = paid_account(api)
67+
assert custom_tenant.trigger_billing_account(account, next_day)
68+
assert count_invoice(api, account) == 1

0 commit comments

Comments
 (0)