def post(cls):
        """Creates one activity in the database.

        Args:
            activity_type (str): Body argument indicating the activity type
            site (str): Body argument indicating the factory location
            typology (str): Body argument indicating the context of the activity
            description (str): Body argument indicating the description of the activity
            estimated_time (int): Body argument indicating the expected duration of the activity, in minutes
            interruptible (bool): Body argument indicating if the activity should be interruptible
            materials (str, optional): Optional body argument indicating the list of materials needed for the activity
            week (int): Body argument indicating the nth week of the year during which the activity has to be performed
            workspace_notes (str, optional): Optional body argument indicating a short description of the workspace

        Returns:
            dict of (str, any): Jsonified activity or error message.
        """
        data = cls._activity_parser.parse_args()

        try:
            activity = MaintenanceActivityModel(**data)
            activity.save_to_db()
        except Exception as e:
            return {"error": str(e)}, 500

        return activity.json(), 201
Exemplo n.º 2
0
def test_no_activities_page_not_found(app, planner_client, maintainer_users,
                                      activity_seed):
    """ Tests a failed retrival of the weekly availabilities given a non-existing current_page """

    with app.app_context():
        # Create one unassigned activity
        activity = MaintenanceActivityModel(**activity_seed)
        activity.save_to_db()

    test_current_page = 3
    test_page_size = len(maintainer_users)
    data = {"current_page": test_current_page, "page_size": test_page_size}

    res = planner_client.get(
        f"/maintainer/{activity_seed['activity_id']}/availabilities",
        data=data)

    assert res.status_code == 404
    assert "message" in res.get_json().keys()
Exemplo n.º 3
0
def test_full_activities_reassign(app, planner_client, maintainer_user,
                                  maintainer_users, activity_seed,
                                  activity_seed_without_id, week_days):
    """ Tests a successful retrival of the weekly availabilities when a maintainer
        already has a full schedule and the activity is already associated with that maintainer"""

    start_time = config.MAINTAINER_WORK_START_HOUR
    end_time = start_time + config.MAINTAINER_WORK_HOURS

    with app.app_context():
        work_hours = config.MAINTAINER_WORK_HOURS
        i = start_time
        while i < end_time - 1:
            activity = MaintenanceActivityModel(**activity_seed_without_id)
            activity.maintainer_username = maintainer_user["username"]
            activity.week_day = "monday"
            activity.start_time = i
            activity.save_to_db()
            i += 1
        activity = MaintenanceActivityModel(**activity_seed)
        activity.maintainer_username = maintainer_user["username"]
        activity.week_day = "monday"
        activity.start_time = i
        activity.save_to_db()

    test_current_page = 1
    test_page_size = len(maintainer_users)
    data = {"current_page": test_current_page, "page_size": test_page_size}

    res = planner_client.get(
        f"/maintainer/{activity_seed['activity_id']}/availabilities",
        data=data)

    validate_successful_response(res, test_current_page, test_page_size,
                                 maintainer_users, activity_seed, week_days)

    maintainer_availability = next(
        row["weekly_percentage_availability"] for row in res.get_json()["rows"]
        if row["user"]["username"] == maintainer_user["username"])

    assert maintainer_availability[
        "monday"] == f"{round(100 - 100*(work_hours-1)/work_hours)}%"
def setup(app, activity_seeds, planner_seed):
    """Before each test it drops every table and recreates them. 
    Then it creates an activity for every dictionary present in activity_seeds
    It also creates a planner that will be used to perform the requests

    Returns:
        boolean: the return status
    """
    with app.app_context():
        from db import db
        db.drop_all()
        db.create_all()
        from models.maintenance_activity import MaintenanceActivityModel
        for seed in activity_seeds:
            activity = MaintenanceActivityModel(**seed)
            activity.save_to_db()
        from models.user import UserModel
        planner = UserModel(**planner_seed)
        planner.save_to_db()
    return True
Exemplo n.º 5
0
def test_no_activities_success(app, planner_client, maintainer_users,
                               activity_seed, week_days):
    """ Tests a successful retrival of the weekly availabilities for a maintainer
    when there is no maintenance activity stored in the database
    """

    with app.app_context():
        # Create one unassigned activity
        activity = MaintenanceActivityModel(**activity_seed)
        activity.save_to_db()

    test_current_page = 1
    test_page_size = len(maintainer_users) - 1
    data = {"current_page": test_current_page, "page_size": test_page_size}

    res = planner_client.get(
        f"/maintainer/{activity_seed['activity_id']}/availabilities",
        data=data)

    validate_successful_response(res, test_current_page, test_page_size,
                                 maintainer_users, activity_seed, week_days)
Exemplo n.º 6
0
def test_full_activities_success(app, planner_client, maintainer_user,
                                 maintainer_users, activity_seed,
                                 activity_seed_without_id, week_days):
    """ Tests a successful retrival of the weekly availabilities when a maintainer
    has an already full schedule """
    start_time = config.MAINTAINER_WORK_START_HOUR
    end_time = start_time + config.MAINTAINER_WORK_HOURS

    with app.app_context():
        # Create many assigned activities without filling all the maintainer work schedule
        i = start_time
        while i < end_time:
            activity = MaintenanceActivityModel(**activity_seed_without_id)
            activity.maintainer_username = maintainer_user["username"]
            activity.week_day = "monday"
            activity.start_time = i
            activity.save_to_db()
            i += 1
        # Create one unassigned activity
        activity = MaintenanceActivityModel(**activity_seed)
        activity.save_to_db()

    test_current_page = 1
    test_page_size = len(maintainer_users)
    data = {"current_page": test_current_page, "page_size": test_page_size}

    res = planner_client.get(
        f"/maintainer/{activity_seed['activity_id']}/availabilities",
        data=data)

    validate_successful_response(res, test_current_page, test_page_size,
                                 maintainer_users, activity_seed, week_days)

    maintainer_availability = next(
        row["weekly_percentage_availability"] for row in res.get_json()["rows"]
        if row["user"]["username"] == maintainer_user["username"])

    assert maintainer_availability["monday"] == "0%"