|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import unittest |
| 4 | +import random |
| 5 | +import json |
| 6 | + |
| 7 | +from testgres import get_new_node |
| 8 | + |
| 9 | + |
| 10 | +insert_cmd = ''' |
| 11 | + INSERT INTO comp.t |
| 12 | + SELECT jsonb_object(array_agg(array[repeat(letter, count), count::text])) |
| 13 | + FROM ( |
| 14 | + SELECT chr(i) AS letter, b AS count |
| 15 | + FROM generate_series(ascii('a'), ascii('z')) i |
| 16 | + FULL OUTER JOIN |
| 17 | + (SELECT b FROM generate_series(10, 20) b) t2 |
| 18 | + ON 1=1 |
| 19 | + ) t3; |
| 20 | +''' |
| 21 | + |
| 22 | +def generate_dict(): |
| 23 | + population = 'qwertyuiopsadfghjklzxcvbnm1234567890' |
| 24 | + res = {} |
| 25 | + |
| 26 | + for i in range(300): |
| 27 | + keylen = random.randint(1, len(population)) |
| 28 | + key = ''.join(random.sample(population, keylen)) |
| 29 | + res[key] = keylen |
| 30 | + |
| 31 | + return res |
| 32 | + |
| 33 | + |
| 34 | +class Tests(unittest.TestCase): |
| 35 | + def test_correctness(self): |
| 36 | + with get_new_node('node1') as node: |
| 37 | + node.init() |
| 38 | + node.append_conf("postgresql.conf", "shared_preload_libraries='jsonbc'\n") |
| 39 | + node.start() |
| 40 | + |
| 41 | + node.psql('postgres', 'create extension jsonbc') |
| 42 | + node.psql('postgres', 'create compression method cm1 handler jsonbc_compression_handler') |
| 43 | + node.psql('postgres', 'create table t1(pk serial, a jsonb compressed cm1);') |
| 44 | + |
| 45 | + data = [] |
| 46 | + for i in range(100): |
| 47 | + d = generate_dict() |
| 48 | + data.append(d) |
| 49 | + node.psql('postgres', "begin; insert into t1 (a) values ('%s'); commit;" % json.dumps(d)) |
| 50 | + |
| 51 | + with node.connect('postgres') as con: |
| 52 | + res = con.execute('select pk, a from t1 order by pk') |
| 53 | + for pk, val in res: |
| 54 | + self.assertEqual(val, data[pk - 1]) |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == "__main__": |
| 58 | + unittest.main() |
0 commit comments