| 1 |
"""Test the beets.export utilities associated with the export plugin.""" |
| 2 |
|
| 3 |
import json |
| 4 |
import re # used to test csv format |
| 5 |
from xml.etree import ElementTree |
| 6 |
from xml.etree.ElementTree import Element |
| 7 |
|
| 8 |
from beets.test.helper import IOMixin, PluginTestCase |
| 9 |
|
| 10 |
|
| 11 |
class ExportPluginTest(IOMixin, PluginTestCase): |
| 12 |
plugin = "export" |
| 13 |
|
| 14 |
def setUp(self): |
| 15 |
super().setUp() |
| 16 |
self.test_values = {"title": "xtitle", "album": "xalbum"} |
| 17 |
|
| 18 |
def execute_command(self, format_type, artist): |
| 19 |
query = ",".join(self.test_values.keys()) |
| 20 |
return self.run_with_output( |
| 21 |
"export", "-f", format_type, "-i", query, artist |
| 22 |
) |
| 23 |
|
| 24 |
def create_item(self): |
| 25 |
(item,) = self.add_item_fixtures() |
| 26 |
item.artist = "xartist" |
| 27 |
item.title = self.test_values["title"] |
| 28 |
item.album = self.test_values["album"] |
| 29 |
item.write() |
| 30 |
item.store() |
| 31 |
return item |
| 32 |
|
| 33 |
def test_json_output(self): |
| 34 |
item1 = self.create_item() |
| 35 |
out = self.execute_command(format_type="json", artist=item1.artist) |
| 36 |
json_data = json.loads(out)[0] |
| 37 |
for key, val in self.test_values.items(): |
| 38 |
assert key in json_data |
| 39 |
assert val == json_data[key] |
| 40 |
|
| 41 |
def test_jsonlines_output(self): |
| 42 |
item1 = self.create_item() |
| 43 |
out = self.execute_command(format_type="jsonlines", artist=item1.artist) |
| 44 |
json_data = json.loads(out) |
| 45 |
for key, val in self.test_values.items(): |
| 46 |
assert key in json_data |
| 47 |
assert val == json_data[key] |
| 48 |
|
| 49 |
def test_csv_output(self): |
| 50 |
item1 = self.create_item() |
| 51 |
out = self.execute_command(format_type="csv", artist=item1.artist) |
| 52 |
csv_list = re.split("\r", re.sub("\n", "", out)) |
| 53 |
head = re.split(",", csv_list[0]) |
| 54 |
vals = re.split(",|\r", csv_list[1]) |
| 55 |
for index, column in enumerate(head): |
| 56 |
assert self.test_values.get(column, None) is not None |
| 57 |
assert vals[index] == self.test_values[column] |
| 58 |
|
| 59 |
def test_xml_output(self): |
| 60 |
item1 = self.create_item() |
| 61 |
out = self.execute_command(format_type="xml", artist=item1.artist) |
| 62 |
library = ElementTree.fromstring(out) |
| 63 |
assert isinstance(library, Element) |
| 64 |
for track in library[0]: |
| 65 |
for details in track: |
| 66 |
tag = details.tag |
| 67 |
txt = details.text |
| 68 |
assert tag in self.test_values, tag |
| 69 |
assert self.test_values[tag] == txt, txt |