|
1 """unittests for gct.apptest module""" |
|
2 |
|
3 from cStringIO import StringIO |
|
4 from unittest import TestSuite |
|
5 |
|
6 |
|
7 from logilab.common.testlib import (TestCase, unittest_main, mock_object, |
|
8 SkipAwareTextTestRunner) |
|
9 from cubicweb.devtools import htmlparser |
|
10 |
|
11 from cubicweb.devtools.testlib import WebTest, EnvBasedTC |
|
12 |
|
13 class WebTestTC(TestCase): |
|
14 |
|
15 def setUp(self): |
|
16 output = StringIO() |
|
17 self.runner = SkipAwareTextTestRunner(stream=output) |
|
18 |
|
19 def test_error_raised(self): |
|
20 class MyWebTest(WebTest): |
|
21 |
|
22 def test_error_view(self): |
|
23 self.add_entity('Bug', title=u"bt") |
|
24 self.view('raising', self.execute('Bug B'), template=None) |
|
25 |
|
26 def test_correct_view(self): |
|
27 self.view('primary', self.execute('EUser U'), template=None) |
|
28 |
|
29 tests = [MyWebTest('test_error_view'), MyWebTest('test_correct_view')] |
|
30 result = self.runner.run(TestSuite(tests)) |
|
31 self.assertEquals(result.testsRun, 2) |
|
32 self.assertEquals(len(result.errors), 0) |
|
33 self.assertEquals(len(result.failures), 1) |
|
34 |
|
35 |
|
36 class TestLibTC(EnvBasedTC): |
|
37 def test_add_entity_with_relation(self): |
|
38 bug = self.add_entity(u'Bug', title=u"toto") |
|
39 self.add_entity(u'Bug', title=u"tata", identical_to=bug) |
|
40 |
|
41 rset = self.execute('Any BA WHERE BA is Bug, BA title "toto"') |
|
42 self.assertEquals(len(rset), 1) |
|
43 bug = tuple(rset.entities())[0] |
|
44 self.assertEquals(bug.identical_to[0].title, "tata") |
|
45 |
|
46 |
|
47 |
|
48 HTML_PAGE = u"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
|
49 <html> |
|
50 <head><title>need a title</title></head> |
|
51 <body> |
|
52 <h1>Hello World !</h1> |
|
53 </body> |
|
54 </html> |
|
55 """ |
|
56 |
|
57 HTML_PAGE2 = u"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
|
58 <html> |
|
59 <head><title>need a title</title></head> |
|
60 <body> |
|
61 <h1>Test</h1> |
|
62 <h1>Hello <a href="http://www.google.com">world</a> !</h1> |
|
63 <h2>h2 title</h2> |
|
64 <h3>h3 title</h3> |
|
65 <h2>antoher h2 title</h2> |
|
66 <h4>h4 title</h4> |
|
67 <p><a href="http://www.logilab.org">Logilab</a> introduces CW !</p> |
|
68 </body> |
|
69 </html> |
|
70 """ |
|
71 |
|
72 HTML_PAGE_ERROR = u"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
|
73 <html> |
|
74 <head><title>need a title</title></head> |
|
75 <body> |
|
76 Logilab</a> introduces CW ! |
|
77 </body> |
|
78 </html> |
|
79 """ |
|
80 |
|
81 HTML_NON_STRICT = u"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
|
82 <html> |
|
83 <head><title>need a title</title></head> |
|
84 <body> |
|
85 <h1><a href="something.com">title</h1> |
|
86 </body> |
|
87 </html> |
|
88 """ |
|
89 |
|
90 |
|
91 class HTMLPageInfoTC(TestCase): |
|
92 """test cases for PageInfo""" |
|
93 def setUp(self): |
|
94 parser = htmlparser.DTDValidator() |
|
95 self.page_info = parser.parse_string(HTML_PAGE2) |
|
96 |
|
97 def test_source1(self): |
|
98 """make sure source is stored correctly""" |
|
99 self.assertEquals(self.page_info.source, HTML_PAGE2) |
|
100 |
|
101 def test_source2(self): |
|
102 """make sure source is stored correctly - raise exception""" |
|
103 parser = htmlparser.DTDValidator() |
|
104 self.assertRaises(AssertionError, parser.parse_string, HTML_PAGE_ERROR) |
|
105 |
|
106 |
|
107 def test_has_title_no_level(self): |
|
108 """tests h? tags information""" |
|
109 self.assertEquals(self.page_info.has_title('Test'), True) |
|
110 self.assertEquals(self.page_info.has_title('Test '), False) |
|
111 self.assertEquals(self.page_info.has_title('Tes'), False) |
|
112 self.assertEquals(self.page_info.has_title('Hello world !'), True) |
|
113 |
|
114 def test_has_title_level(self): |
|
115 """tests h? tags information""" |
|
116 self.assertEquals(self.page_info.has_title('Test', level = 1), True) |
|
117 self.assertEquals(self.page_info.has_title('Test', level = 2), False) |
|
118 self.assertEquals(self.page_info.has_title('Test', level = 3), False) |
|
119 self.assertEquals(self.page_info.has_title('Test', level = 4), False) |
|
120 self.assertRaises(IndexError, self.page_info.has_title, 'Test', level = 5) |
|
121 |
|
122 def test_has_title_regexp_no_level(self): |
|
123 """tests has_title_regexp() with no particular level specified""" |
|
124 self.assertEquals(self.page_info.has_title_regexp('h[23] title'), True) |
|
125 |
|
126 def test_has_title_regexp_level(self): |
|
127 """tests has_title_regexp() with a particular level specified""" |
|
128 self.assertEquals(self.page_info.has_title_regexp('h[23] title', 2), True) |
|
129 self.assertEquals(self.page_info.has_title_regexp('h[23] title', 3), True) |
|
130 self.assertEquals(self.page_info.has_title_regexp('h[23] title', 4), False) |
|
131 |
|
132 def test_appears(self): |
|
133 """tests PageInfo.appears()""" |
|
134 self.assertEquals(self.page_info.appears('CW'), True) |
|
135 self.assertEquals(self.page_info.appears('Logilab'), True) |
|
136 self.assertEquals(self.page_info.appears('Logilab introduces'), True) |
|
137 self.assertEquals(self.page_info.appears('H2 title'), False) |
|
138 |
|
139 def test_has_link(self): |
|
140 """tests has_link()""" |
|
141 self.assertEquals(self.page_info.has_link('Logilab'), True) |
|
142 self.assertEquals(self.page_info.has_link('logilab'), False) |
|
143 self.assertEquals(self.page_info.has_link('Logilab', 'http://www.logilab.org'), True) |
|
144 self.assertEquals(self.page_info.has_link('Logilab', 'http://www.google.com'), False) |
|
145 |
|
146 def test_has_link_regexp(self): |
|
147 """test has_link_regexp()""" |
|
148 self.assertEquals(self.page_info.has_link_regexp('L[oi]gilab'), True) |
|
149 self.assertEquals(self.page_info.has_link_regexp('L[ai]gilab'), False) |
|
150 |
|
151 |
|
152 if __name__ == '__main__': |
|
153 unittest_main() |
|
154 |