File: test_uniform.py

package info (click to toggle)
extruct 0.18.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,048 kB
  • sloc: python: 2,106; makefile: 10
file content (372 lines) | stat: -rw-r--r-- 14,735 bytes parent folder | download
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# mypy: disallow_untyped_defs=False
import unittest

import extruct
from extruct.uniform import _flatten, _uopengraph, flatten_dict, infer_context
from tests import get_testdata


class TestUniform(unittest.TestCase):

    maxDiff = None

    def test_uopengraph(self):
        expected = [
            {
                "@context": {
                    "og": "http://ogp.me/ns#",
                    "fb": "http://www.facebook.com/2008/fbml",
                    "concerts": "http://ogp.me/ns/fb/songkick-concerts#",
                },
                "fb:app_id": "308540029359",
                "og:site_name": "Songkick",
                "@type": "songkick-concerts:artist",
                "og:title": "Elysian Fields",
                "og:description": "Buy tickets for an upcoming Elysian Fields concert near you. List of all Elysian Fields tickets and tour dates for 2017.",
                "og:url": "http://www.songkick.com/artists/236156-elysian-fields",
                "og:image": "http://images.sk-static.com/images/media/img/col4/20100330-103600-169450.jpg",
            }
        ]
        body = get_testdata("songkick", "elysianfields.html")
        data = extruct.extract(body, syntaxes=["opengraph"], uniform=True)
        self.assertEqual(data["opengraph"], expected)

    def test_uopengraph_with_og_array(self):
        expected = [
            {
                "@context": {
                    "og": "http://ogp.me/ns#",
                    "fb": "http://www.facebook.com/2008/fbml",
                    "concerts": "http://ogp.me/ns/fb/songkick-concerts#",
                },
                "fb:app_id": "308540029359",
                "og:site_name": "Songkick",
                "@type": "songkick-concerts:artist",
                "og:title": "Elysian Fields",
                "og:description": "Buy tickets for an upcoming Elysian Fields concert near you. List of all Elysian Fields tickets and tour dates for 2017.",
                "og:url": "http://www.songkick.com/artists/236156-elysian-fields",
                "og:image": [
                    "http://images.sk-static.com/images/media/img/col4/20100330-103600-169450.jpg",
                    "http://images.sk-static.com/SECONDARY_IMAGE.jpg",
                ],
            }
        ]
        body = get_testdata("songkick", "elysianfields.html")
        data = extruct.extract(
            body, syntaxes=["opengraph"], uniform=True, with_og_array=True
        )
        self.assertEqual(data["opengraph"], expected)

    def test_uopengraph_duplicated_priorities(self):
        # Ensures that first seen property is kept when flattening
        data = _uopengraph(
            [
                {
                    "properties": [
                        ("prop_{}".format(k), "value_{}".format(v))
                        for k in range(5)
                        for v in range(5)
                    ],
                    "namespace": "namespace",
                }
            ]
        )
        for k in range(5):
            assert data[0]["prop_{}".format(k)] == "value_0"

        # Ensures that empty is not returned if a property contains any
        # non empty value
        data = _uopengraph(
            [
                {
                    "properties": [
                        ("prop_empty", " "),
                        ("prop_non_empty", " "),
                        ("prop_non_empty", "value!"),
                        ("prop_non_empty2", "value!"),
                        ("prop_non_empty2", " "),
                        ("prop_non_empty3", " "),
                        ("prop_non_empty3", "value!"),
                        ("prop_non_empty3", "other value"),
                    ],
                    "namespace": "namespace",
                }
            ]
        )
        assert data[0]["prop_empty"] == " "
        assert data[0]["prop_non_empty"] == "value!"
        assert data[0]["prop_non_empty2"] == "value!"
        assert data[0]["prop_non_empty3"] == "value!"

    def test_uopengraph_duplicated_with_og_array(self):
        # Ensures that first seen property is kept when flattening
        data = _uopengraph(
            [
                {
                    "properties": [
                        ("prop_{}".format(k), "value_{}".format(v))
                        for k in range(5)
                        for v in range(5)
                    ],
                    "namespace": "namespace",
                }
            ],
            with_og_array=True,
        )
        for k in range(5):
            assert data[0]["prop_{}".format(k)] == [
                "value_0",
                "value_1",
                "value_2",
                "value_3",
                "value_4",
            ]

        # Ensures that empty is not returned if a property contains any
        # non empty value
        data = _uopengraph(
            [
                {
                    "properties": [
                        ("prop_empty", " "),
                        ("prop_non_empty", " "),
                        ("prop_non_empty", "value!"),
                        ("prop_non_empty2", "value!"),
                        ("prop_non_empty2", " "),
                        ("prop_non_empty3", " "),
                        ("prop_non_empty3", "value!"),
                        ("prop_non_empty3", "other value"),
                    ],
                    "namespace": "namespace",
                }
            ],
            with_og_array=True,
        )
        assert data[0]["prop_empty"] == " "
        assert data[0]["prop_non_empty"] == "value!"
        assert data[0]["prop_non_empty2"] == "value!"
        assert data[0]["prop_non_empty3"] == ["value!", "other value"]

    def test_umicroformat(self):
        expected = [
            {
                "@context": "http://microformats.org/wiki/",
                "@type": ["h-hidden-phone", "h-hidden-tablet"],
                "name": [""],
            },
            {
                "@context": "http://microformats.org/wiki/",
                "@type": ["h-hidden-phone"],
                "children": [
                    {"@type": ["h-hidden-phone", "h-hidden-tablet"], "name": [""]},
                    {
                        "@type": ["h-hidden-phone"],
                        "name": [
                            "aJ Styles FastLane 2018 15 x "
                            "17 Framed Plaque w/ Ring "
                            "Canvas"
                        ],
                        "photo": [
                            {
                                "alt": "aJ Styles FastLane 2018 15 x 17 Framed Plaque w/ Ring Canvas",
                                "value": "/on/demandware.static/-/Sites-main/default/dwa3227ee6/images/small/CN1148.jpg",
                            }
                        ],
                    },
                ],
            },
            {
                "@context": "http://microformats.org/wiki/",
                "@type": ["h-entry"],
                "author": [
                    {
                        "@type": ["h-card"],
                        "name": ["W. Developer"],
                        "url": ["http://example.com"],
                        "value": "W. Developer",
                    }
                ],
                "content": [
                    {"html": "<p>Blah blah blah</p>", "value": "Blah blah blah"}
                ],
                "name": ["Microformats are amazing"],
                "published": ["2013-06-13 12:00:00"],
                "summary": ["In which I extoll the virtues of using " "microformats."],
            },
        ]
        body = get_testdata("misc", "microformat_test.html")
        data = extruct.extract(body, syntaxes=["microformat"], uniform=True)
        self.assertEqual(data["microformat"], expected)

    def test_umicrodata(self):
        expected = [
            {
                "@context": "http://schema.org",
                "@type": "Product",
                "brand": "ACME",
                "name": "Executive Anvil",
                "image": "anvil_executive.jpg",
                "description": "Sleeker than ACME's Classic Anvil, the Executive Anvil is perfect for the business traveler looking for something to drop from a height.",
                "mpn": "925872",
                "aggregateRating": {
                    "@type": "AggregateRating",
                    "ratingValue": "4.4",
                    "reviewCount": "89",
                },
                "offers": {
                    "@type": "Offer",
                    "priceCurrency": "USD",
                    "price": "119.99",
                    "priceValidUntil": "2020-11-05",
                    "seller": {"@type": "Organization", "name": "Executive Objects"},
                    "itemCondition": "http://schema.org/UsedCondition",
                    "availability": "http://schema.org/InStock",
                },
            }
        ]
        body = get_testdata("misc", "product_microdata.html")
        data = extruct.extract(body, syntaxes=["microdata"], uniform=True)
        self.assertEqual(data["microdata"], expected)

    def test_udublincore(self):
        expected = [
            {
                "elements": [
                    {
                        "name": "DC.title",
                        "lang": "en",
                        "content": "Expressing Dublin Core\nin HTML/XHTML meta and link elements",
                        "URI": "http://purl.org/dc/elements/1.1/title",
                    },
                    {
                        "name": "DC.creator",
                        "content": "Andy Powell, UKOLN, University of Bath",
                        "URI": "http://purl.org/dc/elements/1.1/creator",
                    },
                    {
                        "name": "DC.identifier",
                        "scheme": "DCTERMS.URI",
                        "content": "http://dublincore.org/documents/dcq-html/",
                        "URI": "http://purl.org/dc/elements/1.1/identifier",
                    },
                    {
                        "name": "DC.format",
                        "scheme": "DCTERMS.IMT",
                        "content": "text/html",
                        "URI": "http://purl.org/dc/elements/1.1/format",
                    },
                ],
                "terms": [
                    {
                        "name": "DCTERMS.issued",
                        "scheme": "DCTERMS.W3CDTF",
                        "content": "2003-11-01",
                        "URI": "http://purl.org/dc/terms/issued",
                    },
                    {
                        "name": "DCTERMS.abstract",
                        "content": "This document describes how\nqualified Dublin Core metadata can be encoded\nin HTML/XHTML <meta> elements",
                        "URI": "http://purl.org/dc/terms/abstract",
                    },
                    {
                        "name": "DC.Date.modified",
                        "content": "2001-07-18",
                        "URI": "http://purl.org/dc/terms/modified",
                    },
                    {
                        "name": "DCTERMS.modified",
                        "content": "2001-07-18",
                        "URI": "http://purl.org/dc/terms/modified",
                    },
                    {
                        "rel": "DCTERMS.replaces",
                        "hreflang": "en",
                        "href": "http://dublincore.org/documents/2000/08/15/dcq-html/",
                        "URI": "http://purl.org/dc/terms/replaces",
                    },
                ],
                "@context": {
                    "DC": "http://purl.org/dc/elements/1.1/",
                    "DCTERMS": "http://purl.org/dc/terms/",
                },
                "@type": "Text",
            }
        ]
        body = get_testdata("misc", "dublincore_test.html")
        data = extruct.extract(body, syntaxes=["dublincore"], uniform=True)
        self.assertEqual(data["dublincore"], expected)

    def test_infer_context(self):
        context = "http://schema.org/UsedCondition"
        self.assertEqual(infer_context(context), ("http://schema.org", "UsedCondition"))

        context = "http://ogp.me/ns#description"
        self.assertEqual(infer_context(context), ("http://ogp.me/ns", "description"))

        context = "http://ogp.me/ns/fb#app_id"
        self.assertEqual(infer_context(context), ("http://ogp.me/ns/fb", "app_id"))

    def test_flatten_dict(self):
        d = {
            "type": "SPANISH INQUISITION",
            "properties": {
                "chief_weapon": "surprise",
                "extra_weapon": "fear",
                "another_one": "ruthless efficiency",
            },
        }
        expected = {
            "@type": "SPANISH INQUISITION",
            "@context": "http://schema.org",
            "chief_weapon": "surprise",
            "extra_weapon": "fear",
            "another_one": "ruthless efficiency",
        }
        self.assertEqual(
            flatten_dict(d, schema_context="http://schema.org", add_context=True),
            expected,
        )

    def test_flatten(self):
        d = {
            "children": [
                {
                    "properties": {"name": [""]},
                    "type": ["h-hidden-tablet", "h-hidden-phone"],
                },
                {
                    "properties": {
                        "name": [
                            "aJ Styles "
                            "FastLane 2018 "
                            "15 x 17 Framed "
                            "Plaque w/ Ring "
                            "Canvas"
                        ],
                        "photo": ["path.jpg"],
                    },
                    "type": ["h-hidden-phone"],
                },
            ],
            "properties": {"name": [""]},
            "type": ["h-hidden-phone"],
        }
        expected = {
            "children": [
                {"name": [""], "@type": ["h-hidden-tablet", "h-hidden-phone"]},
                {
                    "name": [
                        "aJ Styles "
                        "FastLane 2018 "
                        "15 x 17 Framed "
                        "Plaque w/ Ring "
                        "Canvas"
                    ],
                    "photo": ["path.jpg"],
                    "@type": ["h-hidden-phone"],
                },
            ],
            "name": [""],
            "@type": ["h-hidden-phone"],
        }
        self.assertEqual(_flatten(d, schema_context="http://schema.org"), expected)