Skip to content

Commit 8fe951e

Browse files
authored
Merge pull request #158 from teaql/test/context-owned-sql-relation-134
test: prove normal SQL relations use invoking runtime metadata
2 parents e3cc787 + 9b8f197 commit 8fe951e

2 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Java context-owned metadata boundaries (#134)
2+
3+
This is a call-path audit, not a claim that normal relation queries previously used
4+
the process-global registry. The supported SQL path already carried resolved
5+
descriptors; the remaining work was to verify it against two independent runtimes
6+
with the same entity names.
7+
8+
| Entry point | Classification | Current boundary and executable evidence |
9+
| --- | --- | --- |
10+
| `PortableSQLDataService` relation loading | Active supported Q path | Repositories receive the service's metadata snapshot and `attachRelation` receives a resolved relation descriptor. `PortableSQLDatabaseTest.relationHydrationUsesInvokingRuntimeWhenEntityNamesMatch` runs two SQLite-backed runtimes with `TopNParent`/`TopNChild` in both, but distinct `parent_a`/`parent_b` FK columns. It alternates a wrong and absent global registry; both contexts load only their own child. `TOPN_009_relationHydrationUsesServiceMetadataWithoutGlobalRegistry` covers the absent-global case with the established Top-N fixture. |
11+
| `GenericSQLProperty` / `GenericSQLRelation.setPropertyValue(context, …)` | Public legacy mapper; compatibility-only inside this repository | These context-bearing mappers resolve reference placeholders from the invoking context. `LegacyReferenceHydrationContextTest` alternates two same-name models with wrong/absent global metadata. This is not the normal `PortableSQLDataService` graph assembly path. |
12+
| `PortableSQLRepository.resolveDescriptor` | Active when `streamInternal` hydrates an entity or reference | Service repositories have a metadata snapshot. Schema-only dialect repositories may have none; they can reconcile physical schema but reject cross-entity hydration instead of borrowing global metadata. `PortableSQLDatabaseTest.schemaOnlyRepositoryMustNotHydrateRelationFromGlobalMetadata` registers usable global metadata and verifies that hydration still fails closed. |
13+
| `EntityFactory.forContext` / `forMetadata` | Supported entity construction | The factory is bound to invoking-context or explicit metadata. `EntityFactoryContextTest` uses two runtimes with the same type name and a poisoned global registry. |
14+
| `EntityFactory.defaultFactory()` | Public, deprecated no-context compatibility entry point | It uses an explicitly registered process-global factory and fails with a migration hint when no global factory exists. It cannot guarantee runtime isolation when multiple runtimes coexist; prefer `forContext` or `forMetadata`. |
15+
| `Entity.addRelation(context, …)` | Supported relation attachment | Resolves and validates the relation using the invoking context. `BaseEntityTest` exercises same-name models with wrong/absent global metadata and invalid relation rejection. |
16+
| `Entity.addRelation(name, value)` | Public, deprecated no-context compatibility entry point | Not used by the normal service loader. It uses explicitly registered process-global metadata and fails with a migration hint when none exists. It cannot establish which runtime owns the entity; prefer the context overload. |
17+
| `BaseRequest` unbound metadata fallback | Deprecated context-free JSON search compatibility path | `JsonRequests.findWithJson(context, …)` binds invoking-context metadata before evaluating fields; `JsonRequestsContextTest` alternates same-name models with poisoned global metadata. Context-free callers relying on `EntityMetaFactory.registerGlobal` have no cross-runtime isolation guarantee. |
18+
19+
The two no-context public APIs remain compatibility surfaces, not recommended
20+
multi-runtime APIs. A process-global registration is an explicit single-runtime
21+
opt-in, not a substitute for passing a context. Their final migration policy
22+
(warning versus fail-closed even when a global factory is registered) is a
23+
separate compatibility decision; do not infer that deprecation alone makes
24+
them safe across runtimes.
25+
26+
Verification scope for this audit: the focused two-runtime SQL test, full Maven
27+
tests, Java examples, and the existing live PostgreSQL/MySQL CI gate. The
28+
SQLite test proves descriptor selection on the normal SQL relation path; it
29+
does not claim that every SQL dialect was retested with two simultaneous
30+
runtimes.

‎teaql-sql-portable/src/test/java/io/teaql/core/sql/portable/PortableSQLDatabaseTest.java‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,93 @@ public void TOPN_009_relationHydrationUsesServiceMetadataWithoutGlobalRegistry()
219219
}
220220
}
221221

222+
@Test
223+
public void relationHydrationUsesInvokingRuntimeWhenEntityNamesMatch() throws Exception {
224+
RelationRuntime first = relationRuntime("parent_a", 11L);
225+
RelationRuntime second = relationRuntime("parent_b", 21L);
226+
assertNotSame(first.metadata().resolveEntityDescriptor("TopNChild"),
227+
second.metadata().resolveEntityDescriptor("TopNChild"));
228+
229+
EntityMetaFactory previous = EntityMetaFactory.get();
230+
try {
231+
EntityMetaFactory.registerGlobal(second.metadata());
232+
assertEquals(List.of(11L), loadedChildIds(first.context()));
233+
234+
EntityMetaFactory.registerGlobal(first.metadata());
235+
assertEquals(List.of(21L), loadedChildIds(second.context()));
236+
237+
EntityMetaFactory.registerGlobal(null);
238+
assertEquals(List.of(11L), loadedChildIds(first.context()));
239+
assertEquals(List.of(21L), loadedChildIds(second.context()));
240+
} finally {
241+
EntityMetaFactory.registerGlobal(previous);
242+
}
243+
}
244+
245+
private static RelationRuntime relationRuntime(String foreignKeyColumn, long childId)
246+
throws Exception {
247+
SimpleEntityMetaFactory metadata = new SimpleEntityMetaFactory();
248+
EntityDescriptor parent = relationEntity(
249+
"TopNParent", TopNParent.class, TopNParent::new, "top_n_parent_data",
250+
List.of(new Object[] {"id", "INTEGER", Long.class},
251+
new Object[] {"version", "INTEGER", Long.class},
252+
new Object[] {"name", "VARCHAR(100)", String.class}));
253+
metadata.register(parent);
254+
EntityDescriptor child = relationEntity(
255+
"TopNChild", TopNChild.class, TopNChild::new, "top_n_child_data",
256+
List.of(new Object[] {"id", "INTEGER", Long.class},
257+
new Object[] {"version", "INTEGER", Long.class},
258+
new Object[] {"name", "VARCHAR(100)", String.class},
259+
new Object[] {"state", "VARCHAR(100)", String.class}));
260+
GenericSQLRelation relation = (GenericSQLRelation) child.addObjectProperty(
261+
metadata, "parent", "TopNParent", "children", TopNParent.class,
262+
GenericSQLRelation::new);
263+
relation.setTableName("top_n_child_data");
264+
relation.setColumnName(foreignKeyColumn);
265+
relation.setColumnType("INTEGER");
266+
metadata.register(child);
267+
268+
SQLiteTeaQLDatabase database = new SQLiteTeaQLDatabase();
269+
PortableSQLDataService service = new PortableSQLDataService("sql", database, metadata);
270+
TeaQLRuntime runtime = TeaQLRuntime.builder()
271+
.metadata(metadata)
272+
.dataService("sql", service)
273+
.idGenerationService((ctx, entity) -> 100L)
274+
.build();
275+
UserContext runtimeContext = new DefaultUserContext(runtime);
276+
runtimeContext.putAttribute("ensureTable", true);
277+
service.ensureSchema(runtimeContext, "TopNParent");
278+
service.ensureSchema(runtimeContext, "TopNChild");
279+
database.execute("DELETE FROM top_n_parent_data");
280+
database.execute("INSERT INTO top_n_parent_data (id, version, name)"
281+
+ " VALUES (1, 1, 'parent')");
282+
database.execute("INSERT INTO top_n_child_data (id, version, name, state, "
283+
+ foreignKeyColumn + ") VALUES (" + childId + ", 1, 'child', 'visible', 1)");
284+
return new RelationRuntime(metadata, runtimeContext);
285+
}
286+
287+
private static List<Long> loadedChildIds(UserContext runtimeContext) {
288+
TopNChildRequest child = new TopNChildRequest();
289+
child.selectProperty("id");
290+
child.selectProperty("version");
291+
child.selectProperty("name");
292+
TopNParentRequest parent = new TopNParentRequest();
293+
parent.selectProperty("id");
294+
parent.selectProperty("version");
295+
parent.selectProperty("name");
296+
parent.enhanceRelation("children", child);
297+
SmartList<TopNParent> rows = parent
298+
.comment("load same-named relation through the invoking runtime")
299+
.purpose("prove SQL relation hydration is context-owned")
300+
.executeForList(runtimeContext);
301+
assertEquals(1, rows.size());
302+
SmartList<TopNChild> children = rows.get(0).getProperty("children");
303+
assertNotNull(children);
304+
return children.toList(Entity::getId);
305+
}
306+
307+
private record RelationRuntime(SimpleEntityMetaFactory metadata, UserContext context) {}
308+
222309
@Test
223310
public void TOPN_012_canonicalRelationIndexEnsureIsIdempotentOnSQLite() {
224311
registerTopNFixture();

0 commit comments

Comments
 (0)