Repository: ansible/awx
File: awx/main/tests/unit/tasks/test_jobs.py (Python)

Code before:
                AutomationControllerJobScope.CLAIM_LAUNCH_TYPE: 'manual',
                AutomationControllerJobScope.CLAIM_PLAYBOOK_NAME: 'test.yml',
            },
        ),
        (
            {'id': 100, 'name': '', 'job_type': 'run', 'launch_type': 'manual', 'playbook': ''},
            {
                AutomationControllerJobScope.CLAIM_JOB_ID: 100,
                AutomationControllerJobScope.CLAIM_JOB_TYPE: 'run',
                AutomationControllerJobScope.CLAIM_LAUNCH_TYPE: 'manual',
            },
        ),
        (
            {'id': 100, 'name': 'Test', 'job_type': 'run', 'launch_type': 'manual', 'organization': _mock(1, '')},
            {
                AutomationControllerJobScope.CLAIM_JOB_ID: 100,
                AutomationControllerJobScope.CLAIM_JOB_NAME: 'Test',
                AutomationControllerJobScope.CLAIM_JOB_TYPE: 'run',
                AutomationControllerJobScope.CLAIM_LAUNCH_TYPE: 'manual',
                AutomationControllerJobScope.CLAIM_ORGANIZATION_ID: 1,
            },
        ),
    ],
)
def test_populate_claims_for_job(job_attrs, expected_claims):
    job = mock.MagicMock(spec=Job)
    job.id = None
    job.name = None
    job.job_type = None
    job.launch_type = None
    job.playbook = None
    job.organization = None
    job.inventory = None
    job.project = None
    job.execution_environment = None
    job.job_template = None
    job.unified_job_template = None
    job.instance_group = None
    job.launched_by = None

    for attr, value in job_attrs.items():
        setattr(job, attr, value)

    claims = jobs.populate_claims_for_job(job)
    assert claims == expected_claims


Review comment (suggestion):
we have a "job" fixture that is creates a Job django object. Might be more thorough to use that, which would help catch regressions if the job fields change one day

for example, this test uses the fixture https://github.com/fosterseth/awx/blob/292262e14fc66883d97b46f84a9ef891c153e2b6/awx/main/tests/unit/test_tasks.py#L388

Code after:
                AutomationControllerJobScope.CLAIM_ORGANIZATION_NAME: '',
                AutomationControllerJobScope.CLAIM_PLAYBOOK_NAME: '',
            },
        ),
    ],
)
def test_populate_claims_for_workload(job_attrs, expected_claims):
    job = Job()

    for attr, value in job_attrs.items():
        setattr(job, attr, value)

    claims = jobs.populate_claims_for_workload(job)
    assert claims == expected_claims


@pytest.mark.parametrize(
    "workload_attrs,expected_claims",
    [
        (
            {
                'id': 200,
                'name': 'Git Sync',
                'job_type': 'check',
                'launch_type': 'sync',
                'organization': Organization(id=1, name='Test Org'),
                'project': Project(pk=3, id=3, name='Test Project'),
                'unified_job_template': Project(pk=3, id=3, name='Test Project'),
                'execution_environment': ExecutionEnvironment(id=4, name='Test EE'),
                'instance_group': InstanceGroup(id=7, name='Test Instance Group'),
            },
            {
                AutomationControllerJobScope.CLAIM_JOB_ID: 200,
                AutomationControllerJobScope.CLAIM_JOB_NAME: 'Git Sync',
                AutomationControllerJobScope.CLAIM_JOB_TYPE: 'check',
                AutomationControllerJobScope.CLAIM_LAUNCH_TYPE: 'sync',
                AutomationControllerJobScope.CLAIM_LAUNCHED_BY_NAME: 'Test Project',
                AutomationControllerJobScope.CLAIM_LAUNCHED_BY_ID: 3,
                AutomationControllerJobScope.CLAIM_ORGANIZATION_NAME: 'Test Org',
                AutomationControllerJobScope.CLAIM_ORGANIZATION_ID: 1,
                AutomationControllerJobScope.CLAIM_PROJECT_NAME: 'Test Project',
                AutomationControllerJobScope.CLAIM_PROJECT_ID: 3,
                AutomationControllerJobScope.CLAIM_UNIFIED_JOB_TEMPLATE_NAME: 'Test Project',
                AutomationControllerJobScope.CLAIM_UNIFIED_JOB_TEMPLATE_ID: 3,
                AutomationControllerJobScope.CLAIM_EXECUTION_ENVIRONMENT_NAME: 'Test EE',
                AutomationControllerJobScope.CLAIM_EXECUTION_ENVIRONMENT_ID: 4,
                AutomationControllerJobScope.CLAIM_INSTANCE_GROUP_NAME: 'Test Instance Group',
                AutomationControllerJobScope.CLAIM_INSTANCE_GROUP_ID: 7,
            },
        ),
        (