'Test global context filter'

def testContextFilter(self):

    s3db = current.s3db
    assertEqual = self.assertEqual
    assertTrue = self.assertTrue
    assertFalse = self.assertFalse
    s3db.configure('org_office', context={'organisation': 'organisation_id'})
    s3db.configure('pr_person', context={'organisation': 'human_resource.organisation_id'})
    s3db.context = None
    resource = s3db.resource('org_office', uid=['CONTEXT1OFFICE1', 'CONTEXT1OFFICE2', 'CONTEXT2OFFICE1'], context=True)
    data = resource.select(['name'], limit=None)
    items = data['rows']
    assertEqual(len(items), 3)
    names = [item.values()[0] for item in items]
    assertTrue(('Context1Office1' in names))
    assertTrue(('Context1Office2' in names))
    assertTrue(('Context2Office1' in names))
    resource = s3db.resource('pr_person', uid=['CONTEXT1PERSON', 'CONTEXT2PERSON', 'CONTEXT12PERSON'], context=True)
    data = resource.select(['first_name'], limit=None)
    items = data['rows']
    assertEqual(len(items), 3)
    names = [item.values()[0] for item in items]
    assertTrue(('Context1' in names))
    assertTrue(('Context2' in names))
    assertTrue(('Context12' in names))
    s3db.context = FS('(organisation)$uuid').belongs(['CONTEXTORG1'])
    resource = s3db.resource('org_office', uid=['CONTEXT1OFFICE1', 'CONTEXT1OFFICE2', 'CONTEXT2OFFICE1'], context=True)
    data = resource.select(['name'], limit=None)
    items = data['rows']
    assertEqual(len(items), 2)
    names = [item.values()[0] for item in items]
    assertTrue(('Context1Office1' in names))
    assertTrue(('Context1Office2' in names))
    assertFalse(('Context2Office1' in names))
    resource = s3db.resource('pr_person', uid=['CONTEXT1PERSON', 'CONTEXT2PERSON', 'CONTEXT12PERSON'], context=True)
    data = resource.select(['first_name'], limit=None)
    items = data['rows']
    assertEqual(len(items), 2)
    names = [item.values()[0] for item in items]
    assertTrue(('Context1' in names))
    assertFalse(('Context2' in names))
    assertTrue(('Context12' in names))
    s3db.context = FS('(organisation)$uuid').belongs(['CONTEXTORG2'])
    resource = s3db.resource('org_office', uid=['CONTEXT1OFFICE1', 'CONTEXT1OFFICE2', 'CONTEXT2OFFICE1'], context=True)
    data = resource.select(['name'], limit=None)
    items = data['rows']
    assertEqual(len(items), 1)
    names = [item.values()[0] for item in items]
    assertFalse(('Context1Office1' in names))
    assertFalse(('Context1Office2' in names))
    assertTrue(('Context2Office1' in names))
    resource = s3db.resource('pr_person', uid=['CONTEXT1PERSON', 'CONTEXT2PERSON', 'CONTEXT12PERSON'], context=True)
    data = resource.select(['first_name'], limit=None)
    items = data['rows']
    assertEqual(len(items), 2)
    names = [item.values()[0] for item in items]
    assertFalse(('Context1' in names))
    assertTrue(('Context2' in names))
    assertTrue(('Context12' in names))
