aboutsummaryrefslogtreecommitdiff
blob: aeaac6d7ccf3a41d038d0a43ad1047b5b6542824 (plain)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
    test_metadata.py
    ~~~~~~~~~~~~~~~~
    
    metadata generator test suite
    
    :copyright: (c) 2013 by Jauhien Piatlicki
    :license: GPL-2, see LICENSE for more details.
"""

import xml.etree.ElementTree as ET

import tempfile, unittest

from g_sorcery import exceptions, metadata

class TestXMLGenerator(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_generate(self):
        schema = [{'name' : 'desc',
                   'subtags' : [],
                   'multiple' : (False, ""),
                   'required' : True},
                   {'name' : 'contact',
                   'multiple' : (False, ""),
                   'required' : False,
                   'subtags' : [
                       {'name' : 'email',
                        'subtags' : [],
                        'multiple' : (False, ""),
                        'required' : True},
                        
                        {'name' : 'phone',
                        'subtags' : [],
                        'multiple' : (False, ""),
                        'required' : False},
                    ]},
                    {'name' : 'multiple',
                     'subtags' : [],
                     'multiple' : (True, ""),
                     'required' : False},
                     {'name' : 'flag',
                     'subtags' : [],
                     'multiple' : (True, "name"),
                     'required' : False},
                   ]
        xg = metadata.XMLGenerator('test_ext', schema)
        self.assertRaises(exceptions.XMLGeneratorError, xg.generate, {})
        tree = xg.generate({'desc' : 'test xml'})
        self.assertEqual(ET.tostring(tree, encoding='unicode'),
                         '<test_ext><desc>test xml</desc></test_ext>')
        tree = xg.generate({'desc' : 'test xml', 
                            'contact' : {'email' : 'test@example.com',
                                         'phone' : '00-0'}})
        self.assertEqual(ET.tostring(tree, encoding='unicode'),
                    '<test_ext><desc>test xml</desc><contact><email>test@example.com\
</email><phone>00-0</phone></contact></test_ext>')
        tree = xg.generate({'desc' : 'test xml', 
                            'multiple' : ['test1', 'test2', 'test3']})
        self.assertEqual(ET.tostring(tree, encoding='unicode'),
                         '<test_ext><desc>test xml</desc><multiple>test1</multiple>\
<multiple>test2</multiple><multiple>test3</multiple></test_ext>')
        tree = xg.generate({'desc' : 'test xml', 
                            'flag' : [('flag1', 'test1'), ('flag2', 'test2')]})
        self.assertEqual(ET.tostring(tree, encoding='unicode'),
                         '<test_ext><desc>test xml</desc><flag name="flag1">test1</flag>\
<flag name="flag2">test2</flag></test_ext>')


class DummyMetadataGenerator(metadata.MetadataGenerator):
    def __init__(self, db):
        super().__init__(db)

class TestMetadataGenerator(unittest.TestCase):
    def setUp(self):
        self.tempdir = tempfile.TemporaryDirectory()

    def tearDown(self):
        del self.tempdir

    def test_process(self):
        mg = DummyMetadataGenerator(None)
        description = {'herd' : ['test'],
                       'maintainer' : [{'email' : 'test@example.com', 'name' : 'testor'}],
                       'longdescription' : 'test metadata',
                       'use' : {'flag' : [('flag1', 'test flag1'), ('flag2', 'test flag2')]},
                       'upstream' : {'maintainer' : [{'name' : 'TEST'}], 'remote-id' : '001'}}
        self.assertEqual(ET.tostring(mg.process(None, description), encoding='unicode'),
                         '<pkgmetadata><herd>test</herd><maintainer><email>test@example.com</email>\
<name>testor</name></maintainer><longdescription>test metadata</longdescription><use>\
<flag name="flag1">test flag1</flag><flag name="flag2">test flag2</flag></use>\
<upstream><maintainer><name>TEST</name></maintainer><remote-id>001</remote-id></upstream></pkgmetadata>')

def suite():
    suite = unittest.TestSuite()
    suite.addTest(TestXMLGenerator('test_generate'))
    suite.addTest(TestMetadataGenerator('test_process'))
    return suite