Skip to content

Commit

Permalink
Add tests for several web queries, including the regex queries fixed in
Browse files Browse the repository at this point in the history
bugfix beetbox#3867.

Signed-off-by: Graham R. Cobb <g+beets@cobb.uk.net>
  • Loading branch information
GrahamCobb committed Mar 8, 2021
1 parent 553e38f commit 38fb1b5
Showing 1 changed file with 92 additions and 10 deletions.
102 changes: 92 additions & 10 deletions test/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ def setUp(self):
# Add library elements. Note that self.lib.add overrides any "id=<n>"
# and assigns the next free id number.
# The following adds will create items #1, #2 and #3
self.lib.add(Item(title=u'title', path='/path_1', album_id=2))
self.lib.add(Item(title=u'another title', path='/path_2'))
self.lib.add(Item(title=u'and a third'))
self.lib.add(Item(title=u'title', path='/path_1', album_id=2, artist='AAA Singers'))
self.lib.add(Item(title=u'another title', path='/somewhere/path_2', artist='AAA Singers'))
self.lib.add(Item(title=u'and a third', testattr='ABC', path='/somewhere-else/path_2', album_id=2))
# The following adds will create albums #1 and #2
self.lib.add(Album(album=u'album'))
self.lib.add(Album(album=u'other album', artpath='/art_path_2'))
self.lib.add(Album(album=u'album', albumtest='xyz'))
self.lib.add(Album(album=u'other album', artpath='/somewhere-else/art_path_2'))

web.app.config['TESTING'] = True
web.app.config['lib'] = self.lib
Expand All @@ -46,13 +46,17 @@ def test_config_include_paths_true(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(res_json['path'], u'/path_1')

web.app.config['INCLUDE_PATHS'] = False

def test_config_include_artpaths_true(self):
web.app.config['INCLUDE_PATHS'] = True
response = self.client.get('/album/2')
res_json = json.loads(response.data.decode('utf-8'))

self.assertEqual(response.status_code, 200)
self.assertEqual(res_json['artpath'], u'/art_path_2')
self.assertEqual(res_json['artpath'], u'/somewhere-else/art_path_2')

web.app.config['INCLUDE_PATHS'] = False

def test_config_include_paths_false(self):
web.app.config['INCLUDE_PATHS'] = False
Expand Down Expand Up @@ -91,8 +95,8 @@ def test_get_multiple_items_by_id(self):

self.assertEqual(response.status_code, 200)
self.assertEqual(len(res_json['items']), 2)
response_titles = [item['title'] for item in res_json['items']]
assertCountEqual(self, response_titles, [u'title', u'another title'])
response_titles = {item['title'] for item in res_json['items']}
self.assertEqual(response_titles, {u'title', u'another title'})

def test_get_single_item_not_found(self):
response = self.client.get('/item/4')
Expand All @@ -116,13 +120,15 @@ def test_get_single_item_by_path_not_found_if_not_in_library(self):
self.assertEqual(response.status_code, 404)

def test_get_item_empty_query(self):
""" testing item query: <empty> """
response = self.client.get('/item/query/')
res_json = json.loads(response.data.decode('utf-8'))

self.assertEqual(response.status_code, 200)
self.assertEqual(len(res_json['items']), 3)

def test_get_simple_item_query(self):
""" testing item query: another """
response = self.client.get('/item/query/another')
res_json = json.loads(response.data.decode('utf-8'))

Expand All @@ -131,6 +137,49 @@ def test_get_simple_item_query(self):
self.assertEqual(res_json['results'][0]['title'],
u'another title')

def test_query_item_string(self):
""" testing item query: testattr:ABC """
response = self.client.get('/item/query/testattr%3aABC')
res_json = json.loads(response.data.decode('utf-8'))

self.assertEqual(response.status_code, 200)
self.assertEqual(len(res_json['results']), 1)
self.assertEqual(res_json['results'][0]['title'],
u'and a third')

def test_query_item_regex(self):
""" testing item query: testattr::[A-C]+ """
response = self.client.get('/item/query/testattr%3a%3a[A-C]%2b')
res_json = json.loads(response.data.decode('utf-8'))

self.assertEqual(response.status_code, 200)
self.assertEqual(len(res_json['results']), 1)
self.assertEqual(res_json['results'][0]['title'],
u'and a third')

def test_query_item_regex_backslash(self):
#""" testing item query: testattr::\w+ """
response = self.client.get('/item/query/testattr%3a%3a%5cw%2b')
res_json = json.loads(response.data.decode('utf-8'))

self.assertEqual(response.status_code, 200)
self.assertEqual(len(res_json['results']), 1)
self.assertEqual(res_json['results'][0]['title'],
u'and a third')

def test_query_item_path(self):
#""" testing item query: path:\somewhere """
""" Note: path queries are special: the query item must match the path
from the root all the way to a directory, so this matches 1 item """
""" Note: filesystem separators must be specified as '\' """
response = self.client.get('/item/query/path:\\somewhere')
res_json = json.loads(response.data.decode('utf-8'))

self.assertEqual(response.status_code, 200)
self.assertEqual(len(res_json['results']), 1)
self.assertEqual(res_json['results'][0]['title'],
u'another title')

def test_get_all_albums(self):
response = self.client.get('/album/')
res_json = json.loads(response.data.decode('utf-8'))
Expand Down Expand Up @@ -177,10 +226,43 @@ def test_get_album_details(self):
res_json = json.loads(response.data.decode('utf-8'))

self.assertEqual(response.status_code, 200)
self.assertEqual(len(res_json['items']), 1)
self.assertEqual(len(res_json['items']), 2)
self.assertEqual(res_json['items'][0]['album'],
u'other album')
self.assertEqual(res_json['items'][0]['id'], 1)
self.assertEqual(res_json['items'][1]['album'],
u'other album')
response_track_titles = {item['title'] for item in res_json['items']}
self.assertEqual(response_track_titles, {u'title', u'and a third'})

def test_query_album_string(self):
""" testing query: albumtest:xy """
response = self.client.get('/album/query/albumtest%3axy')
res_json = json.loads(response.data.decode('utf-8'))

self.assertEqual(response.status_code, 200)
self.assertEqual(len(res_json['results']), 1)
self.assertEqual(res_json['results'][0]['album'],
u'album')

def test_query_album_artpath_regex(self):
""" testing query: artpath::art_ """
response = self.client.get('/album/query/artpath%3a%3aart_')
res_json = json.loads(response.data.decode('utf-8'))

self.assertEqual(response.status_code, 200)
self.assertEqual(len(res_json['results']), 1)
self.assertEqual(res_json['results'][0]['album'],
u'other album')

def test_query_album_regex_backslash(self):
#""" testing query: albumtest::\w+ """
response = self.client.get('/album/query/albumtest%3a%3a%5cw%2b')
res_json = json.loads(response.data.decode('utf-8'))

self.assertEqual(response.status_code, 200)
self.assertEqual(len(res_json['results']), 1)
self.assertEqual(res_json['results'][0]['album'],
u'album')

def test_get_stats(self):
response = self.client.get('/stats')
Expand Down

0 comments on commit 38fb1b5

Please sign in to comment.