[Grok-dev] Testing a container which uses a custom NameChooser

Darryl Cousins darryl at darrylcousins.net.nz
Tue Jul 31 22:04:22 EDT 2007


Hi Luciano,


On Tue, 2007-07-31 at 08:57 -0300, Luciano Ramalho wrote:
> On 7/31/07, Darryl Cousins <darryl at darrylcousins.net.nz> wrote:
> > import grok
> > grok.grok('kirbi')
> 
> Thanks a lot, Darryl!
> 
> I saw those calls in the grok tests themselves, but I wasn't sure they
> were needed when testing grok apps (as opposed to testing grok
> itself).
> 
> I'll contribute a testing mini-tutorial...
> 
> Now I have two further questions concerning the same test (further along):
> 
> 1) Should I convert *all* strings into unicode for use with Zope 3 or
> does it still use or prefer plain strings for some purposes? I noticed
> the __name__ of the contained objects was converted by Zope to
> unicode.

If you mean strings that are used as attributes such as TextLine or Text
then you will need to use unicode.

> 
> 2) In the pac.py module [1] there is a subscriber declared like this:
> 
> @grok.subscribe(Book, grok.IObjectAddedEvent)
> def bookAdded(book, event):
>     if not book.title:
>         pac = book.__parent__
>         pac.pending_isbns.append(book.isbn13)
> 
> [1] http://svn.zope.org/Sandbox/luciano/kirbi/src/kirbi/pac.py?rev=78499&view=markup
> (or http://tinyurl.com/3ybsed)
> 
> The last line of the doctest reproduced below aims to test that event,
> but it's failing. However, when I try the live app, it works. Do I
> need some additional registration to make the test pass?

Yes, event testing needs to be setup. Check grok/tests/test_grok.py.

You could use test_grok set up directly in your tests:

from grok.test import test_grok

def test_suite():
    modules = ['app', 'pac', 'book']
    suite = unittest.TestSuite()
    for module in modules:
        module_name = 'kirbi.' + module
        test = doctest.DocTestSuite(module_name,
                        setUp=test_grok.setUpZope,
                        tearDown=test_grok.cleanUpZope)
        suite.addTest(test)
    return suite

test_grok.setUpZope set ups event testing for you.

Eventually you will likely need to write your own setUp method to ensure
registrations that your application uses. Check zope.app.testing for
more examples.

Hope this helps,
Darryl

> 
> Regards,,
> 
> Luciano
> 
> <TEST>
> 
> ==============================================
> Pac: The Public Access Catalog container class
> ==============================================
> 
> A Pac is designed to store book records. Let's create one and put a few
> books in it::
> 
>   >>> import grok
>   >>> grok.grok('kirbi')
>   >>> from kirbi.pac import Pac
>   >>> from kirbi.book import Book
>   >>> pac = Pac()
>   >>> pac.addBook(Book(u'Zero'))
>   'k0001'
>   >>> pac.addBook(Book(isbn13='978-0670030583'))
>   '9780670030583'
>   >>> pac.addBook(Book(u'A Tale of Two Cities', isbn13='978-0141439600'))
>   '9780141439600'
>   >>> sorted(pac)
>   [u'9780141439600', u'9780670030583', u'k0001']
> 
> One of the books has ISBN but no Title, so it's put in the fetch queue::
> 
>   >>> pac.pending_isbns
>   ['9780670030583']
> 
> </TEST>



More information about the Grok-dev mailing list