def test_rules_checking_rules_syntax_error():
    r"""The rules contain a syntax error

    outer_diameter ! inner_diameter in the rules

    """
    json_file = join(dirname(__file__),
                     "./json_files/library_wrong_rules_syntax.json")
    with pytest.raises(SyntaxError):
        _, _ = check_library_json_rules(json_file)
def test_rules_checking_negative_weight():
    r"""The parts library file contains a data entry where the weight is
    negative, and this breaks a rule defined in the 'rules'section of the
    library"""
    json_file = join(dirname(__file__),
                     "./json_files/library_negative_weight.json")
    ok, errors = check_library_json_rules(json_file)
    assert ok is False
    assert len(errors) == 1
    assert len(errors["608ZZ"]) == 1
def test_rules_checking_rules_definition_error():
    r"""The rules contain an identifier that is not used to define parts

    outer_diameter changed to out_diam in library_wrong_rule.json

    """
    json_file = join(dirname(__file__),
                     "./json_files/library_wrong_rules.json")
    # with pytest.raises(NameError):
    #     _, _ = check_library_json_rules(json_file)
    library_ok, _ = check_library_json_rules(json_file)
    assert library_ok is False
def test_rules_checking_many_errors():
    r"""608ZZ has a negative weight
    624ZZ has negative weight and an inner diameter that is greater than its
    outer diameter

    """
    json_file = join(dirname(__file__),
                     "./json_files/library_many_errors.json")
    ok, errors = check_library_json_rules(json_file)
    assert ok is False
    assert len(errors) == 2
    assert len(errors["624ZZ"]) == 2
    assert len(errors["608ZZ"]) == 1
Exemple #5
0
def generate_all(base_folder,
                 preview=False,
                 generate_steps=False,
                 generate_stls=False):
    r"""For each folder containing a JSON parts library definition:
    - check the JSON file is OK
    - if so, generate the geometry scripts

    Parameters
    ----------
    base_folder : str
        The root folder from which to generate
    preview : bool
        If True, do everything but creating the geometry scripts
        If False, also generate the geometry scripts
    generate_steps : bool
    generate_stls : bool

    """
    for item in walk(base_folder):
        if "library.json" in item[2]:
            json_filename_ = join(item[0], "library.json")
            logger.info("Library filename : %s" % json_filename_)
            logger.info("Checking the rules for the library JSON ...")
            ok, errors = check_library_json_rules(json_filename=json_filename_)
            if ok:
                logger.info("... done. Rules are OK")
                logger.info("Creating the Python scripts from the library "
                            "JSON ...")
                if preview is False:
                    generate(json_library_filepath=json_filename_,
                             generate_steps=generate_steps,
                             generate_stls=generate_stls)
                logger.info("... done")
            else:
                logger.error("The library contains errors, please "
                             "correct these before generating the scripts")
                print(errors)
def test_rules_checking_happy_path():
    r"""The parts library contains no error"""
    json_file = join(dirname(__file__), "./json_files/good_library.json")
    ok, errors = check_library_json_rules(json_file)
    assert ok is True
    assert errors == {}