Debian Codegraph @

test/ui/test_ui_init.py

raw
1 """Test module for file ui/__init__.py"""
2
3 import unittest
4 from copy import deepcopy
5 from pathlib import Path
6 from random import random
7
8 import pytest
9
10 from beets import config, ui
11 from beets.exceptions import UserError
12 from beets.test.helper import BeetsTestCase, IOMixin
13
14
15 class InputMethodsTest(IOMixin, unittest.TestCase):
16 def _print_helper(self, s):
17 print(s)
18
19 def _print_helper2(self, s, prefix):
20 print(prefix, s)
21
22 def test_input_select_objects(self):
23 full_items = ["1", "2", "3", "4", "5"]
24
25 # Test no
26 self.io.addinput("n")
27 items = ui.input_select_objects(
28 "Prompt", full_items, self._print_helper
29 )
30 assert items == []
31
32 # Test yes
33 self.io.addinput("y")
34 items = ui.input_select_objects(
35 "Prompt", full_items, self._print_helper
36 )
37 assert items == full_items
38
39 # Test selective 1
40 self.io.addinput("s")
41 self.io.addinput("n")
42 self.io.addinput("y")
43 self.io.addinput("n")
44 self.io.addinput("y")
45 self.io.addinput("n")
46 items = ui.input_select_objects(
47 "Prompt", full_items, self._print_helper
48 )
49 assert items == ["2", "4"]
50
51 # Test selective 2
52 self.io.addinput("s")
53 self.io.addinput("y")
54 self.io.addinput("y")
55 self.io.addinput("n")
56 self.io.addinput("y")
57 self.io.addinput("n")
58 items = ui.input_select_objects(
59 "Prompt", full_items, lambda s: self._print_helper2(s, "Prefix")
60 )
61 assert items == ["1", "2", "4"]
62
63 # Test selective 3
64 self.io.addinput("s")
65 self.io.addinput("y")
66 self.io.addinput("n")
67 self.io.addinput("y")
68 self.io.addinput("q")
69 items = ui.input_select_objects(
70 "Prompt", full_items, self._print_helper
71 )
72 assert items == ["1", "3"]
73
74
75 class ParentalDirCreation(IOMixin, BeetsTestCase):
76 def test_memory_path_skips_creation_prompt(self):
77 ui._ensure_db_directory_exists(Path(":memory:"))
78 assert not self.io.getoutput()
79
80 def test_create_yes(self):
81 non_exist_path = self.temp_path / "nonexist" / str(random())
82 # Deepcopy instead of recovering because exceptions might
83 # occur; wish I can use a golang defer here.
84 test_config = deepcopy(config)
85 test_config["library"] = str(non_exist_path)
86 self.io.addinput("y")
87 lib = ui._open_library(test_config)
88 lib._close()
89
90 def test_create_no(self):
91 non_exist_path_parent = self.temp_path / "nonexist"
92 non_exist_path = non_exist_path_parent / str(random())
93 test_config = deepcopy(config)
94 test_config["library"] = str(non_exist_path)
95
96 self.io.addinput("n")
97 with pytest.raises(UserError):
98 ui._open_library(test_config)
99 assert not non_exist_path_parent.exists()