Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
adamstankiewicz committed Sep 3, 2024
1 parent c5707c2 commit 467ff12
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
34 changes: 19 additions & 15 deletions enterprise_access/apps/api/v1/tests/test_allocation_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,18 @@ def setUpTestData(cls):
spend_limit=10000 * 100,
)

# Mock results from the catalog content metadata API endpoint.
cls.mock_catalog_result = {
'count': 2,
'results': [
{
'key': cls.content_key,
'parent_content_key': cls.parent_content_key,
'data': 'things',
},
],
}

def setUp(self):
super().setUp()
self.maxDiff = None
Expand All @@ -461,14 +473,6 @@ def setUp(self):
def delete_assignments():
return self.assignment_configuration.assignments.all().delete()

# Mock results from the catalog content metadata API endpoint.
self.mock_catalog_result = {
'count': 2,
'results': [
{'key': 'course+A', 'data': 'things'}, {'key': 'course+B', 'data': 'stuff'},
],
}

self.addCleanup(delete_assignments)

@mock.patch.object(
Expand Down Expand Up @@ -559,6 +563,7 @@ def test_allocate_happy_path_e2e(
email='[email protected]',
lms_user_id=4277
)
assignment_content_quantity_usd_cents = 12345
LearnerContentAssignmentFactory(
assignment_configuration=self.assignment_configuration,
learner_email='[email protected]',
Expand All @@ -569,7 +574,7 @@ def test_allocate_happy_path_e2e(
parent_content_key=self.parent_content_key,
is_assigned_course_run=True,
content_title=self.content_title,
content_quantity=-12345,
content_quantity=-assignment_content_quantity_usd_cents,
state=LearnerContentAssignmentStateChoices.EXPIRED,
)

Expand All @@ -585,7 +590,7 @@ def test_allocate_happy_path_e2e(
allocate_payload = {
'learner_emails': ['[email protected]', '[email protected]', '[email protected]'],
'content_key': self.content_key,
'content_price_cents': 123.45 * 100, # policy limit is 100000.00 USD, so this should be well below limit
'content_price_cents': assignment_content_quantity_usd_cents, # this should be well below limit
}

response = self.client.post(allocate_url, data=allocate_payload)
Expand All @@ -595,7 +600,7 @@ def test_allocate_happy_path_e2e(
for assignment in self.assignment_configuration.assignments.filter(
state=LearnerContentAssignmentStateChoices.ALLOCATED,
content_key=self.content_key,
content_quantity=-123.45 * 100,
content_quantity=-assignment_content_quantity_usd_cents
)
}
self.assertEqual(3, len(allocation_records_by_email))
Expand All @@ -611,7 +616,7 @@ def test_allocate_happy_path_e2e(
'parent_content_key': self.parent_content_key,
'is_assigned_course_run': True,
'content_title': self.content_title,
'content_quantity': -123.45 * 100,
'content_quantity': -assignment_content_quantity_usd_cents,
'state': LearnerContentAssignmentStateChoices.ALLOCATED,
'transaction_uuid': None,
'uuid': str(foo_record.uuid),
Expand All @@ -623,7 +628,6 @@ def test_allocate_happy_path_e2e(
'reason': AssignmentAutomaticExpiredReason.NINETY_DAYS_PASSED
}
}]

self.assertEqual(response_payload['created'], expected_created_records)

canceled_record = allocation_records_by_email['[email protected]']
Expand All @@ -637,7 +641,7 @@ def test_allocate_happy_path_e2e(
'parent_content_key': self.parent_content_key,
'is_assigned_course_run': True,
'content_title': self.content_title,
'content_quantity': -123.45 * 100,
'content_quantity': -assignment_content_quantity_usd_cents,
'state': LearnerContentAssignmentStateChoices.ALLOCATED,
'transaction_uuid': None,
'uuid': str(canceled_record.uuid),
Expand All @@ -657,7 +661,7 @@ def test_allocate_happy_path_e2e(
'parent_content_key': self.parent_content_key,
'is_assigned_course_run': True,
'content_title': self.content_title,
'content_quantity': -123.45 * 100,
'content_quantity': -assignment_content_quantity_usd_cents,
'state': LearnerContentAssignmentStateChoices.ALLOCATED,
'transaction_uuid': None,
'uuid': str(expired_record.uuid),
Expand Down
16 changes: 13 additions & 3 deletions enterprise_access/apps/content_assignments/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,13 +527,23 @@ def _get_content_title(assignment_configuration, content_key):

def _get_parent_content_key(assignment_configuration, content_key):
"""
Helper to retrieve (from cache) the parent content key of a content_key'ed content_metadata.
Helper to retrieve (from cache) the parent content key of a content_key's content_metadata.
Note: content_key is either a course run key or a course key. Only course run keys have a
parent course key.
If content_key is for a course key, this will return the same key. Otherwise, the content_key
represents a course run, and this will return the run's parent course key.
"""
content_metadata = _get_content_summary(assignment_configuration, content_key)
return content_metadata.get('content_key')
metadata_content_key = content_metadata.get('content_key')

# Check if the assignment's content_key matches the returned content_key. If so, this is a course key
# which has no parent key.
if content_key == metadata_content_key:
return None

# Otherwise, this is a course run key, so return the parent course key
return metadata_content_key


def _get_preferred_course_run_key(assignment_configuration, content_key):
Expand Down Expand Up @@ -573,7 +583,7 @@ def _create_new_assignments(
content_title = _get_content_title(assignment_configuration, content_key)
parent_content_key = _get_parent_content_key(assignment_configuration, content_key)
preferred_course_run_key = _get_preferred_course_run_key(assignment_configuration, content_key)
is_assigned_course_run = content_key != parent_content_key
is_assigned_course_run = not parent_content_key

assignments_to_create = []
for learner_email in learner_emails:
Expand Down

0 comments on commit 467ff12

Please sign in to comment.