Package gavo :: Package helpers :: Module testtricks
[frames] | no frames]

Module testtricks

source code

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.

Classes
  XSDResolver
A resolver for external entities only returning in-tree files.
  QNamer
A hack that generates QNames through getattr.
  XSDTestMixin
provides a assertValidates method doing XSD validation.
Functions
 
getXMLTree(xmlString, debug=False)
returns an ``libxml2`` etree for ``xmlString``, where, for convenience, all namespaces on elements are nuked.
source code
 
getXSDErrorsXerces(data, leaveOffending=False)
returns Xerces error messages for XSD validation of data, or None if data is valid.
source code
 
MyParser(*args, **kwds) source code
 
getJointValidator(schemaPaths)
returns an lxml validator containing the schemas in schemaPaths.
source code
 
getDefaultValidator(extraSchemata=[])
returns a validator that knows the schemata typically useful within the VO.
source code
 
getXSDErrorsLXML(data, leaveOffending=False)
returns error messages for the XSD validation of the string in data.
source code
 
getXSDErrors(data, leaveOffending=False)
returns error messages for the XSD validation of the string in data.
source code
 
getMemDiffer(ofClass=<class 'gavo.base.structure.Structure'>)
returns a function to call that returns a list of new DaCHS structures since this was called.
source code
 
getUnreferenced(items)
returns a list of elements in items that do not have a reference from any other in items.
source code
 
debugReferenceChain(ob)
a sort-of-interactive way to investigate where ob is referenced.
source code
 
memdebug()
a debug method to track memory usage after some code has run.
source code
 
testFile(*args, **kwds)
a context manager that creates a file name with content in inDir.
source code
 
collectedEvents(*args, **kwds)
a context manager collecting event arguments for a while.
source code
Variables
  RESOLVER = <gavo.helpers.testtricks.XSDResolver object>
  XSD_PARSER = <lxml.etree.XMLParser object>
  XS = <gavo.helpers.testtricks.QNamer object>
  VO_SCHEMATA = ['Characterisation-v1.11.xsd', 'ConeSearch-v1.1....
  NEWIDS = set([])
  __package__ = 'gavo.helpers'
Function Details

getXMLTree(xmlString, debug=False)

source code 

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

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.

getXSDErrorsXerces(data, leaveOffending=False)

source code 

returns Xerces error messages for XSD validation of data, or None if data is valid.

See the docstring of XSDTestMixin for how to make this work.

This raises a unittest.SkipTest exception if the validator cannot be found.

MyParser(*args, **kwds)

source code 
Decorators:
  • @contextlib.contextmanager

getJointValidator(schemaPaths)

source code 

returns an lxml validator containing the schemas in schemaPaths.

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

getDefaultValidator(extraSchemata=[])

source code 

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 setup.py 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.

getXSDErrorsLXML(data, leaveOffending=False)

source code 

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

This is the lxml-based implemenation, much less disruptive than the xerces-based one.

getXSDErrors(data, leaveOffending=False)

source code 

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

This is the lxml-based implemenation, much less disruptive than the xerces-based one.

getMemDiffer(ofClass=<class 'gavo.base.structure.Structure'>)

source code 

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 funtion will track.

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

memdebug()

source code 

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

This is typically run from ArchiveService.locateChild, 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.

testFile(*args, **kwds)

source code 

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

The full path name is returned.

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.

Decorators:
  • @contextlib.contextmanager

collectedEvents(*args, **kwds)

source code 

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.

Decorators:
  • @contextlib.contextmanager

Variables Details

VO_SCHEMATA

Value:
['Characterisation-v1.11.xsd',
 'ConeSearch-v1.1.xsd',
 'DataModel-v1.0.xsd',
 'DocRegExt-v1.0.xsd',
 'oai_dc.xsd',
 'OAI-PMH.xsd',
 'RegistryInterface-v1.0.xsd',
 'SIA-v1.2.xsd',
...