コード例 #1
0
def test_handle_my_plans_only_in_rsvps(
    mock_send_reply,
    mock_user,
    make_handler_params,
    make_time,
):
    """
    Checks that the user only appears in plans they have RSVP'd to.
    """
    params = make_handler_params("my-plans")

    plan1 = Plan("tjs", make_time(11, 00), [])
    plan2 = Plan("tjs", make_time(12, 30), [mock_user])
    params.storage.get.return_value = {
        plan1.uuid: plan1,
        plan2.uuid: plan2,
    }

    handle_my_plans(params)

    mock_send_reply.assert_called_with(
        params.client,
        params.message,
        "Here are the lunches you've RSVP'd to:\ntjs @ 12:30pm, 1 RSVP",
    )
コード例 #2
0
def test_handle_rsvp_success(
    mock_send_reply,
    mock_user,
    make_handler_params,
    make_time,
):
    """
    Ensures that handle_rsvp functions correctly when the preconditions are
    met.
    """
    params = make_handler_params("rsvp tjs")

    plan = Plan("tjs", make_time(12, 30), [])
    params.storage.get.return_value = {
        plan.uuid: plan,
    }

    handle_rsvp(params)

    params.storage.put.assert_called_with(
        params.storage.PLANS_ENTRY,
        {plan.uuid: Plan("tjs", make_time(12, 30), [mock_user])},
    )
    mock_send_reply.assert_called_with(
        params.client,
        params.message,
        "Thanks for RSVPing to lunch  at tjs! Enjoy your food, Test Sender!",
    )
コード例 #3
0
def test_handle_un_rsvp_ambiguous(
    mock_send_reply, make_handler_params, make_time,
):
    """
    Ensures that, when the user has provided an ambiguous query, handle_un_rsvp
    prompts them to disambiguate.
    """
    params = make_handler_params("un-rsvp tjs")

    plan1 = Plan("tjs", make_time(11, 00), [])
    plan2 = Plan("tjs", make_time(12, 30), [])
    params.storage.get.return_value = {
        plan1.uuid: plan1,
        plan2.uuid: plan2,
    }

    handle_un_rsvp(params)

    params.storage.put.assert_not_called()
    mock_send_reply.assert_called_with(
        params.client,
        params.message,
        """There are multiple lunches with that lunch_id. Please reissue the command with the time of the lunch you're interested in:
tjs @ 11:00am
tjs @ 12:30pm""",
    )
コード例 #4
0
ファイル: test_delete_plan.py プロジェクト: bern/lunch-bot
def test_handle_delete_plan_ambiguous(
    mock_send_reply, make_handler_params, make_time,
):
    """
    Tests that when there are multiple plans with the same name, delete_plan
    prompts the user to disambiguate a lunch.
    """
    params = make_handler_params("delete-plan tjs")

    plan1 = Plan("tjs", make_time(11, 00), [])
    plan2 = Plan("tjs", make_time(12, 30), [])
    params.storage.get.return_value = {
        plan1.uuid: plan1,
        plan2.uuid: plan2,
    }

    handle_delete_plan(params)

    mock_send_reply.assert_called_with(
        params.client,
        params.message,
        "There are multiple lunches with that lunch_id. Please reissue the command with the time of the lunch you're"
        " interested in:\n"
        "tjs @ 11:00am\n"
        "tjs @ 12:30pm",
    )
コード例 #5
0
def test_handle_rsvp_disambiguate(
    mock_send_reply,
    mock_user,
    make_handler_params,
    make_time,
):
    """
    Ensures that, when there are multiple lunches, the user can construct a
    query to disambiguate between them.
    """
    params = make_handler_params("rsvp tjs 12:30")

    plan1 = Plan("tjs", make_time(11, 00), [])
    plan2 = Plan("tjs", make_time(12, 30), [])
    params.storage.get.return_value = {
        plan1.uuid: plan1,
        plan2.uuid: plan2,
    }

    handle_rsvp(params)

    params.storage.put.assert_called_with(
        params.storage.PLANS_ENTRY,
        {
            plan1.uuid: plan1,
            plan2.uuid: Plan("tjs", make_time(12, 30), [mock_user])
        },
    )
    mock_send_reply.assert_called_with(
        params.client,
        params.message,
        "Thanks for RSVPing to lunch  at tjs! Enjoy your food, Test Sender!",
    )
コード例 #6
0
def test_alert_leaving_ambiguous(mock_send_reply, make_handler_params,
                                 make_time):
    """
    Ensures that, when there are multiple plans, and the user has addressed them
    ambiguously, handle_alert_leaving fails as expected.
    """
    params = make_handler_params("alert-leaving tjs")

    plan1 = Plan("tjs", make_time(11, 0), [])
    plan2 = Plan("tjs", make_time(12, 30), [])
    params.storage.get.return_value = {
        plan1.uuid: plan1,
        plan2.uuid: plan2,
    }

    handle_alert_leaving(params)

    mock_send_reply.assert_called_with(
        params.client,
        params.message,
        "There are multiple lunches with that lunch_id. Please reissue the"
        " command with the time of the lunch you're interested in:\n"
        "tjs @ 11:00am\n"
        "tjs @ 12:30pm",
    )
コード例 #7
0
def test_handle_un_rsvp_disambiguate(
    mock_send_reply, mock_user, make_handler_params, make_time,
):
    """
    Ensures that, when the user has provided an ambiguous query, handle_un_rsvp
    prompts them to disambiguate.
    """
    params = make_handler_params("un-rsvp tjs 12:30")

    plan1 = Plan("tjs", make_time(11, 00), [])
    plan2 = Plan("tjs", make_time(12, 30), [mock_user])
    params.storage.get.return_value = {
        plan1.uuid: plan1,
        plan2.uuid: plan2,
    }

    handle_un_rsvp(params)

    params.storage.put.assert_called_with(
        params.storage.PLANS_ENTRY,
        {plan1.uuid: plan1, plan2.uuid: Plan("tjs", make_time(12, 30), []),},
    )
    mock_send_reply.assert_called_with(
        params.client, params.message, "You've successful un-RSVP'd to lunch at tjs."
    )
コード例 #8
0
ファイル: test_delete_plan.py プロジェクト: bern/lunch-bot
def test_handle_delete_plan_disambiguate(
    mock_send_reply, make_handler_params, make_time,
):
    """
    Tests that when there are multiple plans with the same name, delete_plan
    lets the user disambiguate with a time.
    """
    params = make_handler_params("delete-plan tjs 12:30")

    plan1 = Plan("tjs", make_time(11, 00), [])
    plan2 = Plan("tjs", make_time(12, 30), [])
    params.storage.get.return_value = {
        plan1.uuid: plan1,
        plan2.uuid: plan2,
    }

    handle_delete_plan(params)

    params.storage.put.assert_called_with(
        params.storage.PLANS_ENTRY, {plan1.uuid: plan1}
    )
    params.cron.remove_event.assert_called_with(plan2.uuid)
    mock_send_reply.assert_called_with(
        params.client,
        params.message,
        "You've successfully deleted lunch tjs @ 12:30pm.",
    )
コード例 #9
0
def test_handle_make_plan_success(
    mocker, mock_send_reply, mock_user, make_handler_params, make_time,
):
    """
    Ensures that make_plan correctly inserts a plan when the arguments are
    correct.
    """
    params = make_handler_params("make-plans tjs 12:30")

    mocker.patch("uuid.uuid4", return_value="test_uuid")
    params.storage.get.return_value = {}

    handle_make_plan(params)

    params.storage.put.assert_called_with(
        params.storage.PLANS_ENTRY,
        {"test_uuid": Plan("tjs", make_time(12, 30), [mock_user])},
    )

    params.cron.add_event.assert_called()

    mock_send_reply.assert_called_with(
        params.client,
        params.message,
        "I have added your plan! Enjoy lunch, Test Sender!",
    )
コード例 #10
0
def test_handle_un_rsvp_success(
    mock_send_reply, mock_user, make_handler_params, make_time,
):
    """
    Ensures that handle_un_rsvp succeeds when the required preconditions are
    met.
    """
    params = make_handler_params("un-rsvp tjs")

    plan = Plan("tjs", make_time(12, 30), [mock_user])
    params.storage.get.return_value = {
        plan.uuid: plan,
    }

    handle_un_rsvp(params)

    params.storage.put_assert_called_with(
        params.storage.PLANS_ENTRY, Plan("tjs", make_time(12, 30), [])
    )
    mock_send_reply.assert_called_with(
        params.client, params.message, "You've successful un-RSVP'd to lunch at tjs."
    )
コード例 #11
0
def test_alert_leaving_disambiguate(mock_user, make_handler_params, make_time):
    """
    Ensures that, when there are multiple plans, but the user has addressed a
    specific plan, handle_alert_leaving succeeds.
    """
    params = make_handler_params("alert-leaving tjs 12:30pm")

    plan1 = Plan("tjs", make_time(11, 0), [])
    plan2 = Plan("tjs", make_time(12, 30), [mock_user, mock_user])
    params.storage.get.return_value = {
        plan1.uuid: plan1,
        plan2.uuid: plan2,
    }

    handle_alert_leaving(params)
    params.client.send_message.assert_called_with({
        "type":
        "private",
        "to": ["*****@*****.**", "*****@*****.**"],
        "content":
        "Heads up! You're going to tjs in 2:30:00",
    })
コード例 #12
0
ファイル: test_delete_plan.py プロジェクト: bern/lunch-bot
def test_handle_delete_plan_success(
    mock_send_reply, make_handler_params, make_time,
):
    params = make_handler_params("delete-plan tjs")

    plan = Plan("tjs", make_time(12, 30), [])
    params.storage.get.return_value = {plan.uuid: plan}

    handle_delete_plan(params)

    params.storage.put.assert_called_with(params.storage.PLANS_ENTRY, {})
    params.cron.remove_event.assert_called_with(plan.uuid)
    mock_send_reply.assert_called_with(
        params.client,
        params.message,
        "You've successfully deleted lunch tjs @ 12:30pm.",
    )
コード例 #13
0
ファイル: test_show_plans.py プロジェクト: bern/lunch-bot
def test_handle_show_plans_success(
    mock_send_reply, make_handler_params, make_time,
):
    """
    Ensures that, if there are plans, handle_show_plans shows the user all of
    the plans.
    """
    params = make_handler_params("show-plans")

    plan = Plan("tjs", make_time(12, 30), [])
    params.storage.get.return_value = {
        plan.uuid: plan,
    }

    handle_show_plans(params)

    mock_send_reply.assert_called_with(
        params.client, params.message, "tjs @ 12:30pm, 0 RSVPs"
    )
コード例 #14
0
ファイル: test_delete_plan.py プロジェクト: bern/lunch-bot
def test_handle_delete_plan_bad_id(
    mock_send_reply, make_handler_params, make_time,
):
    """
    Ensures that handle_delete_plan functions correctly when the user provides
    an integer ID that does not correspond to a plan.
    """
    params = make_handler_params("delete-plan not-tjs")

    plan = Plan("tjs", make_time(12, 30), [])
    params.storage.get.return_value = {plan.uuid: plan}

    handle_delete_plan(params)

    mock_send_reply.assert_called_with(
        params.client,
        params.message,
        "That lunch_id doesn't exist! Type show-plans to see each lunch_id and its associated lunch plan.",
    )
コード例 #15
0
def test_handle_make_plan_existing_plan(
    mock_send_reply, make_handler_params, make_time,
):
    """
    Ensures that, when there is already an existing plan with the intended name,
    we prevent the user from making another one.
    """
    params = make_handler_params("make-plan tjs 12:30")

    plan = Plan("tjs", make_time(12, 30), [])
    params.storage.get.return_value = {plan.uuid: plan}

    handle_make_plan(params)

    params.storage.put.assert_not_called()
    mock_send_reply.assert_called_with(
        params.client,
        params.message,
        "Sorry, there is already a plan with that name and time. How about you RSVP instead?",
    )
コード例 #16
0
def test_alert_leaving_bad_id(mock_send_reply, make_handler_params, make_time):
    """
    Ensures that, when there is a plan but not with the ID the user has
    provided, handle_alert_leaving fails as expected.
    """
    params = make_handler_params("alert-leaving not-tjs")

    plan = Plan("tjs", make_time(12, 30), [])
    params.storage.get.return_value = {
        plan.uuid: plan,
    }

    handle_alert_leaving(params)

    mock_send_reply.assert_called_with(
        params.client,
        params.message,
        "That lunch_id doesn't exist! Type show-plans to see each lunch_id and"
        " its associated lunch plan.",
    )
コード例 #17
0
def test_alert_leaving_success(mock_user, make_handler_params, make_time):
    """
    Ensures that, when all the preconditions have been met, handle_alert_leaving
    succeeds.
    """
    params = make_handler_params("alert-leaving tjs")

    plan = Plan("tjs", make_time(12, 30), [mock_user, mock_user, mock_user])
    params.storage.get.return_value = {
        plan.uuid: plan,
    }

    handle_alert_leaving(params)
    params.client.send_message.assert_called_with({
        "type":
        "private",
        "to": ["*****@*****.**", "*****@*****.**", "*****@*****.**"],
        "content":
        "Heads up! You're going to tjs in 2:30:00",
    })
コード例 #18
0
def test_handle_un_rsvp_not_rsvpd(
    mock_send_reply, make_handler_params, make_time,
):
    """
    Ensures that handle_un_rsvp fails as expected when the user is not already
    RSVP'd to the event.
    """
    params = make_handler_params("un-rsvp tjs")

    plan = Plan("tjs", make_time(12, 30), [])
    params.storage.get.return_value = {
        plan.uuid: plan,
    }

    handle_un_rsvp(params)

    params.storage.put.assert_not_called()
    mock_send_reply.assert_called_with(
        params.client,
        params.message,
        "Oops! It looks like you haven't RSVP'd to this lunch_id!",
    )
コード例 #19
0
def test_handle_rsvp_bad_id(
    mock_send_reply,
    make_handler_params,
    make_time,
):
    """
    Ensures that handle_rsvp failes correctly when the plan ID is not malformed
    but does not correspond to a plan.
    """
    params = make_handler_params("rsvp not-tjs")

    plan = Plan("tjs", make_time(12, 30), [])
    params.storage.get.return_value = {plan.uuid: plan}

    handle_rsvp(params)

    params.storage.put.assert_not_called()
    mock_send_reply.assert_called_with(
        params.client,
        params.message,
        "That lunch_id doesn't exist! Type show-plans to see each lunch_id and its associated lunch plan.",
    )
コード例 #20
0
def test_handle_un_rsvp_bad_id(
    mock_send_reply, mock_user, make_handler_params, make_time,
):
    """
    Ensures that handle_un_rsvp fails as expected when the user provides an id
    that is out of range of our available lunches.
    """
    params = make_handler_params("un-rsvp not-tjs")

    plan = Plan("tjs", make_time(12, 30), [mock_user])
    params.storage.get.return_value = {
        plan.uuid: plan,
    }

    handle_un_rsvp(params)

    params.storage.put.assert_not_called()
    mock_send_reply.assert_called_with(
        params.client,
        params.message,
        "That lunch_id doesn't exist! Type show-plans to see each lunch_id and its associated lunch plan.",
    )
コード例 #21
0
def test_handle_rsvp_already_rsvpd(
    mock_send_reply,
    mock_user,
    make_handler_params,
    make_time,
):
    """
    Ensures handle_rsvp fails correctly when the user is already RSVP'd to the
    event to which they're trying to rsvp.
    """
    params = make_handler_params("rsvp tjs")

    plan = Plan("tjs", make_time(12, 30), [mock_user])
    params.storage.get.return_value = {plan.uuid: plan}

    handle_rsvp(params)

    params.storage.put.assert_not_called()
    mock_send_reply.assert_called_with(
        params.client,
        params.message,
        "You've already RSVP'd to this lunch_id! Type my-plans to show all of your current lunch plans.",
    )
コード例 #22
0
def test_handle_my_plans_success(
    mock_send_reply,
    mock_user,
    make_handler_params,
    make_time,
):
    """
    Checks that, when the user has a plan, the correct list is shown.
    """
    params = make_handler_params("my-plans")

    plan = Plan("tjs", make_time(12, 30), [mock_user])
    params.storage.get.return_value = {
        plan.uuid: plan,
    }

    handle_my_plans(params)

    mock_send_reply.assert_called_with(
        params.client,
        params.message,
        "Here are the lunches you've RSVP'd to:\ntjs @ 12:30pm, 1 RSVP",
    )
コード例 #23
0
ファイル: make_plan.py プロジェクト: bern/lunch-bot
def handle_make_plan(params: HandlerParams):
    if len(params.args) != 3:
        common.send_reply(
            params.client,
            params.message,
            "Oops! The make-plan command requires more information. Type help for formatting instructions.",
        )
        return

    try:
        plan_time = common.parse_time(params.args[2])
    except ValueError:
        common.send_reply(
            params.client,
            params.message,
            "Sorry, the time you entered could not be used because it is not a valid date format.",
        )
        return

    now = common.get_now()
    if plan_time < now:
        common.send_reply(
            params.client,
            params.message,
            "Sorry, you can't plan for a lunch before the current time.",
        )
        return

    if not params.storage.contains(params.storage.PLANS_ENTRY):
        params.storage.put(params.storage.PLANS_ENTRY, {})

    plans = params.storage.get(params.storage.PLANS_ENTRY)
    for _, plan in plans.items():
        if plan.restaurant == params.args[1] and plan.time == plan_time:
            common.send_reply(
                params.client,
                params.message,
                "Sorry, there is already a plan with that name and time. How about you RSVP instead?",
            )
            return

    user = User.get_sender(params.message)
    plan = Plan(params.args[1], plan_time, [user])

    plans = params.storage.get(params.storage.PLANS_ENTRY)
    plans[plan.uuid] = plan
    params.storage.put(params.storage.PLANS_ENTRY, plans)

    common.send_reply(
        params.client,
        params.message,
        "I have added your plan! Enjoy lunch, {}!".format(user.full_name),
    )

    params.cron.add_event(
        (plan.time - timedelta(minutes=15)).timestamp(),
        events.AlertLeavingGenerator(plan),
        plan.uuid,
    )

    params.cron.add_event(
        plan.time.timestamp(),
        events.DeletePlanGenerator(plan),
    )