File: test_description.py

package info (click to toggle)
svgwrite 1.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 2,092 kB
  • sloc: python: 12,603; makefile: 117; sh: 5
file content (77 lines) | stat: -rw-r--r-- 2,739 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python
#coding:utf-8
# Author:  mozman --<mozman@gmx.at>
# Purpose: test Description mixin
# Created: 04.11.2010
# Copyright (C) 2010, Manfred Moitzi
# License: MIT License

import unittest
import xml.etree.ElementTree as etree

from svgwrite.base  import BaseElement

class Mock(BaseElement):
    elementname = 'g'

class TestDescription(unittest.TestCase):
    def test_title(self):
        m = Mock()
        m.set_desc(title="TEST")
        self.assertEqual(m.tostring(), '<g><title>TEST</title></g>')

    def test_desc(self):
        m = Mock()
        m.set_desc(desc="TEST")
        self.assertEqual(m.tostring(), '<g><desc>TEST</desc></g>')

    def test_insert(self):
        # insert 'desc# and 'title' always as first subelement
        m = Mock()
        m.add(Mock()) # inner 'g'
        m.set_desc(desc="TEST")
        self.assertEqual(m.tostring(), '<g><desc>TEST</desc><g /></g>')
        m.set_desc("TITLE")
        self.assertEqual(m.tostring(), '<g><title>TITLE</title><desc>TEST</desc><g /></g>')

def get_test_data():
    rdf = etree.Element('rdf:RDF', attrib={'xmlns:rdf': 'http://test/rdf'})
    rdf.append(etree.Element('rdf:Description'))
    return rdf

class TestMetaData(unittest.TestCase):
    def test_metadata_only(self):
        m = Mock()
        m.set_metadata(get_test_data())
        self.assertEqual(m.tostring(), '<g><metadata><rdf:RDF xmlns:rdf="http://test/rdf">' \
                         '<rdf:Description /></rdf:RDF></metadata></g>')

    def test_metadata_insert_after_title(self):
        m = Mock()
        m.set_desc('TITLE')
        m.set_metadata(get_test_data())
        self.assertEqual(m.tostring(), '<g><title>TITLE</title><metadata>'\
                         '<rdf:RDF xmlns:rdf="http://test/rdf"><rdf:Description />'\
                         '</rdf:RDF></metadata></g>')

    def test_metadata_insert_before_others(self):
        m = Mock()
        m.add(Mock())
        m.add(Mock())
        m.set_metadata(get_test_data())
        self.assertEqual(m.tostring(), '<g><metadata><rdf:RDF xmlns:rdf="http://test/rdf">'\
                         '<rdf:Description /></rdf:RDF></metadata><g /><g /></g>')

    def test_metadata_insert_between_title_and_g(self):
        m = Mock()
        m.add(Mock())
        m.add(Mock())
        m.set_desc('TITLE', 'DESC')
        m.set_metadata(get_test_data())
        self.assertEqual(m.tostring(), '<g><title>TITLE</title><desc>DESC</desc>' \
                         '<metadata><rdf:RDF xmlns:rdf="http://test/rdf"><rdf:Description /></rdf:RDF>'\
                         '</metadata><g /><g /></g>')


if __name__=='__main__':
    unittest.main()