'Test whether the realm entity of the component updates automatically'

def testSetRealmEntityWithRealmComponent(self):

    s3db = current.s3db
    auth = current.auth
    settings = current.deployment_settings
    realm_components = s3db.get_config('org_organisation', 'realm_components', 'none')
    s3db.configure('org_organisation', realm_components=['office'])
    assertEqual = self.assertEqual
    try:
        otable = s3db.org_organisation
        ftable = s3db.org_office
        settings.auth.realm_entity = self.realm_entity
        record = otable[self.org_id]
        record.update_record(realm_entity=None)
        record = ftable[self.office_id]
        record.update_record(realm_entity=None)
        record = otable[self.org_id]
        auth.set_realm_entity(otable, record, force_update=True)
        tname = 'org_organisation'
        assertEqual(self.owned_record, (tname, record.id))
        record = otable[self.org_id]
        assertEqual(record.realm_entity, 5)
        record = ftable[self.office_id]
        assertEqual(record.realm_entity, 5)
    finally:
        if (realm_components != 'none'):
            s3db.configure('org_organisation', realm_components=realm_components)
        else:
            s3db.clear_config('org_organisation', 'realm_components')
