/ tests / unit / test_p_quality_01.py
test_p_quality_01.py
  1  """
  2  Unit tests for P-QUALITY-01: encoding quality scoring rule
  3  """
  4  
  5  import importlib
  6  
  7  import pytest
  8  
  9  # Import the rule using importlib to handle hyphenated module name
 10  rule = importlib.import_module("rules.P-QUALITY-01")
 11  
 12  
 13  @pytest.mark.unit
 14  def test_high_quality_4k(make_event):
 15      """Title with '2160p' should score near 0.0 (highest quality)."""
 16      event = make_event(tags=[
 17          ["title", "Example Movie 2160p BluRay"],
 18          ["x", "0123456789abcdef0123456789abcdef01234567"],
 19          ["file", "movie.mkv", "50000000000"],  # 50GB
 20      ])
 21      result = rule.main(event)
 22      assert result["score"] == 0.0
 23  
 24  
 25  @pytest.mark.unit
 26  def test_high_quality_uhd(make_event):
 27      """Title with 'UHD' should score near 0.0 (highest quality)."""
 28      event = make_event(tags=[
 29          ["title", "Example Movie UHD HDR"],
 30          ["x", "0123456789abcdef0123456789abcdef01234567"],
 31          ["file", "movie.mkv", "40000000000"],
 32      ])
 33      result = rule.main(event)
 34      assert result["score"] == 0.0
 35  
 36  
 37  @pytest.mark.unit
 38  def test_high_quality_4k_lowercase(make_event):
 39      """Title with '4k' (lowercase) should score near 0.0."""
 40      event = make_event(tags=[
 41          ["title", "Example Movie 4k"],
 42          ["x", "0123456789abcdef0123456789abcdef01234567"],
 43          ["file", "movie.mkv", "35000000000"],
 44      ])
 45      result = rule.main(event)
 46      assert result["score"] == 0.0
 47  
 48  
 49  @pytest.mark.unit
 50  def test_good_quality_1080p_large(make_event):
 51      """Title '1080p' with >5GB should score 0.1."""
 52      event = make_event(tags=[
 53          ["title", "Example Movie 1080p BluRay"],
 54          ["x", "0123456789abcdef0123456789abcdef01234567"],
 55          ["file", "movie.mkv", "8589934592"],  # 8GB
 56      ])
 57      result = rule.main(event)
 58      assert result["score"] == 0.1
 59  
 60  
 61  @pytest.mark.unit
 62  def test_compressed_1080p(make_event):
 63      """Title '1080p' with <5GB should score 0.4."""
 64      event = make_event(tags=[
 65          ["title", "Example Movie 1080p WEB-DL"],
 66          ["x", "0123456789abcdef0123456789abcdef01234567"],
 67          ["file", "movie.mkv", "2147483648"],  # 2GB
 68      ])
 69      result = rule.main(event)
 70      assert result["score"] == 0.4
 71  
 72  
 73  @pytest.mark.unit
 74  def test_1080p_exactly_5gb(make_event):
 75      """Title '1080p' with exactly 5GB should score 0.4 (not > 5GB)."""
 76      event = make_event(tags=[
 77          ["title", "Example Movie 1080p"],
 78          ["x", "0123456789abcdef0123456789abcdef01234567"],
 79          ["file", "movie.mkv", "5368709120"],  # Exactly 5GB
 80      ])
 81      result = rule.main(event)
 82      assert result["score"] == 0.4
 83  
 84  
 85  @pytest.mark.unit
 86  def test_medium_quality_720p(make_event):
 87      """Title with '720p' should score 0.5."""
 88      event = make_event(tags=[
 89          ["title", "Example Movie 720p HDTV"],
 90          ["x", "0123456789abcdef0123456789abcdef01234567"],
 91          ["file", "movie.mkv", "1073741824"],
 92      ])
 93      result = rule.main(event)
 94      assert result["score"] == 0.5
 95  
 96  
 97  @pytest.mark.unit
 98  def test_low_quality_480p(make_event):
 99      """Title with '480p' should score 0.8."""
100      event = make_event(tags=[
101          ["title", "Example Movie 480p"],
102          ["x", "0123456789abcdef0123456789abcdef01234567"],
103          ["file", "movie.mkv", "524288000"],
104      ])
105      result = rule.main(event)
106      assert result["score"] == 0.8
107  
108  
109  @pytest.mark.unit
110  def test_low_quality_sd(make_event):
111      """Title with 'SD' should score 0.8."""
112      event = make_event(tags=[
113          ["title", "Example Movie SD"],
114          ["x", "0123456789abcdef0123456789abcdef01234567"],
115          ["file", "movie.avi", "524288000"],
116      ])
117      result = rule.main(event)
118      assert result["score"] == 0.8
119  
120  
121  @pytest.mark.unit
122  def test_unknown_quality(make_event):
123      """Title with no resolution hint should score 0.5 (neutral)."""
124      event = make_event(tags=[
125          ["title", "Example Movie"],
126          ["x", "0123456789abcdef0123456789abcdef01234567"],
127          ["file", "movie.mkv", "1073741824"],
128      ])
129      result = rule.main(event)
130      assert result["score"] == 0.5
131  
132  
133  @pytest.mark.unit
134  def test_no_title_tag(make_event):
135      """Event with no title tag should score 0.5 (neutral)."""
136      event = make_event(tags=[
137          ["x", "0123456789abcdef0123456789abcdef01234567"],
138          ["file", "movie.mkv", "1073741824"],
139      ])
140      result = rule.main(event)
141      assert result["score"] == 0.5
142  
143  
144  @pytest.mark.unit
145  def test_multi_file_size_calculation(make_event):
146      """Total size should be calculated from multiple files."""
147      event = make_event(tags=[
148          ["title", "Example Series 1080p"],
149          ["x", "0123456789abcdef0123456789abcdef01234567"],
150          ["file", "episode1.mkv", "2684354560"],  # 2.5GB
151          ["file", "episode2.mkv", "2684354560"],  # 2.5GB
152          ["file", "episode3.mkv", "2684354560"],  # 2.5GB (total 7.5GB > 5GB)
153      ])
154      result = rule.main(event)
155      assert result["score"] == 0.1  # Should be high quality due to >5GB total
156  
157  
158  @pytest.mark.unit
159  def test_case_insensitive_resolution(make_event):
160      """Resolution indicators should be case insensitive."""
161      event = make_event(tags=[
162          ["title", "Example Movie 1080P"],  # Uppercase P
163          ["x", "0123456789abcdef0123456789abcdef01234567"],
164          ["file", "movie.mkv", "8589934592"],  # 8GB
165      ])
166      result = rule.main(event)
167      assert result["score"] == 0.1
168  
169  
170  @pytest.mark.unit
171  def test_invalid_file_size_ignored(make_event):
172      """Files with invalid size should be ignored in calculation."""
173      event = make_event(tags=[
174          ["title", "Example Movie 1080p"],
175          ["x", "0123456789abcdef0123456789abcdef01234567"],
176          ["file", "movie.mkv", "1073741824"],  # Valid 1GB
177          ["file", "invalid.txt", "not-a-number"],  # Invalid, should be ignored
178      ])
179      result = rule.main(event)
180      # Only 1GB counted, should be compressed (0.4)
181      assert result["score"] == 0.4