jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1336072?usp=email )

Change subject: superset: Validate schema before discovering the database
......................................................................

superset: Validate schema before discovering the database

Missing site and schema arguments previously triggered database discovery
with None before validation. This could cause unnecessary requests and
report a lookup or authentication error instead of the intended TypeError.

Move the existing validation ahead of database discovery.

Change-Id: Ib7d6a236d6fa506a3ebcb4952112a295b637a0b6
---
M pywikibot/data/superset.py
M tests/superset_tests.py
2 files changed, 22 insertions(+), 4 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified




diff --git a/pywikibot/data/superset.py b/pywikibot/data/superset.py
index eb9b67e..e4b6adc 100644
--- a/pywikibot/data/superset.py
+++ b/pywikibot/data/superset.py
@@ -203,6 +203,10 @@
             elif self.site:
                 schema_name = f'{self.site.dbName()}_p'

+        # Ensure either site or schema_name is provided
+        if not (self.site or schema_name):
+            raise TypeError('Either site or schema_name must be provided')
+
         # Determine database_id
         if not database_id:
             if self.database_id:
@@ -215,10 +219,6 @@
             msg = f'database_id should be integer, but got "{database_id}"'
             raise TypeError(msg)

-        # Ensure either site or schema_name is provided
-        if not (self.site or schema_name):
-            raise TypeError('Either site or schema_name must be provided')
-
         return database_id, schema_name

     def query(self, sql: str,
diff --git a/tests/superset_tests.py b/tests/superset_tests.py
index a744b1a..69b41ea 100755
--- a/tests/superset_tests.py
+++ b/tests/superset_tests.py
@@ -12,6 +12,7 @@

 import unittest
 from contextlib import suppress
+from unittest.mock import patch

 import pywikibot
 from pywikibot.data.superset import SupersetQuery
@@ -20,6 +21,23 @@
 from tests.aspects import TestCase


+class TestSupersetArguments(TestCase):
+
+    """Test Superset argument validation without network access."""
+
+    net = False
+
+    def test_missing_schema(self) -> None:
+        """Reject a missing schema before attempting database discovery."""
+        superset = SupersetQuery()
+        with patch.object(superset, 'get_database_id_by_schema_name',
+                          return_value=1) as lookup:
+            msg = 'Either site or schema_name must be provided'
+            with self.assertRaisesRegex(TypeError, msg):
+                superset.merge_query_arguments()
+            lookup.assert_not_called()
+
+
 class TestSupersetWithoutAuth(TestCase):

     """Test Superset without auth."""

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1336072?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ib7d6a236d6fa506a3ebcb4952112a295b637a0b6
Gerrit-Change-Number: 1336072
Gerrit-PatchSet: 3
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to