Replace assertAlmostEqual

This commit is contained in:
Šarūnas Nejus 2024-08-12 06:59:38 +01:00
parent fcc4d8481d
commit d3bdd93bf6
No known key found for this signature in database
GPG key ID: DD28F6704DBE3435
4 changed files with 18 additions and 13 deletions

View file

@ -17,6 +17,8 @@
import os import os
import pytest
from beets import importer from beets import importer
from beets.test.helper import AutotagStub, ImportTestCase, PluginMixin from beets.test.helper import AutotagStub, ImportTestCase, PluginMixin
from beets.util import displayable_path, syspath from beets.util import displayable_path, syspath
@ -74,7 +76,7 @@ class ImportAddedTest(PluginMixin, ImportTestCase):
def assertEqualTimes(self, first, second, msg=None): # noqa def assertEqualTimes(self, first, second, msg=None): # noqa
"""For comparing file modification times at a sufficient precision""" """For comparing file modification times at a sufficient precision"""
self.assertAlmostEqual(first, second, places=4, msg=msg) assert first == pytest.approx(second, rel=1e-4), msg
def assertAlbumImport(self): # noqa def assertAlbumImport(self): # noqa
self.importer.run() self.importer.run()

View file

@ -12,8 +12,7 @@
# The above copyright notice and this permission notice shall be # The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software. # included in all copies or substantial portions of the Software.
"""Tests for BPD's implementation of the MPD protocol. """Tests for BPD's implementation of the MPD protocol."""
"""
import importlib.util import importlib.util
import multiprocessing as mp import multiprocessing as mp
@ -30,6 +29,7 @@ from contextlib import contextmanager
from unittest import mock from unittest import mock
import confuse import confuse
import pytest
import yaml import yaml
from beets.test.helper import PluginTestCase from beets.test.helper import PluginTestCase
@ -703,13 +703,13 @@ class BPDPlaybackTest(BPDTestHelper):
self._assert_failed(responses, bpd.ERROR_ARG, pos=3) self._assert_failed(responses, bpd.ERROR_ARG, pos=3)
self._assert_failed(response, bpd.ERROR_ARG) self._assert_failed(response, bpd.ERROR_ARG)
assert "xfade" not in responses[0].data assert "xfade" not in responses[0].data
self.assertAlmostEqual(123, int(responses[2].data["xfade"])) assert 123 == pytest.approx(int(responses[2].data["xfade"]))
def test_cmd_mixrampdb(self): def test_cmd_mixrampdb(self):
with self.run_bpd() as client: with self.run_bpd() as client:
responses = client.send_commands(("mixrampdb", "-17"), ("status",)) responses = client.send_commands(("mixrampdb", "-17"), ("status",))
self._assert_ok(*responses) self._assert_ok(*responses)
self.assertAlmostEqual(-17, float(responses[1].data["mixrampdb"])) assert -17 == pytest.approx(float(responses[1].data["mixrampdb"]))
def test_cmd_mixrampdelay(self): def test_cmd_mixrampdelay(self):
with self.run_bpd() as client: with self.run_bpd() as client:
@ -721,7 +721,7 @@ class BPDPlaybackTest(BPDTestHelper):
("mixrampdelay", "-2"), ("mixrampdelay", "-2"),
) )
self._assert_failed(responses, bpd.ERROR_ARG, pos=4) self._assert_failed(responses, bpd.ERROR_ARG, pos=4)
self.assertAlmostEqual(2, float(responses[1].data["mixrampdelay"])) assert 2 == pytest.approx(float(responses[1].data["mixrampdelay"]))
assert "mixrampdelay" not in responses[3].data assert "mixrampdelay" not in responses[3].data
def test_cmd_setvol(self): def test_cmd_setvol(self):
@ -753,7 +753,7 @@ class BPDPlaybackTest(BPDTestHelper):
("replay_gain_mode", "notanoption"), ("replay_gain_mode", "notanoption"),
) )
self._assert_failed(responses, bpd.ERROR_ARG, pos=2) self._assert_failed(responses, bpd.ERROR_ARG, pos=2)
self.assertAlmostEqual("track", responses[1].data["replay_gain_mode"]) assert "track" == responses[1].data["replay_gain_mode"]
class BPDControlTest(BPDTestHelper): class BPDControlTest(BPDTestHelper):

View file

@ -20,6 +20,8 @@ import math
import unittest import unittest
from random import Random from random import Random
import pytest
from beets import random from beets import random
from beets.test.helper import TestHelper from beets.test.helper import TestHelper
@ -74,6 +76,6 @@ class RandomTest(TestHelper, unittest.TestCase):
mean1, stdev1, median1 = experiment("artist") mean1, stdev1, median1 = experiment("artist")
mean2, stdev2, median2 = experiment("track") mean2, stdev2, median2 = experiment("track")
self.assertAlmostEqual(0, median1, delta=1) assert 0 == pytest.approx(median1, abs=1)
self.assertAlmostEqual(len(self.items) // 2, median2, delta=1) assert len(self.items) // 2 == pytest.approx(median2, abs=1)
assert stdev2 > stdev1 assert stdev2 > stdev1

View file

@ -16,6 +16,7 @@
import unittest import unittest
from typing import ClassVar from typing import ClassVar
import pytest
from mediafile import MediaFile from mediafile import MediaFile
from beets import config from beets import config
@ -149,11 +150,11 @@ class ReplayGainCliTest:
assert item.rg_track_peak is not None assert item.rg_track_peak is not None
assert item.rg_track_gain is not None assert item.rg_track_gain is not None
mediafile = MediaFile(item.path) mediafile = MediaFile(item.path)
self.assertAlmostEqual( assert mediafile.rg_track_peak == pytest.approx(
mediafile.rg_track_peak, item.rg_track_peak, places=6 item.rg_track_peak, abs=1e-6
) )
self.assertAlmostEqual( assert mediafile.rg_track_gain == pytest.approx(
mediafile.rg_track_gain, item.rg_track_gain, places=2 item.rg_track_gain, abs=1e-2
) )
def test_cli_skips_calculated_tracks(self): def test_cli_skips_calculated_tracks(self):