<commit_before>import pytest
from pnc_cli import buildconfigurations
from pnc_cli.swagger_client.apis.buildconfigurations_api import BuildconfigurationsApi
from pnc_cli.swagger_client.apis.productversions_api import ProductversionsApi
from pnc_cli import utils
from test import testutils
import time

current_time_millis = lambda: int(round(time.time() * 1000))
configs_api = BuildconfigurationsApi(utils.get_api_client())
versions_api = ProductversionsApi(utils.get_api_client())
common_fields = ['build_script', 'build_status', 'dependency_ids', 'description', 'environment_id', 'internal_scm', 'internal_scm_revison',
                     'project_id', 'repositories', 'scm_mirror_repo_url', 'scm_mirror_revision', 'scm_repo_url',
                     'scm_revision']
@pytest.fixture(scope='function')
def new_config(request):
    created_bc = configs_api.create_new(
        body=buildconfigurations.create_build_conf_object(name=testutils.gen_random_name(),
                                                          project_id=1,
                                                          environment_id=1,
                                                          build_status="UNKNOWN",
                                                          build_script='mvn clean install',
                                                          product_version_ids=[1],
                                                          scm_repo_url='https://github.com/thauser/simple-maven-build-pnc.git')).content

    def teardown():
        existing = [x.id for x in configs_api.get_all(page_size=10000000).content]
        if created_bc.id in existing:
            configs_api.delete_specific(id=created_bc.id)

    request.addfinalizer(teardown)
    return created_bc


def test_get_all_invalid_params():
    testutils.assert_raises_typeerror(configs_api, 'get_all')


def test_get_all():
    build_configurations = configs_api.get_all(page_index=0, page_size=10000000, sort="", q="").content
    assert build_configurations is not None


def test_create_build_configuration_invalid_params():
    testutils.assert_raises_typeerror(configs_api, 'create_new')


def test_create_build_configuration(new_config):
    bc_names = [bc.name for bc in configs_api.get_all(page_size=1000000).content]
    assert new_config.name in bc_names


def test_get_specific_no_id():
    testutils.assert_raises_valueerror(configs_api, 'get_specific', id=None)


def test_get_specific_invalid_param():
    testutils.assert_raises_typeerror(configs_api, 'get_specific', id=1)


def test_get_specific(new_config):
    assert configs_api.get_specific(id=new_config.id).content.to_dict() == new_config.to_dict()


def test_trigger_no_id():
    testutils.assert_raises_valueerror(configs_api, 'trigger', id=None)


def test_trigger_invalid_params():
    testutils.assert_raises_typeerror(configs_api, 'trigger', id=1)


def test_trigger(new_config):
    configs_api.trigger(id=new_config.id, rebuild_all=True)
    running_build = configs_api.get_specific(id=new_config.id).content
    assert running_build.build_status == "BUILDING"

def test_build_no_id():
    testutils.assert_raises_valueerror(configs_api, 'build', id=None, build_configuration_revision=1, build_task_id=1, submit_time_millis=1)

def test_build_no_revision():
    testutils.assert_raises_valueerror(configs_api, 'build', id=1, build_configuration_revision=None, build_task_id=1, submit_time_millis=1)

def test_build_no_task_id():
    testutils.assert_raises_valueerror(configs_api, 'build', id=1, build_configuration_revision=1, build_task_id=None, submit_time_millis=1)

def test_build_no_time():
    testutils.assert_raises_valueerror(configs_api, 'build', id=1, build_configuration_revision=1, build_task_id=1, submit_time_millis=None)

def test_build_invalid_param():
    testutils.assert_raises_typeerror(configs_api, 'build', id=1, build_configuration_revision=1, build_task_id=1, submit_time_millis=1)

def test_build(new_config):
    response = configs_api.build(id=new_config.id, build_configuration_revision=new_config.id, build_task_id=1, submit_time_millis=current_time_millis())
    assert response.status_code == 200


def test_get_build_configuration_id_by_name(new_config):
    find_by_name = buildconfigurations.get_build_configuration_id_by_name(name=new_config.name)
    assert configs_api.get_specific(id=find_by_name).content.id == configs_api.get_specific(id=new_config.id).content.id


def test_get_all_by_product_id_no_id():
    testutils.assert_raises_valueerror(configs_api, 'get_all_by_product_id', product_id=None)


def test_get_all_by_product_id_invalid_param():
    testutils.assert_raises_typeerror(configs_api, 'get_all_by_product_id', product_id=1)


def test_get_all_by_product_id(new_config):
    # need to add bcs to some product
    response = configs_api.get_all_by_product_id(product_id=1, page_index=0, page_size=1000000, sort="", q="").content
    assert response is not None


def test_get_all_by_product_version_id_no_product_id():
    testutils.assert_raises_valueerror(configs_api, 'get_all_by_product_version_id', product_id=None, version_id=1)


def test_get_all_by_product_version_id_no_version_id():
    testutils.assert_raises_valueerror(configs_api, 'get_all_by_product_version_id', product_id=1, version_id=None)
<commit_msg>use the Runningbuildrecords API to ensure builds are started.<commit_after>import pytest
from pnc_cli import buildconfigurations
from pnc_cli.swagger_client.apis.buildconfigurations_api import BuildconfigurationsApi
from pnc_cli.swagger_client.apis.productversions_api import ProductversionsApi
from pnc_cli.swagger_client.apis.runningbuildrecords_api import RunningbuildrecordsApi
from pnc_cli import utils
from test import testutils
import time

current_time_millis = lambda: int(round(time.time() * 1000))
configs_api = BuildconfigurationsApi(utils.get_api_client())
versions_api = ProductversionsApi(utils.get_api_client())
running_api = RunningbuildrecordsApi(utils.get_api_client())
common_fields = ['build_script', 'build_status', 'dependency_ids', 'description', 'environment_id', 'internal_scm', 'internal_scm_revison',
                     'project_id', 'repositories', 'scm_mirror_repo_url', 'scm_mirror_revision', 'scm_repo_url',
                     'scm_revision']
@pytest.fixture(scope='function')
def new_config(request):
    created_bc = configs_api.create_new(
        body=buildconfigurations.create_build_conf_object(name=testutils.gen_random_name(),
                                                          project_id=1,
                                                          environment_id=1,
                                                          build_status="UNKNOWN",
                                                          build_script='mvn clean install',
                                                          product_version_ids=[1],
                                                          scm_repo_url='https://github.com/thauser/simple-maven-build-pnc.git')).content

    def teardown():
        existing = [x.id for x in configs_api.get_all(page_size=10000000).content]
        if created_bc.id in existing:
            configs_api.delete_specific(id=created_bc.id)

    request.addfinalizer(teardown)
    return created_bc


def test_get_all_invalid_params():
    testutils.assert_raises_typeerror(configs_api, 'get_all')


def test_get_all():
    build_configurations = configs_api.get_all(page_index=0, page_size=10000000, sort="", q="").content
    assert build_configurations is not None


def test_create_build_configuration_invalid_params():
    testutils.assert_raises_typeerror(configs_api, 'create_new')


def test_create_build_configuration(new_config):
    bc_names = [bc.name for bc in configs_api.get_all(page_size=1000000).content]
    assert new_config.name in bc_names


def test_get_specific_no_id():
    testutils.assert_raises_valueerror(configs_api, 'get_specific', id=None)


def test_get_specific_invalid_param():
    testutils.assert_raises_typeerror(configs_api, 'get_specific', id=1)


def test_get_specific(new_config):
    assert configs_api.get_specific(id=new_config.id).content.to_dict() == new_config.to_dict()


def test_trigger_no_id():
    testutils.assert_raises_valueerror(configs_api, 'trigger', id=None)


def test_trigger_invalid_params():
    testutils.assert_raises_typeerror(configs_api, 'trigger', id=1)


def test_trigger(new_config):
    triggered_build = configs_api.trigger(id=new_config.id, rebuild_all=False).content
    assert triggered_build is not None
    build_record = running_api.get_specific(id=triggered_build.id)
    assert build_record is not None

def test_build_no_id():
    testutils.assert_raises_valueerror(configs_api, 'build', id=None, build_configuration_revision=1, build_task_id=1, submit_time_millis=1)

def test_build_no_revision():
    testutils.assert_raises_valueerror(configs_api, 'build', id=1, build_configuration_revision=None, build_task_id=1, submit_time_millis=1)

def test_build_no_task_id():
    testutils.assert_raises_valueerror(configs_api, 'build', id=1, build_configuration_revision=1, build_task_id=None, submit_time_millis=1)

def test_build_no_time():
    testutils.assert_raises_valueerror(configs_api, 'build', id=1, build_configuration_revision=1, build_task_id=1, submit_time_millis=None)

def test_build_invalid_param():
    testutils.assert_raises_typeerror(configs_api, 'build', id=1, build_configuration_revision=1, build_task_id=1, submit_time_millis=1)


@pytest.mark.xfail(reason='seems like this method may be deprecated.')
def test_build(new_config):
    response = configs_api.build(id=new_config.id, build_configuration_revision=new_config.id, build_task_id=1, submit_time_millis=current_time_millis())
    assert response.status_code == 200


def test_get_build_configuration_id_by_name(new_config):
    find_by_name = buildconfigurations.get_build_configuration_id_by_name(name=new_config.name)
    assert configs_api.get_specific(id=find_by_name).content.id == configs_api.get_specific(id=new_config.id).content.id


def test_get_all_by_product_id_no_id():
    testutils.assert_raises_valueerror(configs_api, 'get_all_by_product_id', product_id=None)


def test_get_all_by_product_id_invalid_param():
    testutils.assert_raises_typeerror(configs_api, 'get_all_by_product_id', product_id=1)


def test_get_all_by_product_id(new_config):
    # need to add bcs to some product
    response = configs_api.get_all_by_product_id(product_id=1, page_index=0, page_size=1000000, sort="", q="").content
    assert response is not None


def test_get_all_by_product_version_id_no_product_id():
    testutils.assert_raises_valueerror(configs_api, 'get_all_by_product_version_id', product_id=None, version_id=1)


def test_get_all_by_product_version_id_no_version_id():
    testutils.assert_raises_valueerror(configs_api, 'get_all_by_product_version_id', product_id=1, version_id=None)

{'fraction_non_alphanumeric': 0.08904109589041095, 'fraction_numerical': 0.011872146118721462, 'mean_word_length': 4.700676730869339, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5005163', 'n_tokens_mistral': 3624, 'n_tokens_neox': 3527, 'n_words': 511}