~ubuntu-branches/ubuntu/precise/checkbox/precise

« back to all changes in this revision

Viewing changes to checkbox/tests/report.py

  • Committer: Bazaar Package Importer
  • Author(s): Marc Tardif
  • Date: 2009-01-20 16:46:15 UTC
  • Revision ID: james.westby@ubuntu.com-20090120164615-7iz6nmlef41h4vx2
Tags: 0.4
* Setup bzr-builddeb in native mode.
* Removed LGPL notice from the copyright file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright (c) 2008 Canonical
 
3
#
 
4
# Written by Marc Tardif <marc@interunion.ca>
 
5
#
 
6
# This file is part of Checkbox.
 
7
#
 
8
# Checkbox is free software: you can redistribute it and/or modify
 
9
# it under the terms of the GNU General Public License as published by
 
10
# the Free Software Foundation, either version 3 of the License, or
 
11
# (at your option) any later version.
 
12
#
 
13
# Checkbox is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
# GNU General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU General Public License
 
19
# along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
import unittest
 
22
 
 
23
from xml.dom.minidom import Document, Node
 
24
 
 
25
from checkbox.report import ReportManager, Report
 
26
 
 
27
 
 
28
class StubReport(Report):
 
29
 
 
30
    def register_dumps(self):
 
31
        self._manager.handle_dumps(str, self.dumps_test)
 
32
 
 
33
    def register_loads(self):
 
34
        self._manager.handle_loads("test", self.loads_test)
 
35
        self._manager.handle_loads("default", self.loads_default)
 
36
 
 
37
    def dumps_test(self, obj, parent):
 
38
        text_node = self._manager.document.createTextNode(obj)
 
39
        parent.appendChild(text_node)
 
40
 
 
41
    def loads_test(self, node):
 
42
        return node.childNodes[0].nodeValue.strip()
 
43
 
 
44
    def loads_default(self, node):
 
45
        default = {}
 
46
        for child in (c for c in node.childNodes if c.nodeType != Node.TEXT_NODE):
 
47
            if child.hasAttribute("name"):
 
48
                name = child.getAttribute("name")
 
49
            else:
 
50
                name = child.localName
 
51
            default[str(name)] = self._manager.call_loads(child)
 
52
        return default
 
53
 
 
54
 
 
55
class ReportManagerTest(unittest.TestCase):
 
56
 
 
57
    name = "test"
 
58
 
 
59
    def create_element(self, name):
 
60
        document = Document()
 
61
        element = document.createElement(name)
 
62
        document.appendChild(element)
 
63
        return element
 
64
 
 
65
    def create_text_node(self, name, text):
 
66
        element = self.create_element(name)
 
67
        text_node = element.createTextNode(text)
 
68
        element.appendChild(text_node)
 
69
        return text_node
 
70
 
 
71
    def test_constructor(self):
 
72
        rm = ReportManager(self.name)
 
73
        self.assertTrue(rm.name == self.name)
 
74
 
 
75
    def test_handle_dumps(self):
 
76
        rm = ReportManager(self.name)
 
77
 
 
78
        str_func = lambda obj, parent: "str"
 
79
        rm.handle_dumps(str, str_func)
 
80
        self.assertTrue(rm.call_dumps("test1", None) == "str")
 
81
 
 
82
        int_func = lambda obj, parent: "int"
 
83
        rm.handle_dumps(int, int_func)
 
84
        self.assertTrue(rm.call_dumps(1, None) == "int")
 
85
        self.assertTrue(rm.call_dumps("test2", None) == "str")
 
86
 
 
87
    def test_handle_loads(self):
 
88
        rm = ReportManager(self.name)
 
89
 
 
90
        bar_func = lambda node: "bar"
 
91
        foo_element = self.create_element("foo")
 
92
        rm.handle_loads("foo", bar_func)
 
93
        self.assertTrue(rm.call_loads(foo_element) == "bar")
 
94
 
 
95
        baz_func = lambda node: "baz"
 
96
        bar_element = self.create_element("bar")
 
97
        rm.handle_loads("bar", baz_func)
 
98
        self.assertTrue(rm.call_loads(bar_element) == "baz")
 
99
        self.assertTrue(rm.call_loads(foo_element) == "bar")
 
100
 
 
101
    def test_handle_dumps_and_loads(self):
 
102
        rm = ReportManager(self.name)
 
103
        rm.add(StubReport())
 
104
 
 
105
        document = rm.dumps("test1")
 
106
        str = document.toprettyxml("")
 
107
        self.assertTrue("test1" in str)
 
108
 
 
109
        data = rm.loads(str)
 
110
        self.assertTrue(data.has_key("test"))
 
111
        self.assertTrue(data["test"] == "test1")