<commit_before># Copyright (c) 2012 - 2014 <NAME>, Hupfeldt IT
# All rights reserved. This work is under a BSD license, see LICENSE.TXT.

import os, random
from os.path import join as jp

from pytest import raises

from jenkinsflow import jobload
from .framework import mock_api


here = os.path.abspath(os.path.dirname(__file__))

_context = dict(exec_time=1, params=(), script=None, securitytoken='abc')
_job_xml_template = jp(here, 'framework/job.xml.tenjin')


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())
    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.baseurl is not None and job_name in job.baseurl

    if cleanup:
        api.delete_job(job_name)
        return None

    return job


def test_job_load_new_no_pre_delete():
    api = mock_api.api(__file__)
    full_name, short_name = _random_job_name(api)
    api.job(short_name, 1, 1, 1, 1)
    jobload.update_job_from_template(api, full_name, _job_xml_template, pre_delete=False, context=_context)
    _assert_job(api, full_name, cleanup=True)


def test_job_load_new_pre_delete():
    api = mock_api.api(__file__)
    full_name, short_name = _random_job_name(api)
    api.job(short_name, 1, 1, 1, 1)
    jobload.update_job_from_template(api, full_name, _job_xml_template, pre_delete=True, context=_context)
    _assert_job(api, full_name, cleanup=True)


def test_job_load_existing_pre_delete():
    api = mock_api.api(__file__)
    full_name, short_name = _random_job_name(api)
    api.job(short_name, 1, 1, 1, 1)
    jobload.update_job_from_template(api, full_name, _job_xml_template, pre_delete=True, context=_context)
    _assert_job(api, full_name, cleanup=False)
    jobload.update_job_from_template(api, full_name, _job_xml_template, pre_delete=True, context=_context)
    _assert_job(api, full_name, cleanup=True)


def test_job_load__existing_update():
    api = mock_api.api(__file__)
    full_name, short_name = _random_job_name(api)
    api.job(short_name, 1, 1, 1, 1)
    jobload.update_job_from_template(api, full_name, _job_xml_template, pre_delete=True, context=_context)
    _assert_job(api, full_name, cleanup=False)
    jobload.update_job_from_template(api, full_name, _job_xml_template, pre_delete=False, context=_context)
    _assert_job(api, full_name, cleanup=True)


def test_job_load_non_existing_pre_delete():
    api = mock_api.api(__file__)
    full_name, short_name = _random_job_name(api)
    if api.is_mocked:
        # TODO: Since the Mock framework is not suited for testing this we just accept the KeyError, it will still test some code that is not otherwise tested
        with raises(KeyError):
            jobload.update_job_from_template(api, full_name, _job_xml_template, pre_delete=True, context=_context)
    else:
        jobload.update_job_from_template(api, full_name, _job_xml_template, pre_delete=True, context=_context)
        <commit_msg>pylint<commit_after># Copyright (c) 2012 - 2014 <NAME>, Hupfeldt IT
# All rights reserved. This work is under a BSD license, see LICENSE.TXT.

import os, random
from os.path import join as jp

from pytest import raises

from jenkinsflow import jobload
from .framework import mock_api


here = os.path.abspath(os.path.dirname(__file__))

_context = dict(exec_time=1, params=(), script=None, securitytoken='abc')
_job_xml_template = jp(here, 'framework/job.xml.tenjin')


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())
    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.baseurl is not None and job_name in job.baseurl

    if cleanup:
        api.delete_job(job_name)
        return None

    return job


def test_job_load_new_no_pre_delete():
    api = mock_api.api(__file__)
    full_name, short_name = _random_job_name(api)
    api.job(short_name, 1, 1, 1, 1)
    jobload.update_job_from_template(api, full_name, _job_xml_template, pre_delete=False, context=_context)
    _assert_job(api, full_name, cleanup=True)


def test_job_load_new_pre_delete():
    api = mock_api.api(__file__)
    full_name, short_name = _random_job_name(api)
    api.job(short_name, 1, 1, 1, 1)
    jobload.update_job_from_template(api, full_name, _job_xml_template, pre_delete=True, context=_context)
    _assert_job(api, full_name, cleanup=True)


def test_job_load_existing_pre_delete():
    api = mock_api.api(__file__)
    full_name, short_name = _random_job_name(api)
    api.job(short_name, 1, 1, 1, 1)
    jobload.update_job_from_template(api, full_name, _job_xml_template, pre_delete=True, context=_context)
    _assert_job(api, full_name, cleanup=False)
    jobload.update_job_from_template(api, full_name, _job_xml_template, pre_delete=True, context=_context)
    _assert_job(api, full_name, cleanup=True)


def test_job_load__existing_update():
    api = mock_api.api(__file__)
    full_name, short_name = _random_job_name(api)
    api.job(short_name, 1, 1, 1, 1)
    jobload.update_job_from_template(api, full_name, _job_xml_template, pre_delete=True, context=_context)
    _assert_job(api, full_name, cleanup=False)
    jobload.update_job_from_template(api, full_name, _job_xml_template, pre_delete=False, context=_context)
    _assert_job(api, full_name, cleanup=True)


def test_job_load_non_existing_pre_delete():
    api = mock_api.api(__file__)
    full_name, _ = _random_job_name(api)
    if api.is_mocked:
        # TODO: Since the Mock framework is not suited for testing this we just accept the KeyError, it will still test some code that is not otherwise tested
        with raises(KeyError):
            jobload.update_job_from_template(api, full_name, _job_xml_template, pre_delete=True, context=_context)
    else:
        jobload.update_job_from_template(api, full_name, _job_xml_template, pre_delete=True, context=_context)

{'fraction_non_alphanumeric': 0.08908685968819599, 'fraction_numerical': 0.007954183900731784, 'mean_word_length': 4.837511606313835, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 20}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2604126', 'n_tokens_mistral': 2217, 'n_tokens_neox': 2163, 'n_words': 505}