source: publico/PortalInterlegis/produtos/il.portalinterlegis/trunk/src/il/portalinterlegis/tests/testunitboxes.py @ 6497

Última Alteração nesse arquivo desde 6497 foi 6497, incluída por mazza, 9 anos atrás

usando callables para generalizar boxes

File size: 2.4 KB
Linha 
1# -*- coding: utf-8 -*-
2import unittest2 as unittest
3from mock import patch
4
5from il.portalinterlegis.browser.boxes.manager import BoxManager, DtRow
6from il.portalinterlegis.browser.boxes.interfaces import ISimpleBox
7from mockutils import *
8from itertools import count
9
10_any_ = None # anything, doesn't really matter
11
12class TestUnitBoxes(unittest.TestCase):
13    """ Unit tests for the boxes functionality
14    """
15
16    def setUp(self):
17        self.diff_count = count(1)
18
19    def assertMultiLineEqual(self, first, second, *args):
20        "ignores differences in leading and trailing whitespace in strings"
21        self.assert_(isinstance(first, basestring), (
22                'First argument is not a string'))
23        self.assert_(isinstance(second, basestring), (
24                'Second argument is not a string'))
25
26        first = first.strip()
27        second = second.strip()
28        if first != second:
29            c = self.diff_count.next()
30            for i, s in enumerate([first, second]):
31                with open("out_%s.%s" % (c, i), "w+") as f:
32                    f.write(s)
33        super(TestUnitBoxes, self).assertMultiLineEqual(first, second, *args)
34
35    def test_html(self):
36        with patch.object(BoxManager, 'box_content') as mock:
37            mock.return_value = {'title': 'TIT_1', 'subtitle': 'SUBTIT_1', 'text': 'TEXT_1', 'target': 'alvo'}
38
39            boxmanager = BoxManager(ISimpleBox)
40            self.assertMultiLineEqual('''
41      <div class="simple-box">
42        <a href="/portal/alvo">
43          <h2>TIT_1</h2>
44          <h3 class="icon-news">SUBTIT_1</h3>
45          <p>
46            TEXT_1
47          </p>
48        </a>
49      </div>
50        ''', boxmanager.html(_any_, _any_))
51
52    def test_row(self):
53        context = object()
54        def mock_template(c):
55            # A template is just a callable. A function will do.
56            def f(context):
57                return "\n      %s" % (3*c)
58            return f
59
60        self.assertMultiLineEqual('''
61  <div class="dt-row">
62    <div class="dt-cell dt-position-0 dt-width-1">
63      AAA
64    </div>
65    <div class="dt-cell dt-position-1 dt-width-2">
66      BBB
67    </div>
68    <div class="dt-cell dt-position-3 dt-width-3">
69      CCC
70    </div>
71    <div class="dt-cell dt-position-6 dt-width-1">
72      DDD
73    </div>
74  </div>
75''', DtRow((1, mock_template('A')),
76           (2, mock_template('B')),
77           (3, mock_template('C')),
78           (1, mock_template('D'))).render(context))
79
80
Note: Veja TracBrowser para ajuda no uso do navegador do trac.
 

The contents and data of this website are published under license:
Creative Commons 4.0 Brasil - Atribuir Fonte - Compartilhar Igual.