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.
This commit is contained in:
Greg Price
2013-04-25 16:58:33 -04:00
parent 22e147b2c9
commit 0f7378a171
2 changed files with 12 additions and 3 deletions

View File

@@ -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

View File

@@ -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'))
self.assertEquals(3, self.response.content.count('grade_None'))