velocity-python 0.0.132__py3-none-any.whl → 0.0.135__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.
Potentially problematic release.
This version of velocity-python might be problematic. Click here for more details.
- velocity/__init__.py +1 -1
- velocity/app/tests/__init__.py +1 -0
- velocity/app/tests/test_email_processing.py +112 -0
- velocity/app/tests/test_payment_profile_sorting.py +191 -0
- velocity/app/tests/test_spreadsheet_functions.py +124 -0
- velocity/aws/tests/__init__.py +1 -0
- velocity/aws/tests/test_lambda_handler_json_serialization.py +120 -0
- velocity/aws/tests/test_response.py +163 -0
- velocity/db/core/decorators.py +20 -3
- velocity/db/core/engine.py +33 -7
- velocity/db/exceptions.py +7 -0
- velocity/db/servers/base/initializer.py +2 -1
- velocity/db/servers/mysql/__init__.py +13 -4
- velocity/db/servers/postgres/__init__.py +14 -4
- velocity/db/servers/sqlite/__init__.py +13 -4
- velocity/db/servers/sqlserver/__init__.py +13 -4
- velocity/db/tests/__init__.py +1 -0
- velocity/db/tests/common_db_test.py +0 -0
- velocity/db/tests/postgres/__init__.py +1 -0
- velocity/db/tests/postgres/common.py +49 -0
- velocity/db/tests/postgres/test_column.py +29 -0
- velocity/db/tests/postgres/test_connections.py +25 -0
- velocity/db/tests/postgres/test_database.py +21 -0
- velocity/db/tests/postgres/test_engine.py +205 -0
- velocity/db/tests/postgres/test_general_usage.py +88 -0
- velocity/db/tests/postgres/test_imports.py +8 -0
- velocity/db/tests/postgres/test_result.py +19 -0
- velocity/db/tests/postgres/test_row.py +137 -0
- velocity/db/tests/postgres/test_row_comprehensive.py +707 -0
- velocity/db/tests/postgres/test_schema_locking.py +335 -0
- velocity/db/tests/postgres/test_schema_locking_unit.py +115 -0
- velocity/db/tests/postgres/test_sequence.py +34 -0
- velocity/db/tests/postgres/test_sql_comprehensive.py +471 -0
- velocity/db/tests/postgres/test_table.py +101 -0
- velocity/db/tests/postgres/test_table_comprehensive.py +644 -0
- velocity/db/tests/postgres/test_transaction.py +106 -0
- velocity/db/tests/sql/__init__.py +1 -0
- velocity/db/tests/sql/common.py +177 -0
- velocity/db/tests/sql/test_postgres_select_advanced.py +285 -0
- velocity/db/tests/sql/test_postgres_select_variances.py +517 -0
- velocity/db/tests/test_cursor_rowcount_fix.py +150 -0
- velocity/db/tests/test_db_utils.py +221 -0
- velocity/db/tests/test_postgres.py +212 -0
- velocity/db/tests/test_postgres_unchanged.py +81 -0
- velocity/db/tests/test_process_error_robustness.py +292 -0
- velocity/db/tests/test_result_caching.py +279 -0
- velocity/db/tests/test_result_sql_aware.py +117 -0
- velocity/db/tests/test_row_get_missing_column.py +72 -0
- velocity/db/tests/test_schema_locking_initializers.py +226 -0
- velocity/db/tests/test_schema_locking_simple.py +97 -0
- velocity/db/tests/test_sql_builder.py +165 -0
- velocity/db/tests/test_tablehelper.py +486 -0
- velocity/misc/tests/__init__.py +1 -0
- velocity/misc/tests/test_db.py +90 -0
- velocity/misc/tests/test_fix.py +78 -0
- velocity/misc/tests/test_format.py +64 -0
- velocity/misc/tests/test_iconv.py +203 -0
- velocity/misc/tests/test_merge.py +82 -0
- velocity/misc/tests/test_oconv.py +144 -0
- velocity/misc/tests/test_original_error.py +52 -0
- velocity/misc/tests/test_timer.py +74 -0
- {velocity_python-0.0.132.dist-info → velocity_python-0.0.135.dist-info}/METADATA +1 -1
- velocity_python-0.0.135.dist-info/RECORD +128 -0
- velocity_python-0.0.132.dist-info/RECORD +0 -76
- {velocity_python-0.0.132.dist-info → velocity_python-0.0.135.dist-info}/WHEEL +0 -0
- {velocity_python-0.0.132.dist-info → velocity_python-0.0.135.dist-info}/licenses/LICENSE +0 -0
- {velocity_python-0.0.132.dist-info → velocity_python-0.0.135.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
import sys
|
|
3
|
+
from unittest.mock import patch, MagicMock
|
|
4
|
+
|
|
5
|
+
# Mock the support module before importing the module that depends on it
|
|
6
|
+
sys.modules["support"] = MagicMock()
|
|
7
|
+
sys.modules["support.app"] = MagicMock()
|
|
8
|
+
sys.modules["support.app"].DEBUG = True
|
|
9
|
+
|
|
10
|
+
from velocity.aws.handlers.response import Response # Replace with actual module path
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class TestResponse(unittest.TestCase):
|
|
14
|
+
|
|
15
|
+
def setUp(self):
|
|
16
|
+
self.response = Response()
|
|
17
|
+
|
|
18
|
+
def test_initial_status_code(self):
|
|
19
|
+
self.assertEqual(self.response.status(), 200)
|
|
20
|
+
|
|
21
|
+
def test_set_status_code(self):
|
|
22
|
+
self.response.set_status(404)
|
|
23
|
+
self.assertEqual(self.response.status(), 404)
|
|
24
|
+
|
|
25
|
+
def test_initial_headers(self):
|
|
26
|
+
headers = self.response.headers()
|
|
27
|
+
self.assertIn("Content-Type", headers)
|
|
28
|
+
self.assertEqual(headers["Content-Type"], "application/json")
|
|
29
|
+
|
|
30
|
+
def test_set_headers(self):
|
|
31
|
+
custom_headers = {"x-custom-header": "value"}
|
|
32
|
+
self.response.set_headers(custom_headers)
|
|
33
|
+
headers = self.response.headers()
|
|
34
|
+
self.assertEqual(headers["X-Custom-Header"], "value") # Ensures capitalization
|
|
35
|
+
self.assertIn("Content-Type", headers)
|
|
36
|
+
|
|
37
|
+
def test_alert_action(self):
|
|
38
|
+
self.response.alert("Test message", "Alert Title")
|
|
39
|
+
self.assertEqual(len(self.response.actions), 1)
|
|
40
|
+
self.assertEqual(self.response.actions[0]["action"], "alert")
|
|
41
|
+
self.assertEqual(self.response.actions[0]["payload"]["title"], "Alert Title")
|
|
42
|
+
self.assertEqual(self.response.actions[0]["payload"]["message"], "Test message")
|
|
43
|
+
|
|
44
|
+
def test_toast_action_valid_variant(self):
|
|
45
|
+
self.response.toast("Toast message", "warning")
|
|
46
|
+
self.assertEqual(len(self.response.actions), 1)
|
|
47
|
+
self.assertEqual(self.response.actions[0]["action"], "toast")
|
|
48
|
+
self.assertEqual(
|
|
49
|
+
self.response.actions[0]["payload"]["options"]["variant"], "warning"
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
def test_toast_action_invalid_variant(self):
|
|
53
|
+
with self.assertRaises(ValueError) as context:
|
|
54
|
+
self.response.toast("Invalid toast", "invalid_variant")
|
|
55
|
+
self.assertIn(
|
|
56
|
+
"Notistack variant 'invalid_variant' not in", str(context.exception)
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
def test_load_object_action(self):
|
|
60
|
+
payload = {"key": "value"}
|
|
61
|
+
self.response.load_object(payload)
|
|
62
|
+
self.assertEqual(len(self.response.actions), 1)
|
|
63
|
+
self.assertEqual(self.response.actions[0]["action"], "load-object")
|
|
64
|
+
self.assertEqual(self.response.actions[0]["payload"], payload)
|
|
65
|
+
|
|
66
|
+
def test_update_store_action(self):
|
|
67
|
+
payload = {"key": "value"}
|
|
68
|
+
self.response.update_store(payload)
|
|
69
|
+
self.assertEqual(len(self.response.actions), 1)
|
|
70
|
+
self.assertEqual(self.response.actions[0]["action"], "update-store")
|
|
71
|
+
self.assertEqual(self.response.actions[0]["payload"], payload)
|
|
72
|
+
|
|
73
|
+
def test_file_download_action(self):
|
|
74
|
+
payload = {"file": "file.txt"}
|
|
75
|
+
self.response.file_download(payload)
|
|
76
|
+
self.assertEqual(len(self.response.actions), 1)
|
|
77
|
+
self.assertEqual(self.response.actions[0]["action"], "file-download")
|
|
78
|
+
self.assertEqual(self.response.actions[0]["payload"], payload)
|
|
79
|
+
|
|
80
|
+
def test_redirect_action(self):
|
|
81
|
+
self.response.redirect("https://example.com")
|
|
82
|
+
self.assertEqual(len(self.response.actions), 1)
|
|
83
|
+
self.assertEqual(self.response.actions[0]["action"], "redirect")
|
|
84
|
+
self.assertEqual(
|
|
85
|
+
self.response.actions[0]["payload"]["location"], "https://example.com"
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
def test_signout_action(self):
|
|
89
|
+
self.response.signout()
|
|
90
|
+
self.assertEqual(len(self.response.actions), 1)
|
|
91
|
+
self.assertEqual(self.response.actions[0]["action"], "signout")
|
|
92
|
+
|
|
93
|
+
def test_set_table_action(self):
|
|
94
|
+
payload = {"table": "data"}
|
|
95
|
+
self.response.set_table(payload)
|
|
96
|
+
self.assertEqual(len(self.response.actions), 1)
|
|
97
|
+
self.assertEqual(self.response.actions[0]["action"], "set-table")
|
|
98
|
+
self.assertEqual(self.response.actions[0]["payload"], payload)
|
|
99
|
+
|
|
100
|
+
def test_set_repo_action(self):
|
|
101
|
+
payload = {"repo": "data"}
|
|
102
|
+
self.response.set_repo(payload)
|
|
103
|
+
self.assertEqual(len(self.response.actions), 1)
|
|
104
|
+
self.assertEqual(self.response.actions[0]["action"], "set-repo")
|
|
105
|
+
self.assertEqual(self.response.actions[0]["payload"], payload)
|
|
106
|
+
|
|
107
|
+
def test_exception_handling_debug_on(self):
|
|
108
|
+
with patch("your_module.DEBUG", True), patch(
|
|
109
|
+
"traceback.format_exc", return_value="formatted traceback"
|
|
110
|
+
):
|
|
111
|
+
try:
|
|
112
|
+
raise ValueError("Test exception")
|
|
113
|
+
except ValueError:
|
|
114
|
+
self.response.exception()
|
|
115
|
+
|
|
116
|
+
self.assertEqual(self.response.status(), 500)
|
|
117
|
+
exception_info = self.response.body["python_exception"]
|
|
118
|
+
self.assertEqual(exception_info["value"], "Test exception")
|
|
119
|
+
self.assertEqual(exception_info["traceback"], "formatted traceback")
|
|
120
|
+
|
|
121
|
+
def test_exception_handling_debug_off(self):
|
|
122
|
+
with patch("your_module.DEBUG", False), patch(
|
|
123
|
+
"traceback.format_exc", return_value="formatted traceback"
|
|
124
|
+
):
|
|
125
|
+
try:
|
|
126
|
+
raise ValueError("Test exception")
|
|
127
|
+
except ValueError:
|
|
128
|
+
self.response.exception()
|
|
129
|
+
|
|
130
|
+
self.assertEqual(self.response.status(), 500)
|
|
131
|
+
exception_info = self.response.body["python_exception"]
|
|
132
|
+
self.assertEqual(exception_info["value"], "Test exception")
|
|
133
|
+
self.assertIsNone(exception_info["traceback"])
|
|
134
|
+
|
|
135
|
+
def test_chaining_methods(self):
|
|
136
|
+
response = (
|
|
137
|
+
self.response.set_status(201)
|
|
138
|
+
.alert("Chain Alert")
|
|
139
|
+
.toast("Chain Toast", "info")
|
|
140
|
+
.redirect("https://chained-example.com")
|
|
141
|
+
)
|
|
142
|
+
self.assertEqual(response.status(), 201)
|
|
143
|
+
self.assertEqual(len(response.actions), 3)
|
|
144
|
+
self.assertEqual(
|
|
145
|
+
response.actions[2]["payload"]["location"], "https://chained-example.com"
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
def test_render(self):
|
|
149
|
+
self.response.set_body({"key": "value"})
|
|
150
|
+
rendered_response = self.response.render()
|
|
151
|
+
self.assertEqual(rendered_response["statusCode"], 200)
|
|
152
|
+
self.assertEqual(
|
|
153
|
+
rendered_response["headers"]["Content-Type"], "application/json"
|
|
154
|
+
)
|
|
155
|
+
self.assertIn('"key": "value"', rendered_response["body"])
|
|
156
|
+
|
|
157
|
+
def test_format_header_key(self):
|
|
158
|
+
result = Response._format_header_key("x-custom-header")
|
|
159
|
+
self.assertEqual(result, "X-Custom-Header")
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
if __name__ == "__main__":
|
|
163
|
+
unittest.main()
|
velocity/db/core/decorators.py
CHANGED
|
@@ -99,7 +99,8 @@ def return_default(
|
|
|
99
99
|
|
|
100
100
|
def create_missing(func):
|
|
101
101
|
"""
|
|
102
|
-
If the function call fails with DbColumnMissingError or DbTableMissingError,
|
|
102
|
+
If the function call fails with DbColumnMissingError or DbTableMissingError,
|
|
103
|
+
tries to create them and re-run (only if schema is not locked).
|
|
103
104
|
"""
|
|
104
105
|
|
|
105
106
|
@wraps(func)
|
|
@@ -109,8 +110,16 @@ def create_missing(func):
|
|
|
109
110
|
result = func(self, *args, **kwds)
|
|
110
111
|
self.tx.release_savepoint(sp, cursor=self.cursor())
|
|
111
112
|
return result
|
|
112
|
-
except exceptions.DbColumnMissingError:
|
|
113
|
+
except exceptions.DbColumnMissingError as e:
|
|
113
114
|
self.tx.rollback_savepoint(sp, cursor=self.cursor())
|
|
115
|
+
|
|
116
|
+
# Check if schema is locked
|
|
117
|
+
if self.tx.engine.schema_locked:
|
|
118
|
+
raise exceptions.DbSchemaLockedError(
|
|
119
|
+
f"Cannot create missing column: schema is locked. Original error: {e}"
|
|
120
|
+
) from e
|
|
121
|
+
|
|
122
|
+
# Existing logic for automatic creation
|
|
114
123
|
data = {}
|
|
115
124
|
if "pk" in kwds:
|
|
116
125
|
data.update(kwds["pk"])
|
|
@@ -121,8 +130,16 @@ def create_missing(func):
|
|
|
121
130
|
data.update(arg)
|
|
122
131
|
self.alter(data)
|
|
123
132
|
return func(self, *args, **kwds)
|
|
124
|
-
except exceptions.DbTableMissingError:
|
|
133
|
+
except exceptions.DbTableMissingError as e:
|
|
125
134
|
self.tx.rollback_savepoint(sp, cursor=self.cursor())
|
|
135
|
+
|
|
136
|
+
# Check if schema is locked
|
|
137
|
+
if self.tx.engine.schema_locked:
|
|
138
|
+
raise exceptions.DbSchemaLockedError(
|
|
139
|
+
f"Cannot create missing table: schema is locked. Original error: {e}"
|
|
140
|
+
) from e
|
|
141
|
+
|
|
142
|
+
# Existing logic for automatic creation
|
|
126
143
|
data = {}
|
|
127
144
|
if "pk" in kwds:
|
|
128
145
|
data.update(kwds["pk"])
|
velocity/db/core/engine.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import inspect
|
|
2
2
|
import re
|
|
3
|
+
import os
|
|
4
|
+
from contextlib import contextmanager
|
|
3
5
|
from functools import wraps
|
|
4
6
|
from velocity.db import exceptions
|
|
5
7
|
from velocity.db.core.transaction import Transaction
|
|
@@ -17,11 +19,12 @@ class Engine:
|
|
|
17
19
|
|
|
18
20
|
MAX_RETRIES = 100
|
|
19
21
|
|
|
20
|
-
def __init__(self, driver, config, sql, connect_timeout=5):
|
|
22
|
+
def __init__(self, driver, config, sql, connect_timeout=5, schema_locked=False):
|
|
21
23
|
self.__config = config
|
|
22
24
|
self.__sql = sql
|
|
23
25
|
self.__driver = driver
|
|
24
26
|
self.__connect_timeout = connect_timeout
|
|
27
|
+
self.__schema_locked = schema_locked
|
|
25
28
|
|
|
26
29
|
def __str__(self):
|
|
27
30
|
return f"[{self.sql.server}] engine({self.config})"
|
|
@@ -205,6 +208,29 @@ class Engine:
|
|
|
205
208
|
def sql(self):
|
|
206
209
|
return self.__sql
|
|
207
210
|
|
|
211
|
+
@property
|
|
212
|
+
def schema_locked(self):
|
|
213
|
+
"""Returns True if schema modifications are locked."""
|
|
214
|
+
return self.__schema_locked
|
|
215
|
+
|
|
216
|
+
def lock_schema(self):
|
|
217
|
+
"""Lock schema to prevent automatic modifications."""
|
|
218
|
+
self.__schema_locked = True
|
|
219
|
+
|
|
220
|
+
def unlock_schema(self):
|
|
221
|
+
"""Unlock schema to allow automatic modifications."""
|
|
222
|
+
self.__schema_locked = False
|
|
223
|
+
|
|
224
|
+
@contextmanager
|
|
225
|
+
def unlocked_schema(self):
|
|
226
|
+
"""Temporarily unlock schema for automatic creation."""
|
|
227
|
+
original_state = self.__schema_locked
|
|
228
|
+
self.__schema_locked = False
|
|
229
|
+
try:
|
|
230
|
+
yield self
|
|
231
|
+
finally:
|
|
232
|
+
self.__schema_locked = original_state
|
|
233
|
+
|
|
208
234
|
@property
|
|
209
235
|
def version(self):
|
|
210
236
|
"""
|
|
@@ -353,12 +379,12 @@ class Engine:
|
|
|
353
379
|
if sql:
|
|
354
380
|
formatted_sql_info = f" sql={self._format_sql_with_params(sql, parameters)}"
|
|
355
381
|
|
|
356
|
-
logger.warning(
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
)
|
|
382
|
+
# logger.warning(
|
|
383
|
+
# "Database error caught. Attempting to transform: code=%s message=%s%s",
|
|
384
|
+
# error_code,
|
|
385
|
+
# error_message,
|
|
386
|
+
# formatted_sql_info,
|
|
387
|
+
# )
|
|
362
388
|
|
|
363
389
|
# Direct error code mapping
|
|
364
390
|
if error_code in self.sql.ApplicationErrorCodes:
|
velocity/db/exceptions.py
CHANGED
|
@@ -93,6 +93,12 @@ class DbTransactionError(DbException):
|
|
|
93
93
|
pass
|
|
94
94
|
|
|
95
95
|
|
|
96
|
+
class DbSchemaLockedError(DbApplicationError):
|
|
97
|
+
"""Raised when attempting to modify schema while schema is locked."""
|
|
98
|
+
|
|
99
|
+
pass
|
|
100
|
+
|
|
101
|
+
|
|
96
102
|
class DuplicateRowsFoundError(Exception):
|
|
97
103
|
"""Multiple rows found when expecting single result."""
|
|
98
104
|
|
|
@@ -125,5 +131,6 @@ __all__ = [
|
|
|
125
131
|
"DbDataIntegrityError",
|
|
126
132
|
"DbQueryError",
|
|
127
133
|
"DbTransactionError",
|
|
134
|
+
"DbSchemaLockedError",
|
|
128
135
|
"DuplicateRowsFoundError",
|
|
129
136
|
]
|
|
@@ -16,12 +16,13 @@ class BaseInitializer(ABC):
|
|
|
16
16
|
|
|
17
17
|
@staticmethod
|
|
18
18
|
@abstractmethod
|
|
19
|
-
def initialize(config: Optional[Dict[str, Any]] = None, **kwargs) -> engine.Engine:
|
|
19
|
+
def initialize(config: Optional[Dict[str, Any]] = None, schema_locked: bool = False, **kwargs) -> engine.Engine:
|
|
20
20
|
"""
|
|
21
21
|
Initialize a database engine with the appropriate driver and configuration.
|
|
22
22
|
|
|
23
23
|
Args:
|
|
24
24
|
config: Configuration dictionary (can be None)
|
|
25
|
+
schema_locked: Boolean to lock schema modifications (default: False)
|
|
25
26
|
**kwargs: Additional configuration parameters
|
|
26
27
|
|
|
27
28
|
Returns:
|
|
@@ -8,12 +8,13 @@ class MySQLInitializer(BaseInitializer):
|
|
|
8
8
|
"""MySQL database initializer."""
|
|
9
9
|
|
|
10
10
|
@staticmethod
|
|
11
|
-
def initialize(config=None, **kwargs):
|
|
11
|
+
def initialize(config=None, schema_locked=False, **kwargs):
|
|
12
12
|
"""
|
|
13
13
|
Initialize MySQL engine with mysql.connector driver.
|
|
14
14
|
|
|
15
15
|
Args:
|
|
16
16
|
config: Configuration dictionary
|
|
17
|
+
schema_locked: Boolean to lock schema modifications
|
|
17
18
|
**kwargs: Additional configuration parameters
|
|
18
19
|
|
|
19
20
|
Returns:
|
|
@@ -55,10 +56,18 @@ class MySQLInitializer(BaseInitializer):
|
|
|
55
56
|
required_keys = ["database", "host", "user"]
|
|
56
57
|
MySQLInitializer._validate_required_config(final_config, required_keys)
|
|
57
58
|
|
|
58
|
-
|
|
59
|
+
# Check for environment variable override for schema locking
|
|
60
|
+
if os.environ.get("VELOCITY_SCHEMA_LOCKED", "").lower() in ('true', '1', 'yes'):
|
|
61
|
+
schema_locked = True
|
|
62
|
+
|
|
63
|
+
return engine.Engine(mysql.connector, final_config, SQL, schema_locked=schema_locked)
|
|
59
64
|
|
|
60
65
|
|
|
61
66
|
# Maintain backward compatibility
|
|
62
|
-
def initialize(config=None, **kwargs):
|
|
67
|
+
def initialize(config=None, schema_locked=False, **kwargs):
|
|
63
68
|
"""Backward compatible initialization function."""
|
|
64
|
-
|
|
69
|
+
# Check for environment variable override for schema locking
|
|
70
|
+
if os.environ.get("VELOCITY_SCHEMA_LOCKED", "").lower() in ('true', '1', 'yes'):
|
|
71
|
+
schema_locked = True
|
|
72
|
+
|
|
73
|
+
return MySQLInitializer.initialize(config, schema_locked, **kwargs)
|
|
@@ -9,12 +9,13 @@ class PostgreSQLInitializer(BaseInitializer):
|
|
|
9
9
|
"""PostgreSQL database initializer."""
|
|
10
10
|
|
|
11
11
|
@staticmethod
|
|
12
|
-
def initialize(config=None, **kwargs):
|
|
12
|
+
def initialize(config=None, schema_locked=False, **kwargs):
|
|
13
13
|
"""
|
|
14
14
|
Initialize PostgreSQL engine with psycopg2 driver.
|
|
15
15
|
|
|
16
16
|
Args:
|
|
17
17
|
config: Configuration dictionary
|
|
18
|
+
schema_locked: Boolean to lock schema modifications
|
|
18
19
|
**kwargs: Additional configuration parameters
|
|
19
20
|
|
|
20
21
|
Returns:
|
|
@@ -39,11 +40,15 @@ class PostgreSQLInitializer(BaseInitializer):
|
|
|
39
40
|
required_keys = ["database", "host", "user", "password"]
|
|
40
41
|
PostgreSQLInitializer._validate_required_config(final_config, required_keys)
|
|
41
42
|
|
|
42
|
-
|
|
43
|
+
# Check for environment variable override for schema locking
|
|
44
|
+
if os.environ.get("VELOCITY_SCHEMA_LOCKED", "").lower() in ('true', '1', 'yes'):
|
|
45
|
+
schema_locked = True
|
|
46
|
+
|
|
47
|
+
return engine.Engine(psycopg2, final_config, SQL, schema_locked=schema_locked)
|
|
43
48
|
|
|
44
49
|
|
|
45
50
|
# Maintain backward compatibility
|
|
46
|
-
def initialize(config=None, **kwargs):
|
|
51
|
+
def initialize(config=None, schema_locked=False, **kwargs):
|
|
47
52
|
"""Backward compatible initialization function - matches original behavior exactly."""
|
|
48
53
|
konfig = {
|
|
49
54
|
"database": os.environ["DBDatabase"],
|
|
@@ -54,4 +59,9 @@ def initialize(config=None, **kwargs):
|
|
|
54
59
|
}
|
|
55
60
|
konfig.update(config or {})
|
|
56
61
|
konfig.update(kwargs)
|
|
57
|
-
|
|
62
|
+
|
|
63
|
+
# Check for environment variable override for schema locking
|
|
64
|
+
if os.environ.get("VELOCITY_SCHEMA_LOCKED", "").lower() in ('true', '1', 'yes'):
|
|
65
|
+
schema_locked = True
|
|
66
|
+
|
|
67
|
+
return engine.Engine(psycopg2, konfig, SQL, schema_locked=schema_locked)
|
|
@@ -9,12 +9,13 @@ class SQLiteInitializer(BaseInitializer):
|
|
|
9
9
|
"""SQLite database initializer."""
|
|
10
10
|
|
|
11
11
|
@staticmethod
|
|
12
|
-
def initialize(config=None, **kwargs):
|
|
12
|
+
def initialize(config=None, schema_locked=False, **kwargs):
|
|
13
13
|
"""
|
|
14
14
|
Initialize SQLite engine with sqlite3 driver.
|
|
15
15
|
|
|
16
16
|
Args:
|
|
17
17
|
config: Configuration dictionary
|
|
18
|
+
schema_locked: Boolean to lock schema modifications
|
|
18
19
|
**kwargs: Additional configuration parameters
|
|
19
20
|
|
|
20
21
|
Returns:
|
|
@@ -43,10 +44,18 @@ class SQLiteInitializer(BaseInitializer):
|
|
|
43
44
|
required_keys = ["database"]
|
|
44
45
|
SQLiteInitializer._validate_required_config(final_config, required_keys)
|
|
45
46
|
|
|
46
|
-
|
|
47
|
+
# Check for environment variable override for schema locking
|
|
48
|
+
if os.environ.get("VELOCITY_SCHEMA_LOCKED", "").lower() in ('true', '1', 'yes'):
|
|
49
|
+
schema_locked = True
|
|
50
|
+
|
|
51
|
+
return engine.Engine(sqlite3, final_config, SQL, schema_locked=schema_locked)
|
|
47
52
|
|
|
48
53
|
|
|
49
54
|
# Maintain backward compatibility
|
|
50
|
-
def initialize(config=None, **kwargs):
|
|
55
|
+
def initialize(config=None, schema_locked=False, **kwargs):
|
|
51
56
|
"""Backward compatible initialization function."""
|
|
52
|
-
|
|
57
|
+
# Check for environment variable override for schema locking
|
|
58
|
+
if os.environ.get("VELOCITY_SCHEMA_LOCKED", "").lower() in ('true', '1', 'yes'):
|
|
59
|
+
schema_locked = True
|
|
60
|
+
|
|
61
|
+
return SQLiteInitializer.initialize(config, schema_locked, **kwargs)
|
|
@@ -8,12 +8,13 @@ class SQLServerInitializer(BaseInitializer):
|
|
|
8
8
|
"""SQL Server database initializer."""
|
|
9
9
|
|
|
10
10
|
@staticmethod
|
|
11
|
-
def initialize(config=None, **kwargs):
|
|
11
|
+
def initialize(config=None, schema_locked=False, **kwargs):
|
|
12
12
|
"""
|
|
13
13
|
Initialize SQL Server engine with pytds driver.
|
|
14
14
|
|
|
15
15
|
Args:
|
|
16
16
|
config: Configuration dictionary
|
|
17
|
+
schema_locked: Boolean to lock schema modifications
|
|
17
18
|
**kwargs: Additional configuration parameters
|
|
18
19
|
|
|
19
20
|
Returns:
|
|
@@ -55,10 +56,18 @@ class SQLServerInitializer(BaseInitializer):
|
|
|
55
56
|
required_keys = ["database", "server", "user"]
|
|
56
57
|
SQLServerInitializer._validate_required_config(final_config, required_keys)
|
|
57
58
|
|
|
58
|
-
|
|
59
|
+
# Check for environment variable override for schema locking
|
|
60
|
+
if os.environ.get("VELOCITY_SCHEMA_LOCKED", "").lower() in ('true', '1', 'yes'):
|
|
61
|
+
schema_locked = True
|
|
62
|
+
|
|
63
|
+
return engine.Engine(pytds, final_config, SQL, schema_locked=schema_locked)
|
|
59
64
|
|
|
60
65
|
|
|
61
66
|
# Maintain backward compatibility
|
|
62
|
-
def initialize(config=None, **kwargs):
|
|
67
|
+
def initialize(config=None, schema_locked=False, **kwargs):
|
|
63
68
|
"""Backward compatible initialization function."""
|
|
64
|
-
|
|
69
|
+
# Check for environment variable override for schema locking
|
|
70
|
+
if os.environ.get("VELOCITY_SCHEMA_LOCKED", "").lower() in ('true', '1', 'yes'):
|
|
71
|
+
schema_locked = True
|
|
72
|
+
|
|
73
|
+
return SQLServerInitializer.initialize(config, schema_locked, **kwargs)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Database module tests
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# PostgreSQL tests
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
from velocity.db.servers import postgres
|
|
3
|
+
import env
|
|
4
|
+
env.set()
|
|
5
|
+
|
|
6
|
+
test_db = "test_db_postgres"
|
|
7
|
+
engine = postgres.initialize(database=test_db)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class CommonPostgresTest(unittest.TestCase):
|
|
11
|
+
"""
|
|
12
|
+
Base test class for PostgreSQL tests following the common pattern.
|
|
13
|
+
All PostgreSQL tests should inherit from this class.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
@classmethod
|
|
17
|
+
def setUpClass(cls):
|
|
18
|
+
"""Set up the test database and create any common tables."""
|
|
19
|
+
@engine.transaction
|
|
20
|
+
def setup(tx):
|
|
21
|
+
tx.switch_to_database("postgres")
|
|
22
|
+
tx.execute(f"drop database if exists {test_db}", single=True)
|
|
23
|
+
|
|
24
|
+
# Create the test database
|
|
25
|
+
db = tx.database(test_db)
|
|
26
|
+
if not db.exists():
|
|
27
|
+
db.create()
|
|
28
|
+
db.switch()
|
|
29
|
+
|
|
30
|
+
# Call subclass-specific table creation with commit
|
|
31
|
+
if hasattr(cls, 'create_test_tables'):
|
|
32
|
+
cls.create_test_tables(tx)
|
|
33
|
+
|
|
34
|
+
setup()
|
|
35
|
+
|
|
36
|
+
@classmethod
|
|
37
|
+
def tearDownClass(cls):
|
|
38
|
+
"""Clean up the test database."""
|
|
39
|
+
@engine.transaction
|
|
40
|
+
def cleanup(tx):
|
|
41
|
+
tx.switch_to_database("postgres")
|
|
42
|
+
tx.execute(f"drop database if exists {test_db}", single=True)
|
|
43
|
+
|
|
44
|
+
cleanup()
|
|
45
|
+
|
|
46
|
+
@classmethod
|
|
47
|
+
def create_test_tables(cls, tx):
|
|
48
|
+
"""Override this method in subclasses to create test-specific tables."""
|
|
49
|
+
pass
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
from velocity.db.core.column import Column
|
|
3
|
+
from velocity.db.exceptions import DbColumnMissingError
|
|
4
|
+
from .common import CommonPostgresTest, engine, test_db
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@engine.transaction
|
|
8
|
+
@engine.transaction
|
|
9
|
+
class TestColumn(CommonPostgresTest):
|
|
10
|
+
|
|
11
|
+
@classmethod
|
|
12
|
+
def create_test_tables(cls, tx):
|
|
13
|
+
"""Create test tables for column tests."""
|
|
14
|
+
tx.table("mock_table").create(
|
|
15
|
+
columns={
|
|
16
|
+
"column1": int,
|
|
17
|
+
"column2": str,
|
|
18
|
+
"column3": str,
|
|
19
|
+
}
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
def test_init(self, tx):
|
|
23
|
+
column = tx.table("mock_table").column("column1")
|
|
24
|
+
self.assertIsInstance(column, Column)
|
|
25
|
+
self.assertEqual(column.name, "column1")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
if __name__ == "__main__":
|
|
29
|
+
unittest.main()
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
from .common import CommonPostgresTest, engine, test_db
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
@engine.transaction
|
|
6
|
+
@engine.transaction
|
|
7
|
+
class TestConnections(CommonPostgresTest):
|
|
8
|
+
|
|
9
|
+
@classmethod
|
|
10
|
+
def create_test_tables(cls, tx):
|
|
11
|
+
"""Create test tables for connection tests."""
|
|
12
|
+
tx.table("mock_table").create(
|
|
13
|
+
columns={
|
|
14
|
+
"column1": int,
|
|
15
|
+
"column2": str,
|
|
16
|
+
"column3": str,
|
|
17
|
+
}
|
|
18
|
+
)
|
|
19
|
+
def test_init(self, tx):
|
|
20
|
+
# Test the initialization of the Database object
|
|
21
|
+
assert tx.table("mock_table").exists()
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
if __name__ == "__main__":
|
|
25
|
+
unittest.main()
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
from velocity.db.core.database import Database
|
|
3
|
+
from .common import CommonPostgresTest, engine, test_db
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@engine.transaction
|
|
7
|
+
@engine.transaction
|
|
8
|
+
class TestDatabase(CommonPostgresTest):
|
|
9
|
+
|
|
10
|
+
@classmethod
|
|
11
|
+
def create_test_tables(cls, tx):
|
|
12
|
+
"""No special tables needed for database tests."""
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
def test_init(self, tx):
|
|
16
|
+
# Test the initialization of the Database object
|
|
17
|
+
tx.database
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
if __name__ == "__main__":
|
|
21
|
+
unittest.main()
|