Skip to content

Commit

Permalink
Replace unittest assert methods by assert statement (#5386)
Browse files Browse the repository at this point in the history
... continuing the test suite migration to `pytest` (#5361), this PR
replaces `unittest` method assertions with `assert` statement.

See the table below for a list of the replaced methods and their counts.
This should help clarify the size of the pull request 😆.

To keep things simple for the review, I have replaced each method in its
own commit. This
way, things should be easy to follow if you step through the commits one
by one. 🙂

method                   | count
---                      | ---
**`assertEqual`**        | 1747
**`assertIn`**           | 200
**`assertTrue`**         | 149
**`assertIsNone`**       | 87
**`assertFalse`**        | 85
**`assertRaises`**       | 69
**`assertNotIn`**        | 64
**`assertNotEqual`**     | 39
**`assertIsNotNone`**    | 35
**`assertIsInstance`**   | 35
**`assertLessEqual`**    | 23
**`assertGreater`**      | 15
**`assertLess`**         | 14
**`assertGreaterEqual`** | 14
**`assertAlmostEqual`**  | 9
**`assertRaisesRegex`**  | 4
**`assertCountEqual`**   | 2
**`assertListEqual`**    | 1


❗ Note that this *only* replaces methods provided by
`unittest` by default.
**Custom** assertion method replacements (like `assertExists` will be
addressed in a separate PR).
  • Loading branch information
snejus committed Aug 19, 2024
2 parents 093949b + 4b69b49 commit 38a26af
Show file tree
Hide file tree
Showing 70 changed files with 2,963 additions and 3,271 deletions.
30 changes: 11 additions & 19 deletions beets/test/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,36 +152,28 @@ class Assertions:
"""A mixin with additional unit test assertions."""

def assertExists(self, path): # noqa
self.assertTrue(
os.path.exists(syspath(path)), f"file does not exist: {path!r}"
)
assert os.path.exists(syspath(path)), f"file does not exist: {path!r}"

def assertNotExists(self, path): # noqa
self.assertFalse(
os.path.exists(syspath(path)), f"file exists: {path!r}"
)
assert not os.path.exists(syspath(path)), f"file exists: {path!r}"

def assertIsFile(self, path): # noqa
self.assertExists(path)
self.assertTrue(
os.path.isfile(syspath(path)),
"path exists, but is not a regular file: {!r}".format(path),
)
assert os.path.isfile(
syspath(path)
), "path exists, but is not a regular file: {!r}".format(path)

def assertIsDir(self, path): # noqa
self.assertExists(path)
self.assertTrue(
os.path.isdir(syspath(path)),
"path exists, but is not a directory: {!r}".format(path),
)
assert os.path.isdir(
syspath(path)
), "path exists, but is not a directory: {!r}".format(path)

def assert_equal_path(self, a, b):
"""Check that two paths are equal."""
self.assertEqual(
util.normpath(a),
util.normpath(b),
f"paths are not equal: {a!r} and {b!r}",
)
a_bytes, b_bytes = util.normpath(a), util.normpath(b)

assert a_bytes == b_bytes, f"{a_bytes=} != {b_bytes=}"


# Mock I/O.
Expand Down
2 changes: 1 addition & 1 deletion beets/test/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ def assert_file_not_in_lib(self, *segments):
self.assertNotExists(os.path.join(self.libdir, *segments))

def assert_lib_dir_empty(self):
self.assertEqual(len(os.listdir(syspath(self.libdir))), 0)
assert not os.listdir(syspath(self.libdir))


class AsIsImporterMixin:
Expand Down
24 changes: 11 additions & 13 deletions test/plugins/test_acousticbrainz.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ def test_basic(self):
data = {"key 1": "value 1", "key 2": "value 2"}
scheme = {"key 1": "attribute 1", "key 2": "attribute 2"}
mapping = set(ab._map_data_to_scheme(data, scheme))
self.assertEqual(
mapping, {("attribute 1", "value 1"), ("attribute 2", "value 2")}
)
assert mapping == {
("attribute 1", "value 1"),
("attribute 2", "value 2"),
}

def test_recurse(self):
ab = AcousticPlugin()
Expand All @@ -51,21 +52,18 @@ def test_recurse(self):
},
}
mapping = set(ab._map_data_to_scheme(data, scheme))
self.assertEqual(
mapping,
{
("attribute 1", "value"),
("attribute 2", "subvalue"),
("attribute 3", "subsubvalue"),
},
)
assert mapping == {
("attribute 1", "value"),
("attribute 2", "subvalue"),
("attribute 3", "subsubvalue"),
}

def test_composite(self):
ab = AcousticPlugin()
data = {"key 1": "part 1", "key 2": "part 2"}
scheme = {"key 1": ("attribute", 0), "key 2": ("attribute", 1)}
mapping = set(ab._map_data_to_scheme(data, scheme))
self.assertEqual(mapping, {("attribute", "part 1 part 2")})
assert mapping == {("attribute", "part 1 part 2")}

def test_realistic(self):
ab = AcousticPlugin()
Expand Down Expand Up @@ -98,4 +96,4 @@ def test_realistic(self):
("moods_mirex", "Cluster3"),
("timbre", "bright"),
}
self.assertEqual(mapping, expected)
assert mapping == expected
26 changes: 14 additions & 12 deletions test/plugins/test_advancedrewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"""


import pytest

from beets.test.helper import PluginTestCase
from beets.ui import UserError

Expand All @@ -35,7 +37,7 @@ def test_simple_rewrite_example(self):
albumartist="ODD EYE CIRCLE",
)

self.assertEqual(item.artist, "이달의 소녀 오드아이써클")
assert item.artist == "이달의 소녀 오드아이써클"

def test_advanced_rewrite_example(self):
with self.configure_plugin(
Expand Down Expand Up @@ -63,12 +65,12 @@ def test_advanced_rewrite_example(self):
)

# Assert that all replacements were applied to item_a
self.assertEqual("이달의 소녀 오드아이써클", item_a.artist)
self.assertEqual("LOONA / ODD EYE CIRCLE", item_a.artist_sort)
self.assertEqual("LOONA / ODD EYE CIRCLE", item_a.albumartist_sort)
assert "이달의 소녀 오드아이써클" == item_a.artist
assert "LOONA / ODD EYE CIRCLE" == item_a.artist_sort
assert "LOONA / ODD EYE CIRCLE" == item_a.albumartist_sort

# Assert that no replacements were applied to item_b
self.assertEqual("ODD EYE CIRCLE", item_b.artist)
assert "ODD EYE CIRCLE" == item_b.artist

def test_advanced_rewrite_example_with_multi_valued_field(self):
with self.configure_plugin(
Expand All @@ -84,19 +86,19 @@ def test_advanced_rewrite_example_with_multi_valued_field(self):
artists=["배유빈", "김미현"],
)

self.assertEqual(item.artists, ["유빈", "미미"])
assert item.artists == ["유빈", "미미"]

def test_fail_when_replacements_empty(self):
with self.assertRaises(
with pytest.raises(
UserError,
msg="Advanced rewrites must have at least one replacement",
match="Advanced rewrites must have at least one replacement",
), self.configure_plugin([{"match": "artist:A", "replacements": {}}]):
pass

def test_fail_when_rewriting_single_valued_field_with_list(self):
with self.assertRaises(
with pytest.raises(
UserError,
msg="Field artist is not a multi-valued field but a list was given: C, D",
match="Field artist is not a multi-valued field but a list was given: C, D",
), self.configure_plugin(
[
{
Expand All @@ -115,7 +117,7 @@ def test_combined_rewrite_example(self):
]
):
item = self.add_item(artist="A", albumartist="A")
self.assertEqual(item.artist, "B")
assert item.artist == "B"

item = self.add_item(artist="C", albumartist="C", album="C")
self.assertEqual(item.artist, "D")
assert item.artist == "D"
10 changes: 5 additions & 5 deletions test/plugins/test_albumtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_renames_types(self):
album = self._create_album(album_types=["ep", "remix"])
subject = AlbumTypesPlugin()
result = subject._atypes(album)
self.assertEqual("(EP)(Remix)", result)
assert "(EP)(Remix)" == result
return

def test_returns_only_specified_types(self):
Expand All @@ -46,7 +46,7 @@ def test_returns_only_specified_types(self):
album = self._create_album(album_types=["ep", "remix", "soundtrack"])
subject = AlbumTypesPlugin()
result = subject._atypes(album)
self.assertEqual("(EP)", result)
assert "(EP)" == result

def test_respects_type_order(self):
"""Tests if the types are returned in the same order as config."""
Expand All @@ -56,7 +56,7 @@ def test_respects_type_order(self):
album = self._create_album(album_types=["ep", "remix"])
subject = AlbumTypesPlugin()
result = subject._atypes(album)
self.assertEqual("(Remix)(EP)", result)
assert "(Remix)(EP)" == result
return

def test_ignores_va(self):
Expand All @@ -71,7 +71,7 @@ def test_ignores_va(self):
)
subject = AlbumTypesPlugin()
result = subject._atypes(album)
self.assertEqual("(OST)", result)
assert "(OST)" == result

def test_respects_defaults(self):
"""Tests if the plugin uses the default values if config not given."""
Expand All @@ -88,7 +88,7 @@ def test_respects_defaults(self):
)
subject = AlbumTypesPlugin()
result = subject._atypes(album)
self.assertEqual("[EP][Single][OST][Live][Remix]", result)
assert "[EP][Single][OST][Live][Remix]" == result

def _set_config(
self,
Expand Down
Loading

0 comments on commit 38a26af

Please sign in to comment.