<s> import unittest <EOL> import datetime <EOL> from gluon import * <EOL> from gluon . storage import Storage <EOL> from lxml import etree <EOL> class PRTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> auth = current . auth <EOL> s3db = current . s3db <EOL> auth . override = True <EOL> otable = s3db . org_organisation <EOL> org1 = Storage ( name = "<STR_LIT>" , <EOL> acronym = "<STR_LIT>" , <EOL> country = "<STR_LIT>" , <EOL> website = "<STR_LIT>" ) <EOL> org1_id = otable . insert ( ** org1 ) <EOL> org1 . update ( id = org1_id ) <EOL> s3db . update_super ( otable , org1 ) <EOL> org2 = Storage ( name = "<STR_LIT>" , <EOL> acronym = "<STR_LIT>" , <EOL> country = "<STR_LIT>" , <EOL> website = "<STR_LIT>" ) <EOL> org2_id = otable . insert ( ** org2 ) <EOL> org2 . update ( id = org2_id ) <EOL> s3db . update_super ( otable , org2 ) <EOL> self . org1 = s3db . pr_get_pe_id ( "<STR_LIT>" , org1_id ) <EOL> self . org2 = s3db . pr_get_pe_id ( "<STR_LIT>" , org2_id ) <EOL> def testGetRealmUsers ( self ) : <EOL> auth = current . auth <EOL> s3db = current . s3db <EOL> auth . s3_impersonate ( "<STR_LIT>" ) <EOL> admin_id = auth . user . id <EOL> admin_pe_id = auth . s3_user_pe_id ( admin_id ) <EOL> auth . s3_impersonate ( "<STR_LIT>" ) <EOL> user_id = auth . user . id <EOL> user_pe_id = auth . s3_user_pe_id ( user_id ) <EOL> auth . s3_impersonate ( None ) <EOL> org1 = self . org1 <EOL> org2 = self . org2 <EOL> users = s3db . pr_realm_users ( org1 ) <EOL> self . assertEqual ( users , Storage ( ) ) <EOL> users = s3db . pr_realm_users ( org2 ) <EOL> self . assertEqual ( users , Storage ( ) ) <EOL> s3db . pr_add_affiliation ( org1 , admin_pe_id , role = "<STR_LIT>" , role_type = <NUM_LIT:9> ) <EOL> s3db . pr_add_affiliation ( org2 , user_pe_id , role = "<STR_LIT>" ) <EOL> users = s3db . pr_realm_users ( org1 ) <EOL> self . assertFalse ( user_id in users ) <EOL> self . assertFalse ( admin_id in users ) <EOL> users = s3db . pr_realm_users ( org2 ) <EOL> self . assertTrue ( user_id in users ) <EOL> self . assertFalse ( admin_id in users ) <EOL> users = s3db . pr_realm_users ( [ org1 , org2 ] ) <EOL> self . assertTrue ( user_id in users ) <EOL> self . assertFalse ( admin_id in users ) <EOL> users = s3db . pr_realm_users ( org1 , roles = "<STR_LIT>" ) <EOL> self . assertFalse ( user_id in users ) <EOL> self . assertTrue ( admin_id in users ) <EOL> users = s3db . pr_realm_users ( org2 , roles = "<STR_LIT>" ) <EOL> self . assertFalse ( user_id in users ) <EOL> self . assertFalse ( admin_id in users ) <EOL> users = s3db . pr_realm_users ( [ org1 , org2 ] , roles = "<STR_LIT>" ) <EOL> self . assertFalse ( user_id in users ) <EOL> self . assertTrue ( admin_id in users ) <EOL> users = s3db . pr_realm_users ( org1 , roles = "<STR_LIT>" ) <EOL> self . assertFalse ( user_id in users ) <EOL> self . assertFalse ( admin_id in users ) <EOL> users = s3db . pr_realm_users ( org2 , roles = "<STR_LIT>" ) <EOL> self . assertTrue ( user_id in users ) <EOL> self . assertFalse ( admin_id in users ) <EOL> users = s3db . pr_realm_users ( [ org1 , org2 ] , roles = "<STR_LIT>" ) <EOL> self . assertTrue ( user_id in users ) <EOL> self . assertFalse ( admin_id in users ) <EOL> users = s3db . pr_realm_users ( [ org1 , org2 ] , roles = [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> self . assertTrue ( user_id in users ) <EOL> self . assertTrue ( admin_id in users ) <EOL> users = s3db . pr_realm_users ( [ org1 , org2 ] , role_types = <NUM_LIT:1> ) <EOL> self . assertTrue ( user_id in users ) <EOL> self . assertFalse ( admin_id in users ) <EOL> users = s3db . pr_realm_users ( [ org1 , org2 ] , role_types = <NUM_LIT:9> ) <EOL> self . assertFalse ( user_id in users ) <EOL> self . assertTrue ( admin_id in users ) <EOL> users = s3db . pr_realm_users ( [ org1 , org2 ] , role_types = None ) <EOL> self . assertTrue ( user_id in users ) <EOL> self . assertTrue ( admin_id in users ) <EOL> s3db . pr_remove_affiliation ( org2 , user_pe_id , role = "<STR_LIT>" ) <EOL> users = s3db . pr_realm_users ( [ org1 , org2 ] , role_types = None ) <EOL> self . assertFalse ( user_id in users ) <EOL> self . assertTrue ( admin_id in users ) <EOL> table = auth . settings . table_user <EOL> query = ( table . deleted != True ) <EOL> rows = current . db ( query ) . select ( table . id ) <EOL> all_users = [ row . id for row in rows ] <EOL> users = s3db . pr_realm_users ( None ) <EOL> self . assertTrue ( all ( [ u in users for u in all_users ] ) ) <EOL> def tearDown ( self ) : <EOL> current . db . rollback ( ) <EOL> current . auth . override = False <EOL> class PersonDeduplicateTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> s3db = current . s3db <EOL> ptable = s3db . pr_person <EOL> ctable = s3db . pr_contact <EOL> created_on = current . request . utcnow - datetime . timedelta ( hours = <NUM_LIT:1> ) <EOL> person1 = Storage ( first_name = "<STR_LIT>" , <EOL> last_name = "<STR_LIT>" , <EOL> initials = "<STR_LIT>" , <EOL> date_of_birth = datetime . date ( <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT> ) , <EOL> created_on = created_on ) <EOL> person1_id = ptable . insert ( ** person1 ) <EOL> person1 . update ( id = person1_id ) <EOL> s3db . update_super ( ptable , person1 ) <EOL> self . person1_id = person1_id <EOL> self . pe1_id = s3db . pr_get_pe_id ( ptable , person1_id ) <EOL> person2 = Storage ( first_name = "<STR_LIT>" , <EOL> last_name = "<STR_LIT>" , <EOL> initials = "<STR_LIT>" , <EOL> date_of_birth = datetime . date ( <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT> ) ) <EOL> person2_id = ptable . insert ( ** person2 ) <EOL> person2 . update ( id = person2_id ) <EOL> s3db . update_super ( ptable , person2 ) <EOL> self . person2_id = person2_id <EOL> self . pe2_id = s3db . pr_get_pe_id ( ptable , person2_id ) <EOL> def testHook ( self ) : <EOL> s3db = current . s3db <EOL> deduplicate = s3db . get_config ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertNotEqual ( deduplicate , None ) <EOL> self . assertTrue ( callable ( deduplicate ) ) <EOL> def testMatchNames ( self ) : <EOL> s3db = current . s3db <EOL> from s3 . s3import import S3ImportItem <EOL> deduplicate = s3db . get_config ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> person = Storage ( first_name = "<STR_LIT>" , <EOL> last_name = "<STR_LIT>" ) <EOL> item = self . import_item ( person ) <EOL> deduplicate ( item ) <EOL> self . assertEqual ( item . id , self . person1_id ) <EOL> self . assertEqual ( item . method , S3ImportItem . METHOD . UPDATE ) <EOL> person = Storage ( first_name = "<STR_LIT>" , <EOL> last_name = "<STR_LIT>" ) <EOL> item = self . import_item ( person ) <EOL> deduplicate ( item ) <EOL> self . assertNotEqual ( item . id , self . person1_id ) <EOL> self . assertNotEqual ( item . id , self . person2_id ) <EOL> def testMatchEmail ( self ) : <EOL> s3db = current . s3db <EOL> from s3 . s3import import S3ImportItem <EOL> deduplicate = s3db . get_config ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> person = Storage ( first_name = "<STR_LIT>" , <EOL> last_name = "<STR_LIT>" ) <EOL> item = self . import_item ( person , email = "<STR_LIT>" ) <EOL> deduplicate ( item ) <EOL> self . assertEqual ( item . id , self . person1_id ) <EOL> self . assertEqual ( item . method , S3ImportItem . METHOD . UPDATE ) <EOL> person = Storage ( first_name = "<STR_LIT>" , <EOL> last_name = "<STR_LIT>" ) <EOL> item = self . import_item ( person , email = "<STR_LIT>" ) <EOL> deduplicate ( item ) <EOL> self . assertNotEqual ( item . id , self . person1_id ) <EOL> self . assertNotEqual ( item . id , self . person2_id ) <EOL> ctable = s3db . pr_contact <EOL> email = Storage ( pe_id = self . pe1_id , <EOL> contact_method = "<STR_LIT>" , <EOL> value = "<STR_LIT>" ) <EOL> ctable . insert ( ** email ) <EOL> email = Storage ( pe_id = self . pe2_id , <EOL> contact_method = "<STR_LIT>" , <EOL> value = "<STR_LIT>" ) <EOL> ctable . insert ( ** email ) <EOL> person = Storage ( first_name = "<STR_LIT>" , <EOL> last_name = "<STR_LIT>" ) <EOL> item = self . import_item ( person , email = "<STR_LIT>" ) <EOL> deduplicate ( item ) <EOL> self . assertEqual ( item . id , self . person1_id ) <EOL> self . assertEqual ( item . method , S3ImportItem . METHOD . UPDATE ) <EOL> person = Storage ( first_name = "<STR_LIT>" , <EOL> last_name = "<STR_LIT>" ) <EOL> item = self . import_item ( person ) <EOL> deduplicate ( item ) <EOL> self . assertNotEqual ( item . id , self . person1_id ) <EOL> self . assertNotEqual ( item . id , self . person2_id ) <EOL> person = Storage ( first_name = "<STR_LIT>" , <EOL> last_name = "<STR_LIT>" , <EOL> date_of_birth = datetime . date ( <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT> ) ) <EOL> item = self . import_item ( person ) <EOL> deduplicate ( item ) <EOL> self . assertEqual ( item . id , self . person1_id ) <EOL> self . assertEqual ( item . method , S3ImportItem . METHOD . UPDATE ) <EOL> person = Storage ( first_name = "<STR_LIT>" , <EOL> last_name = "<STR_LIT>" ) <EOL> item = self . import_item ( person , email = "<STR_LIT>" ) <EOL> deduplicate ( item ) <EOL> self . assertNotEqual ( item . id , self . person1_id ) <EOL> self . assertNotEqual ( item . id , self . person2_id ) <EOL> person = Storage ( first_name = "<STR_LIT>" , <EOL> last_name = "<STR_LIT>" , <EOL> date_of_birth = datetime . date ( <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT> ) ) <EOL> item = self . import_item ( person , email = "<STR_LIT>" ) <EOL> deduplicate ( item ) <EOL> self . assertEqual ( item . id , self . person1_id ) <EOL> self . assertEqual ( item . method , S3ImportItem . METHOD . UPDATE ) <EOL> person = Storage ( first_name = "<STR_LIT>" , <EOL> last_name = "<STR_LIT>" ) <EOL> item = self . import_item ( person , email = "<STR_LIT>" ) <EOL> deduplicate ( item ) <EOL> self . assertEqual ( item . id , self . person2_id ) <EOL> self . assertEqual ( item . method , S3ImportItem . METHOD . UPDATE ) <EOL> person = Storage ( first_name = "<STR_LIT>" , <EOL> last_name = "<STR_LIT>" ) <EOL> item = self . import_item ( person , email = "<STR_LIT>" ) <EOL> deduplicate ( item ) <EOL> self . assertNotEqual ( item . id , self . person1_id ) <EOL> self . assertNotEqual ( item . id , self . person2_id ) <EOL> def testMatchInitials ( self ) : <EOL> s3db = current . s3db <EOL> from s3 . s3import import S3ImportItem <EOL> deduplicate = s3db . get_config ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ctable = s3db . pr_contact <EOL> email = Storage ( pe_id = self . pe1_id , <EOL> contact_method = "<STR_LIT>" , <EOL> value = "<STR_LIT>" ) <EOL> ctable . insert ( ** email ) <EOL> email = Storage ( pe_id = self . pe2_id , <EOL> contact_method = "<STR_LIT>" , <EOL> value = "<STR_LIT>" ) <EOL> ctable . insert ( ** email ) <EOL> person = Storage ( initials = "<STR_LIT>" ) <EOL> item = self . import_item ( person ) <EOL> deduplicate ( item ) <EOL> self . assertEqual ( item . id , self . person1_id ) <EOL> self . assertEqual ( item . method , S3ImportItem . METHOD . UPDATE ) <EOL> person = Storage ( first_name = "<STR_LIT>" , <EOL> last_name = "<STR_LIT>" , <EOL> initials = "<STR_LIT>" ) <EOL> item = self . import_item ( person ) <EOL> deduplicate ( item ) <EOL> self . assertEqual ( item . id , self . person2_id ) <EOL> self . assertEqual ( item . method , S3ImportItem . METHOD . UPDATE ) <EOL> person = Storage ( first_name = "<STR_LIT>" , <EOL> last_name = "<STR_LIT>" , <EOL> initials = "<STR_LIT>" ) <EOL> item = self . import_item ( person , email = "<STR_LIT>" ) <EOL> deduplicate ( item ) <EOL> self . assertEqual ( item . id , self . person2_id ) <EOL> self . assertEqual ( item . method , S3ImportItem . METHOD . UPDATE ) <EOL> person = Storage ( initials = "<STR_LIT>" ) <EOL> item = self . import_item ( person ) <EOL> deduplicate ( item ) <EOL> self . assertEqual ( item . id , self . person2_id ) <EOL> self . assertEqual ( item . method , S3ImportItem . METHOD . UPDATE ) <EOL> person = Storage ( initials = "<STR_LIT>" ) <EOL> item = self . import_item ( person , email = "<STR_LIT>" ) <EOL> deduplicate ( item ) <EOL> self . assertEqual ( item . id , self . person1_id ) <EOL> self . assertEqual ( item . method , S3ImportItem . METHOD . UPDATE ) <EOL> def testMatchDOB ( self ) : <EOL> s3db = current . s3db <EOL> deduplicate = s3db . get_config ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ctable = s3db . pr_contact <EOL> email = Storage ( pe_id = self . pe1_id , <EOL> contact_method = "<STR_LIT>" , <EOL> value = "<STR_LIT>" ) <EOL> ctable . insert ( ** email ) <EOL> email = Storage ( pe_id = self . pe2_id , <EOL> contact_method = "<STR_LIT>" , <EOL> value = "<STR_LIT>" ) <EOL> ctable . insert ( ** email ) <EOL> person = Storage ( initials = "<STR_LIT>" , <EOL> date_of_birth = datetime . date ( <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT> ) ) <EOL> item = self . import_item ( person , email = "<STR_LIT>" ) <EOL> deduplicate ( item ) <EOL> self . assertEqual ( item . id , self . person1_id ) <EOL> person = Storage ( initials = "<STR_LIT>" , <EOL> date_of_birth = datetime . date ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> ) ) <EOL> item = self . import_item ( person , email = "<STR_LIT>" ) <EOL> deduplicate ( item ) <EOL> self . assertNotEqual ( item . id , self . person1_id ) <EOL> self . assertNotEqual ( item . id , self . person2_id ) <EOL> def import_item ( self , person , email = None , sms = None ) : <EOL> """<STR_LIT>""" <EOL> from s3 . s3import import S3ImportItem <EOL> def item ( tablename , data ) : <EOL> return Storage ( id = None , <EOL> method = None , <EOL> tablename = tablename , <EOL> data = data , <EOL> components = [ ] , <EOL> METHOD = S3ImportItem . METHOD ) <EOL> import_item = item ( "<STR_LIT>" , person ) <EOL> if email : <EOL> import_item . components . append ( item ( "<STR_LIT>" , <EOL> Storage ( contact_method = "<STR_LIT>" , <EOL> value = email ) ) ) <EOL> if sms : <EOL> import_item . components . append ( item ( "<STR_LIT>" , <EOL> Storage ( contact_method = "<STR_LIT>" , <EOL> value = sms ) ) ) <EOL> return import_item <EOL> def tearDown ( self ) : <EOL> current . db . rollback ( ) <EOL> self . pe_id = None <EOL> self . person_id = None <EOL> class ContactValidationTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> current . auth . override = True <EOL> self . current_setting = current . deployment_settings . get_msg_require_international_phone_numbers ( ) <EOL> def tearDown ( self ) : <EOL> settings = current . deployment_settings <EOL> current . deployment_settings . msg . require_international_phone_numbers = self . current_setting <EOL> current . db . rollback ( ) <EOL> current . auth . override = False <EOL> def testMobilePhoneNumberValidationStandard ( self ) : <EOL> """<STR_LIT>""" <EOL> current . deployment_settings . msg . require_international_phone_numbers = False <EOL> from s3db . pr import S3ContactModel <EOL> onvalidation = S3ContactModel . pr_contact_onvalidation <EOL> form = Storage ( <EOL> vars = Storage ( <EOL> contact_method = "<STR_LIT>" , <EOL> ) <EOL> ) <EOL> form . errors = Storage ( ) <EOL> form . vars . value = "<STR_LIT>" <EOL> onvalidation ( form ) <EOL> self . assertEqual ( form . vars . value , "<STR_LIT>" ) <EOL> self . assertFalse ( "<STR_LIT:value>" in form . errors ) <EOL> form . errors = Storage ( ) <EOL> form . vars . value = "<STR_LIT>" <EOL> onvalidation ( form ) <EOL> self . assertEqual ( form . vars . value , "<STR_LIT>" ) <EOL> self . assertTrue ( "<STR_LIT:value>" in form . errors ) <EOL> def testMobilePhoneNumberValidationInternational ( self ) : <EOL> """<STR_LIT>""" <EOL> current . deployment_settings . msg . require_international_phone_numbers = True <EOL> from s3db . pr import S3ContactModel <EOL> onvalidation = S3ContactModel . pr_contact_onvalidation <EOL> form = Storage ( <EOL> vars = Storage ( <EOL> contact_method = "<STR_LIT>" , <EOL> ) <EOL> ) <EOL> form . errors = Storage ( ) <EOL> form . vars . value = "<STR_LIT>" <EOL> onvalidation ( form ) <EOL> self . assertEqual ( form . vars . value , "<STR_LIT>" ) <EOL> self . assertFalse ( "<STR_LIT:value>" in form . errors ) <EOL> form . errors = Storage ( ) <EOL> form . vars . value = "<STR_LIT>" <EOL> onvalidation ( form ) <EOL> self . assertEqual ( form . vars . value , "<STR_LIT>" ) <EOL> self . assertTrue ( "<STR_LIT:value>" in form . errors ) <EOL> def testMobilePhoneNumberImportValidationStandard ( self ) : <EOL> """<STR_LIT>""" <EOL> s3db = current . s3db <EOL> current . deployment_settings . msg . require_international_phone_numbers = False <EOL> xmlstr = """<STR_LIT>""" <EOL> xmltree = etree . ElementTree ( etree . fromstring ( xmlstr ) ) <EOL> resource = s3db . resource ( "<STR_LIT>" ) <EOL> result = resource . import_xml ( xmltree , ignore_errors = True ) <EOL> resource = s3db . resource ( "<STR_LIT>" , uid = "<STR_LIT>" ) <EOL> self . assertEqual ( resource . count ( ) , <NUM_LIT:1> ) <EOL> row = resource . select ( [ "<STR_LIT:value>" ] , as_rows = True ) . first ( ) <EOL> self . assertNotEqual ( row , None ) <EOL> self . assertEqual ( row . value , "<STR_LIT>" ) <EOL> resource = s3db . resource ( "<STR_LIT>" , uid = "<STR_LIT>" ) <EOL> self . assertEqual ( resource . count ( ) , <NUM_LIT:0> ) <EOL> row = resource . select ( [ "<STR_LIT:value>" ] , as_rows = True ) . first ( ) <EOL> self . assertEqual ( row , None ) <EOL> def testMobilePhoneNumberImportValidationInternational ( self ) : <EOL> """<STR_LIT>""" <EOL> s3db = current . s3db <EOL> current . deployment_settings . msg . require_international_phone_numbers = True <EOL> xmlstr = """<STR_LIT>""" <EOL> xmltree = etree . ElementTree ( etree . fromstring ( xmlstr ) ) <EOL> resource = s3db . resource ( "<STR_LIT>" ) <EOL> result = resource . import_xml ( xmltree , ignore_errors = True ) <EOL> resource = s3db . resource ( "<STR_LIT>" , uid = "<STR_LIT>" ) <EOL> self . assertEqual ( resource . count ( ) , <NUM_LIT:0> ) <EOL> row = resource . select ( [ "<STR_LIT:value>" ] , as_rows = True ) . first ( ) <EOL> self . assertEqual ( row , None ) <EOL> resource = s3db . resource ( "<STR_LIT>" , uid = "<STR_LIT>" ) <EOL> self . assertEqual ( resource . count ( ) , <NUM_LIT:1> ) <EOL> row = resource . select ( [ "<STR_LIT:value>" ] , as_rows = True ) . first ( ) <EOL> self . assertNotEqual ( row , None ) <EOL> self . assertEqual ( row . value , "<STR_LIT>" ) <EOL> def run_suite ( * test_classes ) : <EOL> """<STR_LIT>""" <EOL> loader = unittest . TestLoader ( ) <EOL> suite = unittest . TestSuite ( ) <EOL> for test_class in test_classes : <EOL> tests = loader . loadTestsFromTestCase ( test_class ) <EOL> suite . addTests ( tests ) <EOL> if suite is not None : <EOL> unittest . TextTestRunner ( verbosity = <NUM_LIT:2> ) . run ( suite ) <EOL> return <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> run_suite ( <EOL> PRTests , <EOL> PersonDeduplicateTests , <EOL> ContactValidationTests , <EOL> ) </s>
