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
|
# This file is part of beets.
# Copyright 2019, Carl Suster
#
# 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.
"""Test the beets.export utilities associated with the export plugin.
"""
import unittest
from test.helper import TestHelper
import re # used to test csv format
import json
from xml.etree.ElementTree import Element
from xml.etree import ElementTree
class ExportPluginTest(unittest.TestCase, TestHelper):
def setUp(self):
self.setup_beets()
self.load_plugins('export')
self.test_values = {'title': 'xtitle', 'album': 'xalbum'}
def tearDown(self):
self.unload_plugins()
self.teardown_beets()
def execute_command(self, format_type, artist):
query = ','.join(self.test_values.keys())
out = self.run_with_output(
'export',
'-f', format_type,
'-i', query,
artist
)
return out
def create_item(self):
item, = self.add_item_fixtures()
item.artist = 'xartist'
item.title = self.test_values['title']
item.album = self.test_values['album']
item.write()
item.store()
return item
def test_json_output(self):
item1 = self.create_item()
out = self.execute_command(
format_type='json',
artist=item1.artist
)
json_data = json.loads(out)[0]
for key, val in self.test_values.items():
self.assertTrue(key in json_data)
self.assertEqual(val, json_data[key])
def test_jsonlines_output(self):
item1 = self.create_item()
out = self.execute_command(
format_type='jsonlines',
artist=item1.artist
)
json_data = json.loads(out)
for key, val in self.test_values.items():
self.assertTrue(key in json_data)
self.assertEqual(val, json_data[key])
def test_csv_output(self):
item1 = self.create_item()
out = self.execute_command(
format_type='csv',
artist=item1.artist
)
csv_list = re.split('\r', re.sub('\n', '', out))
head = re.split(',', csv_list[0])
vals = re.split(',|\r', csv_list[1])
for index, column in enumerate(head):
self.assertTrue(self.test_values.get(column, None) is not None)
self.assertEqual(vals[index], self.test_values[column])
def test_xml_output(self):
item1 = self.create_item()
out = self.execute_command(
format_type='xml',
artist=item1.artist
)
library = ElementTree.fromstring(out)
self.assertIsInstance(library, Element)
for track in library[0]:
for details in track:
tag = details.tag
txt = details.text
self.assertTrue(tag in self.test_values, msg=tag)
self.assertEqual(self.test_values[tag], txt, msg=txt)
def suite():
return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == '__main__':
unittest.main(defaultTest='suite')
|