|
| 1 | +--- |
| 2 | +title: Testing semantic providers |
| 3 | +--- |
| 4 | + |
| 5 | +<!-- |
| 6 | +Licensed to the Apache Software Foundation (ASF) under one |
| 7 | +or more contributor license agreements. See the NOTICE file |
| 8 | +distributed with this work for additional information |
| 9 | +regarding copyright ownership. The ASF licenses this file |
| 10 | +to you under the Apache License, Version 2.0 (the |
| 11 | +"License"); you may not use this file except in compliance |
| 12 | +with the License. You may obtain a copy of the License at |
| 13 | +
|
| 14 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | +
|
| 16 | +Unless required by applicable law or agreed to in writing, |
| 17 | +software distributed under the License is distributed on an |
| 18 | +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 19 | +KIND, either express or implied. See the License for the |
| 20 | +specific language governing permissions and limitations |
| 21 | +under the License. |
| 22 | +--> |
| 23 | + |
| 24 | +# Testing semantic providers |
| 25 | + |
| 26 | +The optional `superset_core.semantic_layers.testing` pytest plugin supplies |
| 27 | +`semantic_layer_registration` and `SemanticProviderConformance`. Install pytest |
| 28 | +alongside the SDK's dependencies; the normal SDK import does not load pytest. |
| 29 | +Run the suite in an isolated provider test process without a Superset application. |
| 30 | + |
| 31 | +Load the plugin before test collection so pytest can rewrite its assertions: |
| 32 | + |
| 33 | +```bash |
| 34 | +pytest -p superset_core.semantic_layers.testing path/to/provider_tests |
| 35 | +``` |
| 36 | + |
| 37 | +Inherit the suite in your own test module: |
| 38 | + |
| 39 | +```python |
| 40 | +from superset_core.semantic_layers.testing import SemanticProviderConformance |
| 41 | + |
| 42 | +class TestMyProvider(SemanticProviderConformance): |
| 43 | + pass |
| 44 | +``` |
| 45 | + |
| 46 | +Provide these function-scoped pytest fixtures. Import decorated provider modules |
| 47 | +inside fixtures that depend on `semantic_layer_registration`, not at collection |
| 48 | +time. The fixture bypasses only decoration; host registration and lifecycle need |
| 49 | +separate integration tests. |
| 50 | + |
| 51 | +| Fixture | Value and responsibility | |
| 52 | +| --- | --- | |
| 53 | +| `semantic_view` | A real SDK view over a small deterministic backend dataset. | |
| 54 | +| `semantic_reloaded_view` | A separately constructed view of the same catalog and identity. | |
| 55 | +| `semantic_query_cases` | Mapping from each required case below to `(SemanticQuery, expected_arrow_table)`. Expected results must be independently calculated, not obtained from the provider under test. | |
| 56 | +| `semantic_error_cases` | Mapping from `method:kind` to a no-argument callable that executes the actual provider method against a controlled backend outcome. Methods: `get_table`, `get_values`, `get_row_count`; kinds: `refusal`, `fault`, `incomplete`, `unverified`. Do not supply closures that simply raise the expected SDK exception. | |
| 57 | +| `semantic_capability_cases` | One `(query, expected_table)` per advertised feature value, exercising the feature. Use a positive offset and limit for `ROW_OFFSET`; use an actual group limit for `GROUP_LIMIT`/`GROUP_OTHERS`, an `AdhocExpression` order key for `ADHOC_EXPRESSIONS_IN_ORDERBY`. Features run as separate parametrized cases; unadvertised features are skipped. `ADHOC_COLUMN_EXPRESSIONS` is always skipped with "no SDK query shape yet": `SemanticQuery.dimensions` accepts only `Dimension`, and the host rejects ad-hoc dimensions. No fixture entry is required for that feature; its skip is unverified coverage, not a passing capability check. | |
| 58 | +| `semantic_make_view` | Callable taking an opaque string token, constructing/discovering a view through the provider's binding path. Adapt the factory of your supported SDK version here. An uncached test adapter may construct a view directly; cached providers must exercise the actual binding/discovery path. | |
| 59 | +| `semantic_change_catalog` | No-argument callable changing the controlled source catalog: add a visible member on every call, without clearing provider caches. | |
| 60 | +| `semantic_provider_class` | `(importable_module_name, class_name)` of the layer for an isolated import and static-schema check. Dependencies must already be installed. | |
| 61 | + |
| 62 | +Required query cases are `identity`, `grain`, `null`, `not_null`, `or`, and |
| 63 | +`order_limit`. Use member names distinct from private IDs and display labels. |
| 64 | +For `grain`, use a grain-bearing dimension with data whose aggregation distinguishes the selected grain from raw values. Multiple grain variants for one name are allowed, not required. When the catalog declares no grain-bearing dimension, the case skips with an explicit reason before looking up its fixture entry; such providers may omit the `grain` entry. This reports unexercised grain coverage, not a passing grain check. |
| 65 | +NULL and grouped-OR fixtures must select NULL and non-NULL branches observably; |
| 66 | +the `or` case must use the host-normalized shape: one `OrFilter` containing |
| 67 | +`IN frozenset({value_a, value_b, ...})` (at least two non-NULL values) and |
| 68 | +`IS_NULL` for the same member, combined with an outer AND comparison in the same |
| 69 | +predicate stage. Seed data so both IN values and the NULL branch contribute, |
| 70 | +and removing the parentheses or dropping the outer comparison changes the result. |
| 71 | +Also seed an excluded non-NULL category that satisfies the outer comparison, |
| 72 | +so dropping the whole OR group changes the result. |
| 73 | +The SQLite example uses a February lower bound, with A and NULL rows on both sides |
| 74 | +of that bound: the correct totals are NULL=1, A=5, B=8. This distinguishes either |
| 75 | +ordering of the outer AND and the OR group. A February C row with amount 6 must |
| 76 | +be excluded; dropping the OR group incorrectly adds C=6 to the result. |
| 77 | +Seed more result rows than the limit, in an order that differs from the requested |
| 78 | +sort. The suite compares exact Arrow column names, types, values and row order, |
| 79 | +without prescribing column order or field nullability. |
| 80 | +Expected tables must be nonempty so a provider returning no data cannot pass. |
| 81 | +Multi-row query and capability cases must supply explicit order keys that totally |
| 82 | +order the expected rows, without ties. Do not rely on where NULLs sort unless the |
| 83 | +provider defines that behavior; the example orders nullable groups by their |
| 84 | +non-null, distinct metric values. |
| 85 | + |
| 86 | +The catalog check detects duplicate metric names, duplicate dimension name/grain |
| 87 | +pairs, metric/dimension name collisions, and unstable selection names across |
| 88 | +reloads. It does not make private IDs or display labels into selection keys. |
| 89 | +The metadata test changes the source between two string-token generations. |
| 90 | +The change fixture must retain every existing metric and dimension name/grain |
| 91 | +pair and add at least one member; an empty, shrunken or unchanged second catalog |
| 92 | +fails. For views that echo a token, the test checks that the first view never |
| 93 | +claims the later generation. Legacy views can report `None`; freshness is still |
| 94 | +checked, but token relabeling cannot be verified for those views. |
| 95 | + |
| 96 | +The error lane asserts closed SDK refusal/completeness categories, not HTTP |
| 97 | +status codes, host exception types or presentation messages. Fault cases reject |
| 98 | +common programming errors (`TypeError`, `AttributeError`, `NameError`, |
| 99 | +`AssertionError`, `KeyError`) as well as refusal/completeness exceptions; provider |
| 100 | +lanes should additionally assert their backend-specific fault type. If a backend cannot |
| 101 | +produce a particular `method:kind` outcome, override `test_error_categories`, |
| 102 | +explicitly skip only that case with the backend-specific reason, and delegate |
| 103 | +all other cases to `super().test_error_categories(...)`. Repeat both base |
| 104 | +`@pytest.mark.parametrize` decorators on the override, with the same method and |
| 105 | +kind lists: pytest does not inherit those marks onto an overriding method. |
| 106 | +Record such skips as |
| 107 | +unverified release-gate coverage, not passing conformance; do not fabricate an |
| 108 | +SDK exception just to fill the matrix. The count lane only |
| 109 | +checks failure propagation; it does not settle pending count or value-shape |
| 110 | +rules. Construction fixtures avoid copying a factory signature or context type. |
| 111 | +The host supplies default chronological ordering before calling the provider. |
| 112 | +This suite checks the provider obligation to honor explicit ordering before limit |
| 113 | +and offset, including the ordered grain case. |
| 114 | + |
| 115 | +The import/schema check starts a fresh Python interpreter with the test import |
| 116 | +path. It rejects imports of `superset`, `flask` and `flask_appbuilder`, including |
| 117 | +transitive imports through SDK modules that depend on those frameworks, and calls |
| 118 | +both no-configuration schema forms with common file-opening, DNS and socket |
| 119 | +connection operations blocked. Caught attempts fail too. This is a regression |
| 120 | +tripwire, not a sandbox or proof against every possible I/O mechanism (for example, |
| 121 | +a native driver). Provider lanes must also replace their own transport/credential |
| 122 | +entry points with failures to prove schema generation cannot call them. |
| 123 | + |
| 124 | +The SDK-only examples live in `superset-core/tests/semantic_layers/`: the minimal |
| 125 | +`example_provider.py` demonstrates registration and discovery; `conformance_provider.py` |
| 126 | +and `test_conformance.py` show an in-memory SQLite backend with controlled failures |
| 127 | +and explicit expected answers. SQLite runs in-process, with no server or credentials. |
| 128 | +The example does not define new count semantics or production caching machinery. |
| 129 | + |
| 130 | +Run the examples from the repository root: |
| 131 | + |
| 132 | +```bash |
| 133 | +PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 PYTHONPATH=$PWD/superset-core/src python -m pytest -p superset_core.semantic_layers.testing -q -o addopts='' superset-core/tests/semantic_layers/test_conformance.py |
| 134 | +``` |
| 135 | + |
| 136 | +Passing this suite is a provider-level check, not a claim that a deployed host, |
| 137 | +MCP caller, authorization policy, or live warehouse works. Each provider must |
| 138 | +supply honest discriminating fixtures and retain its host caller and live-provider |
| 139 | +lanes. Provider-lane wiring must use the actual pinned SDK, never copied types. |
0 commit comments