1 | from tests.ditest import DependencyInjectionTestBase |
||
2 | from mock import Mock |
||
3 | import datetime |
||
4 | |||
5 | |||
6 | class XmlFormatTests(DependencyInjectionTestBase): |
||
7 | |||
8 | def setUp(self): |
||
9 | super(XmlFormatTests, self).setUp() |
||
10 | |||
11 | def test_serialize_item_returns_string_with_xml_header(self): |
||
12 | prolog = '<?xml version="1.0" encoding="UTF-8"?>' |
||
13 | doc = '<prov:document' |
||
14 | from niprov.formatxml import XmlFormat |
||
15 | form = XmlFormat(self.dependencies) |
||
16 | out = form.serializeSingle(self.aFile()) |
||
17 | self.assertIn(prolog, out) |
||
18 | self.assertIn(doc, out) |
||
19 | |||
20 | def test_serialize_list_creates_entity_for_each_file(self): |
||
21 | from niprov.formatxml import XmlFormat |
||
22 | form = XmlFormat(self.dependencies) |
||
23 | out = form.serializeList([self.aFile(), self.aFile(), self.aFile()]) |
||
24 | self.assertEqual(3, out.count('<prov:entity')) |
||
25 | |||
26 | def test_has_namespaces(self): |
||
27 | from niprov.formatxml import XmlFormat |
||
28 | form = XmlFormat(self.dependencies) |
||
29 | out = form.serializeSingle(self.aFile()) |
||
30 | prov = 'http://www.w3.org/ns/prov#' |
||
31 | self.assertNamespaceDefined('prov', prov, out) |
||
32 | nfo = 'http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#' |
||
33 | self.assertNamespaceDefined('nfo', nfo, out) |
||
34 | dct = 'http://purl.org/dc/terms/' |
||
35 | self.assertNamespaceDefined('dct', dct, out) |
||
36 | |||
37 | def test_Entity_has_id(self): |
||
38 | from niprov.formatxml import XmlFormat |
||
39 | form = XmlFormat(self.dependencies) |
||
40 | aFile = self.aFile() |
||
41 | doc = self.parseDoc(form.serializeSingle(aFile)) |
||
42 | entity = doc.getElementsByTagName("prov:entity")[0] |
||
43 | self.assertHasAttributeWithValue(entity, 'id', 'niprov:file0') |
||
44 | |||
45 | def test_serialize_file_entity_has_fileUrl_prop(self): |
||
46 | from niprov.formatxml import XmlFormat |
||
47 | form = XmlFormat(self.dependencies) |
||
48 | aFile = self.aFile() |
||
49 | doc = self.parseDoc(form.serializeSingle(aFile)) |
||
50 | entity = doc.getElementsByTagName("prov:entity")[0] |
||
51 | self.assertOneChildWithTagAndText(entity, 'nfo:fileUrl', |
||
52 | str(aFile.location.toUrl())) |
||
53 | |||
54 | def test_serialize_file_entity_has_fileSize_prop(self): |
||
55 | from niprov.formatxml import XmlFormat |
||
56 | form = XmlFormat(self.dependencies) |
||
57 | aFile = self.aFile() |
||
58 | aFile.provenance['size'] = 56789 |
||
59 | doc = self.parseDoc(form.serializeSingle(aFile)) |
||
60 | entity = doc.getElementsByTagName("prov:entity")[0] |
||
61 | self.assertOneChildWithTagAndText(entity, 'nfo:fileSize', str(56789)) |
||
62 | |||
63 | View Code Duplication | def test_serialize_file_entity_has_fileLastModified_prop(self): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
64 | from niprov.formatxml import XmlFormat |
||
65 | form = XmlFormat(self.dependencies) |
||
66 | aFile = self.aFile() |
||
67 | aFile.provenance['created'] = datetime.datetime.now() |
||
68 | doc = self.parseDoc(form.serializeSingle(aFile)) |
||
69 | entity = doc.getElementsByTagName("prov:entity")[0] |
||
70 | self.assertOneChildWithTagAndText(entity, 'nfo:fileLastModified', |
||
71 | aFile.provenance['created'].isoformat()) |
||
72 | |||
73 | def test_serialize_file_entity_has_hash(self): |
||
74 | from niprov.formatxml import XmlFormat |
||
75 | form = XmlFormat(self.dependencies) |
||
76 | aFile = self.aFile() |
||
77 | aFile.provenance['hash'] = 'abraca777' |
||
78 | doc = self.parseDoc(form.serializeSingle(aFile)) |
||
79 | entity = doc.getElementsByTagName("prov:entity")[0] |
||
80 | hasHash = self.assertOneChildWithTagName(entity, 'nfo:hasHash') |
||
81 | hashref = self.getElementContent(hasHash) |
||
82 | allHashes = doc.getElementsByTagName("nfo:FileHash") |
||
83 | hashesWithId = [e for e in allHashes if e.attributes['id'].value==hashref] |
||
84 | self.assertEqual(1, len(hashesWithId)) |
||
85 | hashEl = hashesWithId[0] |
||
86 | self.assertOneChildWithTagAndText(hashEl, 'nfo:hashAlgorithm', 'MD5') |
||
87 | self.assertOneChildWithTagAndText(hashEl, 'nfo:hashValue', |
||
88 | aFile.provenance['hash']) |
||
89 | |||
90 | View Code Duplication | def test_FileHash_id_follows_sensible_format(self): |
|
0 ignored issues
–
show
|
|||
91 | from niprov.formatxml import XmlFormat |
||
92 | form = XmlFormat(self.dependencies) |
||
93 | aFile = self.aFile() |
||
94 | aFile.provenance['hash'] = 'abraca777' |
||
95 | doc = self.parseDoc(form.serializeSingle(aFile)) |
||
96 | entity = doc.getElementsByTagName("prov:entity")[0] |
||
97 | fileId = entity.attributes['id'].value |
||
98 | self.assertOneChildWithTagAndText(entity, 'nfo:hasHash', fileId+'.hash') |
||
99 | |||
100 | def test_file_with_transformation_has_activity_w_corresponding_id(self): |
||
101 | from niprov.formatxml import XmlFormat |
||
102 | form = XmlFormat(self.dependencies) |
||
103 | aFile = self.aFile() |
||
104 | aFile.provenance['transformation'] = 'enchantment' |
||
105 | doc = self.parseDoc(form.serializeSingle(aFile)) |
||
106 | ent, entId = self.assertOneChildWithTagThatHasAnId(doc, 'prov:entity') |
||
107 | act, actId = self.assertOneChildWithTagThatHasAnId(doc, 'prov:activity') |
||
108 | self.assertEqual(entId+'.xform', actId) |
||
109 | |||
110 | def test_file_with_transformation_has_activity_w_label(self): |
||
111 | from niprov.formatxml import XmlFormat |
||
112 | form = XmlFormat(self.dependencies) |
||
113 | aFile = self.aFile() |
||
114 | aFile.provenance['transformation'] = 'enchantment' |
||
115 | doc = self.parseDoc(form.serializeSingle(aFile)) |
||
116 | entity = doc.getElementsByTagName("prov:entity")[0] |
||
117 | activity = doc.getElementsByTagName("prov:activity")[0] |
||
118 | self.assertOneChildWithTagAndText(activity, 'dct:title', 'enchantment') |
||
119 | |||
120 | def test_file_with_transformation_has_wasGeneratedBy_element(self): |
||
121 | from niprov.formatxml import XmlFormat |
||
122 | form = XmlFormat(self.dependencies) |
||
123 | aFile = self.aFile() |
||
124 | aFile.provenance['transformation'] = 'enchantment' |
||
125 | doc = self.parseDoc(form.serializeSingle(aFile)) |
||
126 | ent, entId = self.assertOneChildWithTagThatHasAnId(doc, 'prov:entity') |
||
127 | act, actId = self.assertOneChildWithTagThatHasAnId(doc, 'prov:activity') |
||
128 | wasGen = self.assertOneChildWithTagName(doc, "prov:wasGeneratedBy") |
||
129 | refEnt = self.assertOneChildWithTagName(wasGen, "prov:entity") |
||
130 | refAct = self.assertOneChildWithTagName(wasGen, "prov:activity") |
||
131 | self.assertHasAttributeWithValue(refEnt, 'prov:ref', entId) |
||
132 | self.assertHasAttributeWithValue(refAct, 'prov:ref', actId) |
||
133 | |||
134 | View Code Duplication | def test_file_with_transformation_has_wasGeneratedBy_with_time(self): |
|
0 ignored issues
–
show
|
|||
135 | from niprov.formatxml import XmlFormat |
||
136 | form = XmlFormat(self.dependencies) |
||
137 | aFile = self.aFile() |
||
138 | aFile.provenance['transformation'] = 'enchantment' |
||
139 | aFile.provenance['created'] = datetime.datetime.now() |
||
140 | doc = self.parseDoc(form.serializeSingle(aFile)) |
||
141 | wasGen = self.assertOneChildWithTagName(doc, "prov:wasGeneratedBy") |
||
142 | self.assertOneChildWithTagAndText(wasGen, 'prov:time', |
||
143 | aFile.provenance['created'].isoformat()) |
||
144 | |||
145 | def parseDoc(self, xmlString): |
||
146 | from xml.dom.minidom import parseString |
||
147 | dom = parseString(xmlString) |
||
148 | return dom.documentElement |
||
149 | |||
150 | def assertNamespaceDefined(self, prefix, ns, xmlString): |
||
151 | doc = self.parseDoc(xmlString) |
||
152 | self.assertHasAttributeWithValue(doc, 'xmlns:'+prefix, ns) |
||
153 | |||
154 | def assertHasAttributeWithValue(self, element, attName, attValue): |
||
155 | assert element.hasAttribute(attName), "Can't find attribute: "+attName |
||
156 | self.assertEqual(attValue, element.attributes[attName].value) |
||
157 | |||
158 | def assertOneChildWithTagThatHasAnId(self, parent, tag): |
||
159 | elements = parent.getElementsByTagName(tag) |
||
160 | elements = [e for e in elements if e.hasAttribute('id')] |
||
161 | msg = 'Expected exactly one {0} with ID in {1}, but found {2}' |
||
162 | nElem = len(elements) |
||
163 | self.assertEqual(1, nElem, msg.format(tag, parent.tagName, nElem)) |
||
164 | return elements[0], elements[0].attributes['id'].value |
||
165 | |||
166 | def assertOneChildWithTagName(self, parent, tag): |
||
167 | elements = parent.getElementsByTagName(tag) |
||
168 | msg = 'Expected exactly one {0} in {1}, but found {2}' |
||
169 | nElem = len(elements) |
||
170 | self.assertEqual(1, nElem, msg.format(tag, parent.tagName, nElem)) |
||
171 | return elements[0] |
||
172 | |||
173 | def assertOneChildWithTagAndText(self, parent, tag, expValue): |
||
174 | elem = self.assertOneChildWithTagName(parent, tag) |
||
175 | self.assertEqual(expValue, self.getElementContent(elem)) |
||
176 | |||
177 | def getElementContent(self, element): |
||
178 | rc = [] |
||
179 | for node in element.childNodes: |
||
180 | if node.nodeType == node.TEXT_NODE: |
||
181 | rc.append(node.data) |
||
182 | return ''.join(rc) |
||
183 | |||
184 | def aFile(self): |
||
185 | somefile = Mock() |
||
186 | somefile.provenance = {} |
||
187 | somefile.location.toUrl.return_value = 'xkcd://HAL/location.loc' |
||
188 | return somefile |
||
189 |