# Path: test/framework/api_select.py
# def api(file_name, api_type, login=None, fixed_prefix=None, url_or_dir=None, fake_public_uri=None, invocation_class=None,
#         username=None, password=None):


import os, random
from jenkinsflow import jobload
from .framework import api_select
    num_builds_to_keep=4,
    final_result_use_cli=False,
    set_build_descriptions=()
)


def _random_job_name(api, short_name=None):
    # If short_name is not specified, use a random name to make sure the job doesn't exist
    short_name = short_name or str(random.random()).replace('.', '')
    return api.job_name_prefix + short_name, short_name


def _assert_job(api, job_name, cleanup=False):
    job = api.get_job(job_name)
    assert job is not None
    assert job.name == job_name
    assert job.public_uri is not None and job_name in job.public_uri

    if cleanup:
        api.delete_job(job_name)
        return None

    return job


def test_job_load_new_no_pre_delete(api_type):
    api = api_select.api(__file__, api_type, login=True)
    full_name, short_name = _random_job_name(api)
    api.job(short_name, 1, 1, 1, exec_time=1, non_existing=True)
    jobload.update_job_from_template(api, full_name, api.job_xml_template, pre_delete=False, context=_context)

# Path: test/framework/api_select.py
# def api(file_name, api_type, login=None, fixed_prefix=None, url_or_dir=None, fake_public_uri=None, invocation_class=None,
#         username=None, password=None):


# Path: test/job_load_test.py
import os, random
from jenkinsflow import jobload
from .framework import api_select

    num_builds_to_keep=4,
    final_result_use_cli=False,
    set_build_descriptions=()
)


def _random_job_name(api, short_name=None):
    # If short_name is not specified, use a random name to make sure the job doesn't exist
    short_name = short_name or str(random.random()).replace('.', '')
    return api.job_name_prefix + short_name, short_name


def _assert_job(api, job_name, cleanup=False):
    job = api.get_job(job_name)
    assert job is not None
    assert job.name == job_name
    assert job.public_uri is not None and job_name in job.public_uri

    if cleanup:
        api.delete_job(job_name)
        return None

    return job


def test_job_load_new_no_pre_delete(api_type):
    api = api_select.api(__file__, api_type, login=True)
    full_name, short_name = _random_job_name(api)
    api.job(short_name, 1, 1, 1, exec_time=1, non_existing=True)
    jobload.update_job_from_template(api, full_name, api.job_xml_template, pre_delete=False, context=_context)

    _assert_job(api, full_name, cleanup=True)