import os

from pathlib import Path

from django.test import TestCase

from newsreader.accounts.tests.factories import UserFactory
from newsreader.utils.opml import parse_opml


class OPMImportTestCase(TestCase):
    def setUp(self):
        self.directory = Path(__file__).parent.absolute()
        self.user = UserFactory()

    def test_simple(self):
        path = os.path.join(self.directory, "files", "feeds.opml")

        with open(path, "r") as file:
            rules = list(parse_opml(file, self.user))

            self.assertEquals(len(rules), 4)

    def test_file_without_feeds(self):
        path = os.path.join(self.directory, "files", "empty-feeds.opml")

        with open(path, "r") as file:
            rules = list(parse_opml(file, self.user))

            self.assertEquals(len(rules), 0)

    def test_file_with_missing_rule_properties(self):
        path = os.path.join(self.directory, "files", "missing-feeds.opml")

        with open(path, "r") as file:
            rules = list(parse_opml(file, self.user))

            self.assertEquals(len(rules), 2)

    def test_url_validation(self):
        path = os.path.join(self.directory, "files", "invalid-url-feeds.opml")

        with open(path, "r") as file:
            rules = list(parse_opml(file, self.user))

            self.assertEquals(len(rules), 0)
