Exemple #1
0
def test_response(db_conn, responses_table):
    """
    Expect to record the user's response.
    """

    response_data = {
        'user_id': 'A',
        'card_id': 'BC',
        'unit_id': 'RM',
        'score': 0.9,
        'learned': 0.9,
    }
    response, errors = insert_response(response_data, db_conn)
    assert len(errors) == 1
    response_data['response'] = 42
    response, errors = insert_response(response_data, db_conn)
    assert len(errors) == 0
Exemple #2
0
def test_unit(db_conn, responses_table):
    """
    Expect to require a unit ID.
    """

    response_data = {
        'user_id': 'A',
        'card_id': 'BC',
        'response': 42,
        'score': 0.9,
        'learned': 0.9,
    }
    response, errors = insert_response(response_data, db_conn)
    assert len(errors) == 1
    response_data['unit_id'] = 'A24JLD'
    response, errors = insert_response(response_data, db_conn)
    assert len(errors) == 0
Exemple #3
0
def test_insert_response(db_conn):
    create_response_test_data(db_conn)
    response, errors = insert_response(
        db_conn, {
            'user_id': user_a_uuid,
            'card_id': test_card_uuid,
            'unit_id': test_unit_uuid,
            'response': convert_uuid_to_slug(test_card_option1_uuid),
            'score': 1,
            'learned': 0.6,
        })
    assert not errors
    assert response
Exemple #4
0
def test_score(db_conn, responses_table):
    """
    Expect to have a score between 0 and 1 (including).
    """

    response_data = {
        'user_id': 'A',
        'card_id': 'BC',
        'unit_id': 'RM',
        'response': 42,
        'learned': 0.9,
    }
    response, errors = insert_response(response_data, db_conn)
    assert len(errors) == 1
    response_data['score'] = 1.1
    response, errors = insert_response(response_data, db_conn)
    assert len(errors) == 1
    response_data['score'] = 0
    response, errors = insert_response(response_data, db_conn)
    assert len(errors) == 0
    response_data['score'] = 1
    response, errors = insert_response(response_data, db_conn)
    assert len(errors) == 0
Exemple #5
0
def test_created(db_conn, responses_table):
    """
    Expect to have a created date.
    """

    response, errors = insert_response(
        {
            'user_id': 'A',
            'card_id': 'BC',
            'unit_id': 'RM',
            'response': 42,
            'score': 0.9,
            'learned': 0.9,
        }, db_conn)
    assert len(errors) == 0
    assert response['created']
Exemple #6
0
def update(db_conn, user, card, response):
    """
    Update the card's parameters (and its parents')
    when given a response.
    """

    # TODO-3 split up into smaller methods

    if not card.has_assessment():
        return {
            'response': {},
            'feedback': '',
        }

    errors = card.validate_response(response)
    if errors:
        return {'errors': errors}

    score, feedback = card.score_response(response)
    response = {
        'user_id': user['id'],
        'card_id': card['entity_id'],
        'unit_id': card['unit_id'],
        'response': response,
        'score': score,
    }

    card_parameters = get_card_parameters(
        {'entity_id': card['entity_id']},
        db_conn
    ) or {}
    previous_response = get_latest_response(user['id'],
                                            card['unit_id'],
                                            db_conn)

    now = time()
    time_delta = now - (int(previous_response['created'].strftime("%s"))
                        if previous_response else now)

    learned = (previous_response['learned']
               if previous_response else init_learned)
    guess_distribution = get_distribution(card_parameters, 'guess')
    slip_distribution = get_distribution(card_parameters, 'slip')

    updates = formula_update(score, time_delta,
                             learned, guess_distribution, slip_distribution)

    response['learned'] = updates['learned']
    response, errors = insert_response(response, db_conn)
    if errors:
        return {'errors': errors, 'feedback': feedback}

    updated_card_parameters = {
        'entity_id': card['entity_id'],
        'guess_distribution':
            bundle_distribution(updates['guess_distribution']),
        'slip_distribution':
            bundle_distribution(updates['slip_distribution']),
    }
    if card_parameters.get('id'):
        _, errors = update_card_parameters(
            card_parameters,
            updated_card_parameters,
            db_conn
        )
    else:
        _, errors = insert_card_parameters(
            updated_card_parameters,
            db_conn
        )

    if errors:
        return {'errors': errors, 'feedback': feedback}

    return {'response': response, 'feedback': feedback}