def test_full_name_of_candidates(self): _records = CandidatesFaker(10).records _num = 0 for _record in _records: _name = _record['fullname'].split() _num += 1 if len(_name) == len(set(_name)) else _num self.assertEqual(_num, 10)
def test_regex_email_of_candidates(self): _regex = '^(\w|\.|\_|\-)+[@](\w|\_|\-|\.)+[.]\w{2,3}$' _records = CandidatesFaker(10).records _num = 0 for _record in _records: _num += 1 if re.search(_regex, _record['email']) else _num self.assertEqual(_num, 10)
def test_value_gender_of_candidates(self): _records = CandidatesFaker(100).records check = True for _record in _records: check = False if _record['gender'] != 'F' and _record[ 'gender'] != 'M' else check self.assertTrue(check)
def test_domain_email_of_candidates(self): _domains = ['icloud.com', 'gmail.com', 'yahoo.com', 'hotmail.com'] _records = CandidatesFaker(10).records _num = 0 for _record in _records: if _domains[0] in _record['email'] or \ _domains[1] in _record['email'] or \ _domains[2] in _record['email'] or \ _domains[3] in _record['email']: _num += 1 self.assertEqual(_num, 10)
def test_numbers_phone_of_candidates(self): _seeds = ['en', 'en_US', 'vi_VN'] _regex = lambda x: '^(\\+843|\\+845|\\+847|\\+848|\\+849|03|05|07|08|09)\\d{8}' \ if x == 'vi_VN' else \ '^(\\+1|001)\\d{8}' # _prephone = [ # ['+1', '001'], # ['+1', '001'], # ['+843', '+845', '+847', '+848', '+849', '03', '05', '07', '08', '09'] # ] for _seed in _seeds: _records = list(CandidatesFaker(10, locale=_seed).records) _num = 0 for i in range(len(_records)): _num += 1 if re.search(_regex(_seed), _records[i]['phone']) else _num self.assertEqual(_num, 10)
def test_candidates_class_with_total_candidates_is_undefined(self): errmsg = '[total] must be a positive integer' with self.assertRaises(Exception) as context: CandidatesFaker(None) self.assertEqual(errmsg, context.exception)
def test_defaul_ok(self): _records = list(CandidatesFaker(100).records) self.assertEqual(len(_records), 100)
def main(argv=sys.argv): parser = argparse.ArgumentParser(prog='python3 -m modelmock') subparsers = parser.add_subparsers(help='Sub-commands', dest='sub_command') # create the parser for the "generate" command generate_abc_parser = subparsers.add_parser( 'generate', help='Generate a type-based collection of records') generate_subparsers = generate_abc_parser.add_subparsers( help='[generate] sub-commands', dest='generated_target') generate_agents_parser = generate_subparsers.add_parser( 'agents', help='Generate a collection of agents') generate_agents_parser.add_argument( "--total", type=int, help="Number of agents will be generated") generate_agents_parser.add_argument("--mappings", type=str, help="level_mappings in JSON format") generate_agents_parser.add_argument("--id_method", type=str, choices=['incr', 'uuid'], help="The ID generation method") generate_agents_parser.add_argument("--id_prefix", type=str, default="AGENT", help="Prefix of the Agent IDs") generate_agents_parser.add_argument("--id_padding", type=int, default=4, help="Length of padded numbers of IDs") generate_agents_parser.add_argument("--id_shuffle", type=str, default="true", choices=CHOICES_VALUE, help="The IDs will be shuffled or not") generate_agents_parser.add_argument( "--locale", type=str, help= "The locale which used to generate the user information (phone, email, name, ...)" ) generate_candidates_parser = generate_subparsers.add_parser( 'candidates', help='Generate a collection of candidates') generate_candidates_parser.add_argument( "--total", type=int, help="Number of candidates will be generated") generate_promocodes_parser = generate_subparsers.add_parser( 'promocodes', help='Generate a collection of promotion-codes') generate_promocodes_parser.add_argument( "--total", type=int, help="Number of promotion-codes will be generated") generate_contracts_parser = generate_subparsers.add_parser( 'contracts', help='Generate a collection of contracts') generate_contracts_parser.add_argument( "--total", type=int, required=True, help="Number of contracts will be generated") generate_contracts_parser.add_argument( "--price", type=int, required=True, help="Average price of contracts will be generated") generate_contracts_parser.add_argument( "--multiplier", type=int, default=1, help="Multiplier of contract price (1, 10, 100, 1000, ...)") generate_contracts_parser.add_argument( "--extra_descriptors", type=str, default="[]", help="extra_descriptors in JSON format") generate_purchases_parser = generate_subparsers.add_parser( 'purchases', help='Generate a collection of purchases') generate_purchases_parser.add_argument( "--total_agents", type=int, required=True, help="Number of agents associated with purchases") generate_purchases_parser.add_argument( "--total_contracts", type=int, required=True, help="Number of contracts associated with purchases") generate_purchases_parser.add_argument( "--contract_price", type=int, required=True, help="Average price of purchases will be generated") generate_purchases_parser.add_argument( "--multiplier", type=int, default=1, help="Multiplier of contract price (1, 10, 100, 1000, ...)") generate_purchases_parser.add_argument( "--extra_descriptors", type=str, default="[]", help="extra_descriptors in JSON format") generate_purchases_parser.add_argument("--agent_id_method", type=str, choices=['incr', 'uuid'], help="The ID generation method") generate_purchases_parser.add_argument("--agent_id_prefix", type=str, default="AGENT", help="Prefix of the Agent IDs") generate_purchases_parser.add_argument( "--agent_id_padding", type=int, default=4, help="Length of padded numbers of IDs") generate_purchases_parser.add_argument( "--agent_id_shuffle", type=str, default="true", choices=CHOICES_VALUE, help="The IDs will be shuffled or not") generate_purchases_parser.add_argument("--contract_id_method", type=str, choices=['incr', 'uuid'], help="The ID generation method") generate_purchases_parser.add_argument("--contract_id_prefix", type=str, default="CONTR", help="Prefix of the Contract IDs") generate_purchases_parser.add_argument( "--contract_id_padding", type=int, default=4, help="Length of padded numbers of IDs") generate_purchases_parser.add_argument( "--contract_id_shuffle", type=str, default="true", choices=CHOICES_VALUE, help="The IDs will be shuffled or not") args = parser.parse_args(args=argv[1:]) if args.sub_command == 'generate': if args.generated_target == 'agents': display( AgentsFaker(**dict( total_agents=args.total, level_mappings=json.loads(args.mappings), id_method=args.id_method, id_prefix=args.id_prefix, id_padding=args.id_padding, id_shuffle=( args.id_shuffle is not None and args.id_shuffle.lower() in ['true', 'yes', '1']), locale=args.locale, ))) return 0 if args.generated_target == 'candidates': display(CandidatesFaker(args.total)) return 0 if args.generated_target in [ 'promocodes', 'promotion-codes', 'promotion_codes' ]: display(PromocodesFaker(args.total)) return 0 if args.generated_target == 'contracts': display( ContractsFaker(**dict( total_contracts=args.total, contract_price=args.price, multiplier=args.multiplier, extra_descriptors=json.loads(args.extra_descriptors), ))) return 0 if args.generated_target == 'purchases': pprint( list( generate_purchases( **pick_object_fields(args, [ 'total_agents', 'total_contracts', 'contract_price', 'multiplier', 'extra_descriptors', 'agent_id_method', 'agent_id_prefix', 'agent_id_padding', 'agent_id_shuffle', 'contract_id_method', 'contract_id_prefix', 'contract_id_padding', 'contract_id_shuffle', ])))) return 0 parser.print_help() return -1