|
1 | 1 | ## |
2 | | -# test.test_types |
| 2 | +# .test.test_types - test type representations and I/O |
3 | 3 | ## |
4 | 4 | import unittest |
| 5 | +import struct |
5 | 6 | from ..python.functools import process_tuple |
6 | 7 | from .. import types as pg_types |
7 | 8 | from ..types.io import lib as typlib |
8 | 9 | from ..types.io import builtins |
| 10 | +from ..types.io.contrib_hstore import hstore_factory |
9 | 11 | from ..types import Array |
10 | 12 |
|
| 13 | +class fake_typio(object): |
| 14 | + @staticmethod |
| 15 | + def encode(x): |
| 16 | + return x.encode('utf-8') |
| 17 | + @staticmethod |
| 18 | + def decode(x): |
| 19 | + return x.decode('utf-8') |
| 20 | +hstore_pack, hstore_unpack = hstore_factory(fake_typio) |
| 21 | + |
11 | 22 | # this must pack to that, and |
12 | 23 | # that must unpack to this |
13 | 24 | expectation_samples = { |
|
128 | 139 | b'\x00\x00\x00\x00\xff\xff\xff\xff' |
129 | 140 | ) |
130 | 141 | ], |
| 142 | + |
| 143 | + ('hstore', hstore_pack, hstore_unpack) : [ |
| 144 | + ({}, b'\x00\x00\x00\x00'), |
| 145 | + ({'b' : None}, b'\x00\x00\x00\x01\x00\x00\x00\x01b\xff\xff\xff\xff'), |
| 146 | + ({'b' : 'k'}, b'\x00\x00\x00\x01\x00\x00\x00\x01b\x00\x00\x00\x01k'), |
| 147 | + ({'foo' : 'bar'}, b'\x00\x00\x00\x01\x00\x00\x00\x03foo\x00\x00\x00\x03bar'), |
| 148 | + ({'foo' : None}, b'\x00\x00\x00\x01\x00\x00\x00\x03foo\xff\xff\xff\xff'), |
| 149 | + ], |
131 | 150 | } |
132 | 151 | expectation_samples[('box', typlib.box_pack, typlib.box_unpack)] = \ |
133 | 152 | expectation_samples[('lseg', typlib.lseg_pack, typlib.lseg_unpack)] = [ |
@@ -363,7 +382,31 @@ def testConsistency(self): |
363 | 382 | ) |
364 | 383 | ) |
365 | 384 |
|
366 | | -# Make some slices |
| 385 | + ## |
| 386 | + # Further hstore tests. |
| 387 | + def test_hstore(self): |
| 388 | + # Can't do some tests with the consistency checks |
| 389 | + # because we are not using ordered dictionaries. |
| 390 | + self.failUnlessRaises((ValueError, struct.error), hstore_unpack, b'\x00\x00\x00\x00foo') |
| 391 | + self.failUnlessRaises(ValueError, hstore_unpack, b'\x00\x00\x00\x01') |
| 392 | + self.failUnlessRaises(ValueError, hstore_unpack, b'\x00\x00\x00\x02\x00\x00\x00\x01G\x00\x00\x00\x01G') |
| 393 | + sample = [ |
| 394 | + ([('foo','bar'),('k',None),('zero','heroes')], |
| 395 | + b'\x00\x00\x00\x03\x00\x00\x00\x03foo' + \ |
| 396 | + b'\x00\x00\x00\x03bar\x00\x00\x00\x01k\xFF\xFF\xFF\xFF' + \ |
| 397 | + b'\x00\x00\x00\x04zero\x00\x00\x00\x06heroes'), |
| 398 | + ([('foo',None),('k',None),('zero',None)], |
| 399 | + b'\x00\x00\x00\x03\x00\x00\x00\x03foo' + \ |
| 400 | + b'\xff\xff\xff\xff\x00\x00\x00\x01k\xFF\xFF\xFF\xFF' + \ |
| 401 | + b'\x00\x00\x00\x04zero\xFF\xFF\xFF\xFF'), |
| 402 | + ([], b'\x00\x00\x00\x00'), |
| 403 | + ] |
| 404 | + for x in sample: |
| 405 | + src, serialized = x |
| 406 | + self.failUnlessEqual(hstore_pack(src), serialized) |
| 407 | + self.failUnlessEqual(hstore_unpack(serialized), dict(src)) |
| 408 | + |
| 409 | +# Make some slices; used by testSlicing |
367 | 410 | slice_samples = [ |
368 | 411 | slice(0, None, x+1) for x in range(10) |
369 | 412 | ] + [ |
|
0 commit comments