From 0f7378a171d746d2bf0416ce32a84fbc3d8ca746 Mon Sep 17 00:00:00 2001 From: Greg Price Date: Thu, 25 Apr 2013 16:58:33 -0400 Subject: [PATCH] Modify UserFactory to create a profile for the user This allows specification of profile parameters when creating a user. Because the profile contents are always accessed from the database, the user must be saved to the database before the profile is created. This means that the profile cannot be created if the user is merely being built (and not saved) rather than created. --- common/djangoapps/student/tests/factories.py | 12 +++++++++++- lms/djangoapps/instructor/tests/test_gradebook.py | 3 +-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/common/djangoapps/student/tests/factories.py b/common/djangoapps/student/tests/factories.py index 0d9621fc01..9560025441 100644 --- a/common/djangoapps/student/tests/factories.py +++ b/common/djangoapps/student/tests/factories.py @@ -2,7 +2,7 @@ from student.models import (User, UserProfile, Registration, CourseEnrollmentAllowed, CourseEnrollment) from django.contrib.auth.models import Group from datetime import datetime -from factory import DjangoModelFactory, Factory, SubFactory, PostGenerationMethodCall +from factory import DjangoModelFactory, Factory, SubFactory, PostGenerationMethodCall, post_generation from uuid import uuid4 @@ -45,6 +45,16 @@ class UserFactory(DjangoModelFactory): last_login = datetime(2012, 1, 1) date_joined = datetime(2011, 1, 1) + @post_generation + def profile(obj, create, extracted, **kwargs): + if create: + obj.save() + return UserProfileFactory.create(user=obj, **kwargs) + elif kwargs: + raise Exception("Cannot build a user profile without saving the user") + else: + return None + class AdminFactory(UserFactory): is_staff = True diff --git a/lms/djangoapps/instructor/tests/test_gradebook.py b/lms/djangoapps/instructor/tests/test_gradebook.py index 2de5c18bcd..4b1d22b594 100644 --- a/lms/djangoapps/instructor/tests/test_gradebook.py +++ b/lms/djangoapps/instructor/tests/test_gradebook.py @@ -49,7 +49,6 @@ class TestGradebook(ModuleStoreTestCase): ] for user in self.users: - UserProfileFactory.create(user=user) CourseEnrollmentFactory.create(user=user, course_id=self.course.id) for i in xrange(USER_COUNT-1): @@ -151,4 +150,4 @@ class TestLetterCutoffPolicy(TestGradebook): # User 0 has 0 on Homeworks [1] # User 0 has 0 on the class [1] # One use at the top of the page [1] - self.assertEquals(3, self.response.content.count('grade_None')) \ No newline at end of file + self.assertEquals(3, self.response.content.count('grade_None'))