/ tests / unit / test_d_files_01.py
test_d_files_01.py
  1  """
  2  Unit tests for D-FILES-01: file path and size validation 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.D-FILES-01")
 11  
 12  
 13  @pytest.mark.unit
 14  def test_accepts_valid_single_file(make_event):
 15      """Single file with valid path and size should pass."""
 16      event = make_event(tags=[
 17          ["title", "Test"],
 18          ["x", "0123456789abcdef0123456789abcdef01234567"],
 19          ["file", "video.mkv", "1073741824"],
 20      ])
 21      result = rule.main(event)
 22      assert result["passed"] is True
 23  
 24  
 25  @pytest.mark.unit
 26  def test_accepts_valid_multi_file(make_event):
 27      """Multiple files with valid paths and sizes should pass."""
 28      event = make_event(tags=[
 29          ["title", "Test"],
 30          ["x", "0123456789abcdef0123456789abcdef01234567"],
 31          ["file", "video/episode1.mp4", "1073741824"],
 32          ["file", "video/episode2.mp4", "1258291200"],
 33          ["file", "subtitles/en.srt", "52428"],
 34      ])
 35      result = rule.main(event)
 36      assert result["passed"] is True
 37  
 38  
 39  @pytest.mark.unit
 40  def test_accepts_no_file_tags(make_event):
 41      """Event with no file tags should pass (single-file torrent)."""
 42      event = make_event(tags=[
 43          ["title", "Test"],
 44          ["x", "0123456789abcdef0123456789abcdef01234567"],
 45      ])
 46      result = rule.main(event)
 47      assert result["passed"] is True
 48  
 49  
 50  @pytest.mark.unit
 51  def test_rejects_missing_size(make_event):
 52      """File tag with only path (missing size) should fail."""
 53      event = make_event(tags=[
 54          ["title", "Test"],
 55          ["x", "0123456789abcdef0123456789abcdef01234567"],
 56          ["file", "video.mkv"],
 57      ])
 58      result = rule.main(event)
 59      assert result["passed"] is False
 60  
 61  
 62  @pytest.mark.unit
 63  def test_rejects_negative_size(make_event):
 64      """File tag with negative size should fail."""
 65      event = make_event(tags=[
 66          ["title", "Test"],
 67          ["x", "0123456789abcdef0123456789abcdef01234567"],
 68          ["file", "video.mkv", "-1"],
 69      ])
 70      result = rule.main(event)
 71      assert result["passed"] is False
 72  
 73  
 74  @pytest.mark.unit
 75  def test_rejects_non_numeric_size(make_event):
 76      """File tag with non-numeric size should fail."""
 77      event = make_event(tags=[
 78          ["title", "Test"],
 79          ["x", "0123456789abcdef0123456789abcdef01234567"],
 80          ["file", "video.mkv", "not-a-number"],
 81      ])
 82      result = rule.main(event)
 83      assert result["passed"] is False
 84  
 85  
 86  @pytest.mark.unit
 87  def test_rejects_empty_path(make_event):
 88      """File tag with empty path string should fail."""
 89      event = make_event(tags=[
 90          ["title", "Test"],
 91          ["x", "0123456789abcdef0123456789abcdef01234567"],
 92          ["file", "", "1073741824"],
 93      ])
 94      result = rule.main(event)
 95      assert result["passed"] is False
 96  
 97  
 98  @pytest.mark.unit
 99  def test_accepts_zero_size(make_event):
100      """File with zero size should pass (e.g., padding files)."""
101      event = make_event(tags=[
102          ["title", "Test"],
103          ["x", "0123456789abcdef0123456789abcdef01234567"],
104          ["file", "padding.txt", "0"],
105      ])
106      result = rule.main(event)
107      assert result["passed"] is True
108  
109  
110  @pytest.mark.unit
111  def test_rejects_file_tag_with_extra_elements(make_event):
112      """File tag with more than 3 elements should fail."""
113      event = make_event(tags=[
114          ["title", "Test"],
115          ["x", "0123456789abcdef0123456789abcdef01234567"],
116          ["file", "video.mkv", "1073741824", "extra"],
117      ])
118      result = rule.main(event)
119      assert result["passed"] is False
120  
121  
122  @pytest.mark.unit
123  def test_rejects_when_one_of_many_files_invalid(make_event):
124      """If any file is invalid, the whole event should fail."""
125      event = make_event(tags=[
126          ["title", "Test"],
127          ["x", "0123456789abcdef0123456789abcdef01234567"],
128          ["file", "video1.mkv", "1073741824"],  # Valid
129          ["file", "video2.mkv", "-500"],  # Invalid (negative)
130          ["file", "video3.mkv", "2147483648"],  # Valid
131      ])
132      result = rule.main(event)
133      assert result["passed"] is False
134  
135  
136  @pytest.mark.unit
137  def test_accepts_file_with_unicode_path(make_event):
138      """File with unicode characters in path should pass."""
139      event = make_event(tags=[
140          ["title", "Test"],
141          ["x", "0123456789abcdef0123456789abcdef01234567"],
142          ["file", "映画/ビデオ.mp4", "1073741824"],
143      ])
144      result = rule.main(event)
145      assert result["passed"] is True