| 1 | //! 一个类被词汇表认领之后,**形状要跟着说实话**。 |
| 2 | //! |
| 3 | //! 形状承载的是来历:方 = 词表声明的,圆 = 语料里长出来的。而 `adopt_iri_onto_key` |
| 4 | //! 是在形状还不表示来历的年代写的,它只写 IRI——于是往一个已经有 `person` / |
| 5 | //! `organization` 的库里导 schema.org,那几个类**拿到了 IRI 却仍然是圆的**。 |
| 6 | //! 有 IRI 却是圆的,画面就在说谎。 |
| 7 | //! |
| 8 | //! 这个缝隙只有真导入一次才撞得到:单看 `adopt_iri_onto_key` 的签名和调用点, |
| 9 | //! 没有任何东西提示"还有个字段该一起改"。实测撞上过(demo 库里 5 个类中招)。 |
| 10 | //! |
| 11 | //! 没有 `UTOPIA_DATABASE_URL` 时跳过而不是失败。自建自拆,绝不碰已有的库。 |
| 12 | |
| 13 | use sqlx::PgPool; |
| 14 | use uuid::Uuid; |
| 15 | |
| 16 | #[tokio::test] |
| 17 | async fn adopting_an_iri_turns_the_class_square() -> anyhow::Result<()> { |
| 18 | let Ok(url) = std::env::var("UTOPIA_DATABASE_URL") else { |
| 19 | eprintln!("跳过:未设 UTOPIA_DATABASE_URL"); |
| 20 | return Ok(()); |
| 21 | }; |
| 22 | let pool = PgPool::connect(&url).await?; |
| 23 | // 开跑前先扫地:断言 panic 会跳过 teardown,一次失败会给下一次留垃圾 |
| 24 | sqlx::query("DELETE FROM organizations WHERE name = 'adopt-shape-test'") |
| 25 | .execute(&pool) |
| 26 | .await?; |
| 27 | |
| 28 | let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7()); |
| 29 | sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, 'adopt-shape-test')") |
| 30 | .bind(org) |
| 31 | .execute(&pool) |
| 32 | .await?; |
| 33 | sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, 'adopt-shape-test')") |
| 34 | .bind(ws) |
| 35 | .bind(org) |
| 36 | .execute(&pool) |
| 37 | .await?; |
| 38 | sqlx::query( |
| 39 | "INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, 'adopt-shape-test')", |
| 40 | ) |
| 41 | .bind(kb) |
| 42 | .bind(ws) |
| 43 | .execute(&pool) |
| 44 | .await?; |
| 45 | |
| 46 | let run = async { |
| 47 | // 一个「语料里长出来的」类:没有 IRI,所以是圆的 |
| 48 | let grown = Uuid::now_v7(); |
| 49 | sqlx::query( |
| 50 | "INSERT INTO entity_types (id, kb_id, key, label, color, shape) |
| 51 | VALUES ($1, $2, 'person', 'Person', '#7fd0ff', 'circle')", |
| 52 | ) |
| 53 | .bind(grown) |
| 54 | .bind(kb) |
| 55 | .execute(&pool) |
| 56 | .await?; |
| 57 | |
| 58 | let shape: String = sqlx::query_scalar("SELECT shape FROM entity_types WHERE id = $1") |
| 59 | .bind(grown) |
| 60 | .fetch_one(&pool) |
| 61 | .await?; |
| 62 | assert_eq!(shape, "circle", "还没被认领时该是圆的"); |
| 63 | |
| 64 | // 导入一份词汇表,把 schema.org 的 Person 认到这个 key 上 |
| 65 | let adopted = utopia_store::ontology::adopt_iri_onto_key( |
| 66 | &pool, |
| 67 | kb, |
| 68 | "person", |
| 69 | "https://schema.org/Person", |
| 70 | ) |
| 71 | .await?; |
| 72 | assert_eq!(adopted, Some(grown), "该认到那个已有的类上"); |
| 73 | |
| 74 | let (iri, shape, color): (Option<String>, String, String) = |
| 75 | sqlx::query_as("SELECT iri, shape, color FROM entity_types WHERE id = $1") |
| 76 | .bind(grown) |
| 77 | .fetch_one(&pool) |
| 78 | .await?; |
| 79 | assert_eq!(iri.as_deref(), Some("https://schema.org/Person")); |
| 80 | assert_eq!( |
| 81 | shape, "square", |
| 82 | "认领之后是词表声明的类了,形状要跟着说实话" |
| 83 | ); |
| 84 | // **颜色不该动**:颜色是身份(同一个 key 永远同一个色), |
| 85 | // 认领改变的是来历不是身份 |
| 86 | assert_eq!(color, "#7fd0ff", "认领不该动用户看惯的颜色"); |
| 87 | |
| 88 | // 幂等:再认一次不该改动任何东西(iri 已非空,UPDATE 匹配不上) |
| 89 | let again = utopia_store::ontology::adopt_iri_onto_key( |
| 90 | &pool, |
| 91 | kb, |
| 92 | "person", |
| 93 | "https://example.org/OtherPerson", |
| 94 | ) |
| 95 | .await?; |
| 96 | assert_eq!(again, None, "已被认领的类不该被另一个词汇表抢走"); |
| 97 | let iri2: Option<String> = sqlx::query_scalar("SELECT iri FROM entity_types WHERE id = $1") |
| 98 | .bind(grown) |
| 99 | .fetch_one(&pool) |
| 100 | .await?; |
| 101 | assert_eq!(iri2.as_deref(), Some("https://schema.org/Person")); |
| 102 | |
| 103 | Ok::<(), anyhow::Error>(()) |
| 104 | } |
| 105 | .await; |
| 106 | |
| 107 | sqlx::query("DELETE FROM organizations WHERE name = 'adopt-shape-test'") |
| 108 | .execute(&pool) |
| 109 | .await?; |
| 110 | run |
| 111 | } |