Best Python code snippet using lemoncheesecake
test_autotag.py
Source:test_autotag.py
...79 for i in range(5)]80 likelies, _ = match.current_metadata(items)81 for f in fields:82 self.assertEqual(likelies[f], '%s_1' % f)83def _make_item(title, track, artist=u'some artist'):84 return Item(title=title, track=track,85 artist=artist, album=u'some album',86 length=1,87 mb_trackid='', mb_albumid='', mb_artistid='')88def _make_trackinfo():89 return [90 TrackInfo(u'one', None, u'some artist', length=1, index=1),91 TrackInfo(u'two', None, u'some artist', length=1, index=2),92 TrackInfo(u'three', None, u'some artist', length=1, index=3),93 ]94def _clear_weights():95 """Hack around the lazy descriptor used to cache weights for96 Distance calculations.97 """98 Distance.__dict__['_weights'].computed = False99class DistanceTest(_common.TestCase):100 def tearDown(self):101 super(DistanceTest, self).tearDown()102 _clear_weights()103 def test_add(self):104 dist = Distance()105 dist.add('add', 1.0)106 self.assertEqual(dist._penalties, {'add': [1.0]})107 def test_add_equality(self):108 dist = Distance()109 dist.add_equality('equality', 'ghi', ['abc', 'def', 'ghi'])110 self.assertEqual(dist._penalties['equality'], [0.0])111 dist.add_equality('equality', 'xyz', ['abc', 'def', 'ghi'])112 self.assertEqual(dist._penalties['equality'], [0.0, 1.0])113 dist.add_equality('equality', 'abc', re.compile(r'ABC', re.I))114 self.assertEqual(dist._penalties['equality'], [0.0, 1.0, 0.0])115 def test_add_expr(self):116 dist = Distance()117 dist.add_expr('expr', True)118 self.assertEqual(dist._penalties['expr'], [1.0])119 dist.add_expr('expr', False)120 self.assertEqual(dist._penalties['expr'], [1.0, 0.0])121 def test_add_number(self):122 dist = Distance()123 # Add a full penalty for each number of difference between two numbers.124 dist.add_number('number', 1, 1)125 self.assertEqual(dist._penalties['number'], [0.0])126 dist.add_number('number', 1, 2)127 self.assertEqual(dist._penalties['number'], [0.0, 1.0])128 dist.add_number('number', 2, 1)129 self.assertEqual(dist._penalties['number'], [0.0, 1.0, 1.0])130 dist.add_number('number', -1, 2)131 self.assertEqual(dist._penalties['number'], [0.0, 1.0, 1.0, 1.0,132 1.0, 1.0])133 def test_add_priority(self):134 dist = Distance()135 dist.add_priority('priority', 'abc', 'abc')136 self.assertEqual(dist._penalties['priority'], [0.0])137 dist.add_priority('priority', 'def', ['abc', 'def'])138 self.assertEqual(dist._penalties['priority'], [0.0, 0.5])139 dist.add_priority('priority', 'gh', ['ab', 'cd', 'ef',140 re.compile('GH', re.I)])141 self.assertEqual(dist._penalties['priority'], [0.0, 0.5, 0.75])142 dist.add_priority('priority', 'xyz', ['abc', 'def'])143 self.assertEqual(dist._penalties['priority'], [0.0, 0.5, 0.75,144 1.0])145 def test_add_ratio(self):146 dist = Distance()147 dist.add_ratio('ratio', 25, 100)148 self.assertEqual(dist._penalties['ratio'], [0.25])149 dist.add_ratio('ratio', 10, 5)150 self.assertEqual(dist._penalties['ratio'], [0.25, 1.0])151 dist.add_ratio('ratio', -5, 5)152 self.assertEqual(dist._penalties['ratio'], [0.25, 1.0, 0.0])153 dist.add_ratio('ratio', 5, 0)154 self.assertEqual(dist._penalties['ratio'], [0.25, 1.0, 0.0, 0.0])155 def test_add_string(self):156 dist = Distance()157 sdist = string_dist(u'abc', u'bcd')158 dist.add_string('string', u'abc', u'bcd')159 self.assertEqual(dist._penalties['string'], [sdist])160 self.assertNotEqual(dist._penalties['string'], [0])161 def test_add_string_none(self):162 dist = Distance()163 dist.add_string('string', None, 'string')164 self.assertEqual(dist._penalties['string'], [1])165 def test_add_string_both_none(self):166 dist = Distance()167 dist.add_string('string', None, None)168 self.assertEqual(dist._penalties['string'], [0])169 def test_distance(self):170 config['match']['distance_weights']['album'] = 2.0171 config['match']['distance_weights']['medium'] = 1.0172 _clear_weights()173 dist = Distance()174 dist.add('album', 0.5)175 dist.add('media', 0.25)176 dist.add('media', 0.75)177 self.assertEqual(dist.distance, 0.5)178 # __getitem__()179 self.assertEqual(dist['album'], 0.25)180 self.assertEqual(dist['media'], 0.25)181 def test_max_distance(self):182 config['match']['distance_weights']['album'] = 3.0183 config['match']['distance_weights']['medium'] = 1.0184 _clear_weights()185 dist = Distance()186 dist.add('album', 0.5)187 dist.add('medium', 0.0)188 dist.add('medium', 0.0)189 self.assertEqual(dist.max_distance, 5.0)190 def test_operators(self):191 config['match']['distance_weights']['source'] = 1.0192 config['match']['distance_weights']['album'] = 2.0193 config['match']['distance_weights']['medium'] = 1.0194 _clear_weights()195 dist = Distance()196 dist.add('source', 0.0)197 dist.add('album', 0.5)198 dist.add('medium', 0.25)199 dist.add('medium', 0.75)200 self.assertEqual(len(dist), 2)201 self.assertEqual(list(dist), [('album', 0.2), ('medium', 0.2)])202 self.assertTrue(dist == 0.4)203 self.assertTrue(dist < 1.0)204 self.assertTrue(dist > 0.0)205 self.assertEqual(dist - 0.4, 0.0)206 self.assertEqual(0.4 - dist, 0.0)207 self.assertEqual(float(dist), 0.4)208 def test_raw_distance(self):209 config['match']['distance_weights']['album'] = 3.0210 config['match']['distance_weights']['medium'] = 1.0211 _clear_weights()212 dist = Distance()213 dist.add('album', 0.5)214 dist.add('medium', 0.25)215 dist.add('medium', 0.5)216 self.assertEqual(dist.raw_distance, 2.25)217 def test_items(self):218 config['match']['distance_weights']['album'] = 4.0219 config['match']['distance_weights']['medium'] = 2.0220 _clear_weights()221 dist = Distance()222 dist.add('album', 0.1875)223 dist.add('medium', 0.75)224 self.assertEqual(dist.items(), [('medium', 0.25), ('album', 0.125)])225 # Sort by key if distance is equal.226 dist = Distance()227 dist.add('album', 0.375)228 dist.add('medium', 0.75)229 self.assertEqual(dist.items(), [('album', 0.25), ('medium', 0.25)])230 def test_update(self):231 dist1 = Distance()232 dist1.add('album', 0.5)233 dist1.add('media', 1.0)234 dist2 = Distance()235 dist2.add('album', 0.75)236 dist2.add('album', 0.25)237 dist2.add('media', 0.05)238 dist1.update(dist2)239 self.assertEqual(dist1._penalties, {'album': [0.5, 0.75, 0.25],240 'media': [1.0, 0.05]})241class TrackDistanceTest(_common.TestCase):242 def test_identical_tracks(self):243 item = _make_item(u'one', 1)244 info = _make_trackinfo()[0]245 dist = match.track_distance(item, info, incl_artist=True)246 self.assertEqual(dist, 0.0)247 def test_different_title(self):248 item = _make_item(u'foo', 1)249 info = _make_trackinfo()[0]250 dist = match.track_distance(item, info, incl_artist=True)251 self.assertNotEqual(dist, 0.0)252 def test_different_artist(self):253 item = _make_item(u'one', 1)254 item.artist = u'foo'255 info = _make_trackinfo()[0]256 dist = match.track_distance(item, info, incl_artist=True)257 self.assertNotEqual(dist, 0.0)258 def test_various_artists_tolerated(self):259 item = _make_item(u'one', 1)260 item.artist = u'Various Artists'261 info = _make_trackinfo()[0]262 dist = match.track_distance(item, info, incl_artist=True)263 self.assertEqual(dist, 0.0)264class AlbumDistanceTest(_common.TestCase):265 def _mapping(self, items, info):266 out = {}267 for i, t in zip(items, info.tracks):268 out[i] = t269 return out270 def _dist(self, items, info):271 return match.distance(items, info, self._mapping(items, info))272 def test_identical_albums(self):273 items = []274 items.append(_make_item(u'one', 1))275 items.append(_make_item(u'two', 2))276 items.append(_make_item(u'three', 3))277 info = AlbumInfo(278 artist=u'some artist',279 album=u'some album',280 tracks=_make_trackinfo(),281 va=False,282 album_id=None,283 artist_id=None,284 )285 self.assertEqual(self._dist(items, info), 0)286 def test_incomplete_album(self):287 items = []288 items.append(_make_item(u'one', 1))289 items.append(_make_item(u'three', 3))290 info = AlbumInfo(291 artist=u'some artist',292 album=u'some album',293 tracks=_make_trackinfo(),294 va=False,295 album_id=None,296 artist_id=None,297 )298 dist = self._dist(items, info)299 self.assertNotEqual(dist, 0)300 # Make sure the distance is not too great301 self.assertTrue(dist < 0.2)302 def test_global_artists_differ(self):303 items = []304 items.append(_make_item(u'one', 1))305 items.append(_make_item(u'two', 2))306 items.append(_make_item(u'three', 3))307 info = AlbumInfo(308 artist=u'someone else',309 album=u'some album',310 tracks=_make_trackinfo(),311 va=False,312 album_id=None,313 artist_id=None,314 )315 self.assertNotEqual(self._dist(items, info), 0)316 def test_comp_track_artists_match(self):317 items = []318 items.append(_make_item(u'one', 1))319 items.append(_make_item(u'two', 2))320 items.append(_make_item(u'three', 3))321 info = AlbumInfo(322 artist=u'should be ignored',323 album=u'some album',324 tracks=_make_trackinfo(),325 va=True,326 album_id=None,327 artist_id=None,328 )329 self.assertEqual(self._dist(items, info), 0)330 def test_comp_no_track_artists(self):331 # Some VA releases don't have track artists (incomplete metadata).332 items = []333 items.append(_make_item(u'one', 1))334 items.append(_make_item(u'two', 2))335 items.append(_make_item(u'three', 3))336 info = AlbumInfo(337 artist=u'should be ignored',338 album=u'some album',339 tracks=_make_trackinfo(),340 va=True,341 album_id=None,342 artist_id=None,343 )344 info.tracks[0].artist = None345 info.tracks[1].artist = None346 info.tracks[2].artist = None347 self.assertEqual(self._dist(items, info), 0)348 def test_comp_track_artists_do_not_match(self):349 items = []350 items.append(_make_item(u'one', 1))351 items.append(_make_item(u'two', 2, u'someone else'))352 items.append(_make_item(u'three', 3))353 info = AlbumInfo(354 artist=u'some artist',355 album=u'some album',356 tracks=_make_trackinfo(),357 va=True,358 album_id=None,359 artist_id=None,360 )361 self.assertNotEqual(self._dist(items, info), 0)362 def test_tracks_out_of_order(self):363 items = []364 items.append(_make_item(u'one', 1))365 items.append(_make_item(u'three', 2))366 items.append(_make_item(u'two', 3))367 info = AlbumInfo(368 artist=u'some artist',369 album=u'some album',370 tracks=_make_trackinfo(),371 va=False,372 album_id=None,373 artist_id=None,374 )375 dist = self._dist(items, info)376 self.assertTrue(0 < dist < 0.2)377 def test_two_medium_release(self):378 items = []379 items.append(_make_item(u'one', 1))380 items.append(_make_item(u'two', 2))381 items.append(_make_item(u'three', 3))382 info = AlbumInfo(383 artist=u'some artist',384 album=u'some album',385 tracks=_make_trackinfo(),386 va=False,387 album_id=None,388 artist_id=None,389 )390 info.tracks[0].medium_index = 1391 info.tracks[1].medium_index = 2392 info.tracks[2].medium_index = 1393 dist = self._dist(items, info)394 self.assertEqual(dist, 0)395 def test_per_medium_track_numbers(self):396 items = []397 items.append(_make_item(u'one', 1))398 items.append(_make_item(u'two', 2))399 items.append(_make_item(u'three', 1))400 info = AlbumInfo(401 artist=u'some artist',402 album=u'some album',403 tracks=_make_trackinfo(),404 va=False,405 album_id=None,406 artist_id=None,407 )408 info.tracks[0].medium_index = 1409 info.tracks[1].medium_index = 2410 info.tracks[2].medium_index = 1411 dist = self._dist(items, info)412 self.assertEqual(dist, 0)413class AssignmentTest(unittest.TestCase):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!