gavo.formal.testing module

Helpers for testing code using gavo.formal

class gavo.formal.testing.FakeFile(file_name, payload)[source]

Bases: object

a fake file upload.

Construct this with a file name and a payload, both of which can be bytes or str (which will be utf-8-encoded).

class gavo.formal.testing.FakeRequest(uri=b'', headers=None, args=None, avatar=None, currentSegments=None, cookies=None, user='', password='', isSecure=False)[source]

Bases: DummyRequest

A request for test purposes.

We furnish t.w’s DummyRequest with some extra facilities to let us be a bit lazy in having rather macro tests.

Also, stock twisted DummyRequest produces an endless loop with push producers (which is what we have), so we fix that, too.

You can pass in args as a str -> str mapping; the strings will be encoded as utf-8 so request.args is bytes -> [bytes]. For convenience, we’ll turn single values to lists.

For uploads, you can pass (single) args with FakeFile-valued arguments.

URLPath()[source]
addUpload(name, content)[source]
client: Optional[IAddress] = True
finish()[source]

Record that the request is finished and callback and L{Deferred}s waiting for notification of this.

finishCallback(arg)[source]
getHost()[source]

Get a dummy transport’s host.

@rtype: C{IPv4Address} @returns: a dummy transport’s host

getLocationValue()[source]

returns a location header if this requests redirects, and raises an AssertionError otherwise.

getResponseHeader(headerName)[source]
isSecure()[source]
method = b'GET'
notifyFinish()[source]

Return a L{Deferred} which is called back with L{None} when the request is finished. This will probably only work if you haven’t called C{finish} yet.

property path
prePathURL()[source]
processWithRoot(page)[source]

runs this request on page.

This is probably a bad idea all around, and we should just be using trial. But since sync tests are quite a bit more convenient, here this is. Of course, it only works if resource effectively renders sync (or has a renderSync method).

registerProducer(producer, isPush)[source]

Call an L{IPullProducer}’s C{resumeProducing} method in a loop until it unregisters itself.

@param prod: The producer. @type prod: L{IPullProducer}

@param s: Whether or not the producer is streaming.

session = None
setHeader(name, value)[source]

TODO: make this assert on write() if the header is content-length

setHost(host, port)[source]

Change the host and port the request thinks it’s using.

@type host: C{bytes} @param host: The value to which to change the host header.

@type ssl: C{bool} @param ssl: A flag which, if C{True}, indicates that the request is

considered secure (if C{True}, L{isSecure} will return C{True}).

setLastModified(when)[source]
setResponseCode(code)[source]

Set the HTTP status response code, but takes care that this is called before any data is written.

startedWriting = 0
unregisterProducer()[source]
write(data)[source]
class gavo.formal.testing.RenderTest(methodName='runTest')[source]

Bases: TestCase

a base class for tests of twisted web resources.

assertGETHasStrings(path, args, strings, rm=None, customTest=None)[source]
assertGETIsValid(path, args={})[source]
assertGETLacksStrings(path, args, strings, rm=None)[source]
assertGETRaises(path, args, exc, alsoCheck=None)[source]
assertPOSTHasStrings(path, args, strings, rm=None)[source]
assertResultHasStrings(method, path, args, strings, rm=None, inverse=False, customTest=None)[source]
assertStatus(path, status, args={}, rm=None)[source]
assertStringsIn(result, strings, inverse=False, customTest=None)[source]
errorHandler = None
renderer = None
static runQuery(page, method, path, args, moreHeaders=None, requestMogrifier=None, requestClass=None, produceErrorDocument=None)

runs a query on a page.

The query should look like it’s coming from localhost.

The thing returns a deferred firing a pair of the result (a string) and the request (from which you can glean headers and such).

errorHandler must be a callable accepting a failure and the request if you want to exercise your error handling, too. If you don’t pass it in, exceptions during request handling will be re-raised.

gavo.formal.testing.assertHasStrings(content, strings, inverse=False)[source]

asserts that all strings in the list strings are in content.

If inverse is True, it asserts the strings are not in content.

For generality, both content and strings will be bytified if they’re not bytes already, and only then compared.

When the assertion fails, the bytified content will be dumped to a file remote.data in the current directory.

gavo.formal.testing.bytify(s)[source]

returns s utf-8 encoded if it is a string, unmodified otherwise.

gavo.formal.testing.bytify_seq(s)[source]

returns a list from s with any elements bytified.

s must be a sequence or None. Oh, for convenience we also accept plain bytes and strings.

gavo.formal.testing.debug(arg)[source]
gavo.formal.testing.debytify(b)[source]

returns b utf-8 decoded if it is bytes, unmodified otherwise.

gavo.formal.testing.raiseException(failure)[source]
gavo.formal.testing.runQuery(page, method, path, args, moreHeaders=None, requestMogrifier=None, requestClass=None, produceErrorDocument=None)[source]

runs a query on a page.

The query should look like it’s coming from localhost.

The thing returns a deferred firing a pair of the result (a string) and the request (from which you can glean headers and such).

errorHandler must be a callable accepting a failure and the request if you want to exercise your error handling, too. If you don’t pass it in, exceptions during request handling will be re-raised.