Skip to content

Commit

Permalink
Added test for exporting seg to PathLike
Browse files Browse the repository at this point in the history
  • Loading branch information
jaden-young committed Feb 10, 2018
1 parent 17ed240 commit 108a932
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,35 @@ def test_non_existant_path_like_bytes(self):
with self.assertRaises(FileNotFoundError):
_ = AudioSegment.from_file(path)

def assertWithinRange(self, val, lower_bound, upper_bound):
self.assertTrue(lower_bound < val < upper_bound,
"%s is not in the acceptable range: %s - %s" %
(val, lower_bound, upper_bound))

def assertWithinTolerance(self, val, expected, tolerance=None,
percentage=None):
if percentage is not None:
tolerance = val * percentage
lower_bound = val - tolerance
upper_bound = val + tolerance
self.assertWithinRange(val, lower_bound, upper_bound)

def test_export_pathlib_path(self):
seg1 = AudioSegment.from_file(self.mp3_path_str)
from pathlib import Path
path = Path(tempfile.gettempdir()) / 'pydub-test-export-8ajds.mp3'
try:
seg1.export(path, format='mp3')
seg2 = AudioSegment.from_file(path, format='mp3')

self.assertEqual(len(seg1), len(seg2))
self.assertTrue(len(seg1) > 0)
self.assertWithinTolerance(len(seg1),
len(seg2),
percentage=0.01)
finally:
os.unlink(path)


class FileAccessTests(unittest.TestCase):

Expand Down

0 comments on commit 108a932

Please sign in to comment.