示例#1
0
def test_word_calendar_weeks_returns_list_of_weeks():
    now = datetime.datetime.utcnow()
    items = [
        Item(created=now),
        Item(created=now - datetime.timedelta(days=7)),
    ]
    cal, maximum = get_weekly_word_calendar(items)
    assert len(cal) == 12
    assert len(cal[0]) == 7
def test_get_items_for_user(session):
    u1 = User(id=1)
    u2 = User(id=2)
    session.add_all(
        [
            u1,
            u2,
            Item(id=1, user=u1),
            Item(id=2, user=u2),
        ]
    )
    items = get_items_for_user(session, u1).all()
    assert len(items) == 1 and items[0].id == 1
def test_update_item_adds_event(session):
    item = Item(id=1)
    session.add(item)
    session.commit()

    item = update_item(session, item, grade=1)
    assert item.events[0].grade == 1
def test_item_activate_reject_anonymous_user(flask_anonymous_client, session):
    session.add(Item(id=1, user_id=1))
    session.commit()
    response = flask_anonymous_client.patch(url_for("item_activate",
                                                    item_id=1),
                                            json={"is_active": False})
    assert response.status_code == 401
示例#5
0
def test_word_calendar_ends_on_a_sunday():
    now = datetime.datetime.utcnow()
    monday = now - datetime.timedelta(days=now.weekday())
    sunday = monday.date() + datetime.timedelta(days=6)
    items = [Item(created=monday)]
    cal, _ = get_word_calendar(items)
    assert cal[-1] == (sunday, 0)
def test_include_inactive_items(session):
    user = User(id=1)
    session.add_all(
        [
            user,
            Item(id=1, user=user),
            Item(id=2, user=user, next_iteration=datetime.datetime.utcnow()),
            Item(
                id=3,
                user=user,
                next_iteration=datetime.datetime.utcnow(),
                is_active=False,
            ),
        ]
    )
    items = get_items_for_user(session, user, include_inactive=True).all()
    assert len(items) == 3
def test_update_item_sets_last_learned_timestamp(session):
    item = Item(id=1)
    session.add(item)
    session.commit()
    item = update_item(session, item, grade=1)
    assert abs(item.last_learned - datetime.datetime.utcnow()) <= datetime.timedelta(
        seconds=1
    )
示例#8
0
def test_word_calendar_with_outdated_items_only():
    now = datetime.datetime.utcnow()
    items = [
        Item(created=now - datetime.timedelta(days=13 * 7)),
    ]
    cal, maximum = get_word_calendar(items)
    assert len(cal) == 7 * 12
    assert maximum == 0
def test_item_activate_is_persistent(flask_token_client, session):
    item = Item(id=1, user_id=1, word="foo", is_active=True)
    session.add(item)
    session.commit()

    flask_token_client.patch(url_for("item_activate", item_id=1),
                             json={"is_active": False})

    assert item.is_active is False
示例#10
0
def test_train_view_post_grade_increments_repitition_number(
        flask_client, session):
    item = Item(user_id=1, word="foo")
    session.add(item)
    session.commit()
    response = flask_client.post(url_for("train"), data={"grade": "5"})
    session.refresh(item)
    assert response.status_code == 302
    assert item.repitition_number == 1
def test_update_item_sets_next_iteration_datetime(session):
    now = datetime.datetime.utcnow()

    item = Item(id=1)
    session.add(item)
    session.commit()

    item = update_item(session, item, grade=1)

    assert item.next_iteration > now
示例#12
0
def test_item_activate(flask_token_client, session):
    item = Item(id=1, user_id=1, word="foo", is_active=True)
    session.add(item)
    session.commit()

    response = flask_token_client.patch(url_for("item_activate", item_id=1),
                                        json={"is_active": False})

    assert response.status_code == 200
    assert response.json == {"id": 1, "word": "foo", "is_active": False}
def test_get_items_sorted_by_due_date(session):
    user = User(id=1)
    session.add_all(
        [
            user,
            Item(
                id=1,
                user=user,
                next_iteration=datetime.datetime.utcnow() + datetime.timedelta(days=10),
            ),
            Item(
                id=2,
                user=user,
                next_iteration=datetime.datetime.utcnow() + datetime.timedelta(days=1),
            ),
        ]
    )
    items = get_items_for_user(session, user).all()
    assert [x.id for x in items] == [2, 1]
def test_get_todo_items(session):
    user = User(id=1)
    session.add_all(
        [
            user,
            Item(id=1, user=user, next_iteration=None),
            Item(
                id=2,
                user=user,
                next_iteration=datetime.datetime.utcnow()
                - datetime.timedelta(seconds=1),
            ),
            Item(
                id=3,
                user=user,
                next_iteration=datetime.datetime.utcnow() + datetime.timedelta(days=1),
            ),
        ]
    )
    items = get_items_for_user(session, user, todo=True).all()
    assert [x.id for x in items] == [1, 2]
示例#15
0
def test_train_view_cycles_through_pending_items(flask_client, session):
    url = url_for("train")
    now = datetime.datetime.utcnow()

    session.add_all([
        Item(
            user_id=1,
            word="barbazfoo",
            next_iteration=now - datetime.timedelta(days=2),
        ),
        Item(
            user_id=1,
            word="bazfoobar",
            next_iteration=now - datetime.timedelta(days=1),
        ),
        User(id=2),
        Item(
            user_id=2,  # this item should be skipped!
            word="foobarbaz",
            next_iteration=now - datetime.timedelta(days=3),
        ),
    ])
    session.commit()

    response = flask_client.get(url)
    assert b"barbazfoo" in response.data
    assert b"foobarbaz" not in response.data

    response = flask_client.post(url, data={"grade": "5"})
    assert response.status_code == 302

    response = flask_client.get(url)
    assert b"bazfoobar" in response.data
    assert b"foobarbaz" not in response.data

    response = flask_client.post(url, data={"grade": "5"})
    assert response.status_code == 302

    response = flask_client.get(url)
    assert b"foobarbaz" not in response.data
示例#16
0
def test_word_calendar_is_a_list_of_dates_and_counts():
    now = datetime.datetime.utcnow()
    monday = now - datetime.timedelta(now.weekday())
    items = [Item(created=monday) for _ in range(3)]
    cal, _ = get_word_calendar(items)
    assert cal[-7] == (monday.date(), 3)
示例#17
0
def test_word_calendar_covers_12_weeks():
    now = datetime.datetime.utcnow()
    items = [Item(created=now)]
    cal, _ = get_word_calendar(items)
    assert len(cal) == 7 * 12
示例#18
0
def test_train_view_shows_next_item(flask_client, session):
    session.add(Item(user_id=1, word="foobarbaz"))
    session.commit()
    response = flask_client.get(url_for("train"))
    assert response.status_code == 200
    assert b"foobarbaz" in response.data
示例#19
0
def test_items_list_view(flask_client, session):
    session.add(Item(user_id=1, word="foobarbaz"))
    session.commit()
    response = flask_client.get(url_for("item_list"))
    assert response.status_code == 200
    assert b"foobarbaz" in response.data
示例#20
0
def test_word_calender_returns_calendar_maximum_value():
    now = datetime.datetime.utcnow()
    monday = now - datetime.timedelta(now.weekday())
    items = [Item(created=monday) for _ in range(3)]
    _, maximum = get_word_calendar(items)
    assert maximum == 3
示例#21
0
def test_word_calendar_starts_on_a_monday():
    now = datetime.datetime.utcnow()
    tuesday = now + datetime.timedelta(days=1 - now.weekday())
    items = [Item(created=tuesday)]
    cal, _ = get_word_calendar(items)
    assert cal[0][0].weekday() == 0