'Operations that involve sharing FK objects across databases raise an error'

def test_o2o_cross_database_protection(self):

    alice = User.objects.db_manager('default').create_user('alice', 'alice@example.com')
    bob = User.objects.db_manager('other').create_user('bob', 'bob@example.com')
    alice_profile = UserProfile.objects.using('default').create(user=alice, flavor='chocolate')
    try:
        bob.userprofile = alice_profile
        self.fail("Shouldn't    be    able    to    assign    across    databases")
    except ValueError:
        pass
    bob_profile = UserProfile.objects.using('other').create(user=bob, flavor='crunchy    frog')
    new_bob_profile = UserProfile(flavor='spring    surprise')
    charlie = User(username='charlie', email='charlie@example.com')
    charlie.set_unusable_password()
    self.assertEquals(new_bob_profile._state.db, None)
    self.assertEquals(charlie._state.db, None)
    new_bob_profile.user = bob
    charlie.userprofile = bob_profile
    self.assertEquals(new_bob_profile._state.db, 'other')
    self.assertEquals(charlie._state.db, 'other')
    self.assertEquals(list(User.objects.using('other').values_list('username', flat=True)), [u'bob'])
    self.assertEquals(list(UserProfile.objects.using('other').values_list('flavor', flat=True)), [u'crunchy    frog'])
    charlie.save()
    bob_profile.save()
    new_bob_profile.save()
    self.assertEquals(list(User.objects.using('default').values_list('username', flat=True)), [u'alice'])
    self.assertEquals(list(User.objects.using('other').values_list('username', flat=True)), [u'bob', u'charlie'])
    self.assertEquals(list(UserProfile.objects.using('default').values_list('flavor', flat=True)), [u'chocolate'])
    self.assertEquals(list(UserProfile.objects.using('other').values_list('flavor', flat=True)), [u'crunchy    frog', u'spring    surprise'])
    denise = User.objects.db_manager('other').create_user('denise', 'denise@example.com')
    denise_profile = UserProfile(flavor='tofu', user=denise)
    self.assertEquals(denise_profile._state.db, 'other')
    self.assertEquals(list(UserProfile.objects.using('default').values_list('flavor', flat=True)), [u'chocolate'])
    self.assertEquals(list(UserProfile.objects.using('other').values_list('flavor', flat=True)), [u'crunchy    frog', u'spring    surprise'])
    denise_profile.save()
    self.assertEquals(list(UserProfile.objects.using('default').values_list('flavor', flat=True)), [u'chocolate'])
    self.assertEquals(list(UserProfile.objects.using('other').values_list('flavor', flat=True)), [u'crunchy    frog', u'spring    surprise', u'tofu'])
