Skip to content

Adds JSONB operator tests for the Python integration suite #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions tests/python/tests/test_jsonb_ops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import json
import os
import psycopg
import random

conn_params = {
"user": os.environ.get("CS_DATABASE__USERNAME"),
"password": os.environ.get("CS_DATABASE__PASSWORD"),
"dbname": os.environ.get("CS_DATABASE__NAME"),
"host": os.environ.get("CS_DATABASE__HOST"),
"port": 6432,
}

connection_str = psycopg.conninfo.make_conninfo(**conn_params)

print("Connection to Proxy with {}".format(connection_str))


def test_jsonb_contained_by():
val = {"key": "value"}
column = "encrypted_jsonb"
select_fragment = "%s <@ encrypted_jsonb"
tests = [
(val, True),
({"key": "different value"}, False)
]

for (param, expected) in tests:
param = json.dumps(param)

execute(json.dumps(val), column,
select_fragment=select_fragment,
select_params=[param],
expected=expected)

execute(json.dumps(val).encode(), column,
select_fragment=select_fragment,
select_params=[param.encode()],
expected=expected,
binary=True)

execute(json.dumps(val).encode(), column,
select_fragment=select_fragment,
select_params=[param.encode()],
expected=expected,
binary=True, prepare=True)

execute(json.dumps(val), column,
select_fragment=select_fragment,
select_params=[param],
expected=expected,
binary=False, prepare=True)


def make_id():
return random.randrange(1, 1000000000)


def execute(val, column, binary=None, prepare=None, expected=None,
select_fragment=None, select_params=[]):
with psycopg.connect(connection_str, autocommit=True) as conn:

with conn.cursor() as cursor:

with conn.transaction():
id = make_id()

print("Testing {} Binary: {} Prepare: {}".format(
column, binary, prepare))

sql = "INSERT INTO encrypted (id, {}) VALUES (%s, %s)".format(
column)

cursor.execute(sql, [id, val], binary=binary, prepare=prepare)

sql = "SELECT id, {} FROM encrypted WHERE id = %s".format(
select_fragment)
cursor.execute(
sql, (select_params + [id]),
binary=binary, prepare=prepare)

row = cursor.fetchone()

(result_id, result) = row
expected_result = expected if expected is not None else val

assert result_id == id
assert result == expected_result