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

[((904, 940), 'feed.utils.randomize_case', 'randomize_case', (['FeedVersion.RSS_0_91'], {}), '(FeedVersion.RSS_0_91)\n', (918, 940), False, 'from feed.utils import randomize_case\n'), ((1439, 1474), 'feed.utils.randomize_case', 'randomize_case', (['FeedVersion.RSS_1_0'], {}), '(FeedVersion.RSS_1_0)\n', (1453, 1474), False, 'from feed.utils import randomize_case\n'), ((204, 227), 'feed.consts.FeedVersion.supported', 'FeedVersion.supported', ([], {}), '()\n', (225, 227), False, 'from feed.consts import FeedVersion\n'), ((534, 556), 'feed.consts.FeedVersion.get', 'FeedVersion.get', (['"""2.0"""'], {}), "('2.0')\n", (549, 556), False, 'from feed.consts import FeedVersion\n'), ((760, 793), 'feed.consts.FeedVersion.get', 'FeedVersion.get', (['"""1.0"""'], {'atom': '(True)'}), "('1.0', atom=True)\n", (775, 793), False, 'from feed.consts import FeedVersion\n'), ((950, 981), 'pytest.raises', 'pytest.raises', (['FeedVersionError'], {}), '(FeedVersionError)\n', (963, 981), False, 'import pytest\n'), ((1002, 1027), 'feed.consts.FeedVersion.get', 'FeedVersion.get', (['rss_0_91'], {}), '(rss_0_91)\n', (1017, 1027), False, 'from feed.consts import FeedVersion\n'), ((1131, 1154), 'feed.consts.FeedVersion.supported', 'FeedVersion.supported', ([], {}), '()\n', (1152, 1154), False, 'from feed.consts import FeedVersion\n'), ((1192, 1223), 'pytest.raises', 'pytest.raises', (['FeedVersionError'], {}), '(FeedVersionError)\n', (1205, 1223), False, 'import pytest\n'), ((1244, 1267), 'feed.consts.FeedVersion.get', 'FeedVersion.get', (['"""0.91"""'], {}), "('0.91')\n", (1259, 1267), False, 'from feed.consts import FeedVersion\n'), ((1373, 1396), 'feed.consts.FeedVersion.supported', 'FeedVersion.supported', ([], {}), '()\n', (1394, 1396), False, 'from feed.consts import FeedVersion\n'), ((1484, 1515), 'pytest.raises', 'pytest.raises', (['FeedVersionError'], {}), '(FeedVersionError)\n', (1497, 1515), False, 'import pytest\n'), ((1536, 1560), 'feed.consts.FeedVersion.get', 'FeedVersion.get', (['rss_1_0'], {}), '(rss_1_0)\n', (1551, 1560), False, 'from feed.consts import FeedVersion\n'), ((1663, 1686), 'feed.consts.FeedVersion.supported', 'FeedVersion.supported', ([], {}), '()\n', (1684, 1686), False, 'from feed.consts import FeedVersion\n'), ((1723, 1754), 'pytest.raises', 'pytest.raises', (['FeedVersionError'], {}), '(FeedVersionError)\n', (1736, 1754), False, 'import pytest\n'), ((1775, 1797), 'feed.consts.FeedVersion.get', 'FeedVersion.get', (['"""1.0"""'], {}), "('1.0')\n", (1790, 1797), False, 'from feed.consts import FeedVersion\n'), ((1902, 1925), 'feed.consts.FeedVersion.supported', 'FeedVersion.supported', ([], {}), '()\n', (1923, 1925), False, 'from feed.consts import FeedVersion\n'), ((2142, 2173), 'pytest.raises', 'pytest.raises', (['FeedVersionError'], {}), '(FeedVersionError)\n', (2155, 2173), False, 'import pytest\n'), ((2198, 2219), 'feed.consts.FeedVersion.get', 'FeedVersion.get', (['test'], {}), '(test)\n', (2213, 2219), False, 'from feed.consts import FeedVersion\n'), ((2334, 2357), 'feed.consts.FeedVersion.supported', 'FeedVersion.supported', ([], {}), '()\n', (2355, 2357), False, 'from feed.consts import FeedVersion\n'), ((2658, 2689), 'pytest.raises', 'pytest.raises', (['FeedVersionError'], {}), '(FeedVersionError)\n', (2671, 2689), False, 'import pytest\n'), ((2714, 2749), 'feed.consts.FeedVersion.get', 'FeedVersion.get', (['test'], {'atom': 'prefere'}), '(test, atom=prefere)\n', (2729, 2749), False, 'from feed.consts import FeedVersion\n'), ((2864, 2887), 'feed.consts.FeedVersion.supported', 'FeedVersion.supported', ([], {}), '()\n', (2885, 2887), False, 'from feed.consts import FeedVersion\n'), ((2959, 2990), 'pytest.raises', 'pytest.raises', (['FeedVersionError'], {}), '(FeedVersionError)\n', (2972, 2990), False, 'import pytest\n'), ((3015, 3039), 'feed.consts.FeedVersion.get', 'FeedVersion.get', (['version'], {}), '(version)\n', (3030, 3039), False, 'from feed.consts import FeedVersion\n'), ((3154, 3177), 'feed.consts.FeedVersion.supported', 'FeedVersion.supported', ([], {}), '()\n', (3175, 3177), False, 'from feed.consts import FeedVersion\n'), ((434, 461), 'feed.consts.FeedVersion.RSS_2_0.lower', 'FeedVersion.RSS_2_0.lower', ([], {}), '()\n', (459, 461), False, 'from feed.consts import FeedVersion\n'), ((657, 685), 'feed.consts.FeedVersion.ATOM_1_0.lower', 'FeedVersion.ATOM_1_0.lower', ([], {}), '()\n', (683, 685), False, 'from feed.consts import FeedVersion\n')]