From e6c76d094194f2661e92767a888b49c439a8f956 Mon Sep 17 00:00:00 2001 From: Wayne Witzel III Date: Thu, 26 May 2016 14:42:19 -0400 Subject: [PATCH] fixup objects creation helpers --- awx/main/tests/factories/tower.py | 65 ++++++++++++++++++------------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/awx/main/tests/factories/tower.py b/awx/main/tests/factories/tower.py index a72e1ac246..3220f4c78b 100644 --- a/awx/main/tests/factories/tower.py +++ b/awx/main/tests/factories/tower.py @@ -27,6 +27,17 @@ from .fixtures import ( from .exc import NotUnique +def generate_objects(artifacts, kwargs): + '''generate_objects takes a list of artifacts that are supported by + a create function and compares it to the kwargs passed in to the create + function. If a kwarg is found that is not in the artifacts list a RuntimeError + is raised. + ''' + for k in kwargs.keys(): + if k not in artifacts: + raise RuntimeError('{} is not a valid argument'.format(k)) + return namedtuple("Objects", ",".join(artifacts)) + def generate_role_objects(objects): '''generate_role_objects assembles a dictionary of all possible objects by name. It will raise an exception if any of the objects share a name due to the fact that @@ -167,15 +178,19 @@ class _Mapped(object): # or encapsulated by specific factory fixtures in a conftest # -def create_job_template(name, **kwargs): - Objects = namedtuple("Objects", "job_template, inventory, project, credential, job_type") +def create_job_template(name, roles=None, persisted=True, **kwargs): + Objects = generate_objects(["job_template", + "organization", + "inventory", + "project", + "credential", + "job_type",], kwargs) org = None proj = None inv = None cred = None job_type = kwargs.get('job_type', 'run') - persisted = kwargs.get('persisted', True) if 'organization' in kwargs: org = kwargs['organization'] @@ -202,37 +217,28 @@ def create_job_template(name, **kwargs): job_type=job_type, persisted=persisted) role_objects = generate_role_objects([org, proj, inv, cred]) - apply_roles(kwargs.get('roles'), role_objects, persisted) + apply_roles(roles, role_objects, persisted) return Objects(job_template=jt, project=proj, inventory=inv, credential=cred, - job_type=job_type) + job_type=job_type, + organization=org,) -def create_organization(name, **kwargs): - artifacts = [ - "organization", - "teams", - "users", - "superusers", - "projects", - "labels", - "notification_templates", - "inventories", - ] - - for k in kwargs.keys(): - if k not in artifacts: - raise RuntimeError('{} is not a valid argument'.format(k)) - - Objects = namedtuple("Objects", ",".join(artifacts)) +def create_organization(name, roles=None, persisted=True, **kwargs): + Objects = generate_objects(["organization", + "teams", "users", + "superusers", + "projects", + "labels", + "notification_templates", + "inventories",], kwargs) projects = {} inventories = {} labels = {} notification_templates = {} - persisted = kwargs.get('persisted', True) org = mk_organization(name, '%s-desc'.format(name), persisted=persisted) @@ -269,7 +275,7 @@ def create_organization(name, **kwargs): notification_templates[nt] = mk_notification_template(nt, organization=org, persisted=persisted) role_objects = generate_role_objects([org, superusers, users, teams, projects, labels, notification_templates]) - apply_roles(kwargs.get('roles'), role_objects, persisted) + apply_roles(roles, role_objects, persisted) return Objects(organization=org, superusers=_Mapped(superusers), users=_Mapped(users), @@ -279,11 +285,14 @@ def create_organization(name, **kwargs): notification_templates=_Mapped(notification_templates), inventories=_Mapped(inventories)) -def create_notification_template(name, **kwargs): - Objects = namedtuple("Objects", "notification_template,organization,users,superusers,teams") +def create_notification_template(name, roles=None, persisted=True, **kwargs): + Objects = generate_objects(["notification_template", + "organization", + "users", + "superusers", + "teams",], kwargs) organization = None - persisted = kwargs.get('persisted', True) if 'organization' in kwargs: org = kwargs['organization'] @@ -296,7 +305,7 @@ def create_notification_template(name, **kwargs): users = generate_users(organization, teams, False, persisted, users=kwargs.get('users')) role_objects = generate_role_objects([organization, notification_template]) - apply_roles(kwargs.get('roles'), role_objects, persisted) + apply_roles(roles, role_objects, persisted) return Objects(notification_template=notification_template, organization=organization, users=_Mapped(users),