diff --git a/test/_common.py b/test/_common.py new file mode 100644 index 000000000..925a4db7a --- /dev/null +++ b/test/_common.py @@ -0,0 +1,27 @@ +"""Some common functionality for beets' test cases.""" +import time + +class Timecop(object): + """Mocks the timing system (namely time() and sleep()) for testing. + Inspired by the Ruby timecop library. + """ + def __init__(self): + self.now = time.time() + + def time(self): + return self.now + + def sleep(self, amount): + self.now += amount + + def install(self): + self.orig = { + 'time': time.time, + 'sleep': time.sleep, + } + time.time = self.time + time.sleep = self.sleep + + def restore(self): + time.time = self.orig['time'] + time.sleep = self.orig['sleep'] diff --git a/test/test_mb.py b/test/test_mb.py index 79744d868..444389802 100644 --- a/test/test_mb.py +++ b/test/test_mb.py @@ -17,19 +17,22 @@ import unittest import sys -import os import time -import re import musicbrainz2.model +import _common sys.path.append('..') from beets.autotag import mb -from beets.library import Item def nullfun(): pass class MBQueryWaitTest(unittest.TestCase): - def setup(self): + def setUp(self): # simulate startup mb.last_query_time = 0.0 + self.cop = _common.Timecop() + self.cop.install() + + def tearDown(self): + self.cop.restore() def test_do_not_wait_initially(self): time1 = time.time() @@ -42,7 +45,7 @@ class MBQueryWaitTest(unittest.TestCase): time1 = time.time() mb._query_wrap(nullfun) time2 = time.time() - self.assertTrue(time2 - time1 > 1.0) + self.assertTrue(time2 - time1 >= 1.0) def test_second_distant_query_does_not_wait(self): mb._query_wrap(nullfun) @@ -125,48 +128,6 @@ class MBReleaseDictTest(unittest.TestCase): self.assertFalse('month' in d) self.assertFalse('day' in d) -class MBWhiteBoxTest(unittest.TestCase): - def test_match_album_finds_el_producto(self): - a = iter(mb.match_album('the avalanches', 'el producto')).next() - self.assertEqual(a['album'], 'El Producto') - self.assertEqual(a['artist'], 'The Avalanches') - self.assertEqual(len(a['tracks']), 7) - - def test_match_album_tolerates_small_errors(self): - a = iter(mb.match_album('mia', 'kala ')).next() - self.assertEqual(a['artist'], 'M.I.A.') - self.assertEqual(a['album'], 'Kala') - -class ByIDTest(unittest.TestCase): - def test_match_found(self): - a = mb.album_for_id('73677f81-86e6-4087-9255-9ee81b8e3dd3') - self.assertEqual(a['artist'], 'Paul Simon') - self.assertEqual(a['album'], 'Graceland') - self.assertTrue(len(a['tracks']) > 0) - - def test_no_match_returns_none(self): - a = mb.album_for_id('bogus-id') - self.assertEqual(a, None) - -class SpecialCaseTest(unittest.TestCase): - def test_chkchkchk_by_query(self): - a = iter(mb.find_releases({ - 'artist': '!!!', - 'album': '!!!', - 'tracks': '7', - })).next() - self.assertEqual(a['artist'], '!!!') - self.assertEqual(a['album'], '!!!') - - def test_chkchkchk_by_keys(self): - a = iter(mb.get_releases( - artistName='!!!', - title='!!!', - trackCount=7, - )).next() - self.assertEqual(a['artist'], '!!!') - self.assertEqual(a['album'], '!!!') - def suite(): return unittest.TestLoader().loadTestsFromName(__name__)