import pytest

from feed.consts import FeedVersion
from feed.utils import randomize_case
from feed.errors import FeedVersionError


# FeedVersion.supported


def test_feed_version_supported():
    assert FeedVersion.supported() == {
        FeedVersion.RSS_2_0,
        FeedVersion.ATOM_1_0,
    }


# FeedVersion.get


def test_feed_version_get_supported():
    # RSS full version
    assert (
        FeedVersion.get(randomize_case(FeedVersion.RSS_2_0.lower()))
        == FeedVersion.RSS_2_0
    )
    # RSS only number
    assert FeedVersion.get("2.0") == FeedVersion.RSS_2_0

    # Atom full version
    assert (
        FeedVersion.get(randomize_case(FeedVersion.ATOM_1_0.lower()))
        == FeedVersion.ATOM_1_0
    )
    # Atom only number
    assert FeedVersion.get("1.0", atom=True) == FeedVersion.ATOM_1_0


def test_feed_version_get_unsupported():
    # RSS 0.91 full version
    rss_0_91 = randomize_case(FeedVersion.RSS_0_91)
    with pytest.raises(FeedVersionError) as excinfo:
        FeedVersion.get(rss_0_91)

    ex: FeedVersionError = excinfo.value
    assert ex.version == rss_0_91
    assert ex.supported == FeedVersion.supported()

    # RSS 0.91 only number
    with pytest.raises(FeedVersionError) as excinfo:
        FeedVersion.get("0.91")

    ex: FeedVersionError = excinfo.value
    assert ex.version == "RSS 0.91"
    assert ex.supported == FeedVersion.supported()

    # RSS 1.0 full version
    rss_1_0 = randomize_case(FeedVersion.RSS_1_0)
    with pytest.raises(FeedVersionError) as excinfo:
        FeedVersion.get(rss_1_0)

    ex: FeedVersionError = excinfo.value
    assert ex.version == rss_1_0
    assert ex.supported == FeedVersion.supported()

    # RSS 1.0 only number
    with pytest.raises(FeedVersionError) as excinfo:
        FeedVersion.get("1.0")

    ex: FeedVersionError = excinfo.value
    assert ex.version == "RSS 1.0"
    assert ex.supported == FeedVersion.supported()


def test_feed_version_get_invalid():
    # RSS
    for version, test in [
        ("RSS 3.3", "3.3"),
        ("RSS 0.1", "0.1"),
        ("RSS 1.1", "RSS 1.1"),
        ("RSS 2.1", "RSS 2.1"),
    ]:
        with pytest.raises(FeedVersionError) as excinfo:
            FeedVersion.get(test)

        ex: FeedVersionError = excinfo.value
        assert ex.version == version
        assert ex.supported == FeedVersion.supported()

    # Atom
    for version, test, prefere in [
        ("Atom 0.1", "0.1", True),
        ("Atom 0.91", "0.91", True),
        ("Atom 2.0", "2.0", True),
        ("Atom 0.1", "Atom 0.1", False),
        ("Atom 0.91", "Atom 0.91", False),
        ("Atom 2.0", "Atom 2.0", False),
    ]:
        with pytest.raises(FeedVersionError) as excinfo:
            FeedVersion.get(test, atom=prefere)

        ex: FeedVersionError = excinfo.value
        assert ex.version == version
        assert ex.supported == FeedVersion.supported()

    # Nonsense
    for version in ["foo", "bar", "baz"]:
        with pytest.raises(FeedVersionError) as excinfo:
            FeedVersion.get(version)

        ex: FeedVersionError = excinfo.value
        assert ex.version == version
        assert ex.supported == FeedVersion.supported()


def test_is_property():
    # RSS
    assert FeedVersion.RSS_0_91.is_rss
    assert FeedVersion.RSS_1_0.is_rss
    assert FeedVersion.RSS_2_0.is_rss
    assert not FeedVersion.RSS_0_91.is_atom
    assert not FeedVersion.RSS_1_0.is_atom
    assert not FeedVersion.RSS_2_0.is_atom

    # Atom
    assert FeedVersion.ATOM_1_0.is_atom
    assert not FeedVersion.ATOM_1_0.is_rss

[((41, 15, 41, 51), 'feed.utils.randomize_case', 'randomize_case', ({(41, 30, 41, 50): 'FeedVersion.RSS_0_91'}, {}), '(FeedVersion.RSS_0_91)', False, 'from feed.utils import randomize_case\n'), ((58, 14, 58, 49), 'feed.utils.randomize_case', 'randomize_case', ({(58, 29, 58, 48): 'FeedVersion.RSS_1_0'}, {}), '(FeedVersion.RSS_1_0)', False, 'from feed.utils import randomize_case\n'), ((12, 11, 12, 34), 'feed.consts.FeedVersion.supported', 'FeedVersion.supported', ({}, {}), '()', False, 'from feed.consts import FeedVersion\n'), ((28, 11, 28, 33), 'feed.consts.FeedVersion.get', 'FeedVersion.get', ({(28, 27, 28, 32): '"""2.0"""'}, {}), "('2.0')", False, 'from feed.consts import FeedVersion\n'), ((36, 11, 36, 44), 'feed.consts.FeedVersion.get', 'FeedVersion.get', (), '', False, 'from feed.consts import FeedVersion\n'), ((42, 9, 42, 40), 'pytest.raises', 'pytest.raises', ({(42, 23, 42, 39): 'FeedVersionError'}, {}), '(FeedVersionError)', False, 'import pytest\n'), ((43, 8, 43, 33), 'feed.consts.FeedVersion.get', 'FeedVersion.get', ({(43, 24, 43, 32): 'rss_0_91'}, {}), '(rss_0_91)', False, 'from feed.consts import FeedVersion\n'), ((47, 27, 47, 50), 'feed.consts.FeedVersion.supported', 'FeedVersion.supported', ({}, {}), '()', False, 'from feed.consts import FeedVersion\n'), ((50, 9, 50, 40), 'pytest.raises', 'pytest.raises', ({(50, 23, 50, 39): 'FeedVersionError'}, {}), '(FeedVersionError)', False, 'import pytest\n'), ((51, 8, 51, 31), 'feed.consts.FeedVersion.get', 'FeedVersion.get', ({(51, 24, 51, 30): '"""0.91"""'}, {}), "('0.91')", False, 'from feed.consts import FeedVersion\n'), ((55, 27, 55, 50), 'feed.consts.FeedVersion.supported', 'FeedVersion.supported', ({}, {}), '()', False, 'from feed.consts import FeedVersion\n'), ((59, 9, 59, 40), 'pytest.raises', 'pytest.raises', ({(59, 23, 59, 39): 'FeedVersionError'}, {}), '(FeedVersionError)', False, 'import pytest\n'), ((60, 8, 60, 32), 'feed.consts.FeedVersion.get', 'FeedVersion.get', ({(60, 24, 60, 31): 'rss_1_0'}, {}), '(rss_1_0)', False, 'from feed.consts import FeedVersion\n'), ((64, 27, 64, 50), 'feed.consts.FeedVersion.supported', 'FeedVersion.supported', ({}, {}), '()', False, 'from feed.consts import FeedVersion\n'), ((67, 9, 67, 40), 'pytest.raises', 'pytest.raises', ({(67, 23, 67, 39): 'FeedVersionError'}, {}), '(FeedVersionError)', False, 'import pytest\n'), ((68, 8, 68, 30), 'feed.consts.FeedVersion.get', 'FeedVersion.get', ({(68, 24, 68, 29): '"""1.0"""'}, {}), "('1.0')", False, 'from feed.consts import FeedVersion\n'), ((72, 27, 72, 50), 'feed.consts.FeedVersion.supported', 'FeedVersion.supported', ({}, {}), '()', False, 'from feed.consts import FeedVersion\n'), ((83, 13, 83, 44), 'pytest.raises', 'pytest.raises', ({(83, 27, 83, 43): 'FeedVersionError'}, {}), '(FeedVersionError)', False, 'import pytest\n'), ((84, 12, 84, 33), 'feed.consts.FeedVersion.get', 'FeedVersion.get', ({(84, 28, 84, 32): 'test'}, {}), '(test)', False, 'from feed.consts import FeedVersion\n'), ((88, 31, 88, 54), 'feed.consts.FeedVersion.supported', 'FeedVersion.supported', ({}, {}), '()', False, 'from feed.consts import FeedVersion\n'), ((99, 13, 99, 44), 'pytest.raises', 'pytest.raises', ({(99, 27, 99, 43): 'FeedVersionError'}, {}), '(FeedVersionError)', False, 'import pytest\n'), ((100, 12, 100, 47), 'feed.consts.FeedVersion.get', 'FeedVersion.get', (), '', False, 'from feed.consts import FeedVersion\n'), ((104, 31, 104, 54), 'feed.consts.FeedVersion.supported', 'FeedVersion.supported', ({}, {}), '()', False, 'from feed.consts import FeedVersion\n'), ((108, 13, 108, 44), 'pytest.raises', 'pytest.raises', ({(108, 27, 108, 43): 'FeedVersionError'}, {}), '(FeedVersionError)', False, 'import pytest\n'), ((109, 12, 109, 36), 'feed.consts.FeedVersion.get', 'FeedVersion.get', ({(109, 28, 109, 35): 'version'}, {}), '(version)', False, 'from feed.consts import FeedVersion\n'), ((113, 31, 113, 54), 'feed.consts.FeedVersion.supported', 'FeedVersion.supported', ({}, {}), '()', False, 'from feed.consts import FeedVersion\n'), ((24, 39, 24, 66), 'feed.consts.FeedVersion.RSS_2_0.lower', 'FeedVersion.RSS_2_0.lower', ({}, {}), '()', False, 'from feed.consts import FeedVersion\n'), ((32, 39, 32, 67), 'feed.consts.FeedVersion.ATOM_1_0.lower', 'FeedVersion.ATOM_1_0.lower', ({}, {}), '()', False, 'from feed.consts import FeedVersion\n')]