-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: can_allocate() implementation for assignment-based policies #249
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()) |
95 changes: 95 additions & 0 deletions
95
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,95 @@ | ||
""" | ||
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 ..models import AssignmentPolicy | ||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
avoids circular import