bito-code-review[bot] commented on code in PR #44549:
URL: https://github.com/apache/superset/pull/44549#discussion_r4088272905


##########
tests/unit_tests/utils/test_schema.py:
##########
@@ -0,0 +1,129 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Tests for ``superset/utils/schema.py``."""
+
+from __future__ import annotations
+
+import pytest
+from marshmallow import ValidationError
+
+from superset.utils.schema import (
+    is_query_context_metadata_complete,
+    validate_query_context_metadata,
+)
+
+VALID_QUERY_CONTEXT = (
+    '{"datasource": {"id": 1, "type": "table"}, "queries": [{"metrics": 
["count"]}]}'
+)
+
+
+def test_validate_query_context_metadata_accepts_complete_payload() -> None:
+    """A query_context with both 'datasource' and 'queries' passes."""
+    validate_query_context_metadata(VALID_QUERY_CONTEXT)
+
+
+def test_validate_query_context_metadata_rejects_invalid_json() -> None:
+    """Malformed JSON is still rejected (parity with the plain 
validate_json)."""
+    with pytest.raises(ValidationError):
+        validate_query_context_metadata("{not valid json")
+
+
[email protected](
+    "payload",
+    [
+        '{"queries": [{"metrics": ["count"]}]}',  # missing datasource
+        '{"datasource": {"id": 1, "type": "table"}}',  # missing queries
+        '{"datasource": null, "queries": [{"metrics": ["count"]}]}',  # null 
datasource
+        "{}",  # neither
+    ],
+)
+def test_validate_query_context_metadata_rejects_missing_fields(
+    payload: str,
+) -> None:
+    """apache/superset#35774: QueryContextFactory.create() requires 
'datasource'
+    and 'queries' as keyword-only arguments; a saved query_context missing
+    either fails every read with a raw TypeError instead of a clear error at
+    save time. 'datasource' must be present and non-empty; 'queries' must be
+    present as a list (see the empty-queries acceptance test below)."""
+    with pytest.raises(ValidationError) as exc_info:
+        validate_query_context_metadata(payload)
+    assert "query_context" in str(exc_info.value).lower()
+
+
+def test_validate_query_context_metadata_accepts_empty_queries() -> None:
+    """An empty 'queries' list is a legitimate, complete query_context -- it
+    round-trips through QueryContextFactory.create() without error, so it
+    must not be rejected as 'missing' (see e.g. semantic view chart saves)."""
+    validate_query_context_metadata(
+        '{"datasource": {"id": 1, "type": "table"}, "queries": []}'
+    )
+
+
[email protected](
+    "payload",
+    ["false", "0", "null", "[]", '"a string"'],
+)
+def test_validate_query_context_metadata_rejects_non_object_json(
+    payload: str,
+) -> None:
+    """A syntactically valid but non-object JSON value (e.g. a JSON-encoded
+    ``false``/``0``/``null``) must not silently bypass the required-fields
+    check -- it is falsy as a raw string check would miss it, but it is never
+    a valid query_context."""
+    with pytest.raises(ValidationError):
+        validate_query_context_metadata(payload)
+
+
+def test_is_query_context_metadata_complete_accepts_dict_with_both_fields() -> 
None:
+    assert is_query_context_metadata_complete(
+        {"datasource": {"id": 1, "type": "table"}, "queries": [{}]}
+    )
+
+
+def test_is_query_context_metadata_complete_accepts_empty_queries_list() -> 
None:
+    """An empty 'queries' list is a legitimate, complete value: 'queries' just
+    needs to be present as a list, not non-empty."""
+    assert is_query_context_metadata_complete(
+        {"datasource": {"id": 1, "type": "table"}, "queries": []}
+    )
+
+
[email protected](
+    "value",
+    [
+        None,
+        False,
+        0,
+        [],
+        "a string",
+        {},
+        {"datasource": {"id": 1}},
+        {"queries": [{}]},
+        {"datasource": {}, "queries": [{}]},
+        {"datasource": {"id": 1}, "queries": None},
+        {"datasource": {"id": 1}, "queries": "not-a-list"},
+        {"datasource": {"id": 1}, "queries": []},  # datasource missing 'type'
+        {"datasource": {"type": "table"}, "queries": []},  # datasource 
missing 'id'
+        {"datasource": "table:1", "queries": []},  # datasource not a mapping
+        {
+            "datasource": {"id": 1, "type": "not-a-real-type"},
+            "queries": [],
+        },  # datasource 'type' not a DatasourceType member
+    ],
+)
+def test_is_query_context_metadata_complete_rejects_incomplete_values(value):

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Missing test type annotations</b></div>
   <div id="fix">
   
   This is the only test function in the new file without annotations — 
siblings like 
`test_validate_query_context_metadata_rejects_missing_fields(payload: str) -> 
None` are fully typed. This violates the repo's typing convention for tests 
(BITO.md rule 7819) and is internally inconsistent within the same diff. 
Annotate `value: object` and `-> None`.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #f0f1ba</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



-- 
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]

Reply via email to