-
Notifications
You must be signed in to change notification settings - Fork 2
/
xml_test.py
219 lines (185 loc) · 9.86 KB
/
xml_test.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python3
import unittest
import pyxb
from xsdata.formats.dataclass.parsers import XmlParser
from xsdata.formats.dataclass.serializers import XmlSerializer
from npoapi import base
from npoapi.data import PageSearchResult
from npoapi.data.media_update import *
from npoapi.xml import media
from npoapi.xml import mediaupdate
from npoapi.xml import poms
base.declare_namespaces()
class Tests(unittest.TestCase):
def setUp(self):
pyxb.RequireValidWhenGenerating(True)
self.maxDiff = None
def test_set_duration(self):
xml = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<program type="CLIP" avType="VIDEO" publishStop="2012-01-11T18:16:01.287+01:00" publishStart="2012-01-11T16:16:01.287+01:00" embeddable="true"
xmlns="urn:vpro:media:update:2009">
<broadcaster>VPRO</broadcaster>
<title type="MAIN">Main title</title>
<memberOf position="34">urn:vpro:media:group:2981744</memberOf>
<memberOf>POMS_S_VPRO_159096</memberOf>
<images>
<image type="PICTURE" highlighted="false">
<title>bla</title>
<urn>urn:vpro:image:496158</urn>
</image>
</images>
</program>
"""
update = poms.CreateFromDocument(xml)
self.assertEqual(update.type, media.programTypeEnum.CLIP)
self.assertEqual(update.title[0].value(), "Main title")
update.duration = "PT5M"
print(update.toxml())
self.assertEqual(update.toxml(), """<?xml version="1.0" ?><program avType="VIDEO" embeddable="true" publishStart="2012-01-11T15:16:01.287Z" publishStop="2012-01-11T17:16:01.287Z" type="CLIP" xmlns="urn:vpro:media:update:2009"><broadcaster>VPRO</broadcaster><title type="MAIN">Main title</title><duration>PT5M</duration><memberOf position="34">urn:vpro:media:group:2981744</memberOf><memberOf>POMS_S_VPRO_159096</memberOf><images><image highlighted="false" type="PICTURE"><title>bla</title><urn>urn:vpro:image:496158</urn></image></images></program>""")
print(len(update.images.image))
def test_segment(self):
pyxb.RequireValidWhenGenerating(False)
segment = mediaupdate.segmentUpdateType(midRef="program_mid")
segment.start = "PT0S"
print(segment.toxml(encoding='UTF-8', element_name="segment")),
def test_image(self):
xml = """<program xmlns="urn:vpro:media:update:2009" xmlns:media="urn:vpro:media:2009" xmlns:shared="urn:vpro:shared:2009" type="CLIP" avType="AUDIO" embeddable="true" mid="POMS_VPRO_1421796" urn="urn:vpro:media:program:58516847">
<broadcaster>EO</broadcaster>
<title type="MAIN">Test 2016-04-22T09:38:43.304Z</title>
<locations/>
<scheduleEvents/>
<images>
<image type="PORTRAIT" highlighted="false">
<title>sdf</title>
<description>asdfasdf</description>
<urn>urn:vpro:image:274460</urn>
</image>
</images>
<segments/>
</program>"""
update = poms.CreateFromDocument(xml)
self.assertEqual(len(update.images.image), 1)
def test_image2(self):
xml = """<?xml version="1.0" ?><image highlighted="false" type="PORTRAIT" xmlns="urn:vpro:media:update:2009"><title>sdf</title><description>asdfasdf</description><urn>urn:vpro:image:274460</urn></image>"""
image = poms.CreateFromDocument(xml)
self.assertEqual(image.title, "sdf")
def test_images_collection(self):
xml = """<collection xmlns:update="urn:vpro:media:update:2009" xmlns:media="urn:vpro:media:2009" xmlns:shared="urn:vpro:shared:2009">
<update:image type="PORTRAIT" highlighted="false">
<update:title>sdf</update:title>
<update:description>asdfasdf</update:description>
<update:urn>urn:vpro:image:274460</update:urn>
</update:image>
</collection>"""
images = poms.CreateFromDocument(xml)
self.assertEqual(images.wildcardElements()[0].title, "sdf")
def test_media_form(self):
from npoapi.xml import api
form = api.mediaForm()
form.searches = pyxb.BIND()
form.searches.mediaIds = "crid://pyapi/clip/1"
self.assertEqual('<?xml version="1.0" ?><api:mediaForm xmlns="urn:vpro:media:update:2009" xmlns:api="urn:vpro:api:2013"><api:searches><api:mediaIds><api:matcher>crid://pyapi/clip/1</api:matcher></api:mediaIds></api:searches></api:mediaForm>', form.toxml())
def test_add_person(self):
from npoapi.xml import mediaupdate
program = mediaupdate.program(type="CLIP", avType="MIXED")
program.title.append(mediaupdate.titleUpdateType("hoi ", type="MAIN"))
program.broadcaster.append("VPRO")
program.credits = pyxb.BIND()
person = mediaupdate.personUpdateType(role=media.roleType.ACTOR, givenName = "Pietje", familyName = "Puk")
program.credits.append(person)
print(program.toxml())
def test_memberRefUpdate(self):
from npoapi.xml import mediaupdate
memberOf = mediaupdate.memberRef("owner_mid")
memberOf.position = 4
memberOf.highlighted = False
print(memberOf.toxml())
def test_page_form(self):
from npoapi.xml import api
import datetime
form = api.pagesForm()
form.sortFields = pyxb.BIND()
form.sortFields.append(api.pageSortTypeEnum.lastModified)
form.searches = pyxb.BIND()
form.searches.lastModifiedDates = pyxb.BIND()
end = datetime.datetime(2017, 6, 19, 0, 0)
#now = datetime.datetime.now()
#today = now.replace(hour = 6, minute=0, second=0, microsecond=0)
dateRange = api.dateRangeMatcherType(end = end)
form.searches.lastModifiedDates.append(dateRange)
self.assertEqual('<?xml version="1.0" ?><api:pagesForm xmlns="urn:vpro:media:update:2009" xmlns:api="urn:vpro:api:2013"><api:searches><api:lastModifiedDates><api:matcher><api:end>2017-06-19T00:00:00</api:end></api:matcher></api:lastModifiedDates></api:searches><api:sortFields><api:sort>lastModified</api:sort></api:sortFields></api:pagesForm>',
form.toxml())
def test_page_update(self):
example = """
<page type="AUDIO" url="http://test.vpro.nl/article/1234" xmlns="urn:vpro:pages:update:2013">
<crid>crid://bla/1234</crid>
<broadcaster>VPRO</broadcaster>
<title>Hoi & Hallo foobar</title>
<paragraphs>
<paragraph>
<title>Title of & paragraph</title>
<body>Foo & Bar</body>
</paragraph>
</paragraphs>
</page>
"""
page_update = poms.CreateFromDocument(example.encode("UTF-8"))
self.assertEqual(page_update.title, "Hoi & Hallo foobar")
def test_media_domain(self):
xml = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<program type="CLIP" avType="VIDEO" publishStop="2012-01-11T18:16:01.287+01:00" publishStart="2012-01-11T16:16:01.287+01:00" embeddable="true" xmlns="urn:vpro:media:2009">
<broadcaster id='VPRO'>VPRO</broadcaster>
<title type="MAIN" owner='BROADCASTER'>Main title</title>
</program>
"""
object = poms.CreateFromDocument(xml)
def test_page_domain(self):
xml = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<api:pageSearchResult xmlns:api="urn:vpro:api:2013" xmlns="urn:vpro:media:2009" xmlns:media="urn:vpro:media:2009" xmlns:shared="urn:vpro:shared:2009" xmlns:pages="urn:vpro:pages:2013" total="432" totalQualifier="EQUAL_TO" offset="0" max="0">
<api:items>
<api:item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="api:searchResultItem" score="0.3099519">
<api:result xsi:type="pages:pageType" url="https://www.vprobroadcast.com/play~WO_VPRO_2297327~antibiotics~.html" type="VIDEO" creationDate="2019-11-22T14:15:09.443+01:00" lastModified="2019-11-22T14:15:09.443+01:00" lastPublished="2019-11-22T14:15:24.341+01:00" publishStart="2015-10-16T14:23:14.729+02:00" refCount="0" sortDate="2015-10-16T14:23:14.729+02:00">
<pages:crid>crid://vpro/media/vprobroadcast/WO_VPRO_2297327</pages:crid>
<pages:broadcaster id="VPRO"></pages:broadcaster>
<pages:portal id="VPROBROADCAST" url="https://www.vprobroadcast.com">
<pages:name>www.vprobroadcast.com</pages:name>
</pages:portal>
<pages:title>Antibiotics</pages:title>
<pages:images>
<pages:image type="PICTURE" url="https://images.poms.omroep.nl/image/s360/665086.jpg">
<pages:title>Antibiotica</pages:title>
<pages:description>Labyrint</pages:description>
</pages:image>
</pages:images>
</api:result>
</api:item>
</api:items>
<api:facets/>
<api:selectedFacets/>
</api:pageSearchResult>
"""
pyxb = poms.CreateFromDocument(xml)
self.assertEqual(pyxb.items.item[0].result.title, "Antibiotics")
xsdata = XmlParser().from_string(xml, PageSearchResult)
self.assertEqual(xsdata.items.item[0].result.title, "Antibiotics")
print(xsdata)
def test_render_xsdata(self):
media = Program()
media.av_type = AvTypeEnum.AUDIO
main = TitleUpdateType()
main.type = TextualTypeEnum.MAIN
main.value = "foo bar"
media.title.append(main)
b = base.NpoApiBase()
self.assertEqual("""<?xml version='1.0' encoding='UTF-8'?>
<update:program xmlns:update="urn:vpro:media:update:2009" xmlns:pu="urn:vpro:pages:update:2013" xmlns:pages="urn:vpro:pages:2013" xmlns:media="urn:vpro:media:2009" xmlns:shared="urn:vpro:shared:2009" xmlns:api="urn:vpro:api:2013" xmlns:gtaa="urn:vpro:gtaa:2017" avType="AUDIO" embeddable="true">
<update:title type="MAIN">foo bar</update:title>
</update:program>
""", b.data_to_bytes(media)[0].decode("utf-8"))
def test_parse_xsdata(self):
b = base.NpoApiBase(presentation=base.ObjectPresentation.XSDATA)
o = b.to_object_or_none("""<?xml version='1.0' encoding='UTF-8'?>
<update:program xmlns:update="urn:vpro:media:update:2009" xmlns:pu="urn:vpro:pages:update:2013" xmlns:pages="urn:vpro:pages:2013" xmlns:media="urn:vpro:media:2009" xmlns:shared="urn:vpro:shared:2009" xmlns:api="urn:vpro:api:2013" xmlns:gtaa="urn:vpro:gtaa:2017" avType="AUDIO" embeddable="true">
<update:title type="MAIN">foo bar</update:title>
</update:program>
""")