gavo.helpers.testtricks module

Helper functions and classes for unit tests and similar.

Whatever is useful to unit tests from here should be imported into testhelpers, too. Unit test modules should not be forced to import this.

class gavo.helpers.testtricks.QNamer(ns)[source]

Bases: object

A hack that generates QNames through getattr.

Construct with the desired namespace.

class gavo.helpers.testtricks.XSDResolver[source]

Bases: lxml.etree.Resolver

A resolver for external entities only returning in-tree files.

resolve(self, system_url, public_id, context)[source]

Override this method to resolve an external source by system_url and public_id. The third argument is an opaque context object.

Return the result of one of the resolve_*() methods.

class gavo.helpers.testtricks.XSDTestMixin[source]

Bases: object

provides a assertValidates method doing XSD validation.

assertValidates raises an assertion error with the validator’s messages on an error. You can optionally pass a leaveOffending argument to make the method store the offending document in badDocument.xml.

assertValidates(xmlSource, leaveOffending=False)[source]

a context manager collecting event arguments for a while.

The yielded thing is a list that contains tuples of event name and the event arguments.


a sort-of-interactive way to investigate where ob is referenced.

  • d – enter pdb (look at ob, perhaps at nob)

  • u – follow

  • x – continue execution


returns a validator that knows the schemata typically useful within the VO.

This will currently only work if DaCHS is installed from an SVN checkout with develop.

What’s returned has a method assertValid(et) that raises an exception if the elementtree et is not valid. You can simply call it to get back True for valid and False for invalid.


returns an lxml validator containing the schemas in schemaPaths.

schemaPaths must be actual file paths, absolute or trunk/schema-relative.

gavo.helpers.testtricks.getMemDiffer(ofClass=<class 'gavo.base.structure.Structure'>)[source]

returns a function to call that returns a list of new DaCHS structures since this was called.

If you watch everything, things get hairy because of course the state of this function (for instance) also creates references. Hence, pass ofClass to choose what the function will track.

This will call a gc.collect itself (and wouldn’t make sense without that)


returns a list of elements in items that do not have a reference from any other in items.

gavo.helpers.testtricks.getXMLTree(xmlString, debug=False)[source]

returns an libxml2 etree-like object for xmlString, where, for convenience, all namespaces on elements are nuked.

This will only accept strings.

The libxml2 etree lets you do xpath searching using the xpath method.

Nuking namespaces is of course not a good idea in general, so you might want to think again before you use this in production code.

To facilitate writing tests, in addition to lxml.etree methods the returned object also has the following methods:

  • uniqueXpath(xpath), returning a single match if it’s there and raises an assertion error otherwise.

  • getById(id), returning the unique element with id and raising an assertion error if that doesn’t exist.

  • getByID(id), as getById, by for VOTable-style ID.

  • asString(), returning a string representation of the tree

gavo.helpers.testtricks.getXSDErrors(data, leaveOffending=False)

returns error messages for the XSD validation of the string in data.

gavo.helpers.testtricks.getXSDErrorsLXML(data, leaveOffending=False)[source]

returns error messages for the XSD validation of the string in data.

gavo.helpers.testtricks.memdebug(watchClass=<class 'gavo.base.structure.Structure'>)[source]

a debug method to track memory usage after some code has run.

This is typically run from ArchiveService.getChild, since request processing should be idempotent wrt memory after initial caching.

This is for editing in place by DaCHS plumbers; accordingly, you’re not supposed to make sense of this.

gavo.helpers.testtricks.testFile(name, content, writeGz=False, inDir='/home/msdemlei/gavo/tmp', timestamp=None)[source]

a context manager that creates a file name with content in inDir.

The full path name is returned.

content can be bytes or str; in the latter case, it’s utf-8 encoded before writing.

With writeGz=True, content is gzipped on the fly (don’t do this if the data already is gzipped).

You can pass in name=None to get a temporary file name if you don’t care about the name.

inDir will be created as a side effect if it doesn’t exist but (right now, at least), not be removed.