Skip to content

Commit

Permalink
test: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
varshamenon4 committed Mar 21, 2024
1 parent 207b5d1 commit 2862bf2
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 70 deletions.
40 changes: 14 additions & 26 deletions edx_exams/apps/core/management/commands/bulk_add_course_staff.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,25 @@ def add_course_staff_from_csv(self, csv_file, batch_size, batch_delay):
Add the given set of course staff provided in csv
"""
reader = list(unicodecsv.DictReader(csv_file))
for i in range(0, len(reader), batch_size):
# todo: filter by users that haven't been created
users_to_create = []
for row in reader:
if not User.objects.filter(username=row.get('username')).exists():
users_to_create.append(row)

# bulk create users
for i in range(0, len(users_to_create), batch_size):
User.objects.bulk_create(
User(
username=row.get('username'),
email=row.get('email'),
username=user.get('username'),
email=user.get('email'),
)
for row in reader[i:i + batch_size]
for user in users_to_create[i:i + batch_size]
)
time.sleep(batch_delay)

# bulk create course staff
for i in range(0, len(reader), batch_size):
CourseStaffRole.objects.bulk_create(
CourseStaffRole(
user=User.objects.get(
Expand All @@ -87,25 +97,3 @@ def add_course_staff_from_csv(self, csv_file, batch_size, batch_delay):
for row in reader[i:i + batch_size]
)
time.sleep(batch_delay)

# def add_course_staff(self, username, email, role, course_id):
# """
# Add course staff, in batches
# """
# user, _ = User.objects.get_or_create(
# username=username,
# email=email,
# )
#
# # todo: add bulk create / batching
# CourseStaffRole.objects.get_or_create(
# user=user,
# course_id=course_id,
# role=role,
# )






Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
""" Tests for bulk_add_course_staff management command """
from tempfile import NamedTemporaryFile
from testfixtures import LogCapture
from unittest.mock import patch

import ddt
from django.core.management import call_command
from django.test import TestCase

from edx_exams.apps.core.models import (
CourseStaffRole,
User,
)
from edx_exams.apps.core.test_utils.factories import CourseStaffRoleFactory, UserFactory

# todo: is this the correct logger name?
LOGGER_NAME = 'edx_exams.apps.core.management.commands.bulk_add_course_staff'

from edx_exams.apps.core.test_utils.factories import UserFactory

class TestBulkAddCourseStaff(TestCase):
""" Test bulk_add_course_staff management command """
Expand All @@ -35,7 +27,7 @@ def setUp(self):
self.user.save()

# create course
self.course_id = "course-v1:edx+test+f19"
self.course_id = 'course-v1:edx+test+f19'

def tearDown(self):
"""
Expand All @@ -45,7 +37,7 @@ def tearDown(self):

def _write_test_csv(self, csv, lines):
"""Write a test csv file with the lines provided"""
csv.write(b"username,email,role,course_id\n")
csv.write(b'username,email,role,course_id\n')
for line in lines:
csv.write(line.encode())
csv.seek(0)
Expand All @@ -55,42 +47,44 @@ def test_empty_csv(self):
lines = []
with NamedTemporaryFile() as csv:
csv = self._write_test_csv(csv, lines)
with LogCapture(LOGGER_NAME) as log:
call_command(self.command, f"--csv_path={csv.name}")
# log.check_present(
# (LOGGER_NAME, 'LOG', self.success_log_message)
# )
call_command(self.command, f'--csv_path={csv.name}')
# todo: figure out how to check success but not via logs
# todo: assert number of queries is correct

def test_add_course_staff_with_existing_user(self):
# todo: create course staff with existing user
lines = ["amy,[email protected],staff,course-v1:edx+test+f19\n"]
lines = ['amy,[email protected],staff,course-v1:edx+test+f19\n']
with NamedTemporaryFile() as csv:
csv = self._write_test_csv(csv, lines)
with LogCapture(LOGGER_NAME) as log:
call_command(self.command, f"--csv_path={csv.name}")
# log.check_present(
# (LOGGER_NAME, 'LOG', self.success_log_message)
# )
assert CourseStaffRole.objects.filter(user=self.user.id).exists()
# todo: figure out how to check success but not via logs
# todo: assert number of queries is correct
call_command(self.command, f'--csv_path={csv.name}')
assert CourseStaffRole.objects.filter(user=self.user.id).exists()

def test_add_course_staff_with_new_user(self):
# todo: create course staff with new user
lines = ["pam,[email protected],staff,course-v1:edx+test+f20\n"]
lines = ['pam,[email protected],staff,course-v1:edx+test+f20\n']
with NamedTemporaryFile() as csv:
csv = self._write_test_csv(csv, lines)
with LogCapture(LOGGER_NAME) as log:
call_command(self.command, f"--csv_path={csv.name}")
# log.check_present(
# (LOGGER_NAME, 'LOG', self.success_log_message)
# )
assert CourseStaffRole.objects.filter(course_id="course-v1:edx+test+f20").exists()
# todo: figure out how to check success but not via logs
# todo: assert number of queries is correct
call_command(self.command, f'--csv_path={csv.name}')
assert CourseStaffRole.objects.filter(course_id='course-v1:edx+test+f20').count() == 1

def test_add_course_staff_with_batch_size(self):
# todo: test with setting batch size
# set batch size to 1, add two entries
# assert that bulk create is called twice
return
lines = ['pam,[email protected],staff,course-v1:edx+test+f20\n',
'sam,[email protected],staff,course-v1:edx+test+f20\n']
with NamedTemporaryFile() as csv:
csv = self._write_test_csv(csv, lines)
# todo: figure out how to check success but not via logs
# todo: assert number of queries is correct
call_command(self.command, f'--csv_path={csv.name} --batch_size=1')
assert CourseStaffRole.objects.filter(course_id='course-v1:edx+test+f20').count() == 2

def test_add_course_staff_with_batch_delay(self):
# todo: test with setting batch delay
# todo: see bulk unenroll for example of batch unenroll
return
1 change: 1 addition & 0 deletions requirements/common_constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@




# A central location for most common version constraints
# (across edx repos) for pip-installation.
#
Expand Down
2 changes: 0 additions & 2 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,6 @@ stevedore==5.1.0
# code-annotations
# edx-django-utils
# edx-opaque-keys
testfixtures==8.1.0
# via -r requirements/validation.txt
text-unidecode==1.3
# via
# -r requirements/validation.txt
Expand Down
2 changes: 0 additions & 2 deletions requirements/doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,6 @@ stevedore==5.1.0
# doc8
# edx-django-utils
# edx-opaque-keys
testfixtures==8.1.0
# via -r requirements/test.txt
text-unidecode==1.3
# via
# -r requirements/test.txt
Expand Down
2 changes: 0 additions & 2 deletions requirements/quality.txt
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,6 @@ stevedore==5.1.0
# code-annotations
# edx-django-utils
# edx-opaque-keys
testfixtures==8.1.0
# via -r requirements/test.txt
text-unidecode==1.3
# via
# -r requirements/test.txt
Expand Down
1 change: 0 additions & 1 deletion requirements/test.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ mock
pytest-cov
pytest-django
responses
testfixtures
tox
2 changes: 0 additions & 2 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,6 @@ stevedore==5.1.0
# code-annotations
# edx-django-utils
# edx-opaque-keys
testfixtures==8.1.0
# via -r requirements/test.in
text-unidecode==1.3
# via
# -r requirements/base.txt
Expand Down
4 changes: 0 additions & 4 deletions requirements/validation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -780,10 +780,6 @@ stevedore==5.1.0
# code-annotations
# edx-django-utils
# edx-opaque-keys
testfixtures==8.1.0
# via
# -r requirements/quality.txt
# -r requirements/test.txt
text-unidecode==1.3
# via
# -r requirements/quality.txt
Expand Down

0 comments on commit 2862bf2

Please sign in to comment.