def test_snapshot_user_table_without_all_email_addresses(self): # We'll pretend we're running the snapshot_public_data management command. But # to avoid JSON data being splatted all over stdout, we create a fake_stdout to # capture that data. fake_stdout = StringIO() # Now, set up the test: # Create two Person objects, with corresponding email addresses u1 = django.contrib.auth.models.User.objects.create(username="******", email="*****@*****.**") Person.create_dummy(user=u1) u2 = django.contrib.auth.models.User.objects.create(username="******", email="*****@*****.**") Person.create_dummy(user=u2, show_email=True) # snapshot the public version of the data into fake stdout command = mysite.customs.management.commands.snapshot_public_data.Command() command.handle(output=fake_stdout) # Now, delete the them all and see if they come back django.contrib.auth.models.User.objects.all().delete() Person.objects.all().delete() ## This code re-imports from the snapshot. # for more in serializers.deserialize(), read http://docs.djangoproject.com/en/dev/topics/serialization for obj in django.core.serializers.deserialize("json", fake_stdout.getvalue()): obj.save() ### Now the tests: # Django user objects really should have an email address # so, if we hid it, we make one up based on the user ID new_p1 = Person.objects.get(user__username="******") self.assertEquals(new_p1.user.email, "*****@*****.**" % new_p1.user.id) new_p2 = Person.objects.get(user__username="******") self.assertEquals(new_p2.user.email, "*****@*****.**")
def test_snapshot_user_table_without_all_email_addresses(self): # We'll pretend we're running the snapshot_public_data management command. But # to avoid JSON data being splatted all over stdout, we create a fake_stdout to # capture that data. fake_stdout = StringIO() # Now, set up the test: # Create two Person objects, with corresponding email addresses u1 = django.contrib.auth.models.User.objects.create( username='******', email='*****@*****.**') Person.create_dummy(user=u1) u2 = django.contrib.auth.models.User.objects.create( username='******', email='*****@*****.**') Person.create_dummy(user=u2, show_email=True) # snapshot the public version of the data into fake stdout command = mysite.customs.management.commands.snapshot_public_data.Command( ) command.handle(output=fake_stdout) # Now, delete the them all and see if they come back django.contrib.auth.models.User.objects.all().delete() Person.objects.all().delete() ## This code re-imports from the snapshot. # for more in serializers.deserialize(), read http://docs.djangoproject.com/en/dev/topics/serialization for obj in django.core.serializers.deserialize('json', fake_stdout.getvalue()): obj.save() ### Now the tests: # Django user objects really should have an email address # so, if we hid it, we make one up based on the user ID new_p1 = Person.objects.get(user__username='******') self.assertEquals( new_p1.user.email, '*****@*****.**' % new_p1.user.id) new_p2 = Person.objects.get(user__username='******') self.assertEquals(new_p2.user.email, '*****@*****.**')
def test_snapshot_person(self): fake_stdout = StringIO() # make fake Person who doesn't care if people know where he is zuckerberg = Person.create_dummy( first_name="mark", location_confirmed=True, location_display_name='Palo Alto', latitude=0, longitude=0) self.assertEquals( zuckerberg.get_public_location_or_default(), 'Palo Alto') # ...and make a fake Person who REALLY cares about his location being private munroe = Person.create_dummy(first_name="randall", location_confirmed=False, location_display_name='Cambridge', latitude=0, longitude=0) self.assertEquals(munroe.get_public_location_or_default(), 'Inaccessible Island') # Creating dummy tags, tags_persons and tagtypes # Dummy TagTypes tagtype_understands = TagType(name="understands") tagtype_understands.save() tagtype_can_mentor = TagType(name="can_mentor") tagtype_can_mentor.save() # Dummy Tags tag_facebook_development = Tag( text="Facebook development", tag_type=tagtype_understands) tag_facebook_development.save() tag_something_interesting = Tag( text="Something interesting", tag_type=tagtype_can_mentor) tag_something_interesting.save() # Dummy Links link_zuckerberg = Link_Person_Tag( person=zuckerberg, tag=tag_facebook_development) link_zuckerberg.save() link_munroe = Link_Person_Tag( person=munroe, tag=tag_something_interesting) link_munroe.save() command = mysite.customs.management.commands.snapshot_public_data.Command( ) command.handle(output=fake_stdout) # now, delete fake people zuckerberg.delete() munroe.delete() # ...and tags, tagtypes, and links too tag_facebook_development.delete() tag_something_interesting.delete() tagtype_understands.delete() tagtype_can_mentor.delete() link_zuckerberg.delete() link_munroe.delete() # and delete any User objects too django.contrib.auth.models.User.objects.all().delete() mysite.profile.models.Tag.objects.all().delete() mysite.profile.models.TagType.objects.all().delete() mysite.profile.models.Link_Person_Tag.objects.all().delete() # go go reincarnation gadget for obj in django.core.serializers.deserialize('json', fake_stdout.getvalue()): obj.save() # did we snapshot/save ANY Persons? self.assertTrue(Person.objects.all()) # did our fake Persons get saved? new_zuckerberg = mysite.profile.models.Person.objects.get( user__first_name="mark") new_munroe = mysite.profile.models.Person.objects.get( user__first_name="randall") # check that location_confirmed was saved accurately self.assertEquals(new_zuckerberg.location_confirmed, True) self.assertEquals(new_munroe.location_confirmed, False) # check that location_display_name is appropriate self.assertEquals(new_zuckerberg.location_display_name, 'Palo Alto') self.assertEquals(new_munroe.location_display_name, 'Inaccessible Island') # Check that Zuckerburg has a real lat/long self.assertNotEqual(mysite.profile.models.DEFAULT_LATITUDE, new_zuckerberg.latitude) self.assertNotEqual(mysite.profile.models.DEFAULT_LONGITUDE, new_zuckerberg.longitude) # Check that Randall has no lat/long self.assertEquals(mysite.profile.models.DEFAULT_LATITUDE, new_munroe.latitude) self.assertEquals(mysite.profile.models.DEFAULT_LONGITUDE, new_munroe.longitude) # check that we display both as appropriate self.assertEquals( new_zuckerberg.get_public_location_or_default(), 'Palo Alto') self.assertEquals( new_munroe.get_public_location_or_default(), 'Inaccessible Island') # get tags linked to our two dummy users... new_link_zuckerberg = mysite.profile.models.Link_Person_Tag.objects.get( id=new_zuckerberg.user_id) new_link_munroe = mysite.profile.models.Link_Person_Tag.objects.get( id=new_munroe.user_id) new_tag_facebook_development = mysite.profile.models.Tag.objects.get( link_person_tag__person=new_zuckerberg) new_tag_something_interesting = mysite.profile.models.Tag.objects.get( link_person_tag__person=new_munroe) # ...and tagtypes for the tags new_tagtype_understands = mysite.profile.models.TagType.objects.get( tag__tag_type=new_tag_facebook_development.tag_type) new_tagtype_can_mentor = mysite.profile.models.TagType.objects.get( tag__tag_type=new_tag_something_interesting.tag_type) # finally, check values self.assertEquals(new_link_zuckerberg.person, new_zuckerberg) self.assertEquals(new_link_munroe.person, new_munroe) self.assertEquals(new_tag_facebook_development.text, 'Facebook development') self.assertEquals(new_tag_something_interesting.text, 'Something interesting') self.assertEquals(new_tagtype_understands.name, 'understands') self.assertEquals(new_tagtype_can_mentor.name, 'can_mentor')