Skip to content

Commit 482d46f

Browse files
committed
refactor: improve test class names and docstrings for clarity, ruff formatting
1 parent 1e14631 commit 482d46f

10 files changed

Lines changed: 102 additions & 107 deletions

tests/test_cache.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@
99
from backpack.cache import timed_lru_cache
1010

1111

12-
class Test_Cache(unittest.TestCase):
13-
12+
class TestCache(unittest.TestCase):
1413
@timed_lru_cache(seconds=1)
1514
def test_function(self):
16-
# some heavy process here to be cached
15+
"""Some heavy process here to be cached."""
1716
return True
1817

1918
def test_cache(self):
20-
''' testing module '''
19+
"""Testing module."""
2120
self.assertTrue(self.test_function())
2221
self.assertEqual(True, self.test_function(force_clear=True))
2322
self.assertEqual(True, self.test_function(force_clear=True, show_log=True))

tests/test_errors.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,39 @@
88
import sys
99
import unittest
1010

11-
from backpack.custom_errors import EnvironmentVariableNotFound
12-
from backpack.custom_errors import ApplicationNotFound
11+
from backpack.custom_errors import ApplicationNotFoundError, EnvironmentVariableNotFoundError
1312

1413
mod_path = os.path.dirname(__file__)
1514
if mod_path not in sys.path:
1615
sys.path.append(mod_path)
1716

1817

1918
def get_env_var(name: str):
20-
''' call for an env var or raise EnvironmentVariableNotFound '''
19+
"""Call for an env var or raise EnvironmentVariableNotFoundError."""
2120
try:
2221
value = os.environ[name]
2322
except KeyError as e:
24-
raise EnvironmentVariableNotFound(name) from e
23+
raise EnvironmentVariableNotFoundError(name) from e
2524
return value
2625

2726

2827
def get_app(name: str):
29-
''' raisers error '''
30-
raise ApplicationNotFound(f'Background executable not found {name}')
28+
"""Raisers error."""
29+
raise ApplicationNotFoundError(f'Background executable not found {name}')
3130

3231

33-
class Test_Errors(unittest.TestCase):
34-
32+
class TestErrors(unittest.TestCase):
3533
def test_env_var_error(self):
36-
''' testing module '''
37-
self.assertRaises(EnvironmentVariableNotFound, get_env_var, 'my_env_var')
38-
error = EnvironmentVariableNotFound('my_env_var')
34+
"""Testing module."""
35+
self.assertRaises(EnvironmentVariableNotFoundError, get_env_var, 'my_env_var')
36+
error = EnvironmentVariableNotFoundError('my_env_var')
3937
self.assertEqual(str(error), error.message)
4038

4139
def test_env_app_error(self):
42-
''' testing module '''
43-
self.assertRaises(ApplicationNotFound, get_app, 'my_app.exe')
40+
"""Testing module."""
41+
self.assertRaises(ApplicationNotFoundError, get_app, 'my_app.exe')
4442

45-
error = ApplicationNotFound('my_app.exe')
43+
error = ApplicationNotFoundError('my_app.exe')
4644
self.assertEqual(str(error), error.message)
4745

4846

tests/test_file_utils.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
# ----------------------------------------------------------------------------------------
66

77
import os
8+
import shutil
89
import sys
910
import unittest
10-
import shutil
1111

12-
from backpack.file_utils import replace_strings_in_file, remove_line_from_file
13-
from backpack.file_utils import file_is_writeable
12+
from backpack.file_utils import file_is_writeable, remove_line_from_file, replace_strings_in_file
1413

1514
mod_path = os.path.dirname(__file__)
1615
if mod_path not in sys.path:
@@ -26,21 +25,21 @@
2625
NO_FILE = os.path.join(mod_path, 'test_files', 'no_file.txt')
2726

2827

29-
class Test_Errors(unittest.TestCase):
30-
28+
class TestErrors(unittest.TestCase):
3129
def test_replace_strings_in_file(self):
32-
''' testing module '''
30+
"""Testing module."""
3331
shutil.copy(BASE_FILE, EDITED_FILE)
3432
replace_strings_in_file(EDITED_FILE, STRINGS, NEW_STRING)
3533

3634
def test_remove_line_from_file(self):
37-
''' testing module '''
35+
"""Testing module."""
3836
source_file = os.path.join(mod_path, 'test_files', 'origin_remove.txt')
3937
test_file = os.path.join(mod_path, 'test_files', 'edited_remove.txt')
4038
shutil.copy(source_file, test_file)
4139
remove_line_from_file(test_file, ['REMOVE_ME', 'to be replaced!'])
4240

4341
def test_locked_file(self):
42+
"""Testing module."""
4443
self.assertTrue(file_is_writeable(UNLOCKED_FILE))
4544
self.assertFalse(file_is_writeable(NO_FILE))
4645

tests/test_folder_utils.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,40 @@
77
import os
88
import sys
99
import unittest
10+
1011
import mock
1112

12-
from backpack.folder_utils import create_folders, browse_folder, create_folder
13-
from backpack.folder_utils import remove_files_in_dir, recursive_dir_copy
13+
from backpack.folder_utils import (
14+
browse_folder,
15+
create_folder,
16+
create_folders,
17+
recursive_dir_copy,
18+
remove_files_in_dir,
19+
)
1420

1521
mod_path = os.path.dirname(__file__)
1622
if mod_path not in sys.path:
1723
sys.path.append(mod_path)
1824

1925
# test folders
2026
BASE_PATH = os.path.join(mod_path, 'test_folder')
21-
FOLDERS = [os.path.join(BASE_PATH, 'ALPHA'),
22-
os.path.join(BASE_PATH, 'BETA'),
23-
]
27+
FOLDERS = [
28+
os.path.join(BASE_PATH, 'ALPHA'),
29+
os.path.join(BASE_PATH, 'BETA'),
30+
]
2431
FOLDER_FILES = os.path.join(BASE_PATH, 'files')
2532
FILES = ['readme.txt']
2633

2734

2835
def create_file(path):
29-
''' creates a temp txt file '''
36+
"""Creates a temp txt file."""
3037
with open(os.path.join(path, 'temp.txt'), 'w'):
3138
pass
3239

3340

34-
class Test_Errors(unittest.TestCase):
35-
36-
def setUp(self):
37-
pass
38-
41+
class TestErrors(unittest.TestCase):
3942
def test_create_folders(self):
40-
''' testing module '''
43+
"""Testing module."""
4144
create_folders(FOLDERS, verbose=True)
4245
# check folders and files exists
4346
for f in FOLDERS:
@@ -63,21 +66,22 @@ def test_create_folders(self):
6366

6467
@mock.patch('subprocess.Popen')
6568
def test_browse(self, mock_browse):
69+
"""Testing module."""
6670
# mocked Popen, create folder, browse, delete
6771
create_folder(FOLDERS[0])
6872
browse_folder(FOLDERS[0])
6973
# force false
7074
browse_folder(None)
7175

7276
def test_remove_dir(self):
73-
''' creates a folder with files and remove it '''
77+
"""Creates a folder with files and remove it."""
7478
test_dir = os.path.join(BASE_PATH, 'remove_dir')
7579
create_folder(test_dir)
7680
create_file(test_dir)
7781
remove_files_in_dir(test_dir)
7882

7983
def test_recursive_dir(self):
80-
''' creates a dir with sub dirs and files and copy them '''
84+
"""Creates a dir with sub dirs and files and copy them."""
8185
test_dir = os.path.join(BASE_PATH, 'recursive_dir')
8286
create_folder(test_dir)
8387
create_file(test_dir)

tests/test_json_user_settings.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
# ----------------------------------------------------------------------------------------
66

77
import contextlib
8-
from backpack.json_user_settings import JsonUserSettings
9-
10-
import unittest
118
import os
129
import sys
10+
import unittest
11+
12+
from backpack.json_user_settings import JsonUserSettings
1313

1414
mod_path = os.path.dirname(__file__)
1515
if mod_path not in sys.path:
@@ -21,55 +21,53 @@
2121
TEST_FOLDER = os.path.join(os.path.expanduser('~'), FOLDER)
2222

2323

24-
class Test_windows(unittest.TestCase):
25-
24+
class TestWindows(unittest.TestCase):
2625
@classmethod
2726
def setUp(cls):
28-
# remove test files
27+
"""Remove test files."""
2928
with contextlib.suppress(OSError):
3029
os.remove(TEST_FOLDER)
3130

3231
@classmethod
3332
def tearDownClass(cls):
34-
# remove test files
33+
"""Remove test files."""
3534
with contextlib.suppress(OSError):
3635
os.remove(TEST_FOLDER)
3736

3837
def test_json_user_settings(self):
39-
40-
# make sure folder does not exist
38+
"""Make sure folder does not exist."""
4139
with contextlib.suppress(PermissionError, OSError):
4240
if os.path.exists(TEST_FOLDER):
4341
os.removedirs(TEST_FOLDER)
4442

4543
# class and properties
4644
js = JsonUserSettings(FOLDER, 'user')
47-
assert (js.filepath)
48-
assert (os.path.exists(js.os_user_folder))
49-
assert (isinstance(js.user_data, dict))
45+
assert js.filepath
46+
assert os.path.exists(js.os_user_folder)
47+
assert isinstance(js.user_data, dict)
5048

5149
# load from a missing file
5250
js.filename = 'random_file'
5351
self.assertFalse(js.load_settings())
5452

5553
def test_json_settings_save(self):
56-
''' test json settings: save '''
54+
"""Test json settings: save."""
5755

5856
# save a setting
5957
js = JsonUserSettings(FOLDER, 'tox')
6058
data = {'age': 99}
61-
assert (js.save_settings(data))
59+
assert js.save_settings(data)
6260

6361
# load it back
6462
js = JsonUserSettings(FOLDER, 'tox')
6563
data = js.load_settings()
66-
assert (data['age'] == 99)
64+
assert data['age'] == 99
6765
# save custom setting
6866
js.user_data = {'custom': 'value'}
69-
assert (js.save_settings())
67+
assert js.save_settings()
7068
# load it back
7169
data = js.load_settings()
72-
assert (data['custom'] == 'value')
70+
assert data['custom'] == 'value'
7371

7472

7573
if __name__ == '__main__':

tests/test_json_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@
2121
BROKEN_JSON_FILE = os.path.join(mod_path, 'test_json', 'data_broken.json')
2222

2323

24-
class Test_Errors(unittest.TestCase):
25-
24+
class TestErrors(unittest.TestCase):
2625
def test_json_load(self):
26+
"""Test json_load function."""
2727
data = json_load(JSON_LOAD_FILE)
2828
self.assertTrue(type(data), dict)
2929
self.assertTrue(data.get('user'), 'Max')
3030
self.assertRaises(OSError, json_load, NO_FILE)
3131
self.assertRaises(OSError, json_load, BROKEN_JSON_FILE)
3232

3333
def test_json_save(self):
34+
"""Test json_save function."""
3435
data = {'name': 'max'}
3536
r = json_save(data, JSON_SAVE_FILE)
3637
self.assertTrue(r)

tests/test_jsonmd.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
# ----------------------------------------------------------------------------------------
66

77
import contextlib
8-
from backpack.json_metadata import JsonMetaFile
9-
10-
import unittest
118
import os
129
import sys
10+
import unittest
11+
12+
from backpack.json_metadata import JsonMetaFile
1313

1414
mod_path = os.path.dirname(__file__)
1515
if mod_path not in sys.path:
@@ -23,18 +23,17 @@
2323
ATTRIBUTES = ['foo', 'bar']
2424

2525

26-
class Test_windows(unittest.TestCase):
27-
26+
class TestWindows(unittest.TestCase):
2827
@classmethod
2928
def tearDownClass(cls):
30-
# remove test files
29+
"""Remove test files."""
3130
with contextlib.suppress(OSError):
3231
# os.remove(temp_update_json_test_file)
3332
# os.remove(test_json_file)
3433
pass
3534

3635
def test_metadata(self):
37-
''' testing module '''
36+
"""Testing module."""
3837
meta = JsonMetaFile(NAME, TEST_PATH)
3938

4039
# test properties
@@ -65,22 +64,22 @@ def test_metadata(self):
6564
meta.save()
6665

6766
# load
68-
metaObj = meta.load_as_class()
69-
self.assertEqual(type(metaObj), type)
70-
self.assertEqual(hasattr(metaObj, 'items'), True)
71-
self.assertEqual(metaObj.items, ATTRIBUTES)
67+
meta_obj = meta.load_as_class()
68+
self.assertEqual(type(meta_obj), type)
69+
self.assertEqual(hasattr(meta_obj, 'items'), True)
70+
self.assertEqual(meta_obj.items, ATTRIBUTES)
7271

7372
# load class
7473
meta = JsonMetaFile(NAME, TEST_PATH)
7574
meta.load()
7675

7776
def test_create_from_class(self):
78-
''' save_from_a_class '''
77+
"""save_from_a_class."""
7978
meta = JsonMetaFile(NAME, TEST_PATH)
8079
self.assertEqual(meta.name, NAME)
8180

82-
proxyClass = type('Proxy', (), {'foo': 12, 'items': ATTRIBUTES})
83-
meta.insert_class(proxyClass)
81+
proxy_class = type('Proxy', (), {'foo': 12, 'items': ATTRIBUTES})
82+
meta.insert_class(proxy_class)
8483
self.assertEqual(meta._data['foo'], 12)
8584
self.assertEqual(meta._data['items'], ATTRIBUTES)
8685
meta.save()

0 commit comments

Comments
 (0)