jaaql-middleware-python 5.3.19__py3-none-any.whl → 5.3.20__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jaaql/constants.py +19 -1
- jaaql/db/db_pg_interface.py +33 -7
- jaaql/db/db_utils.py +7 -4
- jaaql/db/db_utils_no_circ.py +75 -3
- jaaql/interpreter/interpret_jaaql.py +11 -6
- jaaql/test/__init__.py +0 -0
- jaaql/test/test_timelines.py +352 -0
- {jaaql_middleware_python-5.3.19.dist-info → jaaql_middleware_python-5.3.20.dist-info}/METADATA +1 -1
- {jaaql_middleware_python-5.3.19.dist-info → jaaql_middleware_python-5.3.20.dist-info}/RECORD +12 -10
- {jaaql_middleware_python-5.3.19.dist-info → jaaql_middleware_python-5.3.20.dist-info}/WHEEL +0 -0
- {jaaql_middleware_python-5.3.19.dist-info → jaaql_middleware_python-5.3.20.dist-info}/licenses/LICENSE.txt +0 -0
- {jaaql_middleware_python-5.3.19.dist-info → jaaql_middleware_python-5.3.20.dist-info}/top_level.txt +0 -0
jaaql/constants.py
CHANGED
|
@@ -18,6 +18,7 @@ KEY__database = "database"
|
|
|
18
18
|
KEY__role = "role"
|
|
19
19
|
KEY__read_only = "read_only"
|
|
20
20
|
KEY__prevent_unused_parameters = "prevent_unused_parameters"
|
|
21
|
+
KEY__timelines = "timelines"
|
|
21
22
|
KEY__install_key = "install_key"
|
|
22
23
|
KEY__jaaql_password = "jaaql_password"
|
|
23
24
|
KEY__super_db_password = "super_db_password"
|
|
@@ -63,6 +64,17 @@ CRON_dayOfWeek = "dayOfWeek"
|
|
|
63
64
|
REGEX__dmbs_object_name = r'^[0-9a-zA-Z_]{1,63}$'
|
|
64
65
|
REGEX__dmbs_procedure_name = r'^[0-9a-zA-Z_$-.\+]{1,63}$'
|
|
65
66
|
|
|
67
|
+
# The optional request key KEY__timelines ({"<timeline>": "<ISO-8601 moment>" | null}) sets, for the request's transaction
|
|
68
|
+
# only, the moment each timeline is viewed at. A moment becomes the transaction-local setting GUC__timeline_prefix + name,
|
|
69
|
+
# which the generated timeline views read as nullif(current_setting('timeline.<name>', true), ''); the prefix must match the
|
|
70
|
+
# DBMS package. Names are lower-cased before they are checked (setting names are case-insensitive). The patterns are for
|
|
71
|
+
# re.fullmatch
|
|
72
|
+
GUC__timeline_prefix = "timeline."
|
|
73
|
+
TIMELINES__max_count = 16
|
|
74
|
+
TIMELINE__max_moment_length = 64
|
|
75
|
+
REGEX__timeline_name = r'^[a-z_][a-z0-9_]{0,62}$'
|
|
76
|
+
REGEX__timeline_moment = r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])([T ]([01][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9](\.[0-9]{1,6})?)?(Z|[+-](0[0-9]|1[0-5]):[0-5][0-9])?)?$'
|
|
77
|
+
|
|
66
78
|
SEPARATOR__comma_space = ", "
|
|
67
79
|
SEPARATOR__comma = ","
|
|
68
80
|
SEPARATOR__space = " "
|
|
@@ -132,6 +144,12 @@ ERR__document_id_not_found = "Document id not found"
|
|
|
132
144
|
ERR__unlock_code_expired = "The short unlock code has expired. Please use the long link found in your email"
|
|
133
145
|
ERR__invalid_lock = "Either security event does not exist, has already been used, has expired"
|
|
134
146
|
ERR__incorrect_lock_code = "Incorrect lock code"
|
|
147
|
+
ERR__timelines_malformed = "'timelines' must be an object mapping timeline names to moments"
|
|
148
|
+
ERR__timelines_too_many = "'timelines' names %d timelines, at most %d are allowed"
|
|
149
|
+
ERR__timeline_name_invalid = "Timeline name %s is invalid: expected a letter or underscore followed by at most 62 letters, digits or underscores"
|
|
150
|
+
ERR__timeline_name_repeated = "Timeline '%s' is named more than once in 'timelines' (timeline names are case-insensitive)"
|
|
151
|
+
ERR__timeline_moment_invalid = "Moment %s for timeline '%s' is invalid: expected null or an ISO-8601 date or date-time of at most %d characters, such as '2024-03-01', '2024-03-01T12:30:00' or '2024-03-01T12:30:00+01:00'"
|
|
152
|
+
ERR__timelines_with_autocommit = "'timelines' cannot be combined with 'autocommit': a moment holds for the request's transaction, which autocommit ends after every statement"
|
|
135
153
|
|
|
136
154
|
PG_ENV__password = "POSTGRES_PASSWORD"
|
|
137
155
|
|
|
@@ -218,5 +236,5 @@ ROLE__dba = "dba"
|
|
|
218
236
|
|
|
219
237
|
PROTOCOL__postgres = "postgresql://"
|
|
220
238
|
|
|
221
|
-
VERSION = "5.3.
|
|
239
|
+
VERSION = "5.3.20"
|
|
222
240
|
|
jaaql/db/db_pg_interface.py
CHANGED
|
@@ -39,6 +39,9 @@ ERR__must_use_canned_query = "Must use canned query as you are not an admin!"
|
|
|
39
39
|
|
|
40
40
|
QUERY__dba_query = "SELECT pg_has_role(datdba::regrole, 'MEMBER') FROM pg_database WHERE datname = %(database)s;"
|
|
41
41
|
QUERY__dba_query_external = "SELECT pg_has_role(datdba::regrole, 'MEMBER') FROM pg_database WHERE datname = current_database();"
|
|
42
|
+
# Applies an interface's session_settings in one statement however many there are, names and values bound as parameters.
|
|
43
|
+
# is_local = true: they end with the request's transaction (COMMIT or ROLLBACK), so they never reach a later checkout
|
|
44
|
+
QUERY__apply_session_settings = "SELECT set_config(s.n, s.v, true) FROM unnest(%s::text[], %s::text[]) AS s(n, v)"
|
|
42
45
|
|
|
43
46
|
try:
|
|
44
47
|
# Pipeline mode needs libpq >= 14. create_app refuses to boot when this is False so a server
|
|
@@ -49,11 +52,19 @@ except Exception:
|
|
|
49
52
|
PIPELINE_SUPPORTED = False
|
|
50
53
|
|
|
51
54
|
|
|
55
|
+
def _execute_pending_statement(cursor, statement):
|
|
56
|
+
# A pending statement is SQL text, or a (SQL, parameters) pair when it carries client-supplied values
|
|
57
|
+
if isinstance(statement, tuple):
|
|
58
|
+
cursor.execute(statement[0], statement[1])
|
|
59
|
+
else:
|
|
60
|
+
cursor.execute(statement)
|
|
61
|
+
|
|
62
|
+
|
|
52
63
|
class JaaqlPGConnection(psycopg.Connection):
|
|
53
64
|
"""
|
|
54
65
|
Connection whose per-checkout session-authorization statements (jaaql__set_session_authorization
|
|
55
|
-
/ SET ROLE) are deferred at checkout and sent
|
|
56
|
-
network round trip per checkout.
|
|
66
|
+
/ SET ROLE, then the transaction-local session settings) are deferred at checkout and sent
|
|
67
|
+
pipelined with the first real query, saving a network round trip per checkout.
|
|
57
68
|
|
|
58
69
|
Safety property: authorization must never be skippable. Any cursor obtained through the normal
|
|
59
70
|
cursor() call while statements are still pending executes them eagerly first, so raw connection
|
|
@@ -87,7 +98,7 @@ class JaaqlPGConnection(psycopg.Connection):
|
|
|
87
98
|
pending = self.jaaql_take_pending_auth()
|
|
88
99
|
with super().cursor() as flush_cursor:
|
|
89
100
|
for statement in pending:
|
|
90
|
-
flush_cursor
|
|
101
|
+
_execute_pending_statement(flush_cursor, statement)
|
|
91
102
|
finally:
|
|
92
103
|
self._jaaql_flushing = False
|
|
93
104
|
return super().cursor(*args, **kwargs)
|
|
@@ -194,11 +205,15 @@ class DBPGInterface(DBInterface):
|
|
|
194
205
|
except Exception:
|
|
195
206
|
pass # Ignore, pool has likely been wiped
|
|
196
207
|
|
|
197
|
-
def __init__(self, config, host: str, port: int, db_name: str, username: str, role: str = None, password: str = None, sub_role: str = None
|
|
208
|
+
def __init__(self, config, host: str, port: int, db_name: str, username: str, role: str = None, password: str = None, sub_role: str = None,
|
|
209
|
+
session_settings: dict = None):
|
|
198
210
|
super().__init__(config, host, username)
|
|
199
211
|
|
|
200
212
|
self.role = role
|
|
201
213
|
self.sub_role = sub_role
|
|
214
|
+
# {setting name: value}, applied transaction-locally after the authorization statements. Built server side (see
|
|
215
|
+
# pop_timeline_settings), never named by the client
|
|
216
|
+
self.session_settings = session_settings
|
|
202
217
|
if sub_role is not None:
|
|
203
218
|
if len([ch for ch in sub_role if not ch.isalnum() and ch not in ['_', '-']]) != 0:
|
|
204
219
|
raise HttpStatusException(ERR__invalid_role, HTTPStatus.UNAUTHORIZED)
|
|
@@ -255,7 +270,14 @@ class DBPGInterface(DBInterface):
|
|
|
255
270
|
# without executing (defensive: every return path should already have cleared it).
|
|
256
271
|
# Without this a later checkout could execute a previous user's authorization
|
|
257
272
|
conn.jaaql_take_pending_auth()
|
|
258
|
-
if
|
|
273
|
+
if conn.autocommit:
|
|
274
|
+
# A request that asked for autocommit hands its connection back still in autocommit and
|
|
275
|
+
# psycopg_pool does not reset it, so the next request to land on it would run statement by
|
|
276
|
+
# statement: a multi-query submit no longer atomic, a transaction-local setting gone before
|
|
277
|
+
# the query it was meant for. transform() applies autocommit again for requests that ask.
|
|
278
|
+
# The pool only hands out idle connections, so this cannot fail on an open transaction
|
|
279
|
+
conn.autocommit = False
|
|
280
|
+
if self.role is not None or self.sub_role is not None or self.session_settings:
|
|
259
281
|
# Deferred: sent pipelined with the first query by execute_query, or flushed eagerly by
|
|
260
282
|
# JaaqlPGConnection.cursor() if the connection is used rawly. Errors these statements
|
|
261
283
|
# raise are translated by _translate_session_auth_error at execution time; dead pool
|
|
@@ -266,6 +288,10 @@ class DBPGInterface(DBInterface):
|
|
|
266
288
|
pending.append("SELECT jaaql_extension.jaaql__set_session_authorization('" + self.role + "', '" + conn.jaaql_reset_key + "');")
|
|
267
289
|
if self.sub_role is not None:
|
|
268
290
|
pending.append("SET ROLE \"" + self.sub_role + "\"")
|
|
291
|
+
if self.session_settings:
|
|
292
|
+
# After the authorization statements, so the settings are applied as the user and only once the
|
|
293
|
+
# parallel verifier has accepted the request
|
|
294
|
+
pending.append((QUERY__apply_session_settings, (list(self.session_settings.keys()), list(self.session_settings.values()))))
|
|
269
295
|
conn.jaaql_set_pending_auth(pending)
|
|
270
296
|
return conn
|
|
271
297
|
|
|
@@ -379,11 +405,11 @@ class DBPGInterface(DBInterface):
|
|
|
379
405
|
if PIPELINE_SUPPORTED and _statement_is_preparable(query) and not conn.autocommit:
|
|
380
406
|
with conn.pipeline():
|
|
381
407
|
for statement in pending_auth:
|
|
382
|
-
auth_cursor
|
|
408
|
+
_execute_pending_statement(auth_cursor, statement)
|
|
383
409
|
execute_main_query()
|
|
384
410
|
else:
|
|
385
411
|
for statement in pending_auth:
|
|
386
|
-
auth_cursor
|
|
412
|
+
_execute_pending_statement(auth_cursor, statement)
|
|
387
413
|
execute_main_query()
|
|
388
414
|
except (InternalError, UndefinedFunction, InvalidParameterValue) as ex:
|
|
389
415
|
translated = self._translate_session_auth_error(ex)
|
jaaql/db/db_utils.py
CHANGED
|
@@ -40,7 +40,8 @@ def try_encode(salt: Union[str, bytes]) -> bytes:
|
|
|
40
40
|
return salt
|
|
41
41
|
|
|
42
42
|
|
|
43
|
-
def create_interface(config, address: str, port: int, database: str, username: str, password: str = None, role: str = None, sub_role: str = None
|
|
43
|
+
def create_interface(config, address: str, port: int, database: str, username: str, password: str = None, role: str = None, sub_role: str = None,
|
|
44
|
+
session_settings: dict = None):
|
|
44
45
|
interface = config[KEY_CONFIG__db][KEY_CONFIG__interface]
|
|
45
46
|
supported = {
|
|
46
47
|
INTERFACE__postgres_key: INTERFACE__postgres_class
|
|
@@ -51,7 +52,8 @@ def create_interface(config, address: str, port: int, database: str, username: s
|
|
|
51
52
|
|
|
52
53
|
# interface_class = getattr(db, supported[interface]) To implement later for accessing non postgres databases
|
|
53
54
|
interface_class = DBPGInterface
|
|
54
|
-
instance = interface_class(config, address, port, database, username, role=role, password=password, sub_role=sub_role
|
|
55
|
+
instance = interface_class(config, address, port, database, username, role=role, password=password, sub_role=sub_role,
|
|
56
|
+
session_settings=session_settings)
|
|
55
57
|
|
|
56
58
|
return instance
|
|
57
59
|
|
|
@@ -192,7 +194,8 @@ def execute_supplied_statements(db_interface, queries: Union[str, list],
|
|
|
192
194
|
return data
|
|
193
195
|
|
|
194
196
|
|
|
195
|
-
def create_interface_for_db(vault, config, user_id: str, database: str, sub_role: str = None):
|
|
197
|
+
def create_interface_for_db(vault, config, user_id: str, database: str, sub_role: str = None, session_settings: dict = None):
|
|
196
198
|
jaaql_uri = vault.get_obj(VAULT_KEY__super_db_credentials)
|
|
197
199
|
address, port, _, username, password = DBInterface.fracture_uri(jaaql_uri)
|
|
198
|
-
return create_interface(config, address, port, database, username, password=password, role=user_id, sub_role=sub_role
|
|
200
|
+
return create_interface(config, address, port, database, username, password=password, role=user_id, sub_role=sub_role,
|
|
201
|
+
session_settings=session_settings)
|
jaaql/db/db_utils_no_circ.py
CHANGED
|
@@ -1,25 +1,97 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from datetime import date
|
|
3
|
+
from http import HTTPStatus
|
|
1
4
|
from jaaql.mvc.exception_queries import QUERY__fetch_application_schemas, KG__application_schema__application, KG__application__is_live, \
|
|
2
5
|
KG__application_schema__name, KEY__is_default
|
|
3
6
|
from queue import Queue
|
|
4
7
|
from jaaql.db.db_utils import execute_supplied_statement, create_interface_for_db, ERR__schema_invalid, CONN_LOST__max_attempts
|
|
5
8
|
from jaaql.exceptions.http_status_exception import HttpStatusException, ConnectionLostError
|
|
6
|
-
from jaaql.interpreter.interpret_jaaql import InterpretJAAQL
|
|
9
|
+
from jaaql.interpreter.interpret_jaaql import InterpretJAAQL, KEY_autocommit
|
|
7
10
|
from jaaql.constants import KEY__application, KEY__database, KEY__schema, KEY__role, DB__jaaql, \
|
|
8
|
-
KEY__read_only, KEY__prevent_unused_parameters
|
|
11
|
+
KEY__read_only, KEY__prevent_unused_parameters, KEY__timelines, GUC__timeline_prefix, TIMELINES__max_count, TIMELINE__max_moment_length, \
|
|
12
|
+
REGEX__timeline_name, REGEX__timeline_moment, ERR__timelines_malformed, ERR__timelines_too_many, ERR__timeline_name_invalid, \
|
|
13
|
+
ERR__timeline_name_repeated, ERR__timeline_moment_invalid, ERR__timelines_with_autocommit
|
|
9
14
|
from jaaql.db.db_interface import DBInterface
|
|
10
15
|
from jaaql.utilities.utils_no_project_imports import objectify
|
|
11
16
|
from jaaql.mvc.generated_queries import application__select
|
|
12
17
|
|
|
18
|
+
ERROR_VALUE__max_length = 80
|
|
19
|
+
|
|
13
20
|
|
|
14
21
|
def get_jaaql_connection_to_db(vault, config, database: str, jaaql_connection: DBInterface):
|
|
15
22
|
return create_interface_for_db(vault, config, jaaql_connection.role, database)
|
|
16
23
|
|
|
17
24
|
|
|
25
|
+
def _describe_rejected_value(value) -> str:
|
|
26
|
+
# Client input quoted in an error message, cut short so a huge value cannot flood the response
|
|
27
|
+
described = repr(value)
|
|
28
|
+
return described if len(described) <= ERROR_VALUE__max_length else described[:ERROR_VALUE__max_length - 3] + "..."
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _is_timeline_moment(moment) -> bool:
|
|
32
|
+
if not isinstance(moment, str) or len(moment) > TIMELINE__max_moment_length or re.fullmatch(REGEX__timeline_moment, moment) is None:
|
|
33
|
+
return False
|
|
34
|
+
try:
|
|
35
|
+
# The pattern fixes the shape and every field's range; only the calendar can say whether the day exists (30 February).
|
|
36
|
+
# Deliberately not datetime.fromisoformat on the whole value: what it accepts differs per Python version (24:00, Z)
|
|
37
|
+
date.fromisoformat(moment[:10])
|
|
38
|
+
except ValueError:
|
|
39
|
+
return False
|
|
40
|
+
return True
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def pop_timeline_settings(inputs: dict):
|
|
44
|
+
"""
|
|
45
|
+
Pops the optional 'timelines' key, {"<timeline>": "<ISO-8601 date or date-time>" | null}, and returns the transaction-local
|
|
46
|
+
settings it asks for as {"timeline.<timeline>": "<moment>"}, or None when there are none. A null moment means the default
|
|
47
|
+
(current versions) and is dropped. The client names timelines, never settings: every name is built here from the prefix.
|
|
48
|
+
Everything is checked before any database work, so a bad request is a 400 rather than an error from the settings statement
|
|
49
|
+
that is batched with, and would be reported against, the request's first query
|
|
50
|
+
"""
|
|
51
|
+
timelines = inputs.pop(KEY__timelines, None)
|
|
52
|
+
if timelines is None:
|
|
53
|
+
return None
|
|
54
|
+
|
|
55
|
+
if not isinstance(timelines, dict):
|
|
56
|
+
raise HttpStatusException(ERR__timelines_malformed, HTTPStatus.BAD_REQUEST)
|
|
57
|
+
if len(timelines) > TIMELINES__max_count:
|
|
58
|
+
raise HttpStatusException(ERR__timelines_too_many % (len(timelines), TIMELINES__max_count), HTTPStatus.BAD_REQUEST)
|
|
59
|
+
|
|
60
|
+
settings = {}
|
|
61
|
+
named = set()
|
|
62
|
+
for timeline, moment in timelines.items():
|
|
63
|
+
name = timeline.lower() if isinstance(timeline, str) and timeline.isascii() else None
|
|
64
|
+
if name is None or re.fullmatch(REGEX__timeline_name, name) is None:
|
|
65
|
+
raise HttpStatusException(ERR__timeline_name_invalid % _describe_rejected_value(timeline), HTTPStatus.BAD_REQUEST)
|
|
66
|
+
if name in named:
|
|
67
|
+
raise HttpStatusException(ERR__timeline_name_repeated % name, HTTPStatus.BAD_REQUEST)
|
|
68
|
+
named.add(name)
|
|
69
|
+
|
|
70
|
+
if moment is None:
|
|
71
|
+
continue
|
|
72
|
+
if not _is_timeline_moment(moment):
|
|
73
|
+
raise HttpStatusException(ERR__timeline_moment_invalid % (_describe_rejected_value(moment), name, TIMELINE__max_moment_length),
|
|
74
|
+
HTTPStatus.BAD_REQUEST)
|
|
75
|
+
settings[GUC__timeline_prefix + name] = moment
|
|
76
|
+
|
|
77
|
+
if len(settings) == 0:
|
|
78
|
+
return None
|
|
79
|
+
|
|
80
|
+
# Under autocommit a transaction-local setting ends with the statement that set it, before the query runs
|
|
81
|
+
if inputs.get(KEY_autocommit):
|
|
82
|
+
raise HttpStatusException(ERR__timelines_with_autocommit, HTTPStatus.BAD_REQUEST)
|
|
83
|
+
|
|
84
|
+
return settings
|
|
85
|
+
|
|
86
|
+
|
|
18
87
|
def get_required_db(vault, config, jaaql_connection: DBInterface, inputs: dict, account_id: str, conn=None, interface: DBInterface = None, db_cache=None):
|
|
19
88
|
if not isinstance(inputs, dict):
|
|
20
89
|
raise HttpStatusException("Expected object or string input")
|
|
21
90
|
|
|
22
91
|
if conn is None:
|
|
92
|
+
# Validated first, so a bad request does no database work at all (not even the application lookup)
|
|
93
|
+
session_settings = pop_timeline_settings(inputs)
|
|
94
|
+
|
|
23
95
|
if KEY__application in inputs:
|
|
24
96
|
schemas = db_cache
|
|
25
97
|
if schemas is None:
|
|
@@ -56,7 +128,7 @@ def get_required_db(vault, config, jaaql_connection: DBInterface, inputs: dict,
|
|
|
56
128
|
|
|
57
129
|
sub_role = inputs.pop(KEY__role) if KEY__role in inputs else None
|
|
58
130
|
|
|
59
|
-
required_db = create_interface_for_db(vault, config, account_id, inputs[KEY__database], sub_role)
|
|
131
|
+
required_db = create_interface_for_db(vault, config, account_id, inputs[KEY__database], sub_role, session_settings=session_settings)
|
|
60
132
|
else:
|
|
61
133
|
if interface is None:
|
|
62
134
|
raise Exception("Must supply interface is connection is supplied!")
|
|
@@ -76,7 +76,8 @@ ASSERT_one = 1
|
|
|
76
76
|
ASSERT_one_plus = "1+"
|
|
77
77
|
ASSERT_one_plus_minimum_allowed = 2
|
|
78
78
|
ASSERT_one_plus_message = "more than 1"
|
|
79
|
-
|
|
79
|
+
ASSERT_zero_or_one = "0..1"
|
|
80
|
+
ASSERT_allowed = [ASSERT_zero, ASSERT_one, ASSERT_one_plus, ASSERT_zero_or_one]
|
|
80
81
|
|
|
81
82
|
REGEX_query_argument = r':([a-zA-Z0-9_.\-])+(?=[^\']*(?:\'[^\']*\'[^\']*)*$)' # Match all :blah not in quotes
|
|
82
83
|
REGEX_enc_query_argument = r'#([a-zA-Z0-9_.\-])+(?=[^\']*(?:\'[^\']*\'[^\']*)*$)' # Match all #blah not in quotes
|
|
@@ -516,7 +517,7 @@ ORDER BY d.column_name;
|
|
|
516
517
|
"rows": [],
|
|
517
518
|
"type_codes": []
|
|
518
519
|
}
|
|
519
|
-
if cur_assert
|
|
520
|
+
if cur_assert in (ASSERT_one, ASSERT_zero_or_one):
|
|
520
521
|
skipped_singletons.append(query_key)
|
|
521
522
|
|
|
522
523
|
elif psql is not None:
|
|
@@ -555,6 +556,9 @@ ORDER BY d.column_name;
|
|
|
555
556
|
if len(res['rows']) == 1 and not was_store:
|
|
556
557
|
for column, row in zip(res['columns'], res['rows'][0]):
|
|
557
558
|
past_parameters[query_key + "." + column] = row
|
|
559
|
+
elif cur_assert == ASSERT_zero_or_one and len(res['rows']) == 0 and not was_store and query_key not in skip_as_restricted:
|
|
560
|
+
for column in res['columns']:
|
|
561
|
+
past_parameters[query_key + "." + column] = None
|
|
558
562
|
|
|
559
563
|
if is_dict_query:
|
|
560
564
|
if was_store:
|
|
@@ -567,7 +571,7 @@ ORDER BY d.column_name;
|
|
|
567
571
|
if cur_assert != ASSERT_none and query_key not in skip_as_restricted:
|
|
568
572
|
if cur_assert == ASSERT_zero and len(res["rows"]) != ASSERT_zero:
|
|
569
573
|
raise HttpStatusException(ERR_assert_expecting % (str(ASSERT_zero), len(res["rows"])), HTTPStatus.BAD_REQUEST)
|
|
570
|
-
elif cur_assert == ASSERT_one and len(res["rows"]) != ASSERT_one:
|
|
574
|
+
elif (cur_assert == ASSERT_one and len(res["rows"]) != ASSERT_one) or (cur_assert == ASSERT_zero_or_one and len(res["rows"]) > ASSERT_one):
|
|
571
575
|
raise SingletonExpected(exc_query_key, descriptor={
|
|
572
576
|
"row_count": len(res["rows"]),
|
|
573
577
|
"columns": res["rows"],
|
|
@@ -995,10 +999,11 @@ WHERE (c.oid, a.attnum) IN (""" + values + ")")
|
|
|
995
999
|
if is_null_extended and "Relation Name" in node:
|
|
996
1000
|
null_extended.add((node.get("Schema"), node["Relation Name"]))
|
|
997
1001
|
join_type = node.get("Join Type")
|
|
998
|
-
for
|
|
1002
|
+
for child in node.get("Plans", []):
|
|
1003
|
+
relationship = child.get("Parent Relationship")
|
|
999
1004
|
walk(child, is_null_extended
|
|
1000
|
-
or (join_type == "Left" and
|
|
1001
|
-
or (join_type == "Right" and
|
|
1005
|
+
or (join_type == "Left" and relationship == "Inner")
|
|
1006
|
+
or (join_type == "Right" and relationship == "Outer")
|
|
1002
1007
|
or join_type == "Full")
|
|
1003
1008
|
|
|
1004
1009
|
walk(plan_payload[0]["Plan"], False)
|
jaaql/test/__init__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The 'timelines' request key: the viewing moment per timeline, applied as transaction-local settings timeline.<name>.
|
|
3
|
+
|
|
4
|
+
python -m unittest jaaql.test.test_timelines
|
|
5
|
+
|
|
6
|
+
The unit tests need only the package's requirements. TestTimelinesAgainstPostgres additionally drives the real request path
|
|
7
|
+
(submit -> get_required_db -> DBPGInterface -> the jaaql extension -> InterpretJAAQL) against a scratch Postgres that has the
|
|
8
|
+
jaaql extension available (e.g. a container of this repository's image); it runs only when JAAQL_TEST_POSTGRES_URI is set to
|
|
9
|
+
postgresql://<superuser>:<password>@<host>:<port>/<database> and creates, then drops, the database jaaql_test_timelines
|
|
10
|
+
"""
|
|
11
|
+
import os
|
|
12
|
+
import queue
|
|
13
|
+
import time
|
|
14
|
+
import unittest
|
|
15
|
+
from http import HTTPStatus
|
|
16
|
+
from unittest import mock
|
|
17
|
+
|
|
18
|
+
from jaaql.constants import GUC__timeline_prefix, TIMELINES__max_count, KEY__timelines
|
|
19
|
+
from jaaql.db import db_pg_interface
|
|
20
|
+
from jaaql.db.db_pg_interface import DBPGInterface, QUERY__apply_session_settings, _execute_pending_statement
|
|
21
|
+
from jaaql.db.db_utils_no_circ import pop_timeline_settings, get_required_db, submit
|
|
22
|
+
from jaaql.exceptions.http_status_exception import HttpStatusException
|
|
23
|
+
from jaaql.exceptions.jaaql_interpretable_handled_errors import SingletonExpected
|
|
24
|
+
|
|
25
|
+
CONFIG = {"DEBUG": {"output_query_exceptions": "false"}, "DATABASE": {"interface": "postgres"}, "SYSTEM": {"logging": False}}
|
|
26
|
+
|
|
27
|
+
ENVIRON__test_postgres_uri = "JAAQL_TEST_POSTGRES_URI"
|
|
28
|
+
TEST_DATABASE = "jaaql_test_timelines"
|
|
29
|
+
TEST_ROLE = "jaaql_test_timelines_user"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class TestPopTimelineSettings(unittest.TestCase):
|
|
33
|
+
|
|
34
|
+
def assertBadRequest(self, timelines, fragment, **extra):
|
|
35
|
+
inputs = {KEY__timelines: timelines, **extra}
|
|
36
|
+
with self.assertRaises(HttpStatusException) as caught:
|
|
37
|
+
pop_timeline_settings(inputs)
|
|
38
|
+
self.assertEqual(HTTPStatus.BAD_REQUEST, caught.exception.response_code)
|
|
39
|
+
self.assertIn(fragment, caught.exception.message)
|
|
40
|
+
|
|
41
|
+
def test_absent_key_gives_no_settings(self):
|
|
42
|
+
inputs = {"query": "SELECT 1"}
|
|
43
|
+
self.assertIsNone(pop_timeline_settings(inputs))
|
|
44
|
+
self.assertEqual({"query": "SELECT 1"}, inputs)
|
|
45
|
+
|
|
46
|
+
def test_key_is_popped_and_names_are_built_server_side(self):
|
|
47
|
+
inputs = {"query": "SELECT 1", KEY__timelines: {"Registratie": "2024-03-01", "geldigheid": "2024-03-01T12:30:00+01:00"}}
|
|
48
|
+
self.assertEqual({GUC__timeline_prefix + "registratie": "2024-03-01", GUC__timeline_prefix + "geldigheid": "2024-03-01T12:30:00+01:00"},
|
|
49
|
+
pop_timeline_settings(inputs))
|
|
50
|
+
self.assertNotIn(KEY__timelines, inputs)
|
|
51
|
+
self.assertEqual("timeline.", GUC__timeline_prefix)
|
|
52
|
+
|
|
53
|
+
def test_null_moments_are_dropped(self):
|
|
54
|
+
self.assertEqual({"timeline.registratie": "2024-03-01"}, pop_timeline_settings({KEY__timelines: {"registratie": "2024-03-01", "geldigheid": None}}))
|
|
55
|
+
self.assertIsNone(pop_timeline_settings({KEY__timelines: {"registratie": None}}))
|
|
56
|
+
self.assertIsNone(pop_timeline_settings({KEY__timelines: {}}))
|
|
57
|
+
self.assertIsNone(pop_timeline_settings({KEY__timelines: None}))
|
|
58
|
+
|
|
59
|
+
def test_accepted_moments(self):
|
|
60
|
+
for moment in ["2024-03-01", "2024-03-01T12:30", "2024-03-01 12:30", "2024-03-01T12:30:15", "2024-03-01T12:30:15.5",
|
|
61
|
+
"2024-03-01T12:30:15.123456", "2024-03-01T12:30:15Z", "2024-03-01T12:30:15.123Z", "2024-03-01T12:30:15-05:30",
|
|
62
|
+
"2024-02-29", "0001-01-01", "9999-12-31T23:59:59", "2024-12-31T23:59:59.999999+14:00", "2024-03-01T00:00:00-00:00"]:
|
|
63
|
+
self.assertEqual({"timeline.t": moment}, pop_timeline_settings({KEY__timelines: {"t": moment}}), moment)
|
|
64
|
+
|
|
65
|
+
def test_rejected_moments(self):
|
|
66
|
+
for moment in ["", "now", "today", "infinity", "-infinity", "2024-3-1", "01-03-2024", "2024/03/01", "20240301", "2024-13-01",
|
|
67
|
+
"2024-00-10", "2024-03-00", "2024-03-32", "2024-04-31", "2023-02-29", "2024-02-30", "0000-01-01", "2024-03-01T24:00",
|
|
68
|
+
"2024-03-01T24:00:00", "2024-03-01T12:60", "2024-03-01T12:30:60", "2024-03-01T12:30:15+24:00", "2024-03-01T12:30:15+01:60",
|
|
69
|
+
"2024-03-01T12:30:15+16:00", "2024-03-01T12:30:15-23:59",
|
|
70
|
+
"2024-03-01t12:30", "2024-03-01T12:30z", "2024-03-01T12", "2024-03-01T12:30:15+0100",
|
|
71
|
+
"2024-03-01T12:30:15+01", "2024-03-01T12:30:15.1234567", "2024-03-01\n", " 2024-03-01", "2024-03-01T12:30:15 CET",
|
|
72
|
+
"2024-03-01'); DROP TABLE x; --", "2024-03-01" + " " * 55, "٢٠٢٤-03-01", 20240301, 2024.0, True, [], {}]:
|
|
73
|
+
self.assertBadRequest({"registratie": moment}, "for timeline 'registratie' is invalid")
|
|
74
|
+
|
|
75
|
+
def test_rejected_names(self):
|
|
76
|
+
for name in ["", "1registratie", "reg-istratie", "reg istratie", "timeline.registratie", "registratie ", "régistratie", "a" * 64,
|
|
77
|
+
"registratie\n", "$registratie", "Kelvin"]:
|
|
78
|
+
self.assertBadRequest({name: "2024-03-01"}, "is invalid: expected a letter or underscore")
|
|
79
|
+
|
|
80
|
+
def test_name_length_limit(self):
|
|
81
|
+
self.assertEqual({"timeline." + "a" * 63: "2024-03-01"}, pop_timeline_settings({KEY__timelines: {"A" * 63: "2024-03-01"}}))
|
|
82
|
+
|
|
83
|
+
def test_names_repeated_after_lower_casing(self):
|
|
84
|
+
self.assertBadRequest({"Registratie": "2024-03-01", "registratie": "2024-04-01"}, "named more than once")
|
|
85
|
+
self.assertBadRequest({"Registratie": None, "registratie": "2024-04-01"}, "named more than once")
|
|
86
|
+
|
|
87
|
+
def test_malformed_and_too_many(self):
|
|
88
|
+
for timelines in ["2024-03-01", ["registratie"], 1, True]:
|
|
89
|
+
self.assertBadRequest(timelines, "must be an object")
|
|
90
|
+
self.assertBadRequest({"t%d" % n: None for n in range(TIMELINES__max_count + 1)}, "at most 16 are allowed")
|
|
91
|
+
self.assertIsNone(pop_timeline_settings({KEY__timelines: {"t%d" % n: None for n in range(TIMELINES__max_count)}}))
|
|
92
|
+
|
|
93
|
+
def test_autocommit(self):
|
|
94
|
+
self.assertBadRequest({"registratie": "2024-03-01"}, "cannot be combined with 'autocommit'", autocommit=True)
|
|
95
|
+
self.assertEqual({"timeline.registratie": "2024-03-01"}, pop_timeline_settings({KEY__timelines: {"registratie": "2024-03-01"}, "autocommit": False}))
|
|
96
|
+
# Nothing would be set, so nothing conflicts
|
|
97
|
+
self.assertIsNone(pop_timeline_settings({KEY__timelines: {"registratie": None}, "autocommit": True}))
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class TestGetRequiredDb(unittest.TestCase):
|
|
101
|
+
|
|
102
|
+
def test_bad_timelines_are_rejected_before_any_database_work(self):
|
|
103
|
+
with mock.patch("jaaql.db.db_utils_no_circ.create_interface_for_db", side_effect=AssertionError("interface created")), \
|
|
104
|
+
mock.patch("jaaql.db.db_utils_no_circ.execute_supplied_statement", side_effect=AssertionError("application looked up")):
|
|
105
|
+
with self.assertRaises(HttpStatusException) as caught:
|
|
106
|
+
get_required_db(None, CONFIG, None, {"application": "app", "query": "SELECT 1", KEY__timelines: {"registratie": "yesterday"}}, "account")
|
|
107
|
+
self.assertEqual(HTTPStatus.BAD_REQUEST, caught.exception.response_code)
|
|
108
|
+
|
|
109
|
+
def test_settings_are_threaded_like_the_role(self):
|
|
110
|
+
inputs = {"database": "db", "query": "SELECT 1", "role": "reader", KEY__timelines: {"REGISTRATIE": "2024-03-01"}}
|
|
111
|
+
with mock.patch("jaaql.db.db_utils_no_circ.create_interface_for_db", return_value="interface") as create:
|
|
112
|
+
self.assertEqual("interface", get_required_db("vault", CONFIG, None, inputs, "account"))
|
|
113
|
+
create.assert_called_once_with("vault", CONFIG, "account", "db", "reader", session_settings={"timeline.registratie": "2024-03-01"})
|
|
114
|
+
self.assertEqual({"database": "db", "query": "SELECT 1"}, inputs)
|
|
115
|
+
|
|
116
|
+
def test_without_timelines_the_interface_gets_none(self):
|
|
117
|
+
with mock.patch("jaaql.db.db_utils_no_circ.create_interface_for_db", return_value="interface") as create:
|
|
118
|
+
get_required_db("vault", CONFIG, None, {"database": "db", "query": "SELECT 1"}, "account")
|
|
119
|
+
create.assert_called_once_with("vault", CONFIG, "account", "db", None, session_settings=None)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
class FakeConnection:
|
|
123
|
+
def __init__(self, autocommit=False):
|
|
124
|
+
self._autocommit = autocommit
|
|
125
|
+
self.autocommit_writes = []
|
|
126
|
+
self.pending = "never set"
|
|
127
|
+
|
|
128
|
+
@property
|
|
129
|
+
def autocommit(self):
|
|
130
|
+
return self._autocommit
|
|
131
|
+
|
|
132
|
+
@autocommit.setter
|
|
133
|
+
def autocommit(self, value):
|
|
134
|
+
self.autocommit_writes.append(value)
|
|
135
|
+
self._autocommit = value
|
|
136
|
+
|
|
137
|
+
def jaaql_set_pending_auth(self, statements):
|
|
138
|
+
self.pending = statements
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
class FakePool:
|
|
142
|
+
def __init__(self, conn):
|
|
143
|
+
self.conn = conn
|
|
144
|
+
|
|
145
|
+
def getconn(self, timeout=None):
|
|
146
|
+
return self.conn
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
class FakeCursor:
|
|
150
|
+
def __init__(self):
|
|
151
|
+
self.executed = []
|
|
152
|
+
|
|
153
|
+
def execute(self, query, params=None):
|
|
154
|
+
self.executed.append((query, params))
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
class TestPendingStatements(unittest.TestCase):
|
|
158
|
+
USERNAME = "jaaql_test_pending_statements"
|
|
159
|
+
DATABASE = "db"
|
|
160
|
+
|
|
161
|
+
def tearDown(self):
|
|
162
|
+
DBPGInterface.HOST_POOLS.pop(self.USERNAME, None)
|
|
163
|
+
DBPGInterface.HOST_POOLS_QUEUES.pop(self.USERNAME, None)
|
|
164
|
+
|
|
165
|
+
def checkout(self, conn, **kwargs):
|
|
166
|
+
interface = DBPGInterface(CONFIG, "localhost", 5432, self.DATABASE, self.USERNAME, password=None, **kwargs)
|
|
167
|
+
DBPGInterface.HOST_POOLS[self.USERNAME][self.DATABASE] = FakePool(conn)
|
|
168
|
+
return interface._get_conn()
|
|
169
|
+
|
|
170
|
+
def test_settings_statement_follows_the_authorization_statements(self):
|
|
171
|
+
conn = self.checkout(FakeConnection(), role="account", sub_role="reader",
|
|
172
|
+
session_settings={"timeline.registratie": "2024-03-01", "timeline.geldigheid": "2024-03-01T12:00:00Z"})
|
|
173
|
+
self.assertEqual(3, len(conn.pending))
|
|
174
|
+
self.assertTrue(conn.pending[0].startswith("SELECT jaaql_extension.jaaql__set_session_authorization('account', '"))
|
|
175
|
+
self.assertEqual("SET ROLE \"reader\"", conn.pending[1])
|
|
176
|
+
self.assertEqual((QUERY__apply_session_settings, (["timeline.registratie", "timeline.geldigheid"], ["2024-03-01", "2024-03-01T12:00:00Z"])),
|
|
177
|
+
conn.pending[2])
|
|
178
|
+
self.assertEqual("SELECT set_config(s.n, s.v, true) FROM unnest(%s::text[], %s::text[]) AS s(n, v)", QUERY__apply_session_settings)
|
|
179
|
+
|
|
180
|
+
def test_without_settings_the_pending_statements_are_unchanged(self):
|
|
181
|
+
conn = self.checkout(FakeConnection(), role="account", sub_role="reader")
|
|
182
|
+
self.assertEqual(2, len(conn.pending))
|
|
183
|
+
self.assertTrue(all(isinstance(statement, str) for statement in conn.pending))
|
|
184
|
+
conn = self.checkout(FakeConnection(), role="account", session_settings={})
|
|
185
|
+
self.assertEqual(1, len(conn.pending))
|
|
186
|
+
|
|
187
|
+
def test_settings_alone_are_deferred_too(self):
|
|
188
|
+
conn = self.checkout(FakeConnection(), session_settings={"timeline.registratie": "2024-03-01"})
|
|
189
|
+
self.assertEqual([(QUERY__apply_session_settings, (["timeline.registratie"], ["2024-03-01"]))], conn.pending)
|
|
190
|
+
conn = self.checkout(FakeConnection())
|
|
191
|
+
self.assertEqual("never set", conn.pending)
|
|
192
|
+
|
|
193
|
+
def test_checkout_ends_a_leaked_autocommit(self):
|
|
194
|
+
conn = self.checkout(FakeConnection(autocommit=True), role="account")
|
|
195
|
+
self.assertEqual([False], conn.autocommit_writes)
|
|
196
|
+
conn = self.checkout(FakeConnection(autocommit=False), role="account")
|
|
197
|
+
self.assertEqual([], conn.autocommit_writes)
|
|
198
|
+
|
|
199
|
+
def test_pending_statement_forms(self):
|
|
200
|
+
cursor = FakeCursor()
|
|
201
|
+
_execute_pending_statement(cursor, "SET ROLE \"reader\"")
|
|
202
|
+
_execute_pending_statement(cursor, (QUERY__apply_session_settings, (["timeline.registratie"], ["2024-03-01"])))
|
|
203
|
+
self.assertEqual([("SET ROLE \"reader\"", None), (QUERY__apply_session_settings, (["timeline.registratie"], ["2024-03-01"]))], cursor.executed)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
class StubVault:
|
|
207
|
+
def __init__(self, uri):
|
|
208
|
+
self.uri = uri
|
|
209
|
+
|
|
210
|
+
def get_obj(self, key):
|
|
211
|
+
return self.uri
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
@unittest.skipUnless(os.environ.get(ENVIRON__test_postgres_uri), "set " + ENVIRON__test_postgres_uri + " to run against a scratch Postgres")
|
|
215
|
+
class TestTimelinesAgainstPostgres(unittest.TestCase):
|
|
216
|
+
"""
|
|
217
|
+
One usable pooled connection (pool of two, DBPGInterface.__init__ keeps one checked out), so consecutive requests share a
|
|
218
|
+
connection, which pg_backend_pid() confirms
|
|
219
|
+
"""
|
|
220
|
+
|
|
221
|
+
@classmethod
|
|
222
|
+
def setUpClass(cls):
|
|
223
|
+
import psycopg
|
|
224
|
+
uri = os.environ[ENVIRON__test_postgres_uri]
|
|
225
|
+
cls.admin_uri = uri
|
|
226
|
+
cls.test_uri = uri.rsplit("/", 1)[0] + "/" + TEST_DATABASE
|
|
227
|
+
with psycopg.connect(uri, autocommit=True) as admin:
|
|
228
|
+
admin.execute("DROP DATABASE IF EXISTS " + TEST_DATABASE + " WITH (FORCE)")
|
|
229
|
+
admin.execute("CREATE DATABASE " + TEST_DATABASE)
|
|
230
|
+
admin.execute("DO $$ BEGIN CREATE ROLE " + TEST_ROLE + "; EXCEPTION WHEN duplicate_object THEN NULL; END $$")
|
|
231
|
+
with psycopg.connect(cls.test_uri, autocommit=True) as conn:
|
|
232
|
+
try:
|
|
233
|
+
conn.execute("CREATE SCHEMA jaaql_extension")
|
|
234
|
+
conn.execute("GRANT USAGE ON SCHEMA jaaql_extension TO PUBLIC")
|
|
235
|
+
conn.execute("CREATE EXTENSION jaaql")
|
|
236
|
+
except psycopg.Error as ex:
|
|
237
|
+
raise unittest.SkipTest("the jaaql extension is not available: " + str(ex))
|
|
238
|
+
conn.execute("""
|
|
239
|
+
CREATE TABLE "person__$log" (id int NOT NULL, name text, registered_from date NOT NULL, registered_to date NOT NULL,
|
|
240
|
+
PRIMARY KEY (id, registered_to));
|
|
241
|
+
CREATE VIEW person AS SELECT id, name, registered_from, registered_to FROM "person__$log"
|
|
242
|
+
WHERE CASE WHEN (SELECT nullif(current_setting('timeline.registration', true), '')::date) IS NULL
|
|
243
|
+
THEN registered_to = '2300-01-01'::date
|
|
244
|
+
ELSE registered_from <= (SELECT nullif(current_setting('timeline.registration', true), '')::date)
|
|
245
|
+
AND (SELECT nullif(current_setting('timeline.registration', true), '')::date) < registered_to END;
|
|
246
|
+
INSERT INTO "person__$log" VALUES (1, 'old', '2020-01-01', '2022-01-01'), (1, 'new', '2022-01-01', '2300-01-01');
|
|
247
|
+
CREATE TABLE audit (note text);
|
|
248
|
+
CREATE FUNCTION "person.name"(id int) RETURNS TABLE (name text, moment text) LANGUAGE sql STABLE AS
|
|
249
|
+
$f$ SELECT P.name, current_setting('timeline.registration', true) FROM person P WHERE P.id = "person.name".id $f$;
|
|
250
|
+
GRANT SELECT ON person TO """ + TEST_ROLE + """;
|
|
251
|
+
GRANT SELECT, INSERT ON audit TO """ + TEST_ROLE + """;
|
|
252
|
+
GRANT EXECUTE ON FUNCTION "person.name"(int) TO """ + TEST_ROLE + """;
|
|
253
|
+
""")
|
|
254
|
+
cls.saved_pool_sizes = (db_pg_interface.PGCONN__min_conns, db_pg_interface.PGCONN__max_conns)
|
|
255
|
+
db_pg_interface.PGCONN__min_conns = 2
|
|
256
|
+
db_pg_interface.PGCONN__max_conns = 2
|
|
257
|
+
cls.vault = StubVault(uri)
|
|
258
|
+
|
|
259
|
+
@classmethod
|
|
260
|
+
def tearDownClass(cls):
|
|
261
|
+
import psycopg
|
|
262
|
+
db_pg_interface.PGCONN__min_conns, db_pg_interface.PGCONN__max_conns = cls.saved_pool_sizes
|
|
263
|
+
for user_pools in DBPGInterface.HOST_POOLS.values():
|
|
264
|
+
if TEST_DATABASE in user_pools:
|
|
265
|
+
user_pools.pop(TEST_DATABASE).close()
|
|
266
|
+
with psycopg.connect(cls.admin_uri, autocommit=True) as admin:
|
|
267
|
+
admin.execute("DROP DATABASE IF EXISTS " + TEST_DATABASE + " WITH (FORCE)")
|
|
268
|
+
admin.execute("DROP ROLE IF EXISTS " + TEST_ROLE)
|
|
269
|
+
|
|
270
|
+
def pool(self):
|
|
271
|
+
return [pools[TEST_DATABASE] for pools in DBPGInterface.HOST_POOLS.values() if TEST_DATABASE in pools][0]
|
|
272
|
+
|
|
273
|
+
def request(self, inputs, prepare_statements=False):
|
|
274
|
+
hook = queue.Queue()
|
|
275
|
+
hook.put((True, None, None))
|
|
276
|
+
inputs = dict({"database": TEST_DATABASE}, **inputs)
|
|
277
|
+
result = submit(self.vault, CONFIG, None, None, inputs, TEST_ROLE, hook, prepare_statements=prepare_statements)
|
|
278
|
+
deadline = time.time() + 5
|
|
279
|
+
while time.time() < deadline and self.pool().get_stats().get("pool_available", 0) < 1:
|
|
280
|
+
time.sleep(0.02)
|
|
281
|
+
return result
|
|
282
|
+
|
|
283
|
+
@staticmethod
|
|
284
|
+
def row(result):
|
|
285
|
+
return dict(zip(result["columns"], result["rows"][0]))
|
|
286
|
+
|
|
287
|
+
PROBE = "SELECT pg_backend_pid() AS pid, txid_current() AS tx, current_setting('timeline.registration', true) AS moment, " \
|
|
288
|
+
"(SELECT string_agg(name, ',') FROM person) AS names"
|
|
289
|
+
|
|
290
|
+
def test_moment_holds_for_every_query_of_the_request_and_ends_with_it(self):
|
|
291
|
+
result = self.request({"query": {"a": self.PROBE, "b": "SELECT txid_current() AS tx, (SELECT string_agg(name, ',') FROM person) AS names"},
|
|
292
|
+
KEY__timelines: {"Registration": "2021-01-01"}})
|
|
293
|
+
first, second = self.row(result["a"]), self.row(result["b"])
|
|
294
|
+
self.assertEqual(("2021-01-01", "old", "old"), (first["moment"], first["names"], second["names"]))
|
|
295
|
+
self.assertEqual(first["tx"], second["tx"])
|
|
296
|
+
|
|
297
|
+
after = self.row(self.request({"query": self.PROBE}))
|
|
298
|
+
self.assertEqual(first["pid"], after["pid"])
|
|
299
|
+
self.assertIn(after["moment"], (None, ""))
|
|
300
|
+
self.assertEqual("new", after["names"])
|
|
301
|
+
|
|
302
|
+
def test_prepared_and_procedure_requests(self):
|
|
303
|
+
for moment, expected in [("2021-01-01", "old"), (None, "new"), ("2021-06-01T10:00:00", "old"), (None, "new")]:
|
|
304
|
+
extra = {} if moment is None else {KEY__timelines: {"registration": moment}}
|
|
305
|
+
self.assertEqual(expected, self.row(self.request(dict({"query": {"q": self.PROBE}}, **extra), prepare_statements=True)["q"])["names"])
|
|
306
|
+
procedure = {"query": {"_jaaql_procedure": "SELECT * FROM \"person.name\"(\n\tid => :id )"}, "parameters": {"id": 1}}
|
|
307
|
+
self.assertEqual(expected, self.row(self.request(dict(procedure, **extra))["_jaaql_procedure"])["name"])
|
|
308
|
+
|
|
309
|
+
def test_bad_timelines_are_rejected_and_nothing_runs(self):
|
|
310
|
+
self.request({"query": "SELECT 1"})
|
|
311
|
+
requests_before = self.pool().get_stats().get("requests_num")
|
|
312
|
+
for timelines in [{"registration": "2021-02-30"}, {"regis-tration": "2021-01-01"}, "2021-01-01"]:
|
|
313
|
+
with self.assertRaises(HttpStatusException) as caught:
|
|
314
|
+
self.request({"query": "INSERT INTO audit VALUES ('ran')", KEY__timelines: timelines})
|
|
315
|
+
self.assertEqual(HTTPStatus.BAD_REQUEST, caught.exception.response_code)
|
|
316
|
+
self.assertEqual(requests_before, self.pool().get_stats().get("requests_num"))
|
|
317
|
+
self.assertEqual([], self.request({"query": "SELECT note FROM audit"})["rows"])
|
|
318
|
+
|
|
319
|
+
def test_autocommit_request_does_not_leak_into_the_next_request(self):
|
|
320
|
+
pid = self.row(self.request({"query": "SELECT pg_backend_pid() AS pid", "autocommit": True}))["pid"]
|
|
321
|
+
result = self.request({"query": {"a": self.PROBE, "b": "SELECT txid_current() AS tx"}, KEY__timelines: {"registration": "2021-01-01"}})
|
|
322
|
+
self.assertEqual(pid, self.row(result["a"])["pid"])
|
|
323
|
+
self.assertEqual(("2021-01-01", "old"), (self.row(result["a"])["moment"], self.row(result["a"])["names"]))
|
|
324
|
+
self.assertEqual(self.row(result["a"])["tx"], self.row(result["b"])["tx"])
|
|
325
|
+
|
|
326
|
+
def test_request_without_timelines_is_unchanged(self):
|
|
327
|
+
row = self.row(self.request({"query": self.PROBE}))
|
|
328
|
+
self.assertIn(row["moment"], (None, ""))
|
|
329
|
+
self.assertEqual("new", row["names"])
|
|
330
|
+
|
|
331
|
+
def optional_singleton(self, moment, assert_, singleton="SELECT id, name FROM person WHERE id = :id"):
|
|
332
|
+
return self.request({"query": {"singleton": {"query": singleton, "assert": assert_},
|
|
333
|
+
"dependent": "SELECT count(*) AS n, :singleton.id IS NULL AS absent FROM person P WHERE P.id = :singleton.id"},
|
|
334
|
+
"parameters": {"id": 1}, KEY__timelines: {"registration": moment}})
|
|
335
|
+
|
|
336
|
+
def test_optional_singleton_without_a_row_answers_instead_of_failing(self):
|
|
337
|
+
found = self.optional_singleton("2021-01-01", "0..1")
|
|
338
|
+
self.assertEqual("old", self.row(found["singleton"])["name"])
|
|
339
|
+
self.assertEqual((1, False), (self.row(found["dependent"])["n"], self.row(found["dependent"])["absent"]))
|
|
340
|
+
|
|
341
|
+
absent = self.optional_singleton("2019-01-01", "0..1")
|
|
342
|
+
self.assertEqual((["id", "name"], []), (absent["singleton"]["columns"], absent["singleton"]["rows"]))
|
|
343
|
+
self.assertEqual((0, True), (self.row(absent["dependent"])["n"], self.row(absent["dependent"])["absent"]))
|
|
344
|
+
|
|
345
|
+
for moment, assert_, singleton in [("2019-01-01", 1, "SELECT id, name FROM person WHERE id = :id"),
|
|
346
|
+
("2021-01-01", "0..1", "SELECT P.id, P.name FROM person P CROSS JOIN generate_series(1, 2) WHERE P.id = :id")]:
|
|
347
|
+
with self.assertRaises(SingletonExpected):
|
|
348
|
+
self.optional_singleton(moment, assert_, singleton)
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
if __name__ == "__main__":
|
|
352
|
+
unittest.main()
|
{jaaql_middleware_python-5.3.19.dist-info → jaaql_middleware_python-5.3.20.dist-info}/METADATA
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: jaaql-middleware-python
|
|
3
|
-
Version: 5.3.
|
|
3
|
+
Version: 5.3.20
|
|
4
4
|
Summary: The jaaql package, allowing for rapid development and deployment of RESTful HTTP applications
|
|
5
5
|
Home-page: https://github.com/JAAQL/JAAQL-middleware-python
|
|
6
6
|
Author: Software Quality Measurement and Improvement bv
|
{jaaql_middleware_python-5.3.19.dist-info → jaaql_middleware_python-5.3.20.dist-info}/RECORD
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
jaaql/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
2
|
jaaql/config_constants.py,sha256=puoR6jfbXw8oXXEfbYNn2ysGwgiGTyuDjOj7F8acsTE,470
|
|
3
|
-
jaaql/constants.py,sha256=
|
|
3
|
+
jaaql/constants.py,sha256=FzUcJyc3_voDJg6md3v_i55O2caBTAJhDRTW4RsCjuM,9058
|
|
4
4
|
jaaql/generated_constants.py,sha256=pxmpTux7rrjJH6iqLb-R8RKJ4t0CeoGdnspjH5DiWgU,11327
|
|
5
5
|
jaaql/jaaql.py,sha256=bfiEW9A8PlZIVjDJd_LOhCLBL8flc_f7w0umR6EWbU4,6614
|
|
6
6
|
jaaql/patch.py,sha256=VE4B6jA492NCIxNIewbynh92jxVulmRFtB9hNEe6q94,149
|
|
@@ -10,9 +10,9 @@ jaaql/config/config-test.ini,sha256=ap9OFqCCbYdWqmUKq0p9GD1IkM4CzeTXDV5gbQGQZoQ,
|
|
|
10
10
|
jaaql/config/config.ini,sha256=o1orXNYRFgf09MKzZBQ3v0oMzWn7N6KwvX5YV0WJtns,321
|
|
11
11
|
jaaql/db/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
12
|
jaaql/db/db_interface.py,sha256=TTtqqbYzQ5vdNjWvA_ebi1_WglDMjt8jBvJYUP_52Yo,10126
|
|
13
|
-
jaaql/db/db_pg_interface.py,sha256=
|
|
14
|
-
jaaql/db/db_utils.py,sha256=
|
|
15
|
-
jaaql/db/db_utils_no_circ.py,sha256=
|
|
13
|
+
jaaql/db/db_pg_interface.py,sha256=23QvKiyp9mJXU0SvI-2bZlGjGJkZTENMXEReljefmeI,25017
|
|
14
|
+
jaaql/db/db_utils.py,sha256=jFh4dPD_HL81kBVHhIDuFqAUqK9fSkrJn88oUiNl_WM,8835
|
|
15
|
+
jaaql/db/db_utils_no_circ.py,sha256=JfmnuaYTeGbZ4WsCiKEjiJhIifcHJ3LEdf5FMOvtvdE,9030
|
|
16
16
|
jaaql/documentation/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
17
17
|
jaaql/documentation/documentation_internal.py,sha256=_tU92TI4Sd8AXj3b7-UOKCN1h4VR7SaakIv2ATC45Bo,22783
|
|
18
18
|
jaaql/documentation/documentation_public.py,sha256=XU1Smw-1heWhCs-FDqTCTVCkAI2COHEy6fk2fhrwFN4,10932
|
|
@@ -27,7 +27,7 @@ jaaql/exceptions/http_status_exception.py,sha256=lD-eI_C9kHmThFP94ARiGvBQdTjbEM2
|
|
|
27
27
|
jaaql/exceptions/jaaql_interpretable_handled_errors.py,sha256=2dFPMrDWeFmlsSKDDY053c0spcl9S5YbQPu3On3-FI8,11817
|
|
28
28
|
jaaql/exceptions/not_yet_implement_exception.py,sha256=T7QC-I7ciWuFFp7C_jIJRMDlpshVdy_01bVuEnu-Txs,321
|
|
29
29
|
jaaql/interpreter/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
30
|
-
jaaql/interpreter/interpret_jaaql.py,sha256=
|
|
30
|
+
jaaql/interpreter/interpret_jaaql.py,sha256=QW_Oo1JUcVSvXWCYTHbBqu-6CrLr_FT4i9w0QEqlASk,50709
|
|
31
31
|
jaaql/migrations/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
32
32
|
jaaql/migrations/migrations.py,sha256=XE46AzNmfAg9qiatIL_6ctjqiiuVMHUNEcVHhmuh4hA,4195
|
|
33
33
|
jaaql/mvc/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
@@ -58,6 +58,8 @@ jaaql/services/migrations_manager_service.py,sha256=GkzX8traQcV0Q6TBp9uf8VPM8hMl
|
|
|
58
58
|
jaaql/services/patch_mms.py,sha256=chae_LNafHuOhewgJrSRe-FnxBi1kgs4kK6lyn-V59A,296
|
|
59
59
|
jaaql/services/patch_shared_var_service.py,sha256=qZw6Kl86Tvz3_YK-Y1VZUx8T28-lg80qiwOEkXmlCFw,117
|
|
60
60
|
jaaql/services/shared_var_service.py,sha256=gcaj1x5C5m3DOcV85jrkIe4Uc_C8mW9nNdqvdHb0eXs,1109
|
|
61
|
+
jaaql/test/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
62
|
+
jaaql/test/test_timelines.py,sha256=GSyhkyn59ioh7-PGRSpPHRLvJzLEUiKm9kGfMTLUlyk,20724
|
|
61
63
|
jaaql/utilities/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
62
64
|
jaaql/utilities/bootstrap_secrets.py,sha256=XUO_LsnHcysWmGdmyaggjgH9hxTaQcU_hyFgyifF2YA,1025
|
|
63
65
|
jaaql/utilities/cron.py,sha256=F5Nenyhjm_WRH43eHCP9WyYIeZUgqMv8cndyBCzPMmo,3224
|
|
@@ -66,8 +68,8 @@ jaaql/utilities/options.py,sha256=75M7-oSeY4wmHGz3-su_c36yf_Csv9HxvTG7aRlGR0A,49
|
|
|
66
68
|
jaaql/utilities/utils.py,sha256=nVYVhuHtWSjdaJaXe9eC1Usja56NO-V9SoBIlcHAK8Q,5166
|
|
67
69
|
jaaql/utilities/utils_no_project_imports.py,sha256=YwcskR15dAqKdEiuF8zGx-tzwWkclSGil_zm_qZ8ylU,4626
|
|
68
70
|
jaaql/utilities/vault.py,sha256=DudyQNtMtaRLL71IGQX0AI16Tg1id98BHTCQPyUNkmE,2243
|
|
69
|
-
jaaql_middleware_python-5.3.
|
|
70
|
-
jaaql_middleware_python-5.3.
|
|
71
|
-
jaaql_middleware_python-5.3.
|
|
72
|
-
jaaql_middleware_python-5.3.
|
|
73
|
-
jaaql_middleware_python-5.3.
|
|
71
|
+
jaaql_middleware_python-5.3.20.dist-info/licenses/LICENSE.txt,sha256=wr50eN0TnkrCSuNwyiI81FLZqmrW3q0JqRlVVQIrGA0,18134
|
|
72
|
+
jaaql_middleware_python-5.3.20.dist-info/METADATA,sha256=lZxT9JQWSWXVfxLZzUPn-A8UeBSn-BDEA1LIWQSwecE,1977
|
|
73
|
+
jaaql_middleware_python-5.3.20.dist-info/WHEEL,sha256=zaaOINJESkSfm_4HQVc5ssNzHCPXhJm0kEUakpsEHaU,91
|
|
74
|
+
jaaql_middleware_python-5.3.20.dist-info/top_level.txt,sha256=k5aKVQy77bPyfyNHITvo5m3FqdSAiiXbjT5S9D9KzgU,6
|
|
75
|
+
jaaql_middleware_python-5.3.20.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
{jaaql_middleware_python-5.3.19.dist-info → jaaql_middleware_python-5.3.20.dist-info}/top_level.txt
RENAMED
|
File without changes
|