File: dalitest.py

package info (click to toggle)
gavodachs 2.11%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 8,972 kB
  • sloc: python: 100,078; xml: 3,014; javascript: 2,360; ansic: 918; sh: 216; makefile: 31
file content (106 lines) | stat: -rw-r--r-- 3,481 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
Various (sync) tests for DALI-type support code.
"""

#c Copyright 2008-2024, the GAVO project <gavo@ari.uni-heidelberg.de>
#c
#c This program is free software, covered by the GNU GPL.  See the
#c COPYING file in the source distribution.


from gavo.helpers import testhelpers, trialhelpers

from twisted.web import server

from gavo import svcs
from gavo.protocols import dali


def _renderErrInfo(errInfo, **kwargs):
	# we're going through DALIErrorResource rather than serveDALIError
	# because that way we're testing both in one go.
	request = trialhelpers.FakeRequest(kwargs.pop("reqPath", ""))
	res = dali.DALIErrorResource(errInfo, **kwargs)
	assert res.render(request)==server.NOT_DONE_YET
	return request


class DALIErrorTest(testhelpers.VerboseTest):
	def testErrorString(self):
		request = _renderErrInfo("Something is wrong", reqPath="/a/b/c")
		tree = testhelpers.getXMLTree(request.accumulator, debug=False)

		self.assertEqual(request.code, 500)
		self.assertEqual(tree[0].get("type"), "results")
		statusInfo = tree.xpath("//INFO[@name='QUERY_STATUS']")[0]
		self.assertEqual(statusInfo.text, "Something is wrong")
		self.assertEqual(statusInfo.get("value"), "ERROR")
		self.assertEqual(
			tree.xpath("//INFO[@name='request']")[0].get("value"),
			"a/b/c")

	def testErrorInfoMin(self):
		request = _renderErrInfo({"msg": "Something is wrong"}, reqPath="")
		tree = testhelpers.getXMLTree(request.accumulator, debug=False)
		self.assertEqual(request.code, 500)
		self.assertEqual(
			tree.xpath("//INFO[@name='QUERY_STATUS']")[0].text,
			"Something is wrong")

	def testErrorInfoCodeAndInt(self):
		request = _renderErrInfo({"msg": "Something is wrong",
			"hint": "This is just a test", "httpCode":  453}, reqPath="")
		tree = testhelpers.getXMLTree(request.accumulator, debug=False)
		self.assertEqual(request.code, 453)
		self.assertEqual(
			tree.xpath("//INFO[@name='QUERY_STATUS']")[0].text,
			"Something is wrong")
		self.assertEqual(
			tree.xpath("//INFO[@name='HINT']")[0].text,
			"This is just a test")

	def testQueryStatus(self):
		request = _renderErrInfo(
			"Something is wrong", queryStatus="CONFUSED", reqPath="")
		tree = testhelpers.getXMLTree(request.accumulator, debug=False)
		self.assertEqual(
			tree.xpath("//INFO[@name='QUERY_STATUS']/@value")[0],
			"CONFUSED")

	def testCodedException(self):
		request = _renderErrInfo(
			svcs.UnknownURI("You made it up"), reqPath="")
		tree = testhelpers.getXMLTree(request.accumulator, debug=False)
		self.assertEqual(request.code, 404)
		self.assertEqual(
			tree.xpath("//INFO[@name='QUERY_STATUS']")[0].text,
			"You made it up")

	def testOtherException(self):
		request = _renderErrInfo(
			SyntaxError("It's broken"), reqPath="")
		tree = testhelpers.getXMLTree(request.accumulator, debug=False)
		self.assertEqual(request.code, 500)
		self.assertEqual(
			tree.xpath("//INFO[@name='QUERY_STATUS']")[0].text,
			"It's broken")

	def testNoWrite(self):
		request = trialhelpers.FakeRequest("")
		request._disconnected = True
		res = dali.DALIErrorResource("Not shown")
		assert res.render(request)==server.NOT_DONE_YET
		self.assertEqual(request.accumulator, b"")

	def testBrokenError(self):
		class BrokenArrow(Exception):
			def __str__(self):
				raise KeyError("sense")
		request = _renderErrInfo(BrokenArrow(), reqPath="")
		self.assertEqual(request.accumulator,
			b"Ouch.  Can't even produce an error any more.\n")



if __name__=="__main__":
	testhelpers.main(DALIErrorTest)