"""
La forma del overlay en los pedidos guardados: de `pattern` a `shape`, las
muestras a la carpeta de las irregulares, y la vuelta.

Se prueban las funciones que convierten una respuesta y una ruta, que es donde
está la regla. El recorrido de las filas es el mismo que el de
`ai.0013_coping_as_one_answer`.
"""

from importlib import import_module

migration = import_module('apps.ai.migrations.0014_overlay_shape')

STONE = {'grout_color': 'sand', 'stone_color': 'shadow_stone'}


def test_the_old_pattern_becomes_the_irregular_shape():
    """Todas las fotos eran irregulares: es lo que la persona vio al elegir."""
    answers = {'pattern': 'natural_stone_cut', **STONE}

    assert migration._to_shape(answers) == {'shape': 'irregular', **STONE}


def test_the_order_of_the_answers_survives():
    answers = {'grout_color': 'sand', 'pattern': 'natural_stone_cut', 'stone_color': 'mocha_mist'}

    assert list(migration._to_shape(answers)) == ['grout_color', 'shape', 'stone_color']


def test_a_request_without_pattern_is_left_alone():
    assert migration._to_shape(STONE) == STONE


def test_running_it_twice_changes_nothing():
    once = migration._to_shape({'pattern': 'natural_stone_cut', **STONE})

    assert migration._to_shape(once) == once


def test_the_way_back_turns_any_shape_into_the_old_pattern():
    """La forma vieja no distinguía `regular`: vuelve como el único patrón que había."""
    assert migration._to_pattern({'shape': 'regular', **STONE}) == {
        'pattern': 'natural_stone_cut',
        **STONE,
    }


def test_a_stored_sample_moves_to_the_irregular_folder():
    assert (
        migration._to_shape_folder('ai/concrete_overlay/stone_color/gray_escape.jpg')
        == 'ai/concrete_overlay/stone_color/irregular/gray_escape.jpg'
    )


def test_a_sample_already_in_a_shape_folder_or_from_another_field_stays():
    for path in (
        'ai/concrete_overlay/stone_color/irregular/gray_escape.jpg',
        'ai/concrete_overlay/stone_color/regular/gray_escape.jpg',
        'ai/pool_renovation/plaster_color/reference/sky_blue.jpg',
    ):
        assert migration._to_shape_folder(path) == path


def test_the_way_back_returns_the_sample_to_the_flat_folder():
    assert (
        migration._to_flat_folder('ai/concrete_overlay/stone_color/irregular/gray_escape.jpg')
        == 'ai/concrete_overlay/stone_color/gray_escape.jpg'
    )
