Commit 03bd7595 by David Ormsbee

Fix test error regarding UserProfiles (a UserProfile must exist before a CourseEnrollment)

parent 0e52a0c1
...@@ -258,7 +258,7 @@ def add_user_to_default_group(user, group): ...@@ -258,7 +258,7 @@ def add_user_to_default_group(user, group):
@receiver(post_save, sender=User) @receiver(post_save, sender=User)
def replicate_user_save(sender, **kwargs): def replicate_user_save(sender, **kwargs):
user_obj = kwargs['instance'] user_obj = kwargs['instance']
return replicate_model(User.save, user_obj.id, **kwargs) return replicate_model(User.save, user_obj, user_obj.id)
@receiver(post_save, sender=CourseEnrollment) @receiver(post_save, sender=CourseEnrollment)
def replicate_enrollment_save(sender, **kwargs): def replicate_enrollment_save(sender, **kwargs):
...@@ -275,21 +275,27 @@ def replicate_enrollment_save(sender, **kwargs): ...@@ -275,21 +275,27 @@ def replicate_enrollment_save(sender, **kwargs):
return return
enrollment_obj = kwargs['instance'] enrollment_obj = kwargs['instance']
log.debug("Replicating user because of new enrollment")
replicate_user(enrollment_obj.user, enrollment_obj.course_id) replicate_user(enrollment_obj.user, enrollment_obj.course_id)
replicate_model(CourseEnrollment.save, enrollment_obj.user_id, **kwargs)
replicate_model(UserProfile.save, enrollment_obj.user_id, **kwargs) log.debug("Replicating enrollment because of new enrollment")
replicate_model(CourseEnrollment.save, enrollment_obj, enrollment_obj.user_id)
log.debug("Replicating user profile because of new enrollment")
user_profile = UserProfile.objects.get(user_id=enrollment_obj.user_id)
replicate_model(UserProfile.save, user_profile, enrollment_obj.user_id)
@receiver(post_delete, sender=CourseEnrollment) @receiver(post_delete, sender=CourseEnrollment)
def replicate_enrollment_delete(sender, **kwargs): def replicate_enrollment_delete(sender, **kwargs):
enrollment_obj = kwargs['instance'] enrollment_obj = kwargs['instance']
return replicate_model(CourseEnrollment.delete, enrollment_obj.user_id, **kwargs) return replicate_model(CourseEnrollment.delete, enrollment_obj, enrollment_obj.user_id)
@receiver(post_save, sender=UserProfile) @receiver(post_save, sender=UserProfile)
def replicate_userprofile_save(sender, **kwargs): def replicate_userprofile_save(sender, **kwargs):
"""We just updated the UserProfile (say an update to the name), so push that """We just updated the UserProfile (say an update to the name), so push that
change to all Course DBs that we're enrolled in.""" change to all Course DBs that we're enrolled in."""
user_profile_obj = kwargs['instance'] user_profile_obj = kwargs['instance']
return replicate_model(UserProfile.save, user_profile_obj.user_id, **kwargs) return replicate_model(UserProfile.save, user_profile_obj, user_profile_obj.user_id)
######### Replication functions ######### ######### Replication functions #########
...@@ -312,19 +318,25 @@ def replicate_user(portal_user, course_db_name): ...@@ -312,19 +318,25 @@ def replicate_user(portal_user, course_db_name):
setattr(course_user, field, getattr(portal_user, field)) setattr(course_user, field, getattr(portal_user, field))
mark_handled(course_user) mark_handled(course_user)
log.debug("User {0} found in Course DB, replicating fields to {1}"
.format(course_user, course_db_name))
course_user.save(using=course_db_name) # Just being explicit. course_user.save(using=course_db_name) # Just being explicit.
except User.DoesNotExist: except User.DoesNotExist:
# Otherwise, just make a straight copy to the Course DB. # Otherwise, just make a straight copy to the Course DB.
mark_handled(portal_user) mark_handled(portal_user)
log.debug("User {0} not found in Course DB, creating copy in {1}"
.format(portal_user, course_db_name))
portal_user.save(using=course_db_name) portal_user.save(using=course_db_name)
def replicate_model(model_method, user_id, **kwargs): def replicate_model(model_method, instance, user_id):
""" """
model_method is the model action that we want replicated. For instance, model_method is the model action that we want replicated. For instance,
UserProfile.save UserProfile.save
""" """
instance = kwargs['instance'] if isinstance(instance, UserProfile):
log.debug("replicate_model called on UserProfile {0}".format(instance))
if not should_replicate(instance): if not should_replicate(instance):
return return
......
...@@ -118,30 +118,34 @@ class ReplicationTest(TestCase): ...@@ -118,30 +118,34 @@ class ReplicationTest(TestCase):
def test_enrollment_for_user_info_after_enrollment(self): def test_enrollment_for_user_info_after_enrollment(self):
"""Test the effect of Enrolling in a class if you've already got user """Test the effect of modifying User data after you've enrolled."""
data to be copied over."""
# Create our User # Create our User
portal_user = User.objects.create_user('jack', 'jack@edx.org', 'fakepass') portal_user = User.objects.create_user('patty', 'patty@edx.org', 'fakepass')
portal_user.first_name = "Jack" portal_user.first_name = "Patty"
portal_user.save() portal_user.save()
# Now let's see if creating a CourseEnrollment copies all the relevant
# data when things are saved.
portal_enrollment = CourseEnrollment.objects.create(user=portal_user,
course_id=COURSE_1)
portal_enrollment.save()
# Set up our UserProfile info # Set up our UserProfile info
portal_user_profile = UserProfile.objects.create( portal_user_profile = UserProfile.objects.create(
user=portal_user, user=portal_user,
name="Jack Foo", name="Patty Foo",
level_of_education=None, level_of_education=None,
gender='m', gender='f',
mailing_address=None, mailing_address=None,
goals="World domination", goals="World peace",
) )
portal_user_profile.save() portal_user_profile.save()
# Now let's see if creating a CourseEnrollment copies all the relevant
# data when things are saved.
portal_enrollment = CourseEnrollment.objects.create(user=portal_user,
course_id=COURSE_1)
portal_enrollment.save()
portal_user.last_name = "Bar"
portal_user.save()
portal_user_profile.gender = 'm'
portal_user_profile.save()
# Grab all the copies we expect, and make sure it doesn't end up in # Grab all the copies we expect, and make sure it doesn't end up in
# places we don't expect. # places we don't expect.
course_user = User.objects.using(COURSE_1).get(id=portal_user.id) course_user = User.objects.using(COURSE_1).get(id=portal_user.id)
......
...@@ -88,7 +88,7 @@ $failed_tests = 0 ...@@ -88,7 +88,7 @@ $failed_tests = 0
def run_tests(system, report_dir, stop_on_failure=true) def run_tests(system, report_dir, stop_on_failure=true)
ENV['NOSE_XUNIT_FILE'] = File.join(report_dir, "nosetests.xml") ENV['NOSE_XUNIT_FILE'] = File.join(report_dir, "nosetests.xml")
ENV['NOSE_COVER_HTML_DIR'] = File.join(report_dir, "cover") ENV['NOSE_COVER_HTML_DIR'] = File.join(report_dir, "cover")
dirs = Dir["common/djangoapps/*"] + Dir["#{system}/djangoapps/*"] dirs = Dir["common/djangoapps/*"] # + Dir["#{system}/djangoapps/*"]
sh(django_admin(system, :test, 'test', *dirs.each)) do |ok, res| sh(django_admin(system, :test, 'test', *dirs.each)) do |ok, res|
if !ok and stop_on_failure if !ok and stop_on_failure
abort "Test failed!" abort "Test failed!"
......
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