Re: [Zope3-Users] Calling a view in a doc test

2007-06-19 Thread Marius Gedminas
On Mon, Jun 18, 2007 at 10:14:14PM +0200, Florian Lindner wrote:
> Am Dienstag, 5. Juni 2007 schrieb Marius Gedminas:
> > I would suggest that you remove everything and keep just
> >
> >   def blogSetUp(test):
> 
> zope.testing.module.setUp(test, 'Blog.doctest')
> 
> it worked after I've added the line above.

That's good.

(I've no idea what zope.testing.module.setUp does).

> >   setup.placelessSetUp()
> >   setup.setUpTraversal()
> >
> >   def blogTearDown(test):
> >   setup.placelessTearDown()
> >
> > > and this is my README.txt containing the test:
> > > >>> context = MyBlog
> >
> > You may want to actually create the object:
> >   >>> context = MyBlog()
> 
> MyBlog is the instance.

That's confusing.  In Python it is customary to have class names start
with a captial letter, and function/variable names start with a
lowercase letter.  (Factory functions, whose primary purpose is to
create new objects, are also sometimes named with a starting capital
letter.)

Regards,
Marius Gedminas
-- 
Any time somebody tells you that you shouldn't do something because it's
"unprofessional," you know that they've run out of real arguments.
-- Joel Spolski


signature.asc
Description: Digital signature
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


Re: [Zope3-Users] Calling a view in a doc test

2007-06-18 Thread Florian Lindner
Am Dienstag, 5. Juni 2007 schrieb Marius Gedminas:
> On Tue, Jun 05, 2007 at 04:49:37PM +0200, Florian Lindner wrote:
> > Am Montag, 4. Juni 2007 schrieb Marius Gedminas:
> > > On Mon, Jun 04, 2007 at 03:14:16PM +0200, Florian Lindner wrote:
> > > > in a doctest I have an object which has a view registered.
> > > > I want to call this view and test for the XML it returns.
> > > > How can I call the view so that it is being rendered, just like
> > > > called by a browser?
> >
> > I have thes setup and tearDown methods:
> >
> > import unittest
> > import zope.testing.module
> > from zope.testing import doctest
> > from zope.component import testing, eventtesting
> > from zope.app.container.tests.placelesssetup import PlacelessSetup
> > from zope.app.testing import setup
> >
> > container_setup = PlacelessSetup()
> >
> > def blogSetUp(test):
> > zope.testing.module.setUp(test, 'Blog.doctest')
> > testing.setUp(test)
> > eventtesting.setUp(test)
> > container_setup.setUp()
> > setup.placelessSetUp()
> > setup.setUpTraversal()
> >
> > def blogTearDown(test):
> > setup.placelessTearDown()
> > zope.testing.module.tearDown(test)
> > testing.tearDown(test)
>
> Oh, my, this feels like cargo-cult programming[1] to me.  For example,
> zope.app.testing.setup.placelessSetUp() calls
> zope.app.container.tests.placelesssetup.PlacelessSetup().setUp() for you
> already, you don't need to do it twice.  In fact the
> CleanUp().cleanUp(), which is the first thing that placelessSetUp()
> calls, undoes all the changfes made by container_setup.setUp().  The
> same applies to zope.component.testing.setUp.
>
>   [1] http://en.wikipedia.org/wiki/Cargo_cult_programming

You're probably right about that, I've very little experience with the testing 
framework.

> I would suggest that you remove everything and keep just
>
>   def blogSetUp(test):

zope.testing.module.setUp(test, 'Blog.doctest')

it worked after I've added the line above.

>   setup.placelessSetUp()
>   setup.setUpTraversal()
>
>   def blogTearDown(test):
>   setup.placelessTearDown()
>
> > and this is my README.txt containing the test:
> > >>> context = MyBlog
>
> You may want to actually create the object:
>   >>> context = MyBlog()

MyBlog is the instance.

> and sometimes it is a good idea to put it into the containment
>
> hierarchy, if you're going to look up things like absolute URLs:
>   >>> from zope.app.folder import rootFolder
>   >>> root = rootFolder()
>   >>> root['my_blog'] = context
>   >>>
> > >>> from zope.publisher.browser import TestRequest
> > >>> from browser.views import RSSFeed
> > >>> request = TestRequest()
> > >>> view = RSSFeed(context, request)
> > >>> print view()
> >
> > Since my code includes a call to absoluteURL I have added your setup and
> > tearDown methods. But there is still an error:

Thanks, now it works!

> > BTW: What would be the name of the MyViewClass if the page would be
> > registered without a class set?
>
> There isn't one.  When you use the  directive, the ZCML
> maaagick creates a new class (with a funky name) at runtime, with extra
> attributes and methods.  You cannot access that class from a unit test,
> because it doesn't exist in your source code.
>
> My suggestion is "don't do that".  If you want a view with just a
> template, and you want to render it in a unit test, define a view class
>
>class MyTrivialViewClass(BrowserPage):
>__call__ = ViewPageTemplateFile('mytemplate.pt')

Regards,

Florian
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


Re: [Zope3-Users] Calling a view in a doc test

2007-06-05 Thread Marius Gedminas
On Tue, Jun 05, 2007 at 04:49:37PM +0200, Florian Lindner wrote:
> Am Montag, 4. Juni 2007 schrieb Marius Gedminas:
> > On Mon, Jun 04, 2007 at 03:14:16PM +0200, Florian Lindner wrote:
> > > in a doctest I have an object which has a view registered.
> > > I want to call this view and test for the XML it returns.
> > > How can I call the view so that it is being rendered, just like called by
> > > a browser?

> I have thes setup and tearDown methods:
> 
> import unittest
> import zope.testing.module
> from zope.testing import doctest
> from zope.component import testing, eventtesting
> from zope.app.container.tests.placelesssetup import PlacelessSetup
> from zope.app.testing import setup
> 
> container_setup = PlacelessSetup()
> 
> def blogSetUp(test):
> zope.testing.module.setUp(test, 'Blog.doctest')
> testing.setUp(test)
> eventtesting.setUp(test)
> container_setup.setUp()
> setup.placelessSetUp()
> setup.setUpTraversal()
> 
> def blogTearDown(test):
> setup.placelessTearDown()
> zope.testing.module.tearDown(test)
> testing.tearDown(test)

Oh, my, this feels like cargo-cult programming[1] to me.  For example,
zope.app.testing.setup.placelessSetUp() calls
zope.app.container.tests.placelesssetup.PlacelessSetup().setUp() for you
already, you don't need to do it twice.  In fact the
CleanUp().cleanUp(), which is the first thing that placelessSetUp()
calls, undoes all the changfes made by container_setup.setUp().  The
same applies to zope.component.testing.setUp.

  [1] http://en.wikipedia.org/wiki/Cargo_cult_programming

I would suggest that you remove everything and keep just

  def blogSetUp(test):
  setup.placelessSetUp()
  setup.setUpTraversal()

  def blogTearDown(test):
  setup.placelessTearDown()

> and this is my README.txt containing the test:
> 
> >>> context = MyBlog

You may want to actually create the object:

  >>> context = MyBlog()

and sometimes it is a good idea to put it into the containment
hierarchy, if you're going to look up things like absolute URLs:

  >>> from zope.app.folder import rootFolder
  >>> root = rootFolder()
  >>> root['my_blog'] = context

> >>> from zope.publisher.browser import TestRequest
> >>> from browser.views import RSSFeed
> >>> request = TestRequest()
> >>> view = RSSFeed(context, request)
> >>> print view()
> 
> Since my code includes a call to absoluteURL I have added your setup and 
> tearDown methods. But there is still an error:
> 
>   File "/home/florian/Zope3/src/zope/traversing/browser/absoluteurl.py", 
> line 34, in absoluteURL
> return zope.component.getMultiAdapter((ob, request), IAbsoluteURL)()
>   File "/home/florian/Zope3/src/zope/traversing/browser/absoluteurl.py", 
> line 55, in __str__
> raise TypeError(_insufficientContext)
> TypeError: There isn't enough context to get URL information. This is 
> probably due to a bug in setting up location information.
> 
> 
> Do you know what's missing here?

See above.

> Thanks,
> 
> Florian
> 
> BTW: What would be the name of the MyViewClass if the page would be 
> registered 
> without a class set?

There isn't one.  When you use the  directive, the ZCML
maaagick creates a new class (with a funky name) at runtime, with extra
attributes and methods.  You cannot access that class from a unit test,
because it doesn't exist in your source code.

My suggestion is "don't do that".  If you want a view with just a
template, and you want to render it in a unit test, define a view class

   class MyTrivialViewClass(BrowserPage):
   __call__ = ViewPageTemplateFile('mytemplate.pt')

HTH,
Marius Gedminas
-- 
You have moved the mouse. NT must be restarted for the changes to take effect.


signature.asc
Description: Digital signature
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


Re: [Zope3-Users] Calling a view in a doc test

2007-06-05 Thread Florian Lindner
Am Montag, 4. Juni 2007 schrieb Marius Gedminas:
> On Mon, Jun 04, 2007 at 03:14:16PM +0200, Florian Lindner wrote:
> > Hello,
> > in a doctest I have an object which has a view registered.
> > I want to call this view and test for the XML it returns.
> > How can I call the view so that it is being rendered, just like called by
> > a browser?
>
> Is that in a unit test, or a functional test?
>
> In a unit test you can do it like this:
> >>> context = ...yourcontentobject...
> >>> from zope.publisher.browser import TestRequest
> >>> request = TestRequest()
> >>> view = MyViewClass(context, request)
> >>> print view()
>
> 
>
> If you want to provide, e.g., form parameters, pass them to the request:
> >>> request = TestRequest(form={'foo': u'Lalala\u1234'})
>
> If your view does anything interesting (e.g. use TALES expressions, or
> refer to other views like context/@@absolute_url), you will need to
> register a whole bunch of components in your doctest setUp methods.
> Don't forget to tear them down afterwards.  IIRC you will need
>
> from zope.app.testing import setup
>
> def setUp(test):
> setup.placelessSetUp()
> setup.setUpTraversal()
>
> def tearDown(test):
> setup.placelessTearDown()
>
> at the very least.  Accessing other views, resources, or, god forbid,
> forms, will require other component registrations.  At some point you
> have two choices: figure out this stuff once and then use copy & paste
> (actually, helper functions defined once in your project), or switch to
> testing your views with functional tests.

Hello,
I have thes setup and tearDown methods:

import unittest
import zope.testing.module
from zope.testing import doctest
from zope.component import testing, eventtesting
from zope.app.container.tests.placelesssetup import PlacelessSetup
from zope.app.testing import setup

container_setup = PlacelessSetup()

def blogSetUp(test):
zope.testing.module.setUp(test, 'Blog.doctest')
testing.setUp(test)
eventtesting.setUp(test)
container_setup.setUp()
setup.placelessSetUp()
setup.setUpTraversal()

def blogTearDown(test):
setup.placelessTearDown()
zope.testing.module.tearDown(test)
testing.tearDown(test)


and this is my README.txt containing the test:

>>> context = MyBlog
>>> from zope.publisher.browser import TestRequest
>>> from browser.views import RSSFeed
>>> request = TestRequest()
>>> view = RSSFeed(context, request)
>>> print view()

Since my code includes a call to absoluteURL I have added your setup and 
tearDown methods. But there is still an error:

  File "/home/florian/Zope3/src/zope/traversing/browser/absoluteurl.py", 
line 34, in absoluteURL
return zope.component.getMultiAdapter((ob, request), IAbsoluteURL)()
  File "/home/florian/Zope3/src/zope/traversing/browser/absoluteurl.py", 
line 55, in __str__
raise TypeError(_insufficientContext)
TypeError: There isn't enough context to get URL information. This is 
probably due to a bug in setting up location information.


Do you know what's missing here?

Thanks,

Florian

BTW: What would be the name of the MyViewClass if the page would be registered 
without a class set?
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


Re: [Zope3-Users] Calling a view in a doc test

2007-06-04 Thread Marius Gedminas
On Mon, Jun 04, 2007 at 03:14:16PM +0200, Florian Lindner wrote:
> Hello,
> in a doctest I have an object which has a view registered.
> I want to call this view and test for the XML it returns.
> How can I call the view so that it is being rendered, just like called by a 
> browser?

Is that in a unit test, or a functional test?

In a unit test you can do it like this:

>>> context = ...yourcontentobject...
>>> from zope.publisher.browser import TestRequest
>>> request = TestRequest()
>>> view = MyViewClass(context, request)
>>> print view()


If you want to provide, e.g., form parameters, pass them to the request:

>>> request = TestRequest(form={'foo': u'Lalala\u1234'})

If your view does anything interesting (e.g. use TALES expressions, or
refer to other views like context/@@absolute_url), you will need to
register a whole bunch of components in your doctest setUp methods.
Don't forget to tear them down afterwards.  IIRC you will need

from zope.app.testing import setup

def setUp(test):
setup.placelessSetUp()
setup.setUpTraversal()

def tearDown(test):
setup.placelessTearDown()

at the very least.  Accessing other views, resources, or, god forbid,
forms, will require other component registrations.  At some point you
have two choices: figure out this stuff once and then use copy & paste
(actually, helper functions defined once in your project), or switch to
testing your views with functional tests.

Marius Gedminas
-- 
Cool. Does it also recode ISO10646-1 pcf files into the funny
permutations and subsets used a long time ago in a galaxy far far away
on the planets Isolatinus XV and Koiruski VIII ...
-- Markus Kuhn inquires about libXft


signature.asc
Description: Digital signature
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


Re: [Zope3-Users] Calling a view in a doc test

2007-06-04 Thread Jim Washington
Florian Lindner wrote:
> Hello,
> in a doctest I have an object which has a view registered.
> I want to call this view and test for the XML it returns.
> How can I call the view so that it is being rendered, just like called by a 
> browser?
>
> Thanks,
>
> Florian
> ___
> Zope3-users mailing list
> Zope3-users@zope.org
> http://mail.zope.org/mailman/listinfo/zope3-users
>   
Hi, Florian

Search for zope.testbrowser.

It's sometimes used in functional doctests.

from zope.testbrowser.testing import Browser
browser = Browser('http://localhost/')
#browser.handleErrors = False
browser.open('/somepath/someview.html')

-Jim Washington
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users