-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_abletonxmpfile.py
119 lines (103 loc) · 4.07 KB
/
test_abletonxmpfile.py
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
import unittest
from datetime import datetime
from lxml import etree
from tempfile import NamedTemporaryFile
import os
from abletonxmpfile import AbletonXMPFile
class TestAbletonXMPFile(unittest.TestCase):
def setUp(self):
# copy test.xml to a temp file
with open("test.xmp", "r") as file:
xml = file.read()
with NamedTemporaryFile(delete=True) as temp_file:
temp_file.write(xml.encode())
self.file_path = temp_file.name
self.xmp_file = AbletonXMPFile(self.file_path)
def test_init_with_existing_file(self):
# Test that the class initializes correctly with an existing file
self.assertFalse(self.xmp_file.is_changed)
self.assertIsNotNone(self.xmp_file.root)
def test_init_with_non_existing_file(self):
# Test that the class initializes correctly with a non-existing file
non_existing_file_path = "non_existing_file.xmp"
xmp_file = AbletonXMPFile(non_existing_file_path)
self.assertEqual(xmp_file.file_path, non_existing_file_path)
self.assertFalse(xmp_file.is_changed)
self.assertIsNotNone(xmp_file.root)
def test_add_tag(self):
# Test adding a tag to an existing item
file_path = "test_file.wav"
tag = "music"
self.xmp_file.add_tag(file_path, tag)
item = self.xmp_file.root.xpath(
f"//ablFR:items/rdf:Bag/rdf:li[ablFR:filePath='{file_path}']",
namespaces=self.xmp_file.nsmap,
)[0]
tags = item.xpath(
"ablFR:keywords/rdf:Bag/rdf:li",
namespaces=self.xmp_file.nsmap,
)
self.assertEqual(len(tags), 1)
self.assertEqual(tags[0].text, tag)
self.assertTrue(self.xmp_file.is_changed)
def test_add_same_tag_twice(self):
# Test adding the same tag twice to an existing item
file_path = "test_file '1>.wav"
tag = "music"
self.xmp_file.add_tag(file_path, tag)
self.xmp_file.add_tag(file_path, tag)
items = self.xmp_file.root.xpath(
f"//ablFR:items/rdf:Bag/rdf:li",
namespaces=self.xmp_file.nsmap,
)
# assert that exactly one of the items has the same file path
items = [
item
for item in items
if item.xpath("ablFR:filePath", namespaces=self.xmp_file.nsmap)[0].text
== file_path
]
self.assertEqual(len(items), 1)
tags = items[0].xpath(
"ablFR:keywords/rdf:Bag/rdf:li",
namespaces=self.xmp_file.nsmap,
)
self.assertEqual(len(tags), 1)
self.assertEqual(tags[0].text, tag)
self.assertTrue(self.xmp_file.is_changed)
def test_add_tag_to_new_item(self):
# Test adding a tag to a new item
file_path = "new_file.wav"
tag = "sound"
self.xmp_file.add_tag(file_path, tag)
item = self.xmp_file.root.xpath(
f"//ablFR:items/rdf:Bag/rdf:li[ablFR:filePath='{file_path}']",
namespaces=self.xmp_file.nsmap,
)[0]
tags = item.xpath(
"ablFR:keywords/rdf:Bag/rdf:li",
namespaces=self.xmp_file.nsmap,
)
self.assertEqual(len(tags), 1)
self.assertEqual(tags[0].text, tag)
self.assertTrue(self.xmp_file.is_changed)
def test_dump(self):
# Test dumping the XML content
xml = self.xmp_file.dump()
self.assertIsInstance(xml, str)
self.assertIn("<x:xmpmeta", xml)
self.assertIn("<ablFR:items>", xml)
self.assertIn("<xmp:MetadataDate>", xml)
def test_save_if_changed(self):
# Test saving the XML content to a file
xml = self.xmp_file.dump()
with NamedTemporaryFile(delete=False) as temp_file:
temp_file.write(xml.encode())
temp_file_path = temp_file.name
self.xmp_file.save_if_changed(temp_file_path)
self.assertFalse(self.xmp_file.is_changed)
with open(temp_file_path, "r") as file:
saved_xml = file.read()
self.assertEqual(xml, saved_xml)
if __name__ == "__main__":
unittest.main()