-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: can_allocate() implementation for assignment-based policies. EN…
…T-7532
- Loading branch information
1 parent
e97af2f
commit a3d4b6b
Showing
8 changed files
with
455 additions
and
10 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
Primary Python API for interacting with Assignment | ||
records and business logic. | ||
""" | ||
from django.db.models import Sum | ||
|
||
from .constants import LearnerContentAssignmentStateChoices | ||
from .models import LearnerContentAssignment | ||
|
||
|
||
def get_assignments_for_policy( | ||
subsidy_access_policy, | ||
state=LearnerContentAssignmentStateChoices.ALLOCATED, | ||
): | ||
""" | ||
Returns a queryset of all ``LearnerContentAssignment`` records | ||
for the given policy, optionally filtered to only those | ||
associated with the given ``learner_emails``. | ||
""" | ||
queryset = LearnerContentAssignment.objects.select_related( | ||
'assignment_policy', | ||
'assignment_policy__subsidy_access_policy', | ||
).filter( | ||
assignment_policy__subsidy_access_policy=subsidy_access_policy, | ||
state=state, | ||
) | ||
return queryset | ||
|
||
|
||
def get_allocated_quantity_for_policy(subsidy_access_policy): | ||
""" | ||
Returns a float representing the total quantity, in USD cents, currently allocated | ||
via Assignments for the given policy. | ||
""" | ||
assignments_queryset = get_assignments_for_policy(subsidy_access_policy) | ||
aggregate = assignments_queryset.aggregate( | ||
total_quantity=Sum('content_quantity'), | ||
) | ||
return aggregate['total_quantity'] |
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
Empty file.
37 changes: 37 additions & 0 deletions
37
enterprise_access/apps/content_assignments/tests/factories.py
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,37 @@ | ||
""" | ||
Factoryboy factories. | ||
""" | ||
|
||
from uuid import uuid4 | ||
|
||
import factory | ||
from faker import Faker | ||
|
||
from ..models import LearnerContentAssignment | ||
|
||
FAKER = Faker() | ||
|
||
|
||
def random_content_key(): | ||
""" | ||
Helper to craft a random content key. | ||
""" | ||
fake_words = [ | ||
FAKER.word() + str(FAKER.random_int()) | ||
for _ in range(3) | ||
] | ||
return 'course-v1:{}+{}+{}'.format(*fake_words) | ||
|
||
|
||
class LearnerContentAssignmentFactory(factory.django.DjangoModelFactory): | ||
""" | ||
Base Test factory for the ``LearnerContentAssisgnment`` model. | ||
""" | ||
class Meta: | ||
model = LearnerContentAssignment | ||
|
||
uuid = factory.LazyFunction(uuid4) | ||
learner_email = factory.LazyAttribute(lambda _: FAKER.email()) | ||
lms_user_id = factory.LazyAttribute(lambda _: FAKER.pyint()) | ||
content_key = factory.LazyAttribute(lambda _: random_content_key()) | ||
content_quantity = factory.LazyAttribute(lambda _: FAKER.pyint()) |
94 changes: 94 additions & 0 deletions
94
enterprise_access/apps/content_assignments/tests/test_api.py
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,94 @@ | ||
""" | ||
Tests for the ``api.py`` module of the content_assignments app. | ||
""" | ||
import uuid | ||
|
||
from django.test import TestCase | ||
|
||
from ...subsidy_access_policy.tests.factories import AssignedLearnerCreditAccessPolicyFactory | ||
from ..api import get_allocated_quantity_for_policy, get_assignments_for_policy | ||
from ..constants import LearnerContentAssignmentStateChoices | ||
from .factories import LearnerContentAssignmentFactory | ||
|
||
ACTIVE_ASSIGNED_LEARNER_CREDIT_POLICY_UUID = uuid.uuid4() | ||
|
||
|
||
class TestContentAssignmentApi(TestCase): | ||
""" | ||
Tests functions of the ``content_assignment.api`` module. | ||
""" | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
cls.active_policy = AssignedLearnerCreditAccessPolicyFactory( | ||
uuid=ACTIVE_ASSIGNED_LEARNER_CREDIT_POLICY_UUID, | ||
spend_limit=10000, | ||
) | ||
cls.assignment_policy = AssignmentPolicy.objects.create( | ||
subsidy_access_policy=cls.active_policy, | ||
) | ||
|
||
def test_get_assignments_for_policy(self): | ||
""" | ||
Simple test to fetch assignment records related to a given policy. | ||
""" | ||
expected_assignments = [ | ||
LearnerContentAssignmentFactory.create( | ||
assignment_policy=self.assignment_policy, | ||
) for _ in range(10) | ||
] | ||
|
||
with self.assertNumQueries(1): | ||
actual_assignments = list(get_assignments_for_policy(self.active_policy)) | ||
|
||
self.assertEqual( | ||
sorted(actual_assignments, key=lambda record: record.uuid), | ||
sorted(expected_assignments, key=lambda record: record.uuid), | ||
) | ||
|
||
def test_get_assignments_for_policy_different_states(self): | ||
""" | ||
Simple test to fetch assignment records related to a given policy, | ||
filtered among different states | ||
""" | ||
expected_assignments = { | ||
LearnerContentAssignmentStateChoices.CANCELLED: [], | ||
LearnerContentAssignmentStateChoices.ACCEPTED: [], | ||
} | ||
for index in range(10): | ||
if index % 2: | ||
state = LearnerContentAssignmentStateChoices.CANCELLED | ||
else: | ||
state = LearnerContentAssignmentStateChoices.ACCEPTED | ||
|
||
expected_assignments[state].append( | ||
LearnerContentAssignmentFactory.create(assignment_policy=self.assignment_policy, state=state) | ||
) | ||
|
||
for filter_state in ( | ||
LearnerContentAssignmentStateChoices.CANCELLED, | ||
LearnerContentAssignmentStateChoices.ACCEPTED, | ||
): | ||
with self.assertNumQueries(1): | ||
actual_assignments = list(get_assignments_for_policy(self.active_policy, filter_state)) | ||
|
||
self.assertEqual( | ||
sorted(actual_assignments, key=lambda record: record.uuid), | ||
sorted(expected_assignments[filter_state], key=lambda record: record.uuid), | ||
) | ||
|
||
def test_get_allocated_quantity_for_policy(self): | ||
""" | ||
Tests to verify that we can fetch the total allocated quantity across a set of assignments | ||
related to some policy. | ||
""" | ||
for amount in (1000, 2000, 3000): | ||
LearnerContentAssignmentFactory.create( | ||
assignment_policy=self.assignment_policy, | ||
content_quantity=amount, | ||
) | ||
|
||
with self.assertNumQueries(1): | ||
actual_amount = get_allocated_quantity_for_policy(self.active_policy) | ||
self.assertEqual(actual_amount, 6000) |
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
Oops, something went wrong.