Skip to content

Commit

Permalink
chore: add tests for batch_dict
Browse files Browse the repository at this point in the history
  • Loading branch information
brobro10000 committed May 8, 2024
1 parent e9817fd commit 687397e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
1 change: 0 additions & 1 deletion enterprise/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""
import datetime
import hashlib
from itertools import islice
import json
import os
import re
Expand Down
18 changes: 18 additions & 0 deletions test_utils/fake_user_id_by_emails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
Utility functions for tests
"""
import math
import random
import string


def generate_emails_and_ids(num_emails):
"""
Generates random emails with random uuids used primarily to test length constraints
"""
emails_and_ids = {
''.join(random.choices(string.ascii_uppercase +
string.digits, k=8)) + '@gmail.com': math.floor(random.random() * 1000)
for _ in range(num_emails)
}
return emails_and_ids
1 change: 0 additions & 1 deletion tests/test_enterprise/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ def test_send_group_membership_invitation_notification(self, mock_braze_api_clie
},
)]
mock_braze_api_client().send_campaign_message.assert_has_calls(calls)
assert 1==2

@mock.patch('enterprise.tasks.EnterpriseCatalogApiClient', return_value=mock.MagicMock())
@mock.patch('enterprise.tasks.BrazeAPIClient', return_value=mock.MagicMock())
Expand Down
38 changes: 38 additions & 0 deletions tests/test_enterprise/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Tests for the `edx-enterprise` utils module.
"""
import math
import unittest
from datetime import timedelta
from unittest import mock
Expand All @@ -16,6 +17,7 @@
from enterprise.constants import MAX_ALLOWED_TEXT_LENGTH
from enterprise.models import EnterpriseCourseEnrollment, LicensedEnterpriseCourseEnrollment
from enterprise.utils import (
batch_dict,
enroll_subsidy_users_in_courses,
get_default_invite_key_expiration_date,
get_idiff_list,
Expand All @@ -27,6 +29,7 @@
truncate_string,
)
from test_utils import FAKE_UUIDS, TEST_PASSWORD, TEST_USERNAME, factories
from test_utils.fake_user_id_by_emails import generate_emails_and_ids

LMS_BASE_URL = 'https://lms.base.url'

Expand Down Expand Up @@ -650,3 +653,38 @@ def test_truncate_string(self):
(truncated_string, was_truncated) = truncate_string(test_string_2)
self.assertTrue(was_truncated)
self.assertEqual(len(truncated_string), MAX_ALLOWED_TEXT_LENGTH)

@ddt.unpack
@ddt.data(
(
50,
200
),
(
50,
56
),
(
50,
24
),
(
1,
200
)
)
def test_batch_dict_multiple_batches_no_remainder(self, items_per_batch, generated_emails_count):
"""
Test that `batch_dict` returns a set of batched dictionaries
"""
fake_user_ids_by_emails = generate_emails_and_ids(generated_emails_count)
for fake_user_id_by_email_chunk in batch_dict(fake_user_ids_by_emails, items_per_batch):
if len(fake_user_id_by_email_chunk) != items_per_batch:
assert len(fake_user_id_by_email_chunk) == generated_emails_count % items_per_batch
else:
assert len(fake_user_id_by_email_chunk) == items_per_batch
assert isinstance(fake_user_id_by_email_chunk, dict)
assert sum(
1 for _ in batch_dict(fake_user_ids_by_emails, items_per_batch)
) == math.ceil(generated_emails_count/items_per_batch)

0 comments on commit 687397e

Please sign in to comment.