Skip to content

Commit

Permalink
fix: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dreulavelle committed Mar 27, 2024
1 parent b5dafac commit 16fa75c
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 140 deletions.
9 changes: 6 additions & 3 deletions RTN/ranker.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ def calculate_resolution_rank(parsed_data: ParsedData, settings: SettingsModel,
if not parsed_data.resolution:
return 0

if parsed_data.is_4k:
return rank_model.uhd if not settings.custom_ranks["uhd"].enable else settings.custom_ranks["uhd"].rank

resolution: str = parsed_data.resolution[0]
match resolution:
case "4K":
return rank_model.uhd if not settings.custom_ranks["uhd"].enable else settings.custom_ranks["uhd"].rank
case "2160p":
return rank_model.uhd if not settings.custom_ranks["uhd"].enable else settings.custom_ranks["uhd"].rank
case "1440p":
return rank_model.uhd if not settings.custom_ranks["uhd"].enable else settings.custom_ranks["uhd"].rank
case "1080p":
return rank_model.fhd if not settings.custom_ranks["fhd"].enable else settings.custom_ranks["fhd"].rank
case "720p":
Expand Down
3 changes: 2 additions & 1 deletion tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ def test_default_title_matching():
assert title_match(title, query) == expected, f"Failed for {title} and {query}"


def test_batch_parse_returns_correct_parsed_data_objects():
def test_batch_parse_processing():
# Test batch parsing retuns a list of ParsedData objects
test_titles = [
"The.Matrix.1999.1080p.BluRay.x264",
"Inception.2010.720p.BRRip.x264",
Expand Down
49 changes: 31 additions & 18 deletions tests/test_ranker.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def custom_ranking_model():
uhd=140,
fhd=100,
hd=50,
sd=-100,
sd=-10,
dolby_video=-1000,
hdr=-1000,
hdr10=-1000,
Expand Down Expand Up @@ -116,30 +116,43 @@ def test_rank_calculation_accuracy(settings_model, ranking_model):
)

rank = get_rank(parsed_data, settings_model, ranking_model)
assert rank == 90, f"Expected rank did not match, got {rank}"

def test_preference_handling(custom_settings_model, custom_ranking_model):
custom_settings_model.preferred.append("/Example/")
parsed_data = ParsedData(raw_title="Example.Movie.2020", parsed_title="Example Movie")
rank_with_preference = get_rank(parsed_data, custom_settings_model, custom_ranking_model)

custom_settings_model.preferred = [] # Remove preference
rank_without_preference = get_rank(parsed_data, custom_settings_model, custom_ranking_model)

assert rank == 273, f"Expected rank did not match, got {rank}"

def test_preference_handling(custom_settings_model, ranking_model):
# Test with preferred title with a preference for Season number in title
# to make sure we can check before-after case. User should be able to set
# their own preferred patterns dynamically.
parsed_data = ParsedData(raw_title="Example.Series.S2.2020", parsed_title="Example Series")
rank_with_preference = get_rank(parsed_data, custom_settings_model, ranking_model)
assert rank_with_preference == 5000, "Preferred title should have rank 5000"
custom_settings_model.preferred = []
rank_without_preference = get_rank(parsed_data, custom_settings_model, ranking_model)
assert rank_with_preference > rank_without_preference, "Preferred title should have higher rank"

def test_resolution_ranking(custom_settings_model, custom_ranking_model):
parsed_data_1080p = ParsedData(raw_title="1080p", parsed_title="1080p", resolution=["1080p"])

def test_resolution_ranking(settings_model, ranking_model):
# Test Valid Resolutions
parsed_data_4k = ParsedData(raw_title="4K", parsed_title="4K", resolution=["4K"])
parsed_data_2160p = ParsedData(raw_title="2160p", parsed_title="2160p", resolution=["2160p"])
parsed_data_1440p = ParsedData(raw_title="1440p", parsed_title="1440p", resolution=["1440p"])
parsed_data_1080p = ParsedData(raw_title="1080p", parsed_title="1080p", resolution=["1080p"])
parsed_data_720p = ParsedData(raw_title="720p", parsed_title="720p", resolution=["720p"])
parsed_data_480p = ParsedData(raw_title="480p", parsed_title="480p", resolution=["480p"])
# Test Invalid Resolution
parsed_data_none = ParsedData(raw_title="None", parsed_title="None", resolution=[])

rank_1080p = get_rank(parsed_data_1080p, custom_settings_model, custom_ranking_model)
rank_4k = get_rank(parsed_data_4k, custom_settings_model, custom_ranking_model)
rank_720p = get_rank(parsed_data_720p, custom_settings_model, custom_ranking_model)
rank_480p = get_rank(parsed_data_480p, custom_settings_model, custom_ranking_model)
rank_4k = get_rank(parsed_data_4k, settings_model, ranking_model)
rank_2160p = get_rank(parsed_data_2160p, settings_model, ranking_model)
rank_1440p = get_rank(parsed_data_1440p, settings_model, ranking_model)
rank_1080p = get_rank(parsed_data_1080p, settings_model, ranking_model)
rank_720p = get_rank(parsed_data_720p, settings_model, ranking_model)
rank_480p = get_rank(parsed_data_480p, settings_model, ranking_model)
rank_none = get_rank(parsed_data_none, settings_model, ranking_model)

assert rank_4k > rank_1080p, "4K resolution should have higher rank than 1080p"
assert rank_2160p > rank_1080p, "2160p resolution should have higher rank than 1080p"
assert rank_1440p > rank_1080p, "1440p resolution should have higher rank than 1080p"
assert rank_1080p > rank_720p, "1080p resolution should have higher rank than 720p"
assert rank_720p > rank_480p, "720p resolution should have higher rank than 480p"
assert rank_480p > 0, "480p resolution should have positive rank"
assert rank_480p < 0, "480p resolution should have negative rank"
assert rank_none == 0, "No resolution should have rank 0"
234 changes: 116 additions & 118 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,121 +37,119 @@ def rank_model():
return DefaultRanking()


class TestSettingsModel:

# Initialize SettingsModel with default values
def test_initialize_with_default_values(self, settings):
assert settings.profile == "default"
assert settings.require == []
assert settings.exclude == []
assert settings.preferred == []
assert settings.custom_ranks == {
"uhd": CustomRank(enable=False, fetch=True, rank=120),
"fhd": CustomRank(enable=False, fetch=True, rank=90),
"hd": CustomRank(enable=False, fetch=True, rank=80),
"sd": CustomRank(enable=False, fetch=True, rank=-120),
"bluray": CustomRank(enable=False, fetch=True, rank=80),
"hdr": CustomRank(enable=False, fetch=True, rank=40),
"hdr10": CustomRank(enable=False, fetch=True, rank=50),
"dolby_video": CustomRank(enable=False, fetch=True, rank=-100),
"dts_x": CustomRank(enable=False, fetch=True, rank=0),
"dts_hd": CustomRank(enable=False, fetch=True, rank=0),
"dts_hd_ma": CustomRank(enable=False, fetch=True, rank=0),
"atmos": CustomRank(enable=False, fetch=True, rank=0),
"truehd": CustomRank(enable=False, fetch=True, rank=0),
"ddplus": CustomRank(enable=False, fetch=True, rank=0),
"aac": CustomRank(enable=False, fetch=True, rank=70),
"ac3": CustomRank(enable=False, fetch=True, rank=50),
"remux": CustomRank(enable=False, fetch=True, rank=-1000),
"webdl": CustomRank(enable=False, fetch=True, rank=90),
"repack": CustomRank(enable=False, fetch=True, rank=5),
"proper": CustomRank(enable=False, fetch=True, rank=4),
"dubbed": CustomRank(enable=False, fetch=True, rank=4),
"subbed": CustomRank(enable=False, fetch=True, rank=2),
"av1": CustomRank(enable=False, fetch=True, rank=0),
}

# Initialize SettingsModel with empty values
def test_initialize_with_empty_values(self):
settings = SettingsModel(
profile="",
require=[],
exclude=[],
preferred=[],
custom_ranks={}
)
assert settings.profile == ""
assert settings.require == []
assert settings.exclude == []
assert settings.preferred == []
assert settings.custom_ranks == {}

# Verify Custom Settings Initialization
def test_initialize_with_custom_values(self):
custom_ranks = {
"uhd": CustomRank(enable=True, fetch=True, rank=200),
}
settings = SettingsModel(
profile="custom",
require=["/4K/", "/1080p/i"],
exclude=["CAM", "TS"],
preferred=["BluRay", "/HDR10+/i"],
custom_ranks=custom_ranks
)
assert settings.profile == "custom"
assert settings.custom_ranks["uhd"].rank == 200

# Test Regex Pattern Compilation
def test_pattern_compilation(self):
settings = SettingsModel(
require=["/4K/", "/1080p/i"], # "/4K/" is case-sensitive, "/1080p/i" is case-insensitive
preferred=["/SeNsitive/", "/case insensitive explicit test: 1080p/i"] # Adjusted for clarity
)

# Ensure that each pattern is a compiled regex.Pattern object.
for pattern in settings.require + settings.preferred:
assert isinstance(pattern, regex.Pattern)

test_case = "This is a 4K test"
assert any(pattern.search(test_case) for pattern in settings.require), "4K case-sensitive match failed" # type: ignore

test_case = "1080P"
assert any(pattern.search(test_case) for pattern in settings.require), "1080p case-insensitive match failed" # type: ignore

test_case = "case sensitive test"
assert not any(pattern.search(test_case) for pattern in settings.preferred), "Case sensitive match failed" # type: ignore

test_case = "case insensitive explicit test: 1080p"
assert any(pattern.search(test_case) for pattern in settings.preferred), "Case insensitive explicit match failed" # type: ignore


# Test Custom Rank Updates
def test_custom_rank_updates(self):
settings = SettingsModel()
original_rank = settings.custom_ranks["uhd"].rank
settings.custom_ranks["uhd"].rank = original_rank + 100
assert settings.custom_ranks["uhd"].rank == original_rank + 100

# Validate Pattern Matching
@pytest.mark.parametrize("title,expected_match", [
("This is a 4K video", True),
("This is a 1080p video", True),
("Low Quality CAM", False),
("Awesome BluRay Release", True),
("Exclusive HDR10+ Content", True),
])
def test_pattern_matching(self, title: str, expected_match: bool):
settings = SettingsModel(
require=["/4K/", "/1080p/i"],
exclude=["CAM|TS|Telesync"],
preferred=["BluRay", "/HDR10+/i"]
)
require_matches = any(pattern.search(title) for pattern in settings.require) # type: ignore
exclude_matches = any(pattern.search(title) for pattern in settings.exclude) # type: ignore
preferred_matches = any(pattern.search(title) for pattern in settings.preferred) # type: ignore
assert expected_match in (require_matches and not exclude_matches, preferred_matches)

# Error Handling and Validation
def test_invalid_pattern_type_error(self):
with pytest.raises(ValidationError):
SettingsModel(require=["4K", 1080]) # 1080 is not a string or Pattern object # type: ignore
# Initialize SettingsModel with default values
def test_initialize_with_default_values(settings):
assert settings.profile == "default"
assert settings.require == []
assert settings.exclude == []
assert settings.preferred == []
assert settings.custom_ranks == {
"uhd": CustomRank(enable=False, fetch=True, rank=120),
"fhd": CustomRank(enable=False, fetch=True, rank=90),
"hd": CustomRank(enable=False, fetch=True, rank=80),
"sd": CustomRank(enable=False, fetch=True, rank=-120),
"bluray": CustomRank(enable=False, fetch=True, rank=80),
"hdr": CustomRank(enable=False, fetch=True, rank=40),
"hdr10": CustomRank(enable=False, fetch=True, rank=50),
"dolby_video": CustomRank(enable=False, fetch=True, rank=-100),
"dts_x": CustomRank(enable=False, fetch=True, rank=0),
"dts_hd": CustomRank(enable=False, fetch=True, rank=0),
"dts_hd_ma": CustomRank(enable=False, fetch=True, rank=0),
"atmos": CustomRank(enable=False, fetch=True, rank=0),
"truehd": CustomRank(enable=False, fetch=True, rank=0),
"ddplus": CustomRank(enable=False, fetch=True, rank=0),
"aac": CustomRank(enable=False, fetch=True, rank=70),
"ac3": CustomRank(enable=False, fetch=True, rank=50),
"remux": CustomRank(enable=False, fetch=True, rank=-1000),
"webdl": CustomRank(enable=False, fetch=True, rank=90),
"repack": CustomRank(enable=False, fetch=True, rank=5),
"proper": CustomRank(enable=False, fetch=True, rank=4),
"dubbed": CustomRank(enable=False, fetch=True, rank=4),
"subbed": CustomRank(enable=False, fetch=True, rank=2),
"av1": CustomRank(enable=False, fetch=True, rank=0),
}

# Initialize SettingsModel with empty values
def test_initialize_with_empty_values():
settings = SettingsModel(
profile="",
require=[],
exclude=[],
preferred=[],
custom_ranks={}
)
assert settings.profile == ""
assert settings.require == []
assert settings.exclude == []
assert settings.preferred == []
assert settings.custom_ranks == {}

# Verify Custom Settings Initialization
def test_initialize_with_custom_values():
custom_ranks = {
"uhd": CustomRank(enable=True, fetch=True, rank=200),
}
settings = SettingsModel(
profile="custom",
require=["/4K/", "/1080p/i"],
exclude=["CAM", "TS"],
preferred=["BluRay", "/HDR10+/i"],
custom_ranks=custom_ranks
)
assert settings.profile == "custom"
assert settings.custom_ranks["uhd"].rank == 200

# Test Regex Pattern Compilation
def test_pattern_compilation():
settings = SettingsModel(
require=["/4K/", "/1080p/i"], # "/4K/" is case-sensitive, "/1080p/i" is case-insensitive
preferred=["/SeNsitive/", "/case insensitive explicit test: 1080p/i"] # Adjusted for clarity
)

# Ensure that each pattern is a compiled regex.Pattern object.
for pattern in settings.require + settings.preferred:
assert isinstance(pattern, regex.Pattern)

test_case = "This is a 4K test"
assert any(pattern.search(test_case) for pattern in settings.require), "4K case-sensitive match failed" # type: ignore

test_case = "1080P"
assert any(pattern.search(test_case) for pattern in settings.require), "1080p case-insensitive match failed" # type: ignore

test_case = "case sensitive test"
assert not any(pattern.search(test_case) for pattern in settings.preferred), "Case sensitive match failed" # type: ignore

test_case = "case insensitive explicit test: 1080p"
assert any(pattern.search(test_case) for pattern in settings.preferred), "Case insensitive explicit match failed" # type: ignore


# Test Custom Rank Updates
def test_custom_rank_updates():
settings = SettingsModel()
original_rank = settings.custom_ranks["uhd"].rank
settings.custom_ranks["uhd"].rank = original_rank + 100
assert settings.custom_ranks["uhd"].rank == original_rank + 100

# Validate Pattern Matching
@pytest.mark.parametrize("title,expected_match", [
("This is a 4K video", True),
("This is a 1080p video", True),
("Low Quality CAM", False),
("Awesome BluRay Release", True),
("Exclusive HDR10+ Content", True),
])
def test_pattern_matching(title: str, expected_match: bool):
settings = SettingsModel(
require=["/4K/", "/1080p/i"],
exclude=["CAM|TS|Telesync"],
preferred=["BluRay", "/HDR10+/i"]
)
require_matches = any(pattern.search(title) for pattern in settings.require) # type: ignore
exclude_matches = any(pattern.search(title) for pattern in settings.exclude) # type: ignore
preferred_matches = any(pattern.search(title) for pattern in settings.preferred) # type: ignore
assert expected_match in (require_matches and not exclude_matches, preferred_matches)

# Error Handling and Validation
def test_invalid_pattern_type_error():
with pytest.raises(ValidationError):
SettingsModel(require=["4K", 1080]) # 1080 is not a string or Pattern object # type: ignore

0 comments on commit 16fa75c

Please sign in to comment.