Debian Codegraph @

test/plugins/test_limit.py

raw
1 """Tests for the 'limit' plugin."""
2
3 from beets.test.helper import IOMixin, PluginTestCase
4
5
6 class LimitPluginTest(IOMixin, PluginTestCase):
7 """Unit tests for LimitPlugin
8
9 Note: query prefix tests do not work correctly with `run_with_output`.
10 """
11
12 plugin = "limit"
13
14 def setUp(self):
15 super().setUp()
16
17 # we'll create an even number of tracks in the library
18 self.num_test_items = 10
19 assert self.num_test_items % 2 == 0
20 for item_no, item in enumerate(
21 self.add_item_fixtures(count=self.num_test_items)
22 ):
23 item.track = item_no + 1
24 item.store()
25
26 # our limit tests will use half of this number
27 self.num_limit = self.num_test_items // 2
28 self.num_limit_prefix = "".join(["'", "<", str(self.num_limit), "'"])
29
30 # a subset of tests has only `num_limit` results, identified by a
31 # range filter on the track number
32 self.track_head_range = f"track:..{self.num_limit}"
33 self.track_tail_range = f"track:{self.num_limit + 1}{'..'}"
34
35 def test_no_limit(self):
36 """Returns all when there is no limit or filter."""
37 result = self.run_with_output("lslimit")
38 assert result.count("\n") == self.num_test_items
39
40 def test_lslimit_head(self):
41 """Returns the expected number with `lslimit --head`."""
42 result = self.run_with_output("lslimit", "--head", str(self.num_limit))
43 assert result.count("\n") == self.num_limit
44
45 def test_lslimit_tail(self):
46 """Returns the expected number with `lslimit --tail`."""
47 result = self.run_with_output("lslimit", "--tail", str(self.num_limit))
48 assert result.count("\n") == self.num_limit
49
50 def test_lslimit_head_invariant(self):
51 """Returns the expected number with `lslimit --head` and a filter."""
52 result = self.run_with_output(
53 "lslimit", "--head", str(self.num_limit), self.track_tail_range
54 )
55 assert result.count("\n") == self.num_limit
56
57 def test_lslimit_tail_invariant(self):
58 """Returns the expected number with `lslimit --tail` and a filter."""
59 result = self.run_with_output(
60 "lslimit", "--tail", str(self.num_limit), self.track_head_range
61 )
62 assert result.count("\n") == self.num_limit
63
64 def test_prefix(self):
65 """Returns the expected number with the query prefix."""
66 result = self.lib.items(self.num_limit_prefix)
67 assert len(result) == self.num_limit
68
69 def test_prefix_when_correctly_ordered(self):
70 """Returns the expected number with the query prefix and filter when
71 the prefix portion (correctly) appears last."""
72 correct_order = f"{self.track_tail_range} {self.num_limit_prefix}"
73 result = self.lib.items(correct_order)
74 assert len(result) == self.num_limit
75
76 def test_prefix_when_incorrectly_ordred(self):
77 """Returns no results with the query prefix and filter when the prefix
78 portion (incorrectly) appears first."""
79 incorrect_order = f"{self.num_limit_prefix} {self.track_tail_range}"
80 result = self.lib.items(incorrect_order)
81 assert len(result) == 0