rusackas commented on code in PR #45507:
URL: https://github.com/apache/superset/pull/45507#discussion_r4332135890
##########
tests/unit_tests/db_engine_specs/test_oceanbase.py:
##########
@@ -59,3 +63,177 @@ def test_get_column_spec(
)
assert_column_spec(spec, native_type, sqla_type, attrs, generic_type,
is_dttm)
+
+
[email protected]
+def oceanbase_dialect(monkeypatch: pytest.MonkeyPatch) -> type:
+ """
+ Stub a minimal ``oceanbase_py`` package replicating the real dialect's
+ bugs (``Connection.execute(raw_string)``, no not-found handling), then
+ run ``OceanBaseEngineSpec``'s ``monkeypatch_dialect()`` against it.
+
+ This exercises the actual patch logic without needing the real driver
+ installed -- it pins ``sqlalchemy<2``, incompatible with this repo's own
+ pin, so it's never part of the base dev environment (see
+ ``requirements/development.in``); CI installs it standalone, with
+ ``--no-deps``, only for the dedicated nightly testcontainers job.
+ """
+
+ class FakeIdentifierPreparer:
+ @staticmethod
+ def quote_identifier(name: str) -> str:
+ return f"`{name}`"
+
+ class FakeOceanBaseDialect:
+ default_schema_name = "test_schema"
+ identifier_preparer = FakeIdentifierPreparer()
+
+ def _get_default_schema_name(self, connection: Any) -> str:
+ return self.default_schema_name
+
+ def has_table(
+ self, connection: Any, table_name: str, schema: Optional[str] =
None
+ ) -> bool:
+ # The original, unpatched method: passes a raw string straight
+ # to Connection.execute() (rejected outright by SQLAlchemy 2.0)
+ # and never catches the "table doesn't exist" error, so it can
+ # only ever return True.
+ connection.execute(f"DESCRIBE {table_name}")
+ return True
+
+ def get_columns(
+ self, connection: Any, table_name: str, schema: Optional[str] =
None
+ ) -> list[dict[str, Any]]:
+ connection.execute(f"SHOW COLUMNS FROM {table_name}")
+ return []
+
+ datatype_module =
python_types.ModuleType("oceanbase_py.sqlalchemy.datatype")
+ datatype_module.parse_sql_type = lambda native_type: native_type # type:
ignore[attr-defined]
+
+ dialect_module = python_types.ModuleType("oceanbase_py.sqlalchemy.dialect")
+ dialect_module.OceanBaseDialect = FakeOceanBaseDialect # type:
ignore[attr-defined]
+
+ sqlalchemy_module = python_types.ModuleType("oceanbase_py.sqlalchemy")
+ sqlalchemy_module.datatype = datatype_module # type: ignore[attr-defined]
+ sqlalchemy_module.dialect = dialect_module # type: ignore[attr-defined]
+
+ package_module = python_types.ModuleType("oceanbase_py")
+ package_module.sqlalchemy = sqlalchemy_module # type: ignore[attr-defined]
+
+ for name, module in {
+ "oceanbase_py": package_module,
+ "oceanbase_py.sqlalchemy": sqlalchemy_module,
+ "oceanbase_py.sqlalchemy.datatype": datatype_module,
+ "oceanbase_py.sqlalchemy.dialect": dialect_module,
+ }.items():
+ monkeypatch.setitem(sys.modules, name, module)
+
+ from superset.db_engine_specs.oceanbase import monkeypatch_dialect
+
+ monkeypatch_dialect()
Review Comment:
Good catch, added that. The fake-driver setup is now a plain helper, and a
new test installs it, drops the module from `sys.modules`, re-imports it, and
checks the fake dialect's `has_table`/`get_columns` came out patched with no
explicit `monkeypatch_dialect()` call. Confirmed it goes red if the trailing
call is deleted.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]