File: test_bytea_codec.py

package info (click to toggle)
py-postgresql 1.2.1%2Bgit20180803.ef7b9a9-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,620 kB
  • sloc: python: 18,317; ansic: 2,024; sql: 282; sh: 26; makefile: 22
file content (45 lines) | stat: -rw-r--r-- 1,366 bytes parent folder | download | duplicates (3)
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
##
# .test.test_bytea_codec
##
import unittest
import struct
from ..encodings import bytea

byte = struct.Struct('B')

class test_bytea_codec(unittest.TestCase):
	def testDecoding(self):
		for x in range(255):
			c = byte.pack(x)
			b = c.decode('bytea')
			# normalize into octal escapes
			if c == b'\\' and b == "\\\\":
				b = "\\" + oct(b'\\'[0])[2:]
			elif not b.startswith("\\"):
				b = "\\" + oct(ord(b))[2:]
			if int(b[1:], 8) != x:
				self.fail(
					"bytea encoding failed at %d; encoded %r to %r" %(x, c, b,)
				)

	def testEncoding(self):
		self.assertEqual('bytea'.encode('bytea'), b'bytea')
		self.assertEqual('\\\\'.encode('bytea'), b'\\')
		self.assertRaises(ValueError, '\\'.encode, 'bytea')
		self.assertRaises(ValueError, 'foo\\'.encode, 'bytea')
		self.assertRaises(ValueError, r'foo\0'.encode, 'bytea')
		self.assertRaises(ValueError, r'foo\00'.encode, 'bytea')
		self.assertRaises(ValueError, r'\f'.encode, 'bytea')
		self.assertRaises(ValueError, r'\800'.encode, 'bytea')
		self.assertRaises(ValueError, r'\7f0'.encode, 'bytea')
		for x in range(255):
			seq = ('\\' + oct(x)[2:].lstrip('0').rjust(3, '0'))
			dx = ord(seq.encode('bytea'))
			if dx != x:
				self.fail(
					"generated sequence failed to map back; current is %d, " \
					"rendered %r, transformed to %d" %(x, seq, dx)
				)

if __name__ == '__main__':
	unittest.main()