def test_update_edu_data():
    """
    tests ingress.update_edu_data routine
    """

    # not a dataframe
    (
        success,
        error,
        _,
        _,
        sub_new_list,
        sub_update_list
    ) = update_edu_data(ENGINE, None)
    assert success is False, error

    # empty dataframe
    success, error, _, _, sub_new_list, sub_update_list = update_edu_data(
        ENGINE, pd.DataFrame()
    )
    assert success is False, error

    # real data
    success, error, _, _, sub_new_list, sub_update_list = update_edu_data(
        ENGINE, eduhub_df1
    )

    assert success, error
    assert len(sub_new_list) == 0
    assert len(sub_update_list) == 2
Example #2
0
def test_usage_update():
    """
    Additional routine to test the budget module.

    In this test we cover the scenatio when budget value is updated.

    """

    # updates
    file_path = os.path.join(CONST_TEST_DIR_DATA, CONST_TEST11_FILENAME)
    eduhub_df = pd.read_csv(file_path)

    (
        success,
        error,
        lab_dict,
        sub_dict,
        sub_new_list,
        sub_update_list,
    ) = update_edu_data(ENGINE, eduhub_df)

    assert success, error
    assert len(sub_new_list) == 0
    assert len(sub_update_list) == 4

    # send notifications
    #   1 changed code from 50 to 90
    #   the other did not change its code
    success, error, count = notify_usage(
        ENGINE, lab_dict, sub_dict, sub_update_list
    )
    assert success, error
    assert count == 2
Example #3
0
def test_indiv_email_new():
    """
    test new individual emails
    """

    # real data
    file_path = os.path.join(CONST_TEST_DIR_DATA, CONST_TEST4_FILENAME)
    eduhub_df = pd.read_csv(file_path)

    (success, error, lab_dict, sub_dict, sub_new_list,
     sub_update_list) = update_edu_data(ENGINE, eduhub_df)

    assert success, error
    assert len(sub_new_list) == 2
    assert len(sub_update_list) == 0

    (success, error, html_content) = indiv_email_new(lab_dict, sub_dict,
                                                     sub_new_list[0])

    assert success, error
    assert len(html_content) == 3812

    (success, error, html_content) = indiv_email_new(lab_dict, sub_dict,
                                                     sub_new_list[1])

    assert success, error
    assert len(html_content) == 5175
def test_get_courses_labs_subscriptions_dict():
    """
    tests get_labs_dict routine
    """

    # new subscriptions
    file_path = os.path.join(CONST_TEST_DIR_DATA, CONST_TEST1_FILENAME)
    eduhub_df = pd.read_csv(file_path)

    (
        success,
        error,
        lab_dict,
        sub_dict,
        sub_new_list,
        sub_update_list,
    ) = update_edu_data(ENGINE, eduhub_df)

    assert success, error
    assert len(sub_new_list) == 2
    assert len(sub_update_list) == 0

    success, error, db_lab_dict = get_labs_dict(ENGINE)
    assert success, error

    assert len(db_lab_dict) == len(lab_dict)

    success, error, db_sub_dict = get_subs_dict(ENGINE)
    assert success, error

    assert len(db_sub_dict) == len(sub_dict)
Example #5
0
def test_importing_test_data():
    """
    imports initial test data
    """

    # 2 new
    file_path = os.path.join(CONST_TEST_DIR_DATA, CONST_TEST1_FILENAME)
    eduhub_df = pd.read_csv(file_path)

    success, error, _, _, sub_new_list, sub_update_list = update_edu_data(
        ENGINE, eduhub_df)

    assert success, error
    assert len(sub_new_list) == 2
    assert len(sub_update_list) == 0

    # 2 updates and 1 new
    file_path = os.path.join(CONST_TEST_DIR_DATA, CONST_TEST2_FILENAME)
    eduhub_df = pd.read_csv(file_path)

    success, error, _, _, sub_new_list, sub_update_list = update_edu_data(
        ENGINE, eduhub_df)

    assert success, error
    assert len(sub_new_list) == 1
    assert len(sub_update_list) == 2

    # 1 update
    file_path = os.path.join(CONST_TEST_DIR_DATA, CONST_TEST3_FILENAME)
    eduhub_df = pd.read_csv(file_path)

    # putting the data into the database
    success, error, _, _, sub_new_list, sub_update_list = update_edu_data(
        ENGINE, eduhub_df)

    assert success, error
    assert len(sub_new_list) == 0
    assert len(sub_update_list) == 3
def test_notify_subscriptions_2():
    """
    1 new and 2 updates

    """

    indv_email_sent_timestamp_utc = datetime(2020,
                                             10,
                                             21,
                                             11,
                                             20,
                                             tzinfo=timezone.utc)

    # importing data 1 new subscription, 2 updates
    file_path = os.path.join(CONST_TEST_DIR_DATA, CONST_TEST2_FILENAME)
    eduhub_df = pd.read_csv(file_path)

    (success, error, lab_dict, sub_dict, new_sub_list,
     upd_sub_list) = update_edu_data(ENGINE, eduhub_df)

    assert success, error
    assert len(new_sub_list) == 1
    assert len(upd_sub_list) == 2

    # sending notices
    success, error, counts = _notify_subscriptions(
        ENGINE,
        lab_dict,
        sub_dict,
        new_sub_list,
        upd_sub_list,
        timestamp_utc=indv_email_sent_timestamp_utc,
    )

    assert success, error
    assert counts[0] == len(new_sub_list)
    assert counts[1] == len(upd_sub_list)
    assert counts[2] == 0
    assert counts[3] == 0
def test_notify_subscriptions_3():
    """
    1 usage and 1 expiry

    """

    indv_email_sent_timestamp_utc = datetime(2020,
                                             10,
                                             22,
                                             10,
                                             10,
                                             tzinfo=timezone.utc)

    # 1 update - only usage
    file_path = os.path.join(CONST_TEST_DIR_DATA, CONST_TEST3_FILENAME)
    eduhub_df = pd.read_csv(file_path)

    (success, error, lab_dict, sub_dict, new_sub_list,
     upd_sub_list) = update_edu_data(ENGINE, eduhub_df)

    assert success, error
    assert len(new_sub_list) == 0
    assert len(upd_sub_list) == 3

    # sending notices
    success, error, counts = _notify_subscriptions(
        ENGINE,
        lab_dict,
        sub_dict,
        new_sub_list,
        upd_sub_list,
        timestamp_utc=indv_email_sent_timestamp_utc,
    )
    assert success, error
    assert counts[0] == 0
    assert counts[1] == 0
    assert counts[2] == 1
    assert counts[3] == 1
Example #8
0
def test_expiry_update():
    """
    Additional routine to test the expiry module.

    In this test we cover the scenatio when expiry date is updated.

    """

    current_date = datetime.datetime(2020, 11, 11)

    # updates
    file_path = os.path.join(CONST_TEST_DIR_DATA, CONST_TEST12_FILENAME)
    eduhub_df = pd.read_csv(file_path)

    (
        success,
        error,
        lab_dict,
        sub_dict,
        sub_new_list,
        sub_update_list,
    ) = update_edu_data(ENGINE, eduhub_df)

    assert success, error
    assert len(sub_new_list) == 0
    assert len(sub_update_list) == 4

    # send notifications
    success, error, count = notify_expire(
        ENGINE,
        lab_dict,
        sub_dict,
        sub_update_list,
        timestamp_utc=current_date,
    )
    assert success, error
    assert count == 3
def update_subscriptions(engine, args, timestamp_utc=None):
    """
    Sends individual notifications

    Arguments:
        engine - an sql engine instance
        args - command line arguments

    Returns:
        success - flag if the action was succesful
        error - error message
        counts - counts of notifications sent
            (new, update, time-based, usage-based)
    """

    success = True
    error = ""

    new_count = -1
    upd_count = -1
    time_count = -1
    usage_count = -1

    counts = (new_count, upd_count, time_count, usage_count)

    log("Notification service started", level=1)

    if hasattr(args, "input_df"):
        crawl_df = args.input_df

    elif hasattr(args, "input_file"):
        # read the crawl data in
        crawl_df = pd.read_csv(args.input_file)
        log("Read %d data entries" % (len(crawl_df)), level=1)
    else:
        success = False
        error = "Input data is not provided"

    if success:
        log("Appending DB with the new crawl data", level=1)
        (
            success,
            error,
            lab_dict,
            sub_dict,
            new_sub_list,
            upd_sub_list,
        ) = update_edu_data(engine, crawl_df)

    if success:
        log("Sending individual notification emails", level=1)
        success, error, counts = _notify_subscriptions(
            engine,
            lab_dict,
            sub_dict,
            new_sub_list,
            upd_sub_list,
            timestamp_utc=timestamp_utc,
        )

    return success, error, counts
Example #10
0
def test_expiry():
    """
    The main routine to test the expiry module
    """

    current_date = datetime.datetime(2020, 11, 10)

    # new subscriptions
    file_path = os.path.join(CONST_TEST_DIR_DATA, CONST_TEST6_FILENAME)
    eduhub_df = pd.read_csv(file_path)

    (
        success,
        error,
        lab_dict,
        sub_dict,
        sub_new_list,
        sub_update_list,
    ) = update_edu_data(ENGINE, eduhub_df)

    assert success, error
    assert len(sub_new_list) == 5
    assert len(sub_update_list) == 0

    # updates
    file_path = os.path.join(CONST_TEST_DIR_DATA, CONST_TEST7_FILENAME)
    eduhub_df = pd.read_csv(file_path)
    (
        success,
        error,
        lab_dict,
        sub_dict,
        sub_new_list,
        sub_update_list,
    ) = update_edu_data(ENGINE, eduhub_df)

    assert success, error
    assert len(sub_new_list) == 1
    assert len(sub_update_list) == 5

    # expires in the future (more than 30 days)
    sub_details = sub_update_list[0][1]
    expires, expiry_code, remain_days = _check_remaining_time(
        sub_details.subscription_expiry_date, current_date=current_date.date())
    assert not expires

    # expires in less than 30 days
    sub_details = sub_update_list[1][1]
    _, _, remain_days = _check_remaining_time(
        sub_details.subscription_expiry_date, current_date=current_date.date())
    success, error, html_content = indiv_email_expiry_notification(
        lab_dict, sub_dict, sub_details, remain_days)

    assert len(html_content) == 3666

    # expires in 1 day
    sub_details = sub_update_list[2][1]
    _, _, remain_days = _check_remaining_time(
        sub_details.subscription_expiry_date, current_date=current_date.date())
    success, error, html_content = indiv_email_expiry_notification(
        lab_dict, sub_dict, sub_details, remain_days)

    assert len(html_content) == 3664

    # send notifications
    success, error, count = notify_expire(ENGINE,
                                          lab_dict,
                                          sub_dict,
                                          sub_update_list,
                                          timestamp_utc=current_date)
    assert success, error
    assert count == 2
Example #11
0
def test_usage():
    """
    The main routine to test the budget module
    """

    # new subscriptions
    file_path = os.path.join(CONST_TEST_DIR_DATA, CONST_TEST8_FILENAME)
    eduhub_df = pd.read_csv(file_path)

    (
        success,
        error,
        lab_dict,
        sub_dict,
        sub_new_list,
        sub_update_list,
    ) = update_edu_data(ENGINE, eduhub_df)

    assert success, error
    assert len(sub_new_list) == 6
    assert len(sub_update_list) == 0

    # updates
    file_path = os.path.join(CONST_TEST_DIR_DATA, CONST_TEST9_FILENAME)
    eduhub_df = pd.read_csv(file_path)

    (
        success,
        error,
        lab_dict,
        sub_dict,
        sub_new_list,
        sub_update_list,
    ) = update_edu_data(ENGINE, eduhub_df)

    assert success, error
    assert len(sub_new_list) == 0
    assert len(sub_update_list) == 6

    # usage more than 50%
    sub_details = sub_update_list[2][1]
    notify, usage_code = _usage_notification(
        sub_details.handout_budget, sub_details.handout_consumed
    )
    assert notify
    assert usage_code == CONST_USAGE_CODE_50

    success, error, html_content = indiv_email_usage_notification(
        lab_dict, sub_dict, sub_details, usage_code
    )
    assert success, error
    assert len(html_content) == 3750

    # usage more than 75%
    sub_details = sub_update_list[3][1]
    notify, usage_code = _usage_notification(
        sub_details.handout_budget, sub_details.handout_consumed
    )
    assert notify
    assert usage_code == CONST_USAGE_CODE_75

    success, error, html_content = indiv_email_usage_notification(
        lab_dict, sub_dict, sub_details, usage_code
    )
    assert success, error

    # usage more than 90%
    sub_details = sub_update_list[4][1]
    notify, usage_code = _usage_notification(
        sub_details.handout_budget, sub_details.handout_consumed
    )
    assert notify
    assert usage_code == CONST_USAGE_CODE_90

    success, error, html_content = indiv_email_usage_notification(
        lab_dict, sub_dict, sub_details, usage_code
    )
    assert success, error

    # usage more than 95%
    sub_details = sub_update_list[5][1]
    notify, usage_code = _usage_notification(
        sub_details.handout_budget, sub_details.handout_consumed
    )
    assert notify
    assert usage_code == CONST_USAGE_CODE_95

    success, error, html_content = indiv_email_usage_notification(
        lab_dict, sub_dict, sub_details, usage_code
    )
    assert success, error

    # send notifications
    success, error, count = notify_usage(
        ENGINE, lab_dict, sub_dict, sub_update_list
    )
    assert success, error
    assert count == 4

    # updates
    file_path = os.path.join(CONST_TEST_DIR_DATA, CONST_TEST10_FILENAME)
    eduhub_df = pd.read_csv(file_path)

    (
        success,
        error,
        lab_dict,
        sub_dict,
        sub_new_list,
        sub_update_list,
    ) = update_edu_data(ENGINE, eduhub_df)

    assert success, error
    assert len(sub_new_list) == 0
    assert len(sub_update_list) == 2

    # send notifications
    #   1 changed code from 50 to 90
    #   the other did not change its code
    success, error, count = notify_usage(
        ENGINE, lab_dict, sub_dict, sub_update_list
    )
    assert success, error
    assert count == 1
def test_summary_new():
    """
    Testing summary 1 subscription
    """

    prev_summary_timestamp_utc = None
    indv_email_sent_timestamp_utc = datetime(2020,
                                             10,
                                             20,
                                             10,
                                             20,
                                             tzinfo=timezone.utc)
    new_summary_timestamp_utc = datetime(2020,
                                         10,
                                         20,
                                         10,
                                         30,
                                         tzinfo=timezone.utc)

    file_path = os.path.join(CONST_TEST_DIR_DATA, CONST_TEST1_FILENAME)
    eduhub_df = pd.read_csv(file_path)

    # importing data
    (
        success,
        error,
        lab_dict,
        sub_dict,
        new_sub_list,
        upd_sub_list,
    ) = update_edu_data(ENGINE, eduhub_df)

    assert success, error
    assert len(new_sub_list) == 2
    assert len(upd_sub_list) == 0

    # sending notices
    success, error, counts = _notify_subscriptions(
        ENGINE,
        lab_dict,
        sub_dict,
        new_sub_list,
        upd_sub_list,
        timestamp_utc=indv_email_sent_timestamp_utc,
    )

    assert success, error
    assert counts[0] == len(new_sub_list)
    assert counts[1] == len(upd_sub_list)
    assert counts[2] == 0
    assert counts[3] == 0

    # finding new subscriptions for the summary
    success, error, new_subs = _find_new_subs(ENGINE,
                                              prev_summary_timestamp_utc)
    assert success, error
    assert len(new_subs) == counts[0]

    # finding updated subscriptions for the summary
    success, error, upd_subs = _find_upd_subs(ENGINE,
                                              prev_summary_timestamp_utc)
    assert success, error
    assert len(upd_subs) == counts[1]

    # finding notifications sent for the summary
    success, error, sent_noti = _find_sent_notifications(
        ENGINE, prev_summary_timestamp_utc)
    assert success, error
    assert len(sent_noti) == counts[0]

    # preps a summary email
    success, error, html_content = _prep_summary_email(
        ENGINE, timestamp_utc=new_summary_timestamp_utc)
    assert success, error
    assert len(html_content) in (2854, 2851)

    # preps and sends out summary email
    success, error = summary_email(ENGINE,
                                   timestamp_utc=new_summary_timestamp_utc)
    assert success, error
def test_summary_upd_2():
    """
    Testing summary 1 usage and 1 expiry notification

    """

    success, error, prev_summary_timestamp_utc = get_latest_log_timestamp(
        ENGINE)
    assert success, error

    indv_email_sent_timestamp_utc = datetime(2020,
                                             10,
                                             22,
                                             10,
                                             10,
                                             tzinfo=timezone.utc)
    new_summary_timestamp_utc = datetime(2020,
                                         10,
                                         22,
                                         11,
                                         30,
                                         tzinfo=timezone.utc)

    # 1 update - only usage
    file_path = os.path.join(CONST_TEST_DIR_DATA, CONST_TEST3_FILENAME)
    eduhub_df = pd.read_csv(file_path)

    (
        success,
        error,
        lab_dict,
        sub_dict,
        new_sub_list,
        upd_sub_list,
    ) = update_edu_data(ENGINE, eduhub_df)

    assert success, error
    assert len(new_sub_list) == 0
    assert len(upd_sub_list) == 3

    # sending notices
    success, error, counts = _notify_subscriptions(
        ENGINE,
        lab_dict,
        sub_dict,
        new_sub_list,
        upd_sub_list,
        timestamp_utc=indv_email_sent_timestamp_utc,
    )
    assert success, error
    assert counts[0] == 0
    assert counts[1] == 0
    assert counts[2] == 1
    assert counts[3] == 1

    # finding new subscriptions for the summary
    success, error, new_subs = _find_new_subs(ENGINE,
                                              prev_summary_timestamp_utc)
    assert success, error
    assert len(new_subs) == counts[0]

    # finding updated subscriptions for the summary
    success, error, new_subs = _find_upd_subs(ENGINE,
                                              prev_summary_timestamp_utc)
    assert success, error
    assert len(new_subs) == counts[1]

    # finding notifications sent for the summary
    success, error, sent_noti = _find_sent_notifications(
        ENGINE, prev_summary_timestamp_utc)
    assert success, error
    assert len(sent_noti) == counts[0] + counts[1] + counts[2] + counts[3]

    # preps a summary email
    success, error, html_content = _prep_summary_email(
        ENGINE, timestamp_utc=new_summary_timestamp_utc)
    assert success, error
    assert len(html_content) in (1889, 1885)

    # preps and sends out summary email
    success, error = summary_email(ENGINE,
                                   timestamp_utc=new_summary_timestamp_utc)
    assert success, error