Commit 0f7378a1 by Greg Price

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.
parent 22e147b2
...@@ -2,7 +2,7 @@ from student.models import (User, UserProfile, Registration, ...@@ -2,7 +2,7 @@ from student.models import (User, UserProfile, Registration,
CourseEnrollmentAllowed, CourseEnrollment) CourseEnrollmentAllowed, CourseEnrollment)
from django.contrib.auth.models import Group from django.contrib.auth.models import Group
from datetime import datetime from datetime import datetime
from factory import DjangoModelFactory, Factory, SubFactory, PostGenerationMethodCall from factory import DjangoModelFactory, Factory, SubFactory, PostGenerationMethodCall, post_generation
from uuid import uuid4 from uuid import uuid4
...@@ -45,6 +45,16 @@ class UserFactory(DjangoModelFactory): ...@@ -45,6 +45,16 @@ class UserFactory(DjangoModelFactory):
last_login = datetime(2012, 1, 1) last_login = datetime(2012, 1, 1)
date_joined = datetime(2011, 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): class AdminFactory(UserFactory):
is_staff = True is_staff = True
......
...@@ -49,7 +49,6 @@ class TestGradebook(ModuleStoreTestCase): ...@@ -49,7 +49,6 @@ class TestGradebook(ModuleStoreTestCase):
] ]
for user in self.users: for user in self.users:
UserProfileFactory.create(user=user)
CourseEnrollmentFactory.create(user=user, course_id=self.course.id) CourseEnrollmentFactory.create(user=user, course_id=self.course.id)
for i in xrange(USER_COUNT-1): for i in xrange(USER_COUNT-1):
...@@ -151,4 +150,4 @@ class TestLetterCutoffPolicy(TestGradebook): ...@@ -151,4 +150,4 @@ class TestLetterCutoffPolicy(TestGradebook):
# User 0 has 0 on Homeworks [1] # User 0 has 0 on Homeworks [1]
# User 0 has 0 on the class [1] # User 0 has 0 on the class [1]
# One use at the top of the page [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'))
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment