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

Darryl Cousins darryl at darrylcousins.net.nz
Tue Jul 31 06:42:33 EDT 2007


Hi,

On Tue, 2007-07-31 at 01:55 -0300, Luciano Ramalho wrote:
> One component of Kirbi is the Pac (public access catalog, a book
> container), defined here:
> 
> http://svn.zope.org/Sandbox/luciano/kirbi/src/kirbi/pac.py?rev=78499&view=markup
> (or http://tinyurl.com/3ybsed)
> 
> It's method addBook uses a custom NameChooser defined in the same
> module. This is working, but I've been unable to write a unit test for
> it.
> 
> The simple test contained in this file:
> 
> http://svn.zope.org/Sandbox/luciano/kirbi/src/kirbi/tests/test_pac.txt?rev=78499&view=markup
> (or http://tinyurl.com/3xu8wt)
> 
> ... breaks with the messages below. What am I missing in the doctest
> to make it pass?

As the traceback points out:

name = INameChooser(self).chooseName(book.isbn13, book)
TypeError: ('Could not adapt', <kirbi.pac.Pac object at 0x67970>,
<InterfaceClass zope.app.container.interfaces.INameChooser>)

... you need to provide the adapter. In zope3 you would do:


zope.component.provideAdapter(IForThisInterface, INameChooser,
WithThisAdapter)

But in your doctest it should work just to do:

import grok
grok.grok('kirbi')

... because 'grokking' the package or module will make all those
registrations for you.

Hope that helps,
Darryl

> 
> --
> Luciano
> 
> 
> ####################################
> 
> Running tests at level 1
> Running unit tests:
>   Running:
> .......
> 
> Failure in test
> /Users/luciano/gsoc2007/Sandbox-luciano/kirbi/src/kirbi/tests/test_pac.txt
> Traceback (most recent call last):
>   File "/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/unittest.py",
> line 260, in run
>     testMethod()
>   File "/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/doctest.py",
> line 2157, in runTest
>     raise self.failureException(self.format_failure(new.getvalue()))
> AssertionError: Failed doctest test for test_pac.txt
>   File "/Users/luciano/gsoc2007/Sandbox-luciano/kirbi/src/kirbi/tests/test_pac.txt",
> line 0
> 
> ----------------------------------------------------------------------
> File "/Users/luciano/gsoc2007/Sandbox-luciano/kirbi/src/kirbi/tests/test_pac.txt",
> line 11, in test_pac.txt
> Failed example:
>     pac.addBook(Book(u'Zero'))
> Exception raised:
>     Traceback (most recent call last):
>       File "/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/doctest.py",
> line 1248, in __run
>         compileflags, 1) in test.globs
>       File "<doctest test_pac.txt[3]>", line 1, in ?
>         pac.addBook(Book(u'Zero'))
>       File "/Users/luciano/gsoc2007/Sandbox-luciano/kirbi/src/kirbi/pac.py",
> line 31, in addBook
>         name = INameChooser(self).chooseName(book.isbn13, book)
>     TypeError: ('Could not adapt', <kirbi.pac.Pac object at 0x67970>,
> <InterfaceClass zope.app.container.interfaces.INameChooser>)
> ----------------------------------------------------------------------
> File "/Users/luciano/gsoc2007/Sandbox-luciano/kirbi/src/kirbi/tests/test_pac.txt",
> line 12, in test_pac.txt
> Failed example:
>     pac.addBook(Book(isbn13='978-0670030583'))
> Exception raised:
>     Traceback (most recent call last):
>       File "/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/doctest.py",
> line 1248, in __run
>         compileflags, 1) in test.globs
>       File "<doctest test_pac.txt[4]>", line 1, in ?
>         pac.addBook(Book(isbn13='978-0670030583'))
>       File "/Users/luciano/gsoc2007/Sandbox-luciano/kirbi/src/kirbi/pac.py",
> line 31, in addBook
>         name = INameChooser(self).chooseName(book.isbn13, book)
>     TypeError: ('Could not adapt', <kirbi.pac.Pac object at 0x67970>,
> <InterfaceClass zope.app.container.interfaces.INameChooser>)
> ----------------------------------------------------------------------
> File "/Users/luciano/gsoc2007/Sandbox-luciano/kirbi/src/kirbi/tests/test_pac.txt",
> line 13, in test_pac.txt
> Failed example:
>     pac.addBook(Book(u'A Tale of Two Cities', isbn13='978-0141439600'))
> Exception raised:
>     Traceback (most recent call last):
>       File "/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/doctest.py",
> line 1248, in __run
>         compileflags, 1) in test.globs
>       File "<doctest test_pac.txt[5]>", line 1, in ?
>         pac.addBook(Book(u'A Tale of Two Cities', isbn13='978-0141439600'))
>       File "/Users/luciano/gsoc2007/Sandbox-luciano/kirbi/src/kirbi/pac.py",
> line 31, in addBook
>         name = INameChooser(self).chooseName(book.isbn13, book)
>     TypeError: ('Could not adapt', <kirbi.pac.Pac object at 0x67970>,
> <InterfaceClass zope.app.container.interfaces.INameChooser>)
> ----------------------------------------------------------------------
> File "/Users/luciano/gsoc2007/Sandbox-luciano/kirbi/src/kirbi/tests/test_pac.txt",
> line 14, in test_pac.txt
> Failed example:
>     sorted(pac)
> Expected:
>     ['9780141439600', '9780670030583', 'k0001']
> Got:
>     []
> ----------------------------------------------------------------------
> File "/Users/luciano/gsoc2007/Sandbox-luciano/kirbi/src/kirbi/tests/test_pac.txt",
> line 19, in test_pac.txt
> Failed example:
>     len(pac)
> Expected:
>     3
> Got:
>     0
> 
> 
> 
>   Ran 7 tests with 1 failures and 0 errors in 0.032 seconds.
> 
> Tests with failures:
>    /Users/luciano/gsoc2007/Sandbox-luciano/kirbi/src/kirbi/tests/test_pac.txt
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> http://mail.zope.org/mailman/listinfo/grok-dev



More information about the Grok-dev mailing list