From 3c618ad04dd82b40267622ddbc2af43cf0abf1eb Mon Sep 17 00:00:00 2001
From: Feanil Patel <feanil@edx.org>
Date: Fri, 3 Apr 2020 11:27:48 -0400
Subject: [PATCH] Fixup CourseEnrollmentFactory

Historically, the CourseEnrollment model used to have a `course_id`
field.  A lot of tests still call the factory using that.  Instead of a
`course_id` field this model now has a `course` field which is a foreign
key to the CourseOverview model.

The factory still accepts the course_id but uses it to create the
relevant CourseOverview object where necessary.  This commit fixes two
issues with the factory.

1. If the course id is passed in as`course_id` instead of `course__id`
then, the generated CourseOverview does not have the correct course_id.

2. Even though the CourseEnrollment model no longer needs the
`course_id` parameter, we were still passing it through.  We now remove
it so that it is not passed through to the CourseEnrollment model
instantiation.
---
 common/djangoapps/student/tests/factories.py | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/common/djangoapps/student/tests/factories.py b/common/djangoapps/student/tests/factories.py
index 6c5f34a3fbd..f8649a33410 100644
--- a/common/djangoapps/student/tests/factories.py
+++ b/common/djangoapps/student/tests/factories.py
@@ -142,6 +142,11 @@ class CourseEnrollmentFactory(DjangoModelFactory):
             course_id = kwargs.get('course_id')
             course_overview = None
             if course_id is not None:
+                # 'course_id' is not needed by the model when course is passed.
+                # This arg used to be called course_id before we added the CourseOverview
+                # foreign key constraint to CourseEnrollment.
+                del kwargs['course_id']
+
                 if isinstance(course_id, six.string_types):
                     course_id = CourseKey.from_string(course_id)
                     course_kwargs.setdefault('id', course_id)
@@ -152,6 +157,9 @@ class CourseEnrollmentFactory(DjangoModelFactory):
                     pass
 
             if course_overview is None:
+                if 'id' not in course_kwargs and course_id:
+                    course_kwargs['id'] = course_id
+
                 course_overview = CourseOverviewFactory(**course_kwargs)
             kwargs['course'] = course_overview
 
-- 
GitLab