from unittest import TestCase

from env_var import env
from env_var.errors import EnvVarValidationError

from .helpers import VAR_NAME, check_validators, set_var


class TestValidators(TestCase):
    def test_email(self):

        valid_values = ("<EMAIL>", "<EMAIL>")
        invalid_values = (
            "not-a <EMAIL>",
            "dsa1",
        )

        check_validators(self, env(VAR_NAME).as_email(), valid_values, invalid_values)

    def test_hostname(self):
        valid_values = (
            "my-server",
            "monet.example.com",
            "localhost",
            "127.0.0.1",
            "8.8.8.8",
        )
        invalid_values = ("zażółćgęśląjaźń", "-example.com", "exam_ple.com")

        check_validators(
            self, env(VAR_NAME).as_hostname(), valid_values, invalid_values
        )

    def test_as_iso_date_string(self):
        valid_values = ("2022-04-09T01:30:03.602Z",)
        invalid_values = (
            # this isn't a valid iso string, and yet it's
            # generated by datetime.isoformat()
            "2022-04-09T03:28:43.398793",
            "Sat Apr 09 2022 03:30:17 GMT+0200 (CEST)",
            "09/04/2022, 03:30:37",
        )

        check_validators(
            self, env(VAR_NAME).as_iso_date_string(), valid_values, invalid_values
        )

    def test_as_uri(self):
        valid_values = (
            "https://google.com",
            "http://localhost:3000",
            "http://192.168.1.100:31333",
        )
        invalid_values = "google.com"

        check_validators(self, env(VAR_NAME).as_uri(), valid_values, invalid_values)

    def test_as_url(self):
        valid_values = (
            "https://google.com",
            "http://localhost:3000",
            "http://192.168.1.100:31333",
        )
        invalid_values = ("urn:isbn:0-486-27557-4", "google.com")

        check_validators(self, env(VAR_NAME).as_url(), valid_values, invalid_values)

    def test_as_uuid(self):
        valid_values = (
            "26f47ed0-b7a4-11ec-b909-0242ac120002",
            "d0b887b2-cb84-47c8-b796-a2351c91e070",
            "26F47ED0-B7A4-11EC-B909-0242AC120002",
            "D0B887B2-CB84-47C8-B796-A2351C91E070",
        )
        invalid_values = ("26f47ed0-b7a4-11ec-b909-0242ac12000",)

        check_validators(self, env(VAR_NAME).as_uuid(), valid_values, invalid_values)

    def test_own_regex(self):
        set_var("testing_is_very_important")

        for valid in (r"^testing.*$", f".*import"):
            self.assertIsNotNone(env(VAR_NAME).should_match(valid).required())

        for invalid in (
            "testing_is_not_important",
            r"testing\s+is.*",
        ):
            with self.assertRaises(EnvVarValidationError):
                env(VAR_NAME).should_match(invalid).required()

from unittest import TestCase

from env_var import env
from env_var.errors import EnvVarValidationError

from .helpers import VAR_NAME, check_validators, set_var


class TestValidators(TestCase):
    def test_email(self):

        valid_values = ("<EMAIL>", "<EMAIL>")
        invalid_values = (
            "not-a <EMAIL>",
            "dsa1",
        )

        check_validators(self, env(VAR_NAME).as_email(), valid_values, invalid_values)

    def test_hostname(self):
        valid_values = (
            "my-server",
            "monet.example.com",
            "localhost",
            "127.0.0.1",
            "8.8.8.8",
        )
        invalid_values = ("zażółćgęśląjaźń", "-example.com", "exam_ple.com")

        check_validators(
            self, env(VAR_NAME).as_hostname(), valid_values, invalid_values
        )

    def test_as_iso_date_string(self):
        valid_values = ("2022-04-09T01:30:03.602Z",)
        invalid_values = (
            # this isn't a valid iso string, and yet it's
            # generated by datetime.isoformat()
            "2022-04-09T03:28:43.398793",
            "Sat Apr 09 2022 03:30:17 GMT+0200 (CEST)",
            "09/04/2022, 03:30:37",
        )

        check_validators(
            self, env(VAR_NAME).as_iso_date_string(), valid_values, invalid_values
        )

    def test_as_uri(self):
        valid_values = (
            "https://google.com",
            "http://localhost:3000",
            "http://192.168.1.100:31333",
        )
        invalid_values = "google.com"

        check_validators(self, env(VAR_NAME).as_uri(), valid_values, invalid_values)

    def test_as_url(self):
        valid_values = (
            "https://google.com",
            "http://localhost:3000",
            "http://192.168.1.100:31333",
        )
        invalid_values = ("urn:isbn:0-486-27557-4", "google.com")

        check_validators(self, env(VAR_NAME).as_url(), valid_values, invalid_values)

    def test_as_uuid(self):
        valid_values = (
            "26f47ed0-b7a4-11ec-b909-0242ac120002",
            "d0b887b2-cb84-47c8-b796-a2351c91e070",
            "26F47ED0-B7A4-11EC-B909-0242AC120002",
            "D0B887B2-CB84-47C8-B796-A2351C91E070",
        )
        invalid_values = ("26f47ed0-b7a4-11ec-b909-0242ac12000",)

        check_validators(self, env(VAR_NAME).as_uuid(), valid_values, invalid_values)

    def test_own_regex(self):
        set_var("testing_is_very_important")

        for valid in (r"^testing.*$", f".*import"):
            self.assertIsNotNone(env(VAR_NAME).should_match(valid).required())

        for invalid in (
            "testing_is_not_important",
            r"testing\s+is.*",
        ):
            with self.assertRaises(EnvVarValidationError):
                env(VAR_NAME).should_match(invalid).required()

# this isn't a valid iso string, and yet it's # generated by datetime.isoformat()