Skip to content

Commit

Permalink
feat: Management command for EnterpriseLearnerEnrollment dummy data (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
zamanafzal committed Mar 3, 2022
1 parent 0b720ed commit 46b6319
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Unreleased
----------

=========================
[4.1.1] - 2022-03-01
---------------------
* Created a new management command for adding EnterpriseLearnerEnrollment dummy data for learner progress report v1.

[4.1.0] - 2022-03-01
---------------------
* Created a new management command for adding dummy data for learner progress report v1.
Expand Down
2 changes: 1 addition & 1 deletion enterprise_data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
Enterprise data api application. This Django app exposes API endpoints used by enterprises.
"""

__version__ = "4.1.0"
__version__ = "4.1.1"

default_app_config = "enterprise_data.apps.EnterpriseDataAppConfig" # pylint: disable=invalid-name
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
Management command for creating enterprise learner enrollments
"""


import sys

from django.core.management.base import BaseCommand, CommandError

import enterprise_data.tests.test_utils


class Command(BaseCommand):
""" Management command for creating dummy `EnterpriseLearnerEnrollment` instances for (LPR) V1."""

help = 'Creates an EnterpriseLearnerEnrollment with randomized attributes'

def add_arguments(self, parser):
parser.add_argument(
'enterprise_customer_uuid',
type=str,
help='UUID for an enterprise'
)
parser.add_argument(
'enterprise_user_id',
type=int,
default=None,
help='enterprise_user_id for an enterprise_user'
)
# this is optional and will be marked as False if not available in argumnets.
parser.add_argument('--consent_granted', action='store_true')

def handle(self, *args, **options):
enterprise_customer_uuid = options['enterprise_customer_uuid']
enterprise_user_id = options.get('enterprise_user_id')
is_consent_granted = options.get('consent_granted')

try:
enterprise_data.tests.test_utils.EnterpriseLearnerEnrollmentFactory(
enterprise_customer_uuid=enterprise_customer_uuid,
enterprise_user_id=enterprise_user_id,
is_consent_granted=is_consent_granted,
)
info = (
'\nCreated EnterpriseLearnerEnrollment with enterprise_customer_uuid '
'{} for EnterpriseUser with enterprise_user_id of {} and consent {}\n\n'.format(
enterprise_customer_uuid,
enterprise_user_id,
is_consent_granted,
)
)
sys.stdout.write(info)
except Exception as exc:
info = (
'Error trying to create EnterpriseUser with uuid '
'{}: {}'.format(enterprise_customer_uuid, exc)
)
raise CommandError(info) from exc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def handle(self, *args, **options):
# TODO: Need to update this to the correct comment for LPR V1 once that is added.
info = (
'You can create some enrollments for this user by running the following '
'command:\n\n ./manage.py create_enterprise_enrollment {} {}\n\n'.format(
'command:\n\n ./manage.py create_enterprise_learner_enrollment_lpr_v1 {} {}\n\n'.format(
ent_user.enterprise_customer_uuid,
ent_user.enterprise_user_id,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"""
Tests for create_enterprise_learner_enrollment_lpr_v1 management command
"""
from unittest import TestCase, mock

from pytest import mark

from django.core.management import call_command

from enterprise_data.models import EnterpriseLearner, EnterpriseLearnerEnrollment
from enterprise_data.tests.test_utils import EnterpriseLearnerFactory


@mark.django_db
class TestCreateEnterpriseLearnerEnrollmentCommand(TestCase):
""" Tests for create_enterprise_learner_enrollment_lpr_v1 management command"""

def setUp(self):
super().setUp()
self.uuid = 'a'*32
self.enterprise_user = EnterpriseLearnerFactory(enterprise_customer_uuid=self.uuid)

def test_create_enterprise_learner_enrollment_lpr_v1_with_dsc_disabled(self):
"""
Management command should successfully be able to create EnterpriseLearnerEnrollment with DSC Disabled
"""
assert EnterpriseLearner.objects.count() == 1
assert EnterpriseLearnerEnrollment.objects.count() == 0

args = [self.uuid, self.enterprise_user.enterprise_user_id]
call_command('create_enterprise_learner_enrollment_lpr_v1', *args)

assert EnterpriseLearnerEnrollment.objects.count() == 1
enterprise_learner_enrollment = EnterpriseLearnerEnrollment.objects.filter(
enterprise_customer_uuid=args[0]
)
assert enterprise_learner_enrollment.count() == 1
assert enterprise_learner_enrollment[0].progress_status is not None
assert enterprise_learner_enrollment[0].letter_grade is not None
assert enterprise_learner_enrollment[0].enterprise_user_id is not None
assert enterprise_learner_enrollment[0].user_username is not None
assert enterprise_learner_enrollment[0].user_email is not None
assert enterprise_learner_enrollment[0].enterprise_user is not None

def test_create_enterprise_learner_enrollment_lpr_v1_with_dsc_enabled(self):
"""
Management command should successfully be able to create EnterpriseLearnerEnrollment with DSC enabled
"""
assert EnterpriseLearner.objects.count() == 1
assert EnterpriseLearnerEnrollment.objects.count() == 0

args = [self.uuid, self.enterprise_user.enterprise_user_id, '--consent_granted']
call_command('create_enterprise_learner_enrollment_lpr_v1', *args)

assert EnterpriseLearnerEnrollment.objects.count() == 1
enterprise_learner_enrollment = EnterpriseLearnerEnrollment.objects.filter(
enterprise_customer_uuid=args[0]
)
assert enterprise_learner_enrollment.count() == 1
assert enterprise_learner_enrollment[0].letter_grade is None
assert enterprise_learner_enrollment[0].last_activity_date is None
assert enterprise_learner_enrollment[0].progress_status is None
assert enterprise_learner_enrollment[0].enterprise_user_id is None
assert enterprise_learner_enrollment[0].user_username is None
assert enterprise_learner_enrollment[0].enterprise_user is None
assert enterprise_learner_enrollment[0].user_email is None
assert EnterpriseLearner.objects.count() == 1

def test_create_enterprise_learner_enrollment_lpr_v1_error(self):
"""
Management command will not create enrollment if an error is thrown
"""
assert EnterpriseLearnerEnrollment.objects.count() == 0

args = [self.uuid, self.enterprise_user.enterprise_user_id]
with mock.patch('enterprise_data.tests.test_utils.EnterpriseLearnerEnrollmentFactory') as mock_factory:
mock_factory.side_effect = [Exception]
with self.assertRaises(Exception):
call_command('create_enterprise_learner_enrollment_lpr_v1', *args)

assert EnterpriseLearnerEnrollment.objects.count() == 0
53 changes: 49 additions & 4 deletions enterprise_data/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def course_end(self):
def passed_timestamp(self):
""" Create a passed timestamp if a course has been passed """
if self.has_passed:
return FAKER.date_time_between( # pylint: disable=no-member
return FAKER.date_time_between( # pylint: disable=no-member
start_date=self.course_start,
end_date=self.course_end,
tzinfo=pytz.utc)
Expand Down Expand Up @@ -149,22 +149,32 @@ class Meta:
enrollment_id = factory.lazy_attribute(
lambda x: FAKER.random_int(min=1, max=999999) # pylint: disable=no-member
)
enterprise_enrollment_id = factory.lazy_attribute(
lambda x: FAKER.random_int(min=1, max=999999) # pylint: disable=no-member,invalid-name
)
enterprise_customer_uuid = str(FAKER.uuid4()) # pylint: disable=no-member
courserun_key = factory.lazy_attribute(lambda x: FAKER.slug()) # pylint: disable=no-member
enrollment_date = factory.lazy_attribute(lambda x: '2018-01-01')
user_current_enrollment_mode = factory.lazy_attribute(lambda x: 'verified')
has_passed = factory.lazy_attribute(lambda x: FAKER.boolean()) # pylint: disable=no-member
is_consent_granted = factory.lazy_attribute(lambda x: FAKER.boolean()) # pylint: disable=no-member
course_title = factory.lazy_attribute(lambda x: ' '.join(FAKER.words(nb=2)).title()) # pylint: disable=no-member
course_start_date = factory.lazy_attribute(lambda x: FAKER.date_time_between( # pylint: disable=no-member
start_date='-2M',
end_date='+2M',
tzinfo=pytz.utc)
)
user_email = factory.lazy_attribute(lambda x: FAKER.email()) # pylint: disable=no-member
current_grade = factory.lazy_attribute(
lambda x: FAKER.pyfloat(right_digits=2, min_value=0, max_value=1) # pylint: disable=no-member
)
letter_grade = factory.lazy_attribute(lambda x: ' '.join(FAKER.words(nb=2)).title())
progress_status = factory.lazy_attribute(lambda x: ' '.join(FAKER.words(nb=2)).title())
enterprise_user_id = factory.lazy_attribute(
lambda x: FAKER.random_int(min=1, max=999999) # pylint: disable=no-member,invalid-name
)
user_email = factory.lazy_attribute(lambda x: FAKER.email()) # pylint: disable=no-member
user_username = factory.Sequence('robot{}'.format) # pylint: disable=no-member
user_account_creation_date = factory.lazy_attribute(lambda x: '2018-01-01') # pylint: disable=no-member
user_country_code = factory.lazy_attribute(lambda x: FAKER.country_code())

@factory.lazy_attribute
def course_end_date(self):
Expand All @@ -177,14 +187,49 @@ def course_end_date(self):
@factory.lazy_attribute
def passed_date(self):
""" Create a passed timestamp if a course has been passed """
if self.has_passed:
if self.has_passed and self.is_consent_granted:
return FAKER.date_time_between( # pylint: disable=no-member
start_date=self.course_start_date,
end_date=self.course_end_date,
tzinfo=pytz.utc
)
return None

@factory.lazy_attribute
def last_activity_date(self):
""" Create a date in between course start and end timestamp"""
if self.is_consent_granted:
return FAKER.date_time_between( # pylint: disable=no-member
start_date=self.course_start_date,
end_date=self.course_end_date,
tzinfo=pytz.utc
)
return None

@factory.post_generation
def set_fields_according_to_consent(
obj,
create,
extracted,
**kwargs
): # pylint: disable=unused-argument, missing-function-docstring
dsc_dependent_fields = [
'last_activity_date',
'progress_status',
'passed_date',
'current_grade',
'letter_grade',
'enterprise_user_id',
'user_email',
'user_account_creation_date',
'user_country_code',
'user_username',
]
if create and obj.is_consent_granted:
for field in dsc_dependent_fields:
setattr(obj, field, None)
obj.save()


def get_dummy_enterprise_api_data(**kwargs):
"""
Expand Down

0 comments on commit 46b6319

Please sign in to comment.