Файли
openidea/sdk/rust/openidea-plugin/tests/api20_database_schema.rs
T
葛俊таgejun123456 b7c2d4eacb feat(plugins): expose database table menus and read-only schema (#7372)
Plugin API 1.20 adds wit/database.wit with describe-table behind the database.schema capability: a read-only view of a connected source's table (columns, keys, indexes) through DbManager::connected_table_details. Plugins can contribute database.table.context menu entries (needs_api 20) that receive the non-secret table identity and the when keys databaseDriver, databaseObjectKind, databaseSchema, databaseTable and databaseSelectionCount.

Co-authored-by: gejun123456 <[email protected]>
2026-12-02 14:25:35 +08:00

42 рядки
1.9 KiB
Rust

use openidea_plugin::{DatabaseColumn, DatabaseTableRef, DatabaseTableSchema, Error, host, mock::MockHost};
#[test]
fn schema_reads_are_project_scoped_separately_granted_and_never_manage_sources() {
let mock = MockHost::install().with_capabilities(&["database.schema", "project.read"]);
mock.set_project_root(Some("/project"));
let table = DatabaseTableRef {
source_id: "user-created:inventory".into(),
catalog: None,
schema: "public".into(),
table: "users".into(),
};
let schema = DatabaseTableSchema {
columns: vec![DatabaseColumn {
name: "id".into(),
data_type: "BIGINT".into(),
primary_key: true,
..Default::default()
}],
..Default::default()
};
mock.set_database_table_schema("/project", &table, schema.clone());
let project = host::projects().unwrap().remove(0);
assert_eq!(project.database_table_schema(&table).unwrap(), schema);
assert!(matches!(project.data_sources(), Err(Error::Denied(_))));
mock.add_project("/other");
let other = host::projects().unwrap().into_iter().find(|p| p.root().ok().as_deref() == Some("/other")).unwrap();
assert!(matches!(other.database_table_schema(&table), Err(Error::NotFound(_))));
mock.set_restricted(true);
assert!(matches!(project.database_table_schema(&table), Err(Error::Denied(_))));
mock.set_restricted(false);
assert!(project.database_table_schema(&DatabaseTableRef { table: "missing".into(), ..table.clone() }).is_err());
}
#[test]
fn project_read_and_source_ownership_do_not_grant_schema_reads() {
let mock = MockHost::install().with_capabilities(&["project.read", "database.sources"]);
mock.set_project_root(Some("/project"));
let p = host::projects().unwrap().remove(0);
assert!(matches!(p.database_table_schema(&DatabaseTableRef::default()), Err(Error::Denied(_))));
}