From 5e4600afd8a733cd168da0e9d12c1e59520d6ca9 Mon Sep 17 00:00:00 2001 From: Thomas Scholtes Date: Tue, 26 Aug 2014 12:01:53 +0200 Subject: [PATCH] info: add tests and omit None values --- beetsplug/info.py | 13 ++++++-- test/test_info.py | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 test/test_info.py diff --git a/beetsplug/info.py b/beetsplug/info.py index be26e5c4d..9ece52d7f 100644 --- a/beetsplug/info.py +++ b/beetsplug/info.py @@ -98,13 +98,20 @@ def library_data(item): def print_data(path, data): - maxwidth = max(len(key) for key in data) + formatted = {} + for key, value in data.iteritems(): + if isinstance(value, list): + formatted[key] = u'; '.join(value) + if value is not None: + formatted[key] = value + + maxwidth = max(len(key) for key in formatted) lineformat = u'{{0:>{0}}}: {{1}}'.format(maxwidth) ui.print_(displayable_path(path)) - for field in sorted(data): - value = data[field] + for field in sorted(formatted): + value = formatted[field] if isinstance(value, list): value = u'; '.join(value) ui.print_(lineformat.format(field, value)) diff --git a/test/test_info.py b/test/test_info.py new file mode 100644 index 000000000..6f3be8294 --- /dev/null +++ b/test/test_info.py @@ -0,0 +1,80 @@ +# This file is part of beets. +# Copyright 2014, Thomas Scholtes. +# +# 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. + +import os +import os.path +from _common import unittest +from helper import TestHelper + +from beets.mediafile import MediaFile + + +class InfoTest(unittest.TestCase, TestHelper): + + def setUp(self): + self.setup_beets() + self.load_plugins('info') + + def tearDown(self): + self.unload_plugins() + self.teardown_beets() + + def run_command(self, *args): + super(InfoTest, self).run_command('info', *args) + + def test_path(self): + path = self.create_mediafile_fixture() + + mediafile = MediaFile(path) + mediafile.albumartist = 'AAA' + mediafile.disctitle = 'DDD' + mediafile.genres = ['a', 'b', 'c'] + mediafile.composer = None + mediafile.save() + + out = self.run_with_output(path) + self.assertIn(path, out) + self.assertIn('albumartist: AAA', out) + self.assertIn('disctitle: DDD', out) + self.assertIn('genres: a; b; c', out) + self.assertNotIn('composer:', out) + + def test_item_query(self): + items = self.add_item_fixtures(count=2) + items[0].album = 'xxxx' + items[0].write() + items[0].album = 'yyyy' + items[0].store() + + out = self.run_with_output('album:yyyy') + self.assertIn(items[0].path, out) + self.assertIn('album: xxxx', out) + + self.assertNotIn(items[1].path, out) + + def test_item_library_query(self): + item, = self.add_item_fixtures() + item.album = 'xxxx' + item.store() + + out = self.run_with_output('--library', 'album:xxxx') + self.assertIn(item.path, out) + self.assertIn('album: xxxx', out) + + +def suite(): + return unittest.TestLoader().loadTestsFromName(__name__) + +if __name__ == '__main__': + unittest.main(defaultTest='suite')