forked from beetbox/beets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ui_init.py
173 lines (148 loc) · 5.19 KB
/
test_ui_init.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# This file is part of beets.
# Copyright 2016, Adrian Sampson.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
"""Test module for file ui/__init__.py
"""
import os
import shutil
import unittest
from copy import deepcopy
from random import random
from beets import config, ui
from beets.test import _common
from beets.test.helper import control_stdin
class InputMethodsTest(_common.TestCase):
def setUp(self):
super().setUp()
self.io.install()
def _print_helper(self, s):
print(s)
def _print_helper2(self, s, prefix):
print(prefix, s)
def test_input_select_objects(self):
full_items = ["1", "2", "3", "4", "5"]
# Test no
self.io.addinput("n")
items = ui.input_select_objects(
"Prompt", full_items, self._print_helper
)
self.assertEqual(items, [])
# Test yes
self.io.addinput("y")
items = ui.input_select_objects(
"Prompt", full_items, self._print_helper
)
self.assertEqual(items, full_items)
# Test selective 1
self.io.addinput("s")
self.io.addinput("n")
self.io.addinput("y")
self.io.addinput("n")
self.io.addinput("y")
self.io.addinput("n")
items = ui.input_select_objects(
"Prompt", full_items, self._print_helper
)
self.assertEqual(items, ["2", "4"])
# Test selective 2
self.io.addinput("s")
self.io.addinput("y")
self.io.addinput("y")
self.io.addinput("n")
self.io.addinput("y")
self.io.addinput("n")
items = ui.input_select_objects(
"Prompt", full_items, lambda s: self._print_helper2(s, "Prefix")
)
self.assertEqual(items, ["1", "2", "4"])
# Test selective 3
self.io.addinput("s")
self.io.addinput("y")
self.io.addinput("n")
self.io.addinput("y")
self.io.addinput("q")
items = ui.input_select_objects(
"Prompt", full_items, self._print_helper
)
self.assertEqual(items, ["1", "3"])
class InitTest(_common.LibTestCase):
def setUp(self):
super().setUp()
def test_human_bytes(self):
tests = [
(0, "0.0 B"),
(30, "30.0 B"),
(pow(2, 10), "1.0 KiB"),
(pow(2, 20), "1.0 MiB"),
(pow(2, 30), "1.0 GiB"),
(pow(2, 40), "1.0 TiB"),
(pow(2, 50), "1.0 PiB"),
(pow(2, 60), "1.0 EiB"),
(pow(2, 70), "1.0 ZiB"),
(pow(2, 80), "1.0 YiB"),
(pow(2, 90), "1.0 HiB"),
(pow(2, 100), "big"),
]
for i, h in tests:
self.assertEqual(h, ui.human_bytes(i))
def test_human_seconds(self):
tests = [
(0, "0.0 seconds"),
(30, "30.0 seconds"),
(60, "1.0 minutes"),
(90, "1.5 minutes"),
(125, "2.1 minutes"),
(3600, "1.0 hours"),
(86400, "1.0 days"),
(604800, "1.0 weeks"),
(31449600, "1.0 years"),
(314496000, "1.0 decades"),
]
for i, h in tests:
self.assertEqual(h, ui.human_seconds(i))
class ParentalDirCreation(_common.TestCase):
def test_create_yes(self):
non_exist_path = _common.util.py3_path(
os.path.join(self.temp_dir, b"nonexist", str(random()).encode())
)
# Deepcopy instead of recovering because exceptions might
# occur; wish I can use a golang defer here.
test_config = deepcopy(config)
test_config["library"] = non_exist_path
with control_stdin("y"):
lib = ui._open_library(test_config)
lib._close()
def test_create_no(self):
non_exist_path_parent = _common.util.py3_path(
os.path.join(self.temp_dir, b"nonexist")
)
non_exist_path = _common.util.py3_path(
os.path.join(non_exist_path_parent.encode(), str(random()).encode())
)
test_config = deepcopy(config)
test_config["library"] = non_exist_path
with control_stdin("n"):
try:
lib = ui._open_library(test_config)
except ui.UserError:
if os.path.exists(non_exist_path_parent):
shutil.rmtree(non_exist_path_parent)
raise OSError("Parent directories should not be created.")
else:
if lib:
lib._close()
raise OSError("Parent directories should not be created.")
def suite():
return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__":
unittest.main(defaultTest="suite")