gavo.helpers.testhelpers module

Helper classes for the DaCHS’ unit tests.

WARNING: This messes up some global state. DO NOT import into modules doing regular work. testtricks is the module for that kind for stuff.

class gavo.helpers.testhelpers.CatchallUI[source]

Bases: object

A replacement for base.ui, collecting the messages being sent.

This is to write tests against producing UI events. Use it with the messageCollector context manager below.

record(evType, args, kwargs)[source]
gavo.helpers.testhelpers.DataServer(data)[source]

a context manager for briefly running a web server returning data.

This yields the base URL the server is listening on.

class gavo.helpers.testhelpers.FakeSimbad(*args, **kwargs)[source]

Bases: object

we monkeypatch simbadinterface such that we don’t query simbad during tests.

Also, we don’t persist cached Simbad responses. It’s a bit sad that that functionality therefore doesn’t get exercised.

query(ident)[source]
simbadData = {'Aldebaran': {'RA': 68.98016279, 'dec': 16.50930235, 'oname': 'Aldebaran', 'otype': 'LP?'}, 'M1': {'RA': 83.63308333, 'dec': 22.0145, 'oname': 'M1', 'otype': 'SNR'}, 'Wozzlfoo7xx': None}
class gavo.helpers.testhelpers.ForkingSubprocess(args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=None, startupinfo=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=(), *, user=None, group=None, extra_groups=None, encoding=None, errors=None, text=None, umask=-1, pipesize=-1, process_group=None)[source]

Bases: Popen

A subprocess that doesn’t exec but fork.

class gavo.helpers.testhelpers.ResourceInstance(original)[source]

Bases: object

A helper class for TestResource.

See that docstring for info on what this is about; in case you encounter one of these but need the real thing, just pull .original.

class gavo.helpers.testhelpers.SamplesBasedAutoTest(name, bases, dict)[source]

Bases: type

A metaclass that builds tests out of a samples attribute of a class.

To use this, give the class a samples attribute containing a sequence of anything, and a _runTest(sample) method receiving one item of that sequence.

The metaclass will create one test<n> method for each sample.

class gavo.helpers.testhelpers.SimpleSampleComparisonTest(methodName='runTest')[source]

Bases: VerboseTest

A base class for tests that simply run a function and compare for equality.

The function to be called is in the functionToRun attribute (wrap it in a staticmethod).

The samples are pairs of (input, output). Output may be an exception (or just the serialised form of the exception).

class gavo.helpers.testhelpers.StandIn(**kwargs)[source]

Bases: object

A class having the attributes passed as kwargs to the constructor.

class gavo.helpers.testhelpers.TestResource[source]

Bases: TestResourceManager

A wrapper for testresources maintaining some backward compatibility.

testresources 2.0.1 pukes into the namespaces of what’s returned from make. I’ve not really researched what they intend people to return from make these days, but in order to avoid major surgery on the code, this class simply wraps objects that don’t allow arbitrary attributes with ResourceInstance when returned from make.

To make that happen, I need to override code from testresources itself, which is a pity. In case this breaks: Take all methods that call .make and replace make with _make_and_wrap.

Caution: when you implement reset(), you’ll have to wrap the result with testhelpers.ResourceInstance manually; but then you’d have to copy dependencies manually, which is crazy, and so I think manual reset currently really is broken.

class gavo.helpers.testhelpers.VerboseTest(methodName='runTest')[source]

Bases: ResourcedTestCase

A TestCase with a couple of convenient assert methods.

assertAlmostEqualVector(first, second, places=7, msg=None)[source]
assertEqualForArgs(callable, result, *args)[source]
assertEqualIgnoringAliases(result, expectation)[source]
assertEqualToWithin(a, b, ratio=1e-07, msg=None)[source]

asserts that abs(a-b/(a+b))<ratio.

If a+b are an underflow, we error out right now.

assertOutput(toExec, argList, expectedStdout=None, expectedStderr='', expectedRetcode=0, input=None, stdoutStrings=None)[source]

checks that execName called with argList has the given output and return value.

expectedStdout and expectedStderr can be functions. In that case, the output is passed to the function, and an assertionError is raised if the functions do not return true.

The 0th argument in argList is automatically added, only pass “real” command line arguments.

toExec may also be a zero-argument python function. In that case, the process is forked and the function is called, with sys.argv according to argList. This helps to save startup time for python main functions.

assertRaisesVerbose(exception, callable, args, msg)[source]
assertRaisesWithMsg(exception, errMsg, callable, args, msg=None, **kwargs)[source]
assertRuns(callable, args, msg=None)[source]
gavo.helpers.testhelpers.captureOutput(callable, args=(), kwargs={})[source]

runs callable(*args, **kwargs) and captures the output.

The function returns a tuple of return value, stdout output, stderr output.

gavo.helpers.testhelpers.cleanXML(aString)[source]

removes IDs and some other detritus from XML literals.

The result will be invalid XML, and all this assumes the fixed-prefix logic of the DC software.

For new tests, you should just getXMLTree and XPath tests.

aString can be bytes, in which case it will be interpreted as ASCII. cleanXML returns a string in any case.

gavo.helpers.testhelpers.computeWCSKeys(pos, size, cutCrap=False)[source]

returns a dictionary containing a 2D WCS structure for an image centered at pos with angular size. Both are 2-tuples in degrees.

gavo.helpers.testhelpers.ensureResources()[source]
gavo.helpers.testhelpers.fakeTime(t)[source]

makes time.time() return t within the controlled block.

gavo.helpers.testhelpers.forceConnection(conn)[source]

makes the sqlsupport connection context managers return conn in the controlled block.

It is up to the programmer to make sure that actually makes sense in a given test. As opposed to the originals, there will be no automatic commits here. Caveat emptor.

gavo.helpers.testhelpers.getServerInThread(data, onlyOnce=False, contentType='text/plain')[source]

runs a server in a thread and returns thread and base url.

onlyOnce will configure the server such that it destroys itself after having handled one request. The thread would still need to be joined.

So, better use the DataServer context manager.

gavo.helpers.testhelpers.getTestData(dataId)[source]
gavo.helpers.testhelpers.getTestRD(id='test.rd')[source]
gavo.helpers.testhelpers.getTestTable(tableName, id='test.rd')[source]
gavo.helpers.testhelpers.hasUDF(udfName)[source]

returns true if this system has an ADQL user defined function named udfName.

This is intended to be used as in:

@unittest.skipUnless(testhelpers.hasUDF("GAVO_FOO"), "...")

Caution: UDF names are upper case here.

gavo.helpers.testhelpers.main(testClass, methodPrefix=None)[source]
gavo.helpers.testhelpers.messageCollector()[source]

A context manager recording UI events.

The object returned by the context manager is a CatchallUI; get the events accumulated during the run time in its events attribute.

gavo.helpers.testhelpers.tempConfig(*items)[source]

sets (sect, key, value) pairs in the config temporarily and re-sets the original values after the controlled block.

gavo.helpers.testhelpers.userconfigContent(content)[source]

a context manager to temporarily set some content to userconfig.

This cleans up after itself and clears any userconfig cache before it sets to work.

content are RD elements without the root (resource) tag.