Ejemplo n.º 1
0
def test_is_child():
    # Check that part 1 is a child, but part 3 is not
    test_parts = api_initialize.import_json('test_data.json')
    part1 = api_helper.get_part(1, test_parts)
    part3 = api_helper.get_part(3, test_parts)
    assert api_helper.is_child(part1, test_parts) == True
    assert api_helper.is_child(part3, test_parts) == False
Ejemplo n.º 2
0
def test_get_part():
    # get part 1 and make sure it has the correct values
    test_parts = api_initialize.import_json('test_data.json')
    part = api_helper.get_part(1, test_parts)
    assert part['id'] == 1
    assert part['name'] == 'screw'
    assert part['children'] == []
    assert part['attributes'] == ['thread']
    assert part['children attribute values'] == []

    with pytest.raises(InvalidUsage):
        api_helper.get_part(17, test_parts)
Ejemplo n.º 3
0
def get_top_children(part_id):
    # get_top_children returns all top-level children of a part
    # with id given by part_id

    part = api_helper.get_part(part_id, parts)
    top_children_id = part['children']
    top_children = [
        api_helper.get_part(top_child_id, parts)
        for top_child_id in top_children_id
    ]

    return jsonify({'top children': top_children})
Ejemplo n.º 4
0
def test_remove_children():
    # remove part 1 from part 3 and make sure the new values are correct
    test_parts = api_initialize.import_json('test_data.json')
    parent = api_helper.get_part(3, test_parts)
    new_children, new_children_attributes = api_helper.remove_children(
        parent, [1])
    assert new_children == [2]
    assert new_children_attributes == [[]]
Ejemplo n.º 5
0
def test_add_children():
    # Add part 4 to part 3 and make sure the new values are correct
    test_parts = api_initialize.import_json('test_data.json')
    parent = api_helper.get_part(3, test_parts)
    new_children, new_children_attributes = api_helper.add_children(
        parent, [4], [['green']], test_parts)
    assert new_children == [1, 2, 4]
    assert new_children_attributes == [['8-24'], [], ['green']]
Ejemplo n.º 6
0
def get_all_children(part_id):
    # get_all_children returns all children of a part with id given by part_id
    # It returns all top-level children, the children of those children,
    # etc. all the way down

    children_id = api_helper.get_all_children_id(part_id, parts)
    children = [
        api_helper.get_part(child_id, parts) for child_id in children_id
    ]

    return jsonify({'all children': children})
Ejemplo n.º 7
0
def modify_children(parent_id):
    # modify_children attaches/removes one or more parts to/from a parent part
    # The function below mostly consists of checking for errors, and then the
    # actual addition/removal takes place in helper functions.
    # User mast pass a json with the following fields:
    # action: a string that is either "attach" or "remove"
    # children: a list of the id's of children to attach or remove
    # If children are being attached, there must also be a field
    # "attribute values" which specifies the value of each attribute the
    # children have. It's a list of lists. For example if the children being
    # attached have id's [3, 6, 4] then the first item of "attribute values"
    # is a list of attributes for the part with id 3. If this child with id 3
    # has attributes "color" and "material", then the attribute values list
    # might be ["red", "plastic"]

    parent = api_helper.get_part(parent_id, parts)

    if not request.json:
        # if request is not in json format, error
        raise InvalidUsage('Modification must be in JSON format',
                           status_code=400)

    if 'children' not in request.json:
        # if json request doesn't include a 'children' key, throw an error
        raise InvalidUsage('children field missing from request',
                           status_code=400)

    if 'action' not in request.json:
        # If json request doesn't include an 'action' key, throw an error
        raise InvalidUsage('action field missing from request',
                           status_code=400)

    # Extract list of children to add/remove from json request
    # If the user didn't give the children ids in list form, make it a list
    # Want to allow the case where the user wants to attach/remove a single
    # part, and instead of sending a list of ids just sends the id as an int
    parts_list = request.json['children']
    if type(parts_list) is not list:
        parts_list = [parts_list]

    # call a function to modify the list of children
    # depending on the 'action' keyword
    if request.json['action'] == 'attach':
        # attach children
        # check to make sure there's a list of attribute values, and it
        # is the same length as the list of new children to add
        if 'attribute values' not in request.json:
            raise InvalidUsage('attribute values field missing from request',
                               status_code=400)

        # since attribute values exist, extract from json
        attribute_values = request.json['attribute values']

        # if it's not a list, make it a list
        if type(attribute_values) is not list:
            attribute_values = [attribute_values]
        # if the lengths of the children list and attribute values list
        # don't line up, throw an error
        if not len(attribute_values) == len(parts_list):
            raise InvalidUsage(
                "list of attributes does not match list of "
                "children to add",
                status_code=400)

        # now that these checks have been done, can actually add children
        new_children_list, new_children_attributes_list = api_helper.add_children(
            parent, parts_list, attribute_values, parts)
    elif request.json['action'] == 'remove':
        # remove children
        new_children_list, new_children_attributes_list = api_helper.remove_children(
            parent, parts_list)
    else:
        raise InvalidUsage('action field must specify attach or remove',
                           status_code=400)

    # modify the list of children, but first create a copy of the current
    # list, in case we have to revert after checking for loops
    old_children_list = parent['children'].copy()
    parent['children'] = new_children_list

    if api_helper.check_loops(parts):
        # check to see if any loops have been created
        # if so, revert to the old list of children and raise error
        parent['children'] = old_children_list
        raise InvalidUsage(
            "Cannot add parts to assembly, as it creates an "
            "infinite loop",
            status_code=409)

    parent['children attribute values'] = new_children_attributes_list
    return jsonify({'assembly': parent})
Ejemplo n.º 8
0
def get_part(part_id):
    # get_part returns the specific part given by id

    part = api_helper.get_part(part_id, parts)
    return jsonify({'part': part})