'Test instantiation of filtered component'

@unittest.skipIf((not current.deployment_settings.has_module('org')), 'org    module    disabled')
def testAttachFilteredComponent(self):

    assertEqual = self.assertEqual
    s3db = current.s3db
    s3db.add_components('org_organisation', org_office={'name': 'test', 'joinby': 'organisation_id', 'filterby': {'office_type_id': 5}})
    resource = s3db.resource('org_organisation', components=['test'])
    component = resource.components['test']
    table = component.table
    assertEqual(component.tablename, 'org_office')
    assertEqual(component._alias, 'org_test_office')
    assertEqual(table._tablename, 'org_test_office')
    assertEqual(str(component.filter), str((table.office_type_id == 5)))
    s3db.add_components('org_organisation', org_office={'name': 'test', 'joinby': 'organisation_id', 'filterby': {'office_type_id': [5]}})
    resource = s3db.resource('org_organisation', components=['test'])
    component = resource.components['test']
    table = component.table
    assertEqual(str(component.filter), str((table.office_type_id == 5)))
    s3db.add_components('org_organisation', org_office={'name': 'test', 'joinby': 'organisation_id', 'filterby': {'office_type_id': (4, 5)}})
    resource = s3db.resource('org_organisation', components=['test'])
    component = resource.components['test']
    table = component.table
    assertEqual(str(component.filter), str(table.office_type_id.belongs(4, 5)))
    s3db.add_components('org_organisation', org_office={'name': 'test', 'joinby': 'organisation_id', 'filterby': {'office_type_id': []}})
    resource = s3db.resource('org_organisation', components=['test'])
    component = resource.components['test']
    assertEqual(component.filter, None)
    del current.model.components['org_organisation']['test']
