"""
La reescritura del coping en los pedidos guardados: de interruptor más estilo a
una sola respuesta, y la vuelta.

Se prueban las dos funciones que convierten las respuestas de un contenedor,
que es donde está la regla. El recorrido de las filas es el mismo que el de
`jobs.0002_summary_keys_in_english`.
"""

from importlib import import_module

migration = import_module('apps.ai.migrations.0013_coping_as_one_answer')

FIELD = {'material': 'marble', 'color': 'white', 'size': '24x24'}


def test_the_switch_on_becomes_its_style():
    answers = {**FIELD, 'coping': True, 'coping_style': 'straight_cut'}

    assert migration._one_answer(answers) == {**FIELD, 'coping': 'straight_cut'}


def test_the_switch_off_becomes_leaving_the_edge_as_it_is():
    assert migration._one_answer({**FIELD, 'coping': False}) == {**FIELD, 'coping': 'keep'}


def test_the_switch_on_without_a_style_repeats_the_field_pavers():
    """La forma vieja lo permitía; la lista no tiene un «coping sin estilo»."""
    assert migration._one_answer({**FIELD, 'coping': True}) == {
        **FIELD,
        'coping': 'match_field_pavers',
    }


def test_a_request_without_coping_is_left_alone():
    assert migration._one_answer(FIELD) == FIELD


def test_the_order_of_the_answers_survives():
    answers = {'material': 'marble', 'coping': True, 'coping_style': 'bullnose', 'seal': 'matte'}

    assert list(migration._one_answer(answers)) == ['material', 'coping', 'seal']


def test_running_it_twice_changes_nothing():
    once = migration._one_answer({**FIELD, 'coping': True, 'coping_style': 'tumbled'})

    assert migration._one_answer(once) == once


def test_the_way_back_restores_the_switch_and_its_style():
    assert migration._two_answers({**FIELD, 'coping': 'tumbled'}) == {
        **FIELD,
        'coping': True,
        'coping_style': 'tumbled',
    }
    assert migration._two_answers({**FIELD, 'coping': 'keep'}) == {**FIELD, 'coping': False}


def test_the_way_back_turns_no_coping_into_the_switch_off():
    """`none` no existía en la forma vieja: vuelve como apagado, que es lo más cercano."""
    assert migration._two_answers({**FIELD, 'coping': 'none'}) == {**FIELD, 'coping': False}
