'Test update of a pre-linked person record upon user account update'

def testUpdateLinkedPerson(self):

    auth = current.auth
    s3db = current.s3db
    assertEqual = self.assertEqual
    assertNotEqual = self.assertNotEqual
    assertTrue = self.assertTrue
    assertFalse = self.assertFalse
    utable = auth.settings.table_user
    user = Storage(first_name='TestLTPR', last_name='User', email='testltpr@example.com', password='XYZ')
    user_id = utable.insert(**user)
    assertTrue((user_id is not None))
    user['id'] = user_id
    person_id = auth.s3_link_to_person(user, self.org_id)
    assertNotEqual(person_id, None)
    assertFalse(isinstance(person_id, list))
    assertEqual(person_id, self.person_id)
    update = Storage(first_name='TestLTPR2', last_name='User', email='testltpr2@example.com')
    current.db((utable.id == user_id)).update(**update)
    update['id'] = user_id
    update_id = auth.s3_link_to_person(user, self.org_id)
    assertEqual(update_id, person_id)
    ptable = s3db.pr_person
    person = ptable[update_id]
    assertEqual(person.first_name, update['first_name'])
    assertEqual(person.last_name, update['last_name'])
    ctable = s3db.pr_contact
    query = ((ctable.pe_id == self.pe_id) & (ctable.contact_method == 'EMAIL'))
    contacts = current.db(query).select()
    assertEqual(len(contacts), 2)
    emails = [contact.value for contact in contacts]
    assertTrue((user.email in emails))
    assertTrue((update.email in emails))
