Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 65ba2b0e02
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 438 lines (360 sloc) 14.728 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
# This file is part of beets.
# Copyright 2011, Adrian Sampson.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

"""Various tests for querying the library database.
"""
import os

import _common
from _common import unittest
import beets.library

pqp = beets.library.CollectionQuery._parse_query_part

some_item = _common.item()

class QueryParseTest(unittest.TestCase):
    def test_one_basic_term(self):
        q = 'test'
        r = (None, 'test', False)
        self.assertEqual(pqp(q), r)

    def test_one_keyed_term(self):
        q = 'test:val'
        r = ('test', 'val', False)
        self.assertEqual(pqp(q), r)

    def test_colon_at_end(self):
        q = 'test:'
        r = (None, 'test:', False)
        self.assertEqual(pqp(q), r)

    def test_one_basic_regexp(self):
        q = r':regexp'
        r = (None, 'regexp', True)
        self.assertEqual(pqp(q), r)

    def test_keyed_regexp(self):
        q = r'test::regexp'
        r = ('test', 'regexp', True)
        self.assertEqual(pqp(q), r)

    def test_escaped_colon(self):
        q = r'test\:val'
        r = (None, 'test:val', False)
        self.assertEqual(pqp(q), r)

    def test_escaped_colon_in_regexp(self):
        q = r':test\:regexp'
        r = (None, 'test:regexp', True)
        self.assertEqual(pqp(q), r)

class AnySubstringQueryTest(unittest.TestCase):
    def setUp(self):
        self.lib = beets.library.Library(':memory:')
        self.lib.add(some_item)

    def test_no_restriction(self):
        q = beets.library.AnySubstringQuery('title')
        self.assertEqual(self.lib.items(q).next().title, 'the title')

    def test_restriction_completeness(self):
        q = beets.library.AnySubstringQuery('title', ['title'])
        self.assertEqual(self.lib.items(q).next().title, 'the title')

    def test_restriction_soundness(self):
        q = beets.library.AnySubstringQuery('title', ['artist'])
        self.assertRaises(StopIteration, self.lib.items(q).next)

class AnyRegexpQueryTest(unittest.TestCase):
    def setUp(self):
        self.lib = beets.library.Library(':memory:')
        self.lib.add(some_item)

    def test_no_restriction(self):
        q = beets.library.AnyRegexpQuery(r'^the ti')
        self.assertEqual(self.lib.items(q).next().title, 'the title')

    def test_restriction_completeness(self):
        q = beets.library.AnyRegexpQuery(r'^the ti', ['title'])
        self.assertEqual(self.lib.items(q).next().title, 'the title')

    def test_restriction_soundness(self):
        q = beets.library.AnyRegexpQuery(r'^the ti', ['artist'])
        self.assertRaises(StopIteration, self.lib.items(q).next)

    def test_restriction_soundness_2(self):
        q = beets.library.AnyRegexpQuery(r'the ti$', ['title'])
        self.assertRaises(StopIteration, self.lib.items(q).next)

# Convenient asserts for matching items.
class AssertsMixin(object):
    def assert_matched(self, result_iterator, title):
        self.assertEqual(result_iterator.next().title, title)
    def assert_done(self, result_iterator):
        self.assertRaises(StopIteration, result_iterator.next)
    def assert_matched_all(self, result_iterator):
        self.assert_matched(result_iterator, 'Littlest Things')
        self.assert_matched(result_iterator, 'Take Pills')
        self.assert_matched(result_iterator, 'Lovers Who Uncover')
        self.assert_matched(result_iterator, 'Boracay')
        self.assert_done(result_iterator)

class GetTest(unittest.TestCase, AssertsMixin):
    def setUp(self):
        self.lib = beets.library.Library(
            os.path.join(_common.RSRC, 'test.blb')
        )

    def test_get_empty(self):
        q = ''
        results = self.lib.items(q)
        self.assert_matched_all(results)

    def test_get_none(self):
        q = None
        results = self.lib.items(q)
        self.assert_matched_all(results)

    def test_get_one_keyed_term(self):
        q = 'artist:Lil'
        results = self.lib.items(q)
        self.assert_matched(results, 'Littlest Things')
        self.assert_done(results)

    def test_get_one_keyed_regexp(self):
        q = r'artist::L.+y'
        results = self.lib.items(q)
        self.assert_matched(results, 'Littlest Things')
        self.assert_done(results)

    def test_get_one_unkeyed_term(self):
        q = 'Terry'
        results = self.lib.items(q)
        self.assert_matched(results, 'Boracay')
        self.assert_done(results)

    def test_get_one_unkeyed_regexp(self):
        q = r':y$'
        results = self.lib.items(q)
        self.assert_matched(results, 'Boracay')
        self.assert_done(results)

    def test_get_no_matches(self):
        q = 'popebear'
        results = self.lib.items(q)
        self.assert_done(results)

    def test_invalid_key(self):
        q = 'pope:bear'
        results = self.lib.items(q)
        self.assert_matched_all(results)

    def test_term_case_insensitive(self):
        q = 'UNCoVER'
        results = self.lib.items(q)
        self.assert_matched(results, 'Lovers Who Uncover')
        self.assert_done(results)

    def test_regexp_case_sensitive(self):
        q = r':UNCoVER'
        results = self.lib.items(q)
        self.assert_done(results)
        q = r':Uncover'
        results = self.lib.items(q)
        self.assert_matched(results, 'Lovers Who Uncover')
        self.assert_done(results)

    def test_term_case_insensitive_with_key(self):
        q = 'album:stiLL'
        results = self.lib.items(q)
        self.assert_matched(results, 'Littlest Things')
        self.assert_done(results)

    def test_key_case_insensitive(self):
        q = 'ArTiST:Allen'
        results = self.lib.items(q)
        self.assert_matched(results, 'Littlest Things')
        self.assert_done(results)

    def test_unkeyed_term_matches_multiple_columns(self):
        q = 'little'
        results = self.lib.items(q)
        self.assert_matched(results, 'Littlest Things')
        self.assert_matched(results, 'Lovers Who Uncover')
        self.assert_matched(results, 'Boracay')
        self.assert_done(results)

    def test_unkeyed_regexp_matches_multiple_columns(self):
        q = r':^T'
        results = self.lib.items(q)
        self.assert_matched(results, 'Take Pills')
        self.assert_matched(results, 'Lovers Who Uncover')
        self.assert_matched(results, 'Boracay')
        self.assert_done(results)

    def test_keyed_term_matches_only_one_column(self):
        q = 'artist:little'
        results = self.lib.items(q)
        self.assert_matched(results, 'Lovers Who Uncover')
        self.assert_matched(results, 'Boracay')
        self.assert_done(results)

    def test_keyed_regexp_matches_only_one_column(self):
        q = r'album::\sS'
        results = self.lib.items(q)
        self.assert_matched(results, 'Littlest Things')
        self.assert_matched(results, 'Lovers Who Uncover')
        self.assert_done(results)

    def test_multiple_terms_narrow_search(self):
        q = 'little ones'
        results = self.lib.items(q)
        self.assert_matched(results, 'Lovers Who Uncover')
        self.assert_matched(results, 'Boracay')
        self.assert_done(results)

    def test_multiple_regexps_narrow_search(self):
        q = r':\sS :^T'
        results = self.lib.items(q)
        self.assert_matched(results, 'Lovers Who Uncover')
        self.assert_done(results)

    def test_mixed_terms_regexps_narrow_search(self):
        q = r':\sS lily'
        results = self.lib.items(q)
        self.assert_matched(results, 'Littlest Things')
        self.assert_done(results)

class MemoryGetTest(unittest.TestCase, AssertsMixin):
    def setUp(self):
        self.album_item = _common.item()
        self.album_item.title = 'album item'
        self.single_item = _common.item()
        self.single_item.title = 'singleton item'
        self.single_item.comp = False

        self.lib = beets.library.Library(':memory:')
        self.lib.add(self.single_item)
        self.album = self.lib.add_album([self.album_item])

    def test_singleton_true(self):
        q = 'singleton:true'
        results = self.lib.items(q)
        self.assert_matched(results, 'singleton item')
        self.assert_done(results)

    def test_singleton_false(self):
        q = 'singleton:false'
        results = self.lib.items(q)
        self.assert_matched(results, 'album item')
        self.assert_done(results)

    def test_compilation_true(self):
        q = 'comp:true'
        results = self.lib.items(q)
        self.assert_matched(results, 'album item')
        self.assert_done(results)

    def test_compilation_false(self):
        q = 'comp:false'
        results = self.lib.items(q)
        self.assert_matched(results, 'singleton item')
        self.assert_done(results)

    def test_unknown_field_name_ignored(self):
        q = 'xyzzy:nonsense'
        results = self.lib.items(q)
        titles = [i.title for i in results]
        self.assertTrue('singleton item' in titles)
        self.assertTrue('album item' in titles)
        self.assertEqual(len(titles), 2)

    def test_unknown_field_name_ignored_in_album_query(self):
        q = 'xyzzy:nonsense'
        results = self.lib.albums(q)
        names = [a.album for a in results]
        self.assertEqual(names, ['the album'])

    def test_item_field_name_ignored_in_album_query(self):
        q = 'format:nonsense'
        results = self.lib.albums(q)
        names = [a.album for a in results]
        self.assertEqual(names, ['the album'])

    def test_unicode_query(self):
        self.single_item.title = u'caf\xe9'
        self.lib.store(self.single_item)

        q = u'title:caf\xe9'
        results = self.lib.items(q)
        self.assert_matched(results, u'caf\xe9')
        self.assert_done(results)

class PathQueryTest(unittest.TestCase, AssertsMixin):
    def setUp(self):
        self.lib = beets.library.Library(':memory:')

        path_item = _common.item()
        path_item.path = '/a/b/c.mp3'
        path_item.title = 'path item'
        self.lib.add(path_item)

    def test_path_exact_match(self):
        q = 'path:/a/b/c.mp3'
        results = self.lib.items(q)
        self.assert_matched(results, 'path item')
        self.assert_done(results)

    def test_parent_directory_no_slash(self):
        q = 'path:/a'
        results = self.lib.items(q)
        self.assert_matched(results, 'path item')
        self.assert_done(results)

    def test_parent_directory_with_slash(self):
        q = 'path:/a/'
        results = self.lib.items(q)
        self.assert_matched(results, 'path item')
        self.assert_done(results)

    def test_no_match(self):
        q = 'path:/xyzzy/'
        results = self.lib.items(q)
        self.assert_done(results)

    def test_fragment_no_match(self):
        q = 'path:/b/'
        results = self.lib.items(q)
        self.assert_done(results)

    def test_nonnorm_path(self):
        q = 'path:/x/../a/b'
        results = self.lib.items(q)
        self.assert_matched(results, 'path item')
        self.assert_done(results)

    def test_slashed_query_matches_path(self):
        q = '/a/b'
        results = self.lib.items(q)
        self.assert_matched(results, 'path item')
        self.assert_done(results)

    def test_non_slashed_does_not_match_path(self):
        q = 'c.mp3'
        results = self.lib.items(q)
        self.assert_done(results)

    def test_slashes_in_explicit_field_does_not_match_path(self):
        q = 'title:/a/b'
        results = self.lib.items(q)
        self.assert_done(results)

class BrowseTest(unittest.TestCase, AssertsMixin):
    def setUp(self):
        self.lib = beets.library.Library(
            os.path.join(_common.RSRC, 'test.blb')
        )

    def test_album_list(self):
        albums = list(self.lib.albums())
        album_names = [a.album for a in albums]
        for aname in ['Alright, Still', 'Person Pitch', 'Sing Song',
                      'Terry Tales & Fallen Gates EP']:
            self.assert_(aname in album_names)
        self.assertEqual(len(albums), 4)

    def test_item_list(self):
        items = self.lib.items()
        self.assert_matched(items, 'Littlest Things')
        self.assert_matched(items, 'Take Pills')
        self.assert_matched(items, 'Lovers Who Uncover')
        self.assert_matched(items, 'Boracay')
        self.assert_done(items)

    def test_albums_matches_album(self):
        albums = list(self.lib.albums('person'))
        self.assertEqual(len(albums), 1)

    def test_albums_matches_albumartist(self):
        albums = list(self.lib.albums('panda'))
        self.assertEqual(len(albums), 1)

    def test_items_matches_title(self):
        items = self.lib.items('boracay')
        self.assert_matched(items, 'Boracay')
        self.assert_done(items)

    def test_items_does_not_match_year(self):
        items = self.lib.items('2007')
        self.assert_done(items)

    #FIXME Haven't tested explicit (non-query) criteria.

class CountTest(unittest.TestCase):
    def setUp(self):
        self.lib = beets.library.Library(':memory:')
        self.item = some_item
        self.lib.add(self.item)

    def test_count_gets_single_item(self):
        with self.lib.transaction() as tx:
            songs, totaltime = beets.library.TrueQuery().count(tx)
        self.assertEqual(songs, 1)
        self.assertEqual(totaltime, self.item.length)

    def test_count_works_for_empty_library(self):
        self.lib.remove(self.item)
        with self.lib.transaction() as tx:
            songs, totaltime = beets.library.TrueQuery().count(tx)
        self.assertEqual(songs, 0)
        self.assertEqual(totaltime, 0.0)

class StringParseTest(unittest.TestCase):
    def test_single_field_query(self):
        q = beets.library.AndQuery.from_string(u'albumtype:soundtrack')
        self.assertEqual(len(q.subqueries), 1)
        subq = q.subqueries[0]
        self.assertTrue(isinstance(subq, beets.library.SubstringQuery))
        self.assertEqual(subq.field, 'albumtype')
        self.assertEqual(subq.pattern, 'soundtrack')

def suite():
    return unittest.TestLoader().loadTestsFromName(__name__)

if __name__ == '__main__':
    unittest.main(defaultTest='suite')
Something went wrong with that request. Please try again.