Exemplo n.º 1
0
def setup_users(now=None):
    now = now or datetime.utcnow()

    # mark specific events for single user with user_string as user_id
    event_mappings = {
        'i': 'user:logged_in',
        'o': 'user:logged_out',
        'c': 'user:created',
        'u': 'user:updated',
        'd': 'user:deleted',
    }

    users = [
        ('iocud', 11),
        ('iocd',  22),
        ('iou',   33),
        ('icd',   44),
        ('io',    55),
        ('ocud',  66),
        ('cud',   77),
        ('od',    88),
        ('d',     99)
    ]

    for user in users:
        for u in user[0]:
            event_name = event_mappings.get(u, None)
            if event_name:
                mark_event(event_name, user[1])

    return users
Exemplo n.º 2
0
def test_authenticated_redis(app, auth_bitmap):
    assert auth_bitmap.redis_auth == 'foobared'

    # Test to make sure auth_bitmap setup with different set of params
    assert auth_bitmap.redis_url != app.config['BITMAPIST_REDIS_URL']

    mark_event('active', 42)
    assert 42 in MonthEvents('active', now.year, now.month)

    unmark_event('active', 42)
    assert 42 not in MonthEvents('active', now.year, now.month)
Exemplo n.º 3
0
def setup_users(now=None):
    now = now or datetime.utcnow()

    # mark specific events for single user with user_string as user_id
    event_mappings = {
        'i': 'user:logged_in',
        'o': 'user:logged_out',
        'c': 'user:created',
        'u': 'user:updated',
        'd': 'user:deleted',
    }

    users = [('iocud', 11), ('iocd', 22), ('iou', 33), ('icd', 44), ('io', 55),
             ('ocud', 66), ('cud', 77), ('od', 88), ('d', 99)]

    for user in users:
        for u in user[0]:
            event_name = event_mappings.get(u, None)
            if event_name:
                mark_event(event_name, user[1])

    return users
Exemplo n.º 4
0
def test_get_cohort_contents():
    # Generate events to ensure specific cohort returns

    # for cohort @days, m=4, n=4:
    #     6: [[5]   [0]   [0]   [1]   ]
    #     7: [[2]   [1]   [2]   [None]]
    #     0: [[None][None][None][None]]
    #     5: [[4]   [None][None][None]]
    #     --   ----  ----  ----  ----
    #   T 18  11     1     2     1

    # ^^^ as_percent (not with replacement):
    #     6: [[83%] [0%]  [0%]  [17%] ]
    #     7: [[29%] [14%] [29%] [None]]
    #     0: [[None][None][None][None]]
    #     5: [[80%] [None][None][None]]
    #     --   ----  ----  ----  ----
    #   T 18  11     1     2     1

    # ^^^ with_replacement (not as percent):
    #     6: [[5]   [3]   [0]   [1]   ]
    #     7: [[2]   [2]   [4]   [None]]
    #     0: [[None][None][None][None]]
    #     5: [[4]   [None][None][None]]
    #     --   ----  ----  ----  ----
    #   T 18  11     5     4     1

    # TO BUILD: 18 users
    # [[5+0] [0+3] [0+0] [1+0] ]
    # [[2+0] [1+1] [2+2] [None]]
    # [[None][None][None][None]]
    # [[4+0] [None][None][None]]

    # construct events/history based on desired outcomes (see commented tables)

    event1 = 'user:popped'
    event2 = 'user:locked'

    dates = [now - timedelta(days=d) for d in range(4)]
    dates.reverse()

    # users = range(100, 118)
    users_event1 = [
        range(100, 106),
        range(106, 113),
        [],
        range(113, 118)
    ]
    users_event2 = [
        [[100, 101, 102, 103, 104], [100, 101, 102], [], [105]],
        [[106, 107], [107, 108], [106, 107, 109, 110], []],
        [[], [], [], []],
        [[113, 114, 115, 116], [], [], []]
    ]

    # mark constructed events
    for i, row_date in enumerate(dates):
        for user_id in users_event1[i]:
            mark_event(event1, user_id, now=row_date)

        for j in range(len(dates)):
            col_date = row_date + timedelta(days=j)
            for user_id in users_event2[i][j]:
                mark_event(event2, user_id, now=col_date)

    # establish expected cohort returns
    expected_c0 = [
        [5, 0, 0, 1],
        [2, 1, 2, None],
        [None, None, None, None],
        [4, None, None, None]
    ]

    # expected_cp = [
    #     [0, 0, 0, 0],
    #     [0, 0, 0, None],
    #     [0, 0, None, None],
    #     [0, None, None, None]
    # ]

    expected_cr = [
        [5, 3, 0, 1],
        [2, 2, 4, None],
        [None, None, None, None],
        [4, None, None, None]
    ]

    expected_totals = [6, 7, 0, 5]

    # get cohort(s)
    c0, d0, t0 = get_cohort(event1, event2, time_group='days', num_rows=4, num_cols=4)
    # cp, dp, tp = get_cohort(event1, event2, time_group='days', num_rows=4, num_cols=4,
    #                         as_percent=True)
    cr, dr, tr = get_cohort(event1, event2, time_group='days', num_rows=4, num_cols=4,
                            with_replacement=True)

    # assert that returned values match expected values
    assert c0 == expected_c0
    # assert cp == expected_cp
    assert cr == expected_cr

    assert t0 == expected_totals
    # assert tp == expected_totals
    assert tr == expected_totals
Exemplo n.º 5
0
def test_unmark_function(app, client):
    mark_event('active', 126)
    assert 126 in MonthEvents('active', now.year, now.month)

    unmark_event('active', 126)
    assert 126 not in MonthEvents('active', now.year, now.month)
Exemplo n.º 6
0
def test_mark_function(app, client):
    mark_event('active', 125)
    assert 125 in MonthEvents('active', now.year, now.month)
Exemplo n.º 7
0
def test_unmark_function(app, client):
    mark_event('active', 126)
    assert 126 in MonthEvents('active', now.year, now.month)

    unmark_event('active', 126)
    assert 126 not in MonthEvents('active', now.year, now.month)
Exemplo n.º 8
0
def test_mark_function(app, client):
    mark_event('active', 125)
    assert 125 in MonthEvents('active', now.year, now.month)
Exemplo n.º 9
0
def test_get_cohort_contents():
    # Generate events to ensure specific cohort returns

    # for cohort @days, m=4, n=4:
    #     6: [[5]   [0]   [0]   [1]   ]
    #     7: [[2]   [1]   [2]   [None]]
    #     0: [[None][None][None][None]]
    #     5: [[4]   [None][None][None]]
    #     --   ----  ----  ----  ----
    #   T 18  11     1     2     1

    # ^^^ as_percent (not with replacement):
    #     6: [[83%] [0%]  [0%]  [17%] ]
    #     7: [[29%] [14%] [29%] [None]]
    #     0: [[None][None][None][None]]
    #     5: [[80%] [None][None][None]]
    #     --   ----  ----  ----  ----
    #   T 18  11     1     2     1

    # ^^^ with_replacement (not as percent):
    #     6: [[5]   [3]   [0]   [1]   ]
    #     7: [[2]   [2]   [4]   [None]]
    #     0: [[None][None][None][None]]
    #     5: [[4]   [None][None][None]]
    #     --   ----  ----  ----  ----
    #   T 18  11     5     4     1

    # TO BUILD: 18 users
    # [[5+0] [0+3] [0+0] [1+0] ]
    # [[2+0] [1+1] [2+2] [None]]
    # [[None][None][None][None]]
    # [[4+0] [None][None][None]]

    # construct events/history based on desired outcomes (see commented tables)

    event1 = 'user:popped'
    event2 = 'user:locked'

    dates = [now - timedelta(days=d) for d in range(4)]
    dates.reverse()

    # users = range(100, 118)
    users_event1 = [range(100, 106), range(106, 113), [], range(113, 118)]
    users_event2 = [[[100, 101, 102, 103, 104], [100, 101, 102], [], [105]],
                    [[106, 107], [107, 108], [106, 107, 109, 110], []],
                    [[], [], [], []], [[113, 114, 115, 116], [], [], []]]

    # mark constructed events
    for i, row_date in enumerate(dates):
        for user_id in users_event1[i]:
            mark_event(event1, user_id, now=row_date)

        for j in range(len(dates)):
            col_date = row_date + timedelta(days=j)
            for user_id in users_event2[i][j]:
                mark_event(event2, user_id, now=col_date)

    # establish expected cohort returns
    expected_c0 = [[5, 0, 0, 1], [2, 1, 2, None], [None, None, None, None],
                   [4, None, None, None]]

    # expected_cp = [
    #     [0, 0, 0, 0],
    #     [0, 0, 0, None],
    #     [0, 0, None, None],
    #     [0, None, None, None]
    # ]

    expected_cr = [[5, 3, 0, 1], [2, 2, 4, None], [None, None, None, None],
                   [4, None, None, None]]

    expected_totals = [6, 7, 0, 5]

    # get cohort(s)
    c0, d0, t0 = get_cohort(event1,
                            event2,
                            time_group='days',
                            num_rows=4,
                            num_cols=4)
    # cp, dp, tp = get_cohort(event1, event2, time_group='days', num_rows=4, num_cols=4,
    #                         as_percent=True)
    cr, dr, tr = get_cohort(event1,
                            event2,
                            time_group='days',
                            num_rows=4,
                            num_cols=4,
                            with_replacement=True)

    # assert that returned values match expected values
    assert c0 == expected_c0
    # assert cp == expected_cp
    assert cr == expected_cr

    assert t0 == expected_totals
    # assert tp == expected_totals
    assert tr == expected_totals