Author: bree
Date: Wed Feb  1 18:59:09 2006
New Revision: 2260

Added:
   azax/branches/snowsprint/tests/test_azaxview.py
      - copied, changed from r2258, 
azax/branches/snowsprint/tests/test_azaxresponse.py
Removed:
   azax/branches/snowsprint/tests/test_azaxresponse.py
Log:
Fixing test after restructuring.

TODO: functional test to be added

Copied: azax/branches/snowsprint/tests/test_azaxview.py (from r2258, 
azax/branches/snowsprint/tests/test_azaxresponse.py)
==============================================================================
--- azax/branches/snowsprint/tests/test_azaxresponse.py (original)
+++ azax/branches/snowsprint/tests/test_azaxview.py     Wed Feb  1 18:59:09 2006
@@ -21,143 +21,113 @@
 import unittest, os
 from zope.testing import doctest
 from Testing.ZopeTestCase import ZopeTestCase
-
-from Products.azax.azaxresponse import AzaxResponse
-from Products.azax.azaxresponse import HTML_DTD
+from Products.azax import AzaxBaseView
+#from Products.azax.azaxresponse import HTML_DTD
 from Products.azax import config 
+from ZPublisher.HTTPRequest import HTTPRequest
+from cStringIO import StringIO
 
-class FakeResponse:
-    _stuff = {}
+# fake request
 
-    def setHeader(self, name, value):
-        self._stuff[name] = value
+def dummy(kdict, name='dummy'):
+    return type(name, (object,), kdict)()
 
-class AzaxResponseTestCase(ZopeTestCase):
+def setHeader(self, a, b):
+    self.__dict__.update(locals())
 
-    def cleanupXMLForTesting(self, xml):
-        return xml.replace(' />', '/>')
+fakerequest = HTTPRequest(StringIO(),
+                          dict(SERVER_NAME='dummy', SERVER_PORT='8080'),
+                          dummy(dict(setHeader=setHeader, name='Response')))
 
-    def test_instanciation(self):
-        ob = AzaxResponse()
-        self.assertNotEquals(ob, None)
+class FakeContent(object):
+    pass
+    
+#s = getGlobalService(Presentation)
+#s.provideView(for_, name, IBrowserRequest, class_, layer)
+#initializeClass(FakeContent)
 
-    def _wrapped_commands(self, inline):
-        return '%s\n<html xmlns="http://www.w3.org/1999/xhtml"; 
xmlns:kukit="http://www.kukit.org/commands/1.0";>' \
-            '<body>%s</body></html>' % (HTML_DTD, inline)
+class AzaxViewTestCase(ZopeTestCase):
 
-    def test_str_(self):
-        ob = AzaxResponse()
-        self.assertEquals(self.cleanupXMLForTesting(str(ob)), 
self._wrapped_commands(''))
+    def afterSetUp(self):
+        # fake content
+        fakecontent = FakeContent()
+        # Set up a fake view (with no content)
+        self.view = AzaxBaseView(fakecontent, fakerequest)
+        
+class TestAzaxView(AzaxViewTestCase):
     
-    def assertXMLEquals(self, a, b):
-        etree = config.etree
-        a = etree.fromstring(a)
-        a = etree.tostring(a)
-        b = etree.fromstring(b)
-        b = etree.tostring(b)
-        self.assertEquals(a, b)
+    def test_empty(self):
+        view = self.view
+        commands = view.getCommands()
+        self.assertEqual(len(commands), 0)
 
     def test_addCommand(self):
-        ob = AzaxResponse()
-        command = ob.addCommand('name', 'selector')
-        self.assertXMLEquals(self.cleanupXMLForTesting(
-            config.etree.tostring(command.ob)), '<command selector="selector" 
name="name"/>')
-        self.assertXMLEquals(self.cleanupXMLForTesting(str(ob)),
-         self._wrapped_commands('<kukit:command selector="selector" 
name="name"/>'))
+        view = self.view
+        command = view.addCommand('name', 'selector')
+        commands = view.getCommands()
+        self.assertEqual(len(commands), 1)
+        self.assertEqual(command.getName(), 'name')
+        self.assertEqual(command.getSelector(), 'selector')
+        params = command.getParams()
+        self.assertEqual(len(params), 0)
+        
+    # XXX since lxml is gone, the next cases are no problem anymore
+    # Nevertheless, we test all these cases
+    
+    def _checkSetHtmlResult(self, content):
+        view = self.view
+        view.setHtmlAsChild('div.class', content)
+        commands = view.getCommands()
+        self.assertEqual(len(commands), 1)
+        command = commands[0]
+        self.assertEqual(command.getName(), 'setHtmlAsChild')
+        self.assertEqual(command.getSelector(), 'div.class')
+        params = command.getParams()
+        self.assertEqual(len(params), 1)
+        self.assertEqual(params[0].getName(), 'html')
+        self.assertEqual(params[0].getContent(), content)
 
     def test_setHtmlAsChildTextPlusEntity(self):
-        ob = AzaxResponse()
-        ob.setHtmlAsChild('div.class', '&nbsp;')
-        result = HTML_DTD + \
-          '<html ' \
-          'xmlns="http://www.w3.org/1999/xhtml"; 
xmlns:kukit="http://www.kukit.org/commands/1.0";>' \
-          '<body><kukit:command selector="div.class" name="setHtmlAsChild">' \
-          '<kukit:param 
name="html"></kukit:param></kukit:command></body></html>'
-          
-        self.assertXMLEquals(self.cleanupXMLForTesting(str(ob)), result)
+        self._checkSetHtmlResult('&nbsp;')
 
     def test_setHtmlAsChildTextOnly(self):
-        ob = AzaxResponse()
-        ob.setHtmlAsChild('div.class', 'new_content')
-        self.assertXMLEquals(self.cleanupXMLForTesting(str(ob)),
-         self._wrapped_commands('<kukit:command selector="div.class" 
name="setHtmlAsChild">' \
-         '<kukit:param name="html">new_content</kukit:param></kukit:command>'))
-    
-    def test_setHtmlAsChildTagOnly(self):
-        ob = AzaxResponse()
-        ob.setHtmlAsChild('div.class', '<p>new_content</p>')
-        self.assertXMLEquals(self.cleanupXMLForTesting(str(ob)),
-         self._wrapped_commands('<kukit:command selector="div.class" 
name="setHtmlAsChild">' \
-         '<kukit:param name="html"><p>' \
-         'new_content</p></kukit:param></kukit:command>'))
+        self._checkSetHtmlResult('new content')
 
+    def test_setHtmlAsChildTagOnly(self):
+        self._checkSetHtmlResult('<p>new_content</p>')
+        
     def test_setHtmlAsChildTagPlusText(self):
-        ob = AzaxResponse()
-        ob.setHtmlAsChild('div.class', '<p>new_content</p>after')
-        self.assertXMLEquals(self.cleanupXMLForTesting(str(ob)),
-         self._wrapped_commands('<kukit:command selector="div.class" 
name="setHtmlAsChild">' \
-         '<kukit:param name="html"><p>' \
-         'new_content</p>after</kukit:param></kukit:command>') )
-
+        self._checkSetHtmlResult('<p>new_content</p>after')
+        
     def test_setHtmlAsChildTextTagPlusText(self):
-        ob = AzaxResponse()
-        ob.setHtmlAsChild('div.class', 'before<p>new_content</p>after')
-        self.assertXMLEquals(self.cleanupXMLForTesting(str(ob)),
-         self._wrapped_commands('<kukit:command selector="div.class" 
name="setHtmlAsChild">' \
-         '<kukit:param name="html">before<p>' \
-         'new_content</p>after</kukit:param></kukit:command>'))
-
-    def test___call__(self):
-        response = FakeResponse()
-        ob = AzaxResponse(response)
-        ob.setHtmlAsChild('div.class', 'new_content')
-        ob()
-        self.assertEquals(response._stuff['Content-Type'], 'text/xml')
-
-class ElementTreeTestCase(AzaxResponseTestCase):
-    def afterSetUp(self):
-        from elementtree import ElementTree as etree
-        config.etree = etree
+        self._checkSetHtmlResult('before<p>new_content</p>after')
 
-    def test_etree(self):
-        self.assertEquals(config.etree.__name__, 'elementtree.ElementTree')
+class FTestAzaxResponse(AzaxViewTestCase):
+    'Functional tests'
+    # XXX don't work at the moment
+    
+    def cleanupXMLForTesting(self, xml):
+        return xml.replace(' />', '/>')
 
-class CElementTreeTestCase(AzaxResponseTestCase):
-    def afterSetUp(self):
-        import cElementTree as etree
-        config.etree = etree
+    def _wrapped_commands(self, inline):
+        return '%s\n<html xmlns="http://www.w3.org/1999/xhtml"; 
xmlns:kukit="http://www.kukit.org/commands/1.0";>' \
+#            '<body>%s</body></html>' % (HTML_DTD, inline)
 
-    def test_etree(self):
-        self.assertEquals(config.etree.__name__, 'cElementTree')
+    def assertXMLEquals(self, a, b):
+        self.assertEqual(a, b)
 
-class LxmlTestCase(AzaxResponseTestCase):
-    def afterSetUp(self):
-        from Products.lxml import etree
-        config.etree = etree
+    def assertCommandsEqual(self, a, b):
+        self.assertEqual(a, _wrapped_commands(b))
 
-    def test_etree(self):
-        self.assertEquals(config.etree.__name__, 'lxml.etree')
+    def test_empty(self):
+        view = self.view
+        commands = view.getCommands()
+        self.assertEqual(len(commands), 0)
+        self.assertEquals(view.request.response.getHeader('Content-Type'), 
'text/xml')
 
 def test_suite():
     suites = []
-    try:
-        from Products.lxml import etree
-    except ImportError:
-        pass
-    else:
-        suites.append(unittest.makeSuite(LxmlTestCase))
-    try:
-        from elementtree import ElementTree as etree
-    except ImportError:
-        pass
-    #else:
-    #    suites.append(unittest.makeSuite(ElementTreeTestCase))
-    try:
-        import cElementTree as etree
-    except ImportError:
-        pass
-    #else:
-    #    suites.append(unittest.makeSuite(CElementTreeTestCase))
-    suites.append(doctest.DocTestSuite('Products.azax.azaxresponse'))
+    suites.append(unittest.makeSuite(TestAzaxView))
+    #suites.append(unittest.makeSuite(FTestAzaxView))
+    suites.append(doctest.DocTestSuite('Products.azax.azaxview'))
     return unittest.TestSuite(suites)
-
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to