|
| 1 | +# -*- encoding: utf-8 -*- |
| 2 | +# $Id: test_integrity.py,v 1.5 2008/04/01 03:33:43 jwp Exp $ |
| 3 | +## |
| 4 | +# copyright 2006, pg/python project. |
| 5 | +# http://python.projects.postgresql.org |
| 6 | +## |
| 7 | +import os |
| 8 | +import unittest |
| 9 | +import random |
| 10 | +import itertools |
| 11 | + |
| 12 | +iot = '_dst' |
| 13 | +if __name__ == '__main__': |
| 14 | + execute("CREATE TEMP TABLE _dst (i bigint)") |
| 15 | + copyin = query("COPY _dst FROM STDIN") |
| 16 | + loadin = query("INSERT INTO _dst VALUES ($1)") |
| 17 | + |
| 18 | +getq = "SELECT i FROM generate_series(0, %d) AS g(i)" |
| 19 | +copy = "COPY (%s) TO STDOUT" |
| 20 | + |
| 21 | +def random_read(curs, remaining_rows): |
| 22 | + """ |
| 23 | + Read from one of the three methods using a random amount if sized. |
| 24 | + - 50% chance of curs.read(random()) |
| 25 | + - 40% chance of next() |
| 26 | + - 10% chance of read() # no count |
| 27 | + """ |
| 28 | + if random.random() > 0.5: |
| 29 | + rrows = random.randrange(0, remaining_rows) |
| 30 | + return curs.read(rrows), rrows |
| 31 | + elif random.random() < 0.1: |
| 32 | + return curs.read(), -1 |
| 33 | + else: |
| 34 | + try: |
| 35 | + return [curs.next()], 1 |
| 36 | + except StopIteration: |
| 37 | + return [], 1 |
| 38 | + |
| 39 | +def random_select_get(limit): |
| 40 | + return query(getq %(limit - 1,)) |
| 41 | + |
| 42 | +def random_copy_get(limit): |
| 43 | + return query(copy %(getq %(limit - 1,),)) |
| 44 | + |
| 45 | +class test_integrity(unittest.TestCase): |
| 46 | + """ |
| 47 | + test the integrity of the get and put interfaces on queries |
| 48 | + and result handles. |
| 49 | + """ |
| 50 | + def test_select(self): |
| 51 | + total = 0 |
| 52 | + while total < 10000: |
| 53 | + limit = random.randrange(500000) |
| 54 | + read = 0 |
| 55 | + total += limit |
| 56 | + p = random_select_get(limit)() |
| 57 | + last = ([(-1,)], 1) |
| 58 | + completed = [last[0]] |
| 59 | + while True: |
| 60 | + next = random_read(p, (limit - read) or 10) |
| 61 | + thisread = len(next[0]) |
| 62 | + read += thisread |
| 63 | + completed.append(next[0]) |
| 64 | + if thisread: |
| 65 | + self.failUnlessEqual( |
| 66 | + last[0][-1][0], next[0][0][0] - 1, |
| 67 | + "first row(-1) of next failed to match the last row of the previous" |
| 68 | + ) |
| 69 | + last = next |
| 70 | + elif next[1] != 0: |
| 71 | + # done |
| 72 | + break |
| 73 | + self.failUnlessEqual(read, limit) |
| 74 | + self.failUnlessEqual(list(range(-1, limit)), [ |
| 75 | + x[0] for x in itertools.chain(*completed) |
| 76 | + ]) |
| 77 | + |
| 78 | + def test_insert(self): |
| 79 | + pass |
| 80 | + |
| 81 | + if 'pg' in dir(__builtins__) and pg.version_info >= (8,2,0): |
| 82 | + def test_copy_out(self): |
| 83 | + total = 0 |
| 84 | + while total < 10000000: |
| 85 | + limit = random.randrange(500000) |
| 86 | + read = 0 |
| 87 | + total += limit |
| 88 | + p = random_copy_get(limit)() |
| 89 | + last = ([-1], 1) |
| 90 | + completed = [last[0]] |
| 91 | + while True: |
| 92 | + next = random_read(p, (limit - read) or 10) |
| 93 | + next = ([int(x) for x in next[0]], next[1]) |
| 94 | + thisread = len(next[0]) |
| 95 | + read += thisread |
| 96 | + completed.append(next[0]) |
| 97 | + if thisread: |
| 98 | + self.failUnlessEqual( |
| 99 | + last[0][-1], next[0][0] - 1, |
| 100 | + "first row(-1) of next failed to match the last row of the previous" |
| 101 | + ) |
| 102 | + last = next |
| 103 | + elif next[1] != 0: |
| 104 | + # done |
| 105 | + break |
| 106 | + self.failUnlessEqual(read, limit) |
| 107 | + self.failUnlessEqual( |
| 108 | + list(range(-1, limit)), |
| 109 | + list(itertools.chain(*completed)) |
| 110 | + ) |
| 111 | + |
| 112 | + def test_copy_in(self): |
| 113 | + pass |
| 114 | + |
| 115 | +if __name__ == '__main__': |
| 116 | + from types import ModuleType |
| 117 | + this = ModuleType("this") |
| 118 | + this.__dict__.update(globals()) |
| 119 | + unittest.main(this) |
0 commit comments