1414from model_bakery import baker
1515
1616from kobo .apps .kobo_auth .shortcuts import User
17- from kobo .apps .organizations .constants import SupportedUsageType , UsageType
17+ from kobo .apps .organizations .constants import UsageType
1818from kobo .apps .organizations .models import Organization
1919from kobo .apps .organizations .utils import get_billing_dates
2020from kobo .apps .stripe .models import ExceededLimitCounter
@@ -67,75 +67,57 @@ def setUpTestData(cls):
6767
6868 def test_get_organization_subscription_limits (self ):
6969 free_plan = generate_free_plan ()
70+ first_paid_plan_limits = {
71+ f'{ UsageType .MT_CHARACTERS } _limit' : '100' ,
72+ f'{ UsageType .ASR_SECONDS } _limit' : '200' ,
73+ f'{ UsageType .LLM_REQUESTS } _limit' : '300' ,
74+ f'{ UsageType .SUBMISSION } _limit' : '400' ,
75+ f'{ UsageType .STORAGE_BYTES } _limit' : '500' ,
76+ }
77+ # second plan for org2 where we append (not add) 1 to all the limits
78+ second_paid_plan_limits = {
79+ key : f'{ value } 1' for key , value in first_paid_plan_limits .items ()
80+ }
7081 product_metadata = {
71- f'{ UsageType .MT_CHARACTERS } _limit' : '1234' ,
72- f'{ UsageType .ASR_SECONDS } _limit' : '5678' ,
73- f'{ UsageType .SUBMISSION } _limit' : '91011' ,
74- f'{ UsageType .STORAGE_BYTES } _limit' : '121314' ,
7582 'product_type' : 'plan' ,
7683 'plan_type' : 'enterprise' ,
7784 }
78- # create a second plan for org2 where we append (not add) 1 to all the limits
85+ first_product_metadata = {
86+ ** first_paid_plan_limits ,
87+ ** product_metadata ,
88+ }
7989 second_product_metadata = {
80- key : f' { value } 1' if key . endswith ( 'limit' ) else value
81- for key , value in product_metadata . items ()
90+ ** second_paid_plan_limits ,
91+ ** product_metadata ,
8292 }
83- generate_plan_subscription (self .organization , metadata = product_metadata )
93+
94+ generate_plan_subscription (self .organization , metadata = first_product_metadata )
8495 generate_plan_subscription (
8596 self .second_organization , metadata = second_product_metadata
8697 )
8798 all_limits = get_organizations_subscription_limits ()
88- assert (
89- all_limits [self .organization .id ][f'{ UsageType .MT_CHARACTERS } _limit' ] == 1234
90- )
91- assert (
92- all_limits [self .second_organization .id ][f'{ UsageType .MT_CHARACTERS } _limit' ]
93- == 12341
94- )
95- assert (
96- all_limits [self .organization .id ][f'{ UsageType .ASR_SECONDS } _limit' ] == 5678
97- )
98- assert (
99- all_limits [self .second_organization .id ][f'{ UsageType .ASR_SECONDS } _limit' ]
100- == 56781
101- )
102- assert (
103- all_limits [self .organization .id ][f'{ UsageType .SUBMISSION } _limit' ] == 91011
104- )
105- assert (
106- all_limits [self .second_organization .id ][f'{ UsageType .SUBMISSION } _limit' ]
107- == 910111
108- )
109- assert (
110- all_limits [self .organization .id ][f'{ UsageType .STORAGE_BYTES } _limit' ]
111- == 121314
112- )
113- assert (
114- all_limits [self .second_organization .id ][f'{ UsageType .STORAGE_BYTES } _limit' ]
115- == 1213141
116- )
11799
118100 other_orgs = Organization .objects .exclude (
119101 id__in = [self .organization .id , self .second_organization .id ]
120102 )
121- for org in other_orgs :
122- assert all_limits [org .id ][f'{ UsageType .MT_CHARACTERS } _limit' ] == int (
123- free_plan .metadata [f'{ UsageType .MT_CHARACTERS } _limit' ]
124- )
125- assert all_limits [org .id ][f'{ UsageType .ASR_SECONDS } _limit' ] == int (
126- free_plan .metadata [f'{ UsageType .ASR_SECONDS } _limit' ]
127- )
128- assert all_limits [org .id ][f'{ UsageType .SUBMISSION } _limit' ] == int (
129- free_plan .metadata [f'{ UsageType .SUBMISSION } _limit' ]
130- )
131- assert all_limits [org .id ][f'{ UsageType .STORAGE_BYTES } _limit' ] == int (
132- free_plan .metadata [f'{ UsageType .STORAGE_BYTES } _limit' ]
103+ for usage_type , _ in UsageType .choices :
104+ assert all_limits [self .organization .id ][f'{ usage_type } _limit' ] == float (
105+ first_paid_plan_limits [f'{ usage_type } _limit' ]
133106 )
107+ assert all_limits [self .second_organization .id ][
108+ f'{ usage_type } _limit'
109+ ] == float (second_paid_plan_limits [f'{ usage_type } _limit' ])
110+
111+ for org in other_orgs :
112+ assert all_limits [org .id ][f'{ usage_type } _limit' ] == float (
113+ free_plan .metadata [f'{ usage_type } _limit' ]
114+ )
134115
135116 def test__prioritizes_price_metadata (self ):
136117 product_metadata = {
137118 f'{ UsageType .MT_CHARACTERS } _limit' : '1' ,
138119 f'{ UsageType .ASR_SECONDS } _limit' : '1' ,
120+ f'{ UsageType .LLM_REQUESTS } _limit' : '1' ,
139121 f'{ UsageType .SUBMISSION } _limit' : '1' ,
140122 f'{ UsageType .STORAGE_BYTES } _limit' : '1' ,
141123 'product_type' : 'plan' ,
@@ -144,14 +126,15 @@ def test__prioritizes_price_metadata(self):
144126 price_metadata = {
145127 f'{ UsageType .MT_CHARACTERS } _limit' : '2' ,
146128 f'{ UsageType .ASR_SECONDS } _limit' : '2' ,
129+ f'{ UsageType .LLM_REQUESTS } _limit' : '2' ,
147130 f'{ UsageType .SUBMISSION } _limit' : '2' ,
148131 f'{ UsageType .STORAGE_BYTES } _limit' : '2' ,
149132 }
150133 generate_plan_subscription (
151134 self .organization , metadata = product_metadata , price_metadata = price_metadata
152135 )
153136 limits = get_paid_subscription_limits ([self .organization .id ]).first ()
154- for usage_type , _ in SupportedUsageType .choices :
137+ for usage_type , _ in UsageType .choices :
155138 assert limits [f'{ usage_type } _limit' ] == '2'
156139
157140 def test_get_subscription_limits_takes_most_recent_active_subscriptions (self ):
@@ -477,6 +460,7 @@ def test_get_org_effective_limits(self, include_onetime_addons):
477460 plan_product_metadata = {
478461 f'{ UsageType .MT_CHARACTERS } _limit' : '1' ,
479462 f'{ UsageType .ASR_SECONDS } _limit' : '1' ,
463+ f'{ UsageType .LLM_REQUESTS } _limit' : '1' ,
480464 f'{ UsageType .SUBMISSION } _limit' : '1' ,
481465 f'{ UsageType .STORAGE_BYTES } _limit' : '1' ,
482466 'product_type' : 'plan' ,
@@ -485,6 +469,7 @@ def test_get_org_effective_limits(self, include_onetime_addons):
485469 product_metadata = {
486470 f'{ UsageType .MT_CHARACTERS } _limit' : '2' ,
487471 f'{ UsageType .ASR_SECONDS } _limit' : '2' ,
472+ f'{ UsageType .LLM_REQUESTS } _limit' : '2' ,
488473 f'{ UsageType .SUBMISSION } _limit' : '2' ,
489474 f'{ UsageType .STORAGE_BYTES } _limit' : '2' ,
490475 'product_type' : 'plan' ,
@@ -497,6 +482,7 @@ def test_get_org_effective_limits(self, include_onetime_addons):
497482 one_time_nlp_addon_metadata = {
498483 f'{ UsageType .MT_CHARACTERS } _limit' : '15' ,
499484 f'{ UsageType .ASR_SECONDS } _limit' : '20' ,
485+ f'{ UsageType .LLM_REQUESTS } _limit' : '20' ,
500486 }
501487 nlp_addon = _create_one_time_addon_product (one_time_nlp_addon_metadata )
502488 customer = baker .make (Customer , subscriber = self .organization )
@@ -526,11 +512,17 @@ def test_get_org_effective_limits(self, include_onetime_addons):
526512 assert (
527513 results [self .second_organization .id ][f'{ UsageType .ASR_SECONDS } _limit' ] == 2
528514 )
515+ assert (
516+ results [self .second_organization .id ][f'{ UsageType .LLM_REQUESTS } _limit' ] == 2
517+ )
529518 if include_onetime_addons :
530519 assert (
531520 results [self .organization .id ][f'{ UsageType .MT_CHARACTERS } _limit' ] == 16
532521 )
533522 assert results [self .organization .id ][f'{ UsageType .ASR_SECONDS } _limit' ] == 21
523+ assert (
524+ results [self .organization .id ][f'{ UsageType .LLM_REQUESTS } _limit' ] == 21
525+ )
534526 assert (
535527 results [self .second_organization .id ][f'{ UsageType .SUBMISSION } _limit' ]
536528 == 12
@@ -540,6 +532,7 @@ def test_get_org_effective_limits(self, include_onetime_addons):
540532 results [self .organization .id ][f'{ UsageType .MT_CHARACTERS } _limit' ] == 1
541533 )
542534 assert results [self .organization .id ][f'{ UsageType .ASR_SECONDS } _limit' ] == 1
535+ assert results [self .organization .id ][f'{ UsageType .LLM_REQUESTS } _limit' ] == 1
543536 assert (
544537 results [self .second_organization .id ][f'{ UsageType .SUBMISSION } _limit' ]
545538 == 2
@@ -603,6 +596,7 @@ def setUp(self):
603596 product_metadata = {
604597 f'{ UsageType .MT_CHARACTERS } _limit' : '1' ,
605598 f'{ UsageType .ASR_SECONDS } _limit' : '1' ,
599+ f'{ UsageType .LLM_REQUESTS } _limit' : '1' ,
606600 f'{ UsageType .SUBMISSION } _limit' : '1' ,
607601 f'{ UsageType .STORAGE_BYTES } _limit' : '1' ,
608602 'product_type' : 'plan' ,
@@ -627,7 +621,7 @@ def test_check_exceeded_limit_adds_counters(self):
627621 ):
628622 self .add_submissions (count = 2 , asset = self .asset , username = 'someuser' )
629623 self .add_nlp_trackers ()
630- for usage_type , _ in SupportedUsageType .choices :
624+ for usage_type , _ in UsageType .choices :
631625 check_exceeded_limit (self .someuser , usage_type )
632626 assert (
633627 ExceededLimitCounter .objects .filter (
@@ -638,7 +632,7 @@ def test_check_exceeded_limit_adds_counters(self):
638632
639633 def test_check_exceeded_limit_updates_counters (self ):
640634 today = timezone .now ()
641- for usage_type , _ in SupportedUsageType .choices :
635+ for usage_type , _ in UsageType .choices :
642636 baker .make (
643637 ExceededLimitCounter ,
644638 user = self .anotheruser ,
@@ -655,7 +649,7 @@ def test_check_exceeded_limit_updates_counters(self):
655649 ):
656650 self .add_submissions (count = 2 , asset = self .asset , username = 'someuser' )
657651 self .add_nlp_trackers ()
658- for usage_type , _ in SupportedUsageType .choices :
652+ for usage_type , _ in UsageType .choices :
659653 check_exceeded_limit (self .someuser , usage_type )
660654 counter = ExceededLimitCounter .objects .get (
661655 user_id = self .anotheruser .id , limit_type = usage_type
0 commit comments