import pytest

from lang_main import io

CONTENT = 'test_lang_main'


@pytest.mark.parametrize(
    'overwrite',
    [True, False],
)
def test_create_saving_folder(tmp_path, overwrite):
    target_dir = tmp_path / 'test'
    assert not target_dir.exists()
    io.create_saving_folder(target_dir, overwrite_existing=overwrite)
    assert target_dir.exists()
    assert target_dir.is_dir()
    io.create_saving_folder(str(target_dir), overwrite_existing=overwrite)
    assert target_dir.exists()
    assert target_dir.is_dir()


def test_save_load(tmp_path):
    save_pth = tmp_path / 'test_lang_main.pkl'
    io.save_pickle(CONTENT, save_pth)
    loaded = io.load_pickle(save_pth)
    assert loaded == CONTENT
    b64_str = io.encode_to_base64_str(CONTENT)
    b64_str_file = io.encode_file_to_base64_str(save_pth)
    assert b64_str == b64_str_file
    b64_decoded = io.decode_from_base64_str(b64_str)
    assert b64_decoded == CONTENT
    b64_decoded_file = io.decode_from_base64_str(b64_str_file)
    assert b64_decoded_file == CONTENT


def test_get_entry_point(tmp_path):
    save_pth = tmp_path / 'test_lang_main.pkl'
    io.save_pickle(CONTENT, save_pth)
    pth = io.get_entry_point(
        tmp_path,
        'test_lang_main',
        '.pkl',
        check_existence=True,
    )
    assert pth.exists()
    with pytest.raises(FileNotFoundError):
        _ = io.get_entry_point(
            tmp_path,
            'test_lang_main2',
            '.pkl',
            check_existence=True,
        )
    pth = io.get_entry_point(
        tmp_path,
        'test_lang_main2',
        '.pkl',
        check_existence=False,
    )
    assert not pth.exists()
