"""Tests from Lab 02. Run: pytest test_entities_practice.py"""
import math

from entities_practice import digit_runs, entity_accuracy, entity_correct


def test_digit_runs_join_separated_groups():
    assert digit_runs("account 4471-2209 to my savings") == ["44712209"]
    assert digit_runs("account 4471 2209") == ["44712209"]


def test_digit_words_count_as_digits():
    assert digit_runs("account four four seven one two two zero nine") == ["44712209"]
    assert digit_runs("card ending in oh three five eight") == ["0358"]


def test_other_words_break_a_run():
    assert digit_runs("call 555 210 8847 or extension 12") == ["5552108847", "12"]
    assert digit_runs("no numbers here") == []


def test_number_formatting_is_still_correct():
    # Lab 02, Exercise 3: 2 WER errors, but the entity is right
    assert entity_correct("digits", "44712209", "transfer from account 44712209 to my savings")
    assert entity_correct("digits", "44712209", "from account four four seven one, two two zero nine")


def test_a_lost_leading_zero_is_wrong():
    # The WER normalizer would turn 0358 into 358 and call this perfect
    assert not entity_correct("digits", "0358", "my card ending in 358 was charged twice")


def test_a_longer_number_is_wrong():
    assert not entity_correct("digits", "44712209", "account 447122090")


def test_names_need_exact_spelling():
    assert entity_correct("name", "Siobhan", "I would like to speak with siobhan about my mortgage")
    assert not entity_correct("name", "Siobhan", "I would like to speak with Chavon about my mortgage")
    assert not entity_correct("name", "lisinopril", "the lysinopril can be taken in the morning")


def test_names_match_whole_words_only():
    assert not entity_correct("name", "Tran", "please transfer it")
    assert entity_correct("name", "Anh Tran", "escalated to Anh Tran on the networking team")


def test_entity_accuracy():
    items = [
        ("digits", "44712209", "account 4471 2209"),
        ("digits", "0358", "card ending in 358"),
        ("name", "Siobhan", "speak with Chavon"),
        ("name", "Nguyen", "Siobhan Nguyen about my mortgage"),
    ]
    assert math.isclose(entity_accuracy(items), 0.5)
    assert entity_accuracy([]) == 0.0
