import pytest

from dicts import (
    RAW_ROWS, QUESTIONS, Response,
    to_response, to_responses,
    blanks_per_question, points_earned, score_by_student,
)


# ---- Task 1 ----

def test_raw_rows_loaded():
    assert len(RAW_ROWS) == 9
    assert RAW_ROWS[0] == {"student": "ana", "qid": "q1", "answer": "4"}


# ---- Task 2 ----

# Write your own test function for ans_is_blank here.


# ---- Task 3 ----  (add your own tests: an all-spaces answer, a mixed-case answer)

def test_to_response_clean():
    assert to_response(RAW_ROWS[0]) == Response("ana", "q1", "4")


def test_to_response_csv_rows():
    assert to_response(RAW_ROWS[1]) == Response("ana", "q2", "def")   # stripped, lowercased
    assert to_response(RAW_ROWS[4]) == Response("ben", "q1", None)    # empty answer
    assert to_response(RAW_ROWS[8]) == Response("cy", "q4", None)     # short row


def test_to_responses_length():
    assert len(to_responses(RAW_ROWS)) == 9


# ---- Task 4 ----

def test_blanks_per_question():
    rs = to_responses(RAW_ROWS)
    assert blanks_per_question(rs) == {"q1": 1, "q4": 1}
    assert blanks_per_question([]) == {}


# ---- Task 5 ----

def test_points_earned():
    assert points_earned(Response("ana", "q4", "float"), QUESTIONS) == 2
    assert points_earned(Response("ana", "q4", "int"), QUESTIONS) == 0
    assert points_earned(Response("ana", "q4", None), QUESTIONS) == 0


# ---- Task 6 ----

def test_score_by_student():
    assert score_by_student(to_responses(RAW_ROWS), QUESTIONS) == {"ana": 5, "ben": 2, "cy": 1}
    assert score_by_student([], QUESTIONS) == {}
