Skip to content

Commit

Permalink
test: Relative links source validation is tested
Browse files Browse the repository at this point in the history
  • Loading branch information
myhailo-chernyshov-rg committed Dec 18, 2024
1 parent 30bbef0 commit bc298c2
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from argparse import Namespace
from pathlib import Path

import pytest

from cc2olx.cli import parse_args

Expand Down Expand Up @@ -58,3 +61,32 @@ def test_parse_args_passport_file(imscc_file, passports_csv):
output="output",
relative_links_source=None,
)


def test_parse_args_with_correct_relative_links_source(imscc_file: Path) -> None:
"""
Positive input test for relative links source argument.
"""
relative_links_source = "https://example.com"

parsed_args = parse_args(["-i", str(imscc_file), "-s", relative_links_source])

assert parsed_args == Namespace(
inputs=[imscc_file],
loglevel="INFO",
result="folder",
link_file=None,
passport_file=None,
output="output",
relative_links_source=relative_links_source,
)


def test_parse_args_with_incorrect_relative_links_source(imscc_file: Path) -> None:
"""
Test arguments parser detects incorrect relative links sources.
"""
relative_links_source = "ws://example.com",

with pytest.raises(SystemExit):
parse_args(["-i", str(imscc_file), "-s", relative_links_source])
Empty file.
49 changes: 49 additions & 0 deletions tests/test_validators/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import argparse

import pytest

from cc2olx.validators.cli import LinkSourceValidator


class TestLinkSourceValidator:
"""
Test link source validator.
"""

@pytest.mark.parametrize(
"links_source",
(
"http://example.com",
"http://example.com/",
"https://example.com",
"http://192.168.0.1",
"http://192.168.0.1:8000/",
"https://[2001:0db8:85a3::8a2e:0370:7334]/",
)
)
def test_original_value_is_returned_if_it_is_valid(self, links_source: str) -> None:
"""
Test whether the validator returns original value it is valid.
"""
assert LinkSourceValidator()(links_source) == links_source

@pytest.mark.parametrize(
"links_source",
(
"ftp://example.com",
"ws://example.com",
"just_string",
"http://192.168.0.1.9",
"http://192.168.0.1:-56",
"http://192.999.0.1",
"https://m192.168.0.1",
"https://2001:0db8:85a3::8a2e:0370:7334/",
"https://[2001:db8:85a3::8a2e:0370:7334::]/",
)
)
def test_wrong_values_are_detected(self, links_source) -> None:
"""
Test whether the validator raises an error if the value is invalid.
"""
with pytest.raises(argparse.ArgumentTypeError, match="Enter a valid URL."):
LinkSourceValidator()(links_source)

0 comments on commit bc298c2

Please sign in to comment.