オントロジーの構築ファンクションユニットテストオントロジー編集の確認

注: 以下の翻訳の正確性は検証されていません。AIPを利用して英語版の原文から機械的に翻訳されたものです。

オントロジーの編集を検証する

verifyOntologyEditFunction() API を使用して、関数が実行した編集を検証できます。この API は "@foundry/functions-testing-lib" からインポートする必要があります。これにより、以下のワークフローに対する単体テストを作成できます。

単一値と複数値の検証メソッド

それぞれの種類の編集は、単一値メソッドまたは複数値メソッドで検証できます。単一値メソッドは1件の編集を検証し、単一の値を受け取ります。一方、複数値メソッドは同じ種類の複数件の編集を検証し、それらの値の配列を受け取ります。この2つの形式は相互に置き換えられず、誤った形式を使用すると型エラーになります。

検証する編集1件の編集を検証複数件の編集を検証
オブジェクトの作成.createsObject({ objectType, properties }).createsObjects([{ objectType, properties }])
オブジェクトのプロパティの編集.modifiesObject({ object, properties }).modifiesObjects([{ object, properties }])
オブジェクトの削除.deletesObject(object).deletesObjects([object])
リンクの作成.addsLink({ link, linkedObject }).addsLinks([{ link, linkedObject }])
リンクの削除.removesLink({ link, unlinkedObject }).removesLinks([{ link, unlinkedObject }])

4つのリンクメソッドは、それまでに収集された編集を受け取るコールバックも受け付けます。これは、リンク先のオブジェクトがテスト対象の関数によって作成された場合に便利です。この場合、.addsLink と .removesLink はコールバックから単一のリンクを返し、.addsLinks と .removesLinks は配列を返します。例については、新しく作成されたオブジェクトに対する編集を検証するを参照してください。

.hasNoMoreEdits() には複数値形式がありません。また、結果を返さないため、検証チェーンの最後に配置する必要があります。

オブジェクトの作成を検証する

.createsObject メソッドを使用して、オブジェクトの作成を検証できます。1回の呼び出しで複数のオブジェクトの作成を検証するには、代わりに .createsObjects を使用します。以下に例を示します。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import { MyFunctions } from ".." import { Objects , ExampleDataAirport } from "@foundry/ontology-api"; import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib"; describe("example test suite", () => { const myFunctions = new MyFunctions(); test("create airport", () => { verifyOntologyEditFunction(() => myFunctions.createAirport("airportCode", "airportDisplayName")) .createsObject( { objectType: ExampleDataAirport, properties: { airport: "airportCode", displayAirportName: "airportDisplayName", }, }); }); });

これを使用して、次の関数をテストできます。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 import { Function, OntologyEditFunction, Edits } from "@foundry/functions-api"; import { Objects, ExampleDataAirport } from "@foundry/ontology-api"; export class MyFunctions { @Edits(ExampleDataAirport) @OntologyEditFunction() public createAirport(airport: string, displayName: string): void { const newAirport = Objects.create().exampleDataAirport(airport); newAirport.displayAirportName = displayName; } }

新しく作成されたオブジェクトに対する編集を検証する

新しく作成されたオブジェクトに関する編集を検証できます。たとえば、新しい ExampleDataFlight オブジェクトを作成し、new-flight-delay-0 へのリンクが作成されることを検証したい場合があります。以下に例を示します。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import { MyFunctions } from ".." import { Objects , ExampleDataFlight, ExampleFlightDelayEvent } from "@foundry/ontology-api"; import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib"; describe("example test suite", () => { const myFunctions = new MyFunctions(); test("single key with single created object", () => { const flight = Objects.create().exampleDataFlight("flightTest"); verifyOntologyEditFunction(() => myFunctions.createAndLinkDelays(flight, 1)) .createsObject({ objectType: ExampleFlightDelayEvent, properties: { eventId: "new-flight-delay-0", }, }) .addsLink(edits => ({ link: flight.flightDelayEvent, linkedObject: edits.createdObjects.byObjectType(ExampleFlightDelayEvent)[0], })) }); });

これを使用して、次の関数をテストできます。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import { Function, Integer, OntologyEditFunction, Edits } from "@foundry/functions-api"; import { Objects, ExampleDataFlight, ExampleFlightDelayEvent } from "@foundry/ontology-api"; export class MyFunctions { @Edits(ExampleDataFlight, ExampleFlightDelayEvent ) @OntologyEditFunction() public createAndLinkDelays(flight: ExampleDataFlight, numDelay: Integer): void { for (let n = 0; n < numDelay; n++) { const delay = Objects.create().exampleFlightDelayEvent(`new-flight-delay-${n}`); flight.flightDelayEvent.add(delay); } } }

オブジェクトのプロパティの編集を検証する

.modifiesObject を使用して、プロパティの編集を検証できます。1回の呼び出しで複数のオブジェクトのプロパティの編集を検証するには、.modifiesObjects に配列を渡します。以下に例を示します。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import { MyFunctions } from ".." import { Objects , ExampleDataFlight, ExampleFlightDelayEvent } from "@foundry/ontology-api"; import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib"; describe("example test suite", () => { const myFunctions = new MyFunctions(); test("modifies aircraft of the flight", () => { const flight = Objects.create().exampleDataFlight("NY -> LA"); const oldAircraft = Objects.create().exampleDataAircraft("N11111"); flight.aircraft.set(oldAircraft); const newAircraft = Objects.create().exampleDataAircraft("A00000"); verifyOntologyEditFunction(() => myFunctions.assignAircraftToFlight(flight, newAircraft)) .modifiesObject( { object: flight, properties: { tailNumber: "A00000" } }) }); });

これを使用して、次の関数をテストできます。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 import { Function, OntologyEditFunction, Edits } from "@foundry/functions-api"; import { Objects, ExampleDataFlight, ExampleFlightDelayEvent } from "@foundry/ontology-api"; export class MyFunctions { @Edits(ExampleDataFlight) @OntologyEditFunction() public assignAircraftToFlight(flight: ExampleDataFlight, aircraft: ExampleDataAircraft): void { flight.aircraft.clear(); aircraft.flight.set(flight); flight.tailNumber = aircraft.tailNumber; } }

オブジェクトにほかの編集がないことを検証する

任意の .hasNoMoreEdits() を使用して、ほかの編集がないことを確認できます。これにより、指定した編集のみが許可され、ほかの編集が検出された場合は検証に失敗します。以下に例を示します。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import { MyFunctions } from ".." import { Objects , ExampleDataFlight, ExampleFlightDelayEvent } from "@foundry/ontology-api"; import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib"; describe("example test suite", () => { const myFunctions = new MyFunctions(); test("single key with linked object", () => { const flight = Objects.create().exampleDataFlight("flightAnotherTest"); const delay = Objects.create().exampleFlightDelayEvent("new-flight-delay") verifyOntologyEditFunction(() => myFunctions.linkDelays(flight, delay)) .addsLink({link: flight.flightDelayEvent, linkedObject: delay }) .hasNoMoreEdits(); }); });

.hasNoMoreEdits() を使用する際、実行された特定の種類の編集を無視できます。そのためには、以下の一部またはすべてを含むオブジェクトを渡します。

  • ignoreExtraCreatedObjects: true
  • ignoreExtraModifiedObjects: true
  • ignoreExtraDeletedObjects: true
  • ignoreExtraLinkedObjects: true
  • ignoreExtraUnlinkedObjects: true

オブジェクトへのリンクの作成を検証する

.addsLink を使用して、オブジェクトへのリンクの作成を検証できます。以下に例を示します。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import { MyFunctions } from ".." import { Objects , ExampleDataFlight, ExampleFlightDelayEvent } from "@foundry/ontology-api"; import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib"; describe("example test suite", () => { const myFunctions = new MyFunctions(); test("single key with linked object", () => { const flight = Objects.create().exampleDataFlight("flightAnotherTest"); const delay = Objects.create().exampleFlightDelayEvent("new-flight-delay") verifyOntologyEditFunction(() => myFunctions.linkDelays(flight, delay)) .addsLink({link: flight.flightDelayEvent, linkedObject: delay }) .hasNoMoreEdits(); }); });

このテストは、同じリンクを逆方向でテストすることと同等です。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import { MyFunctions } from ".." import { Objects , ExampleDataFlight, ExampleFlightDelayEvent } from "@foundry/ontology-api"; import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib"; describe("example test suite", () => { const myFunctions = new MyFunctions(); test("single key with linked object reverse", () => { const flight = Objects.create().exampleDataFlight("flightAnotherTest"); const delay = Objects.create().exampleFlightDelayEvent("new-flight-delay") verifyOntologyEditFunction(() => myFunctions.linkDelays(flight, delay)) .addsLink({link: delay.flight, linkedObject: flight }) .hasNoMoreEdits(); }); });

これを使用して、次の関数をテストできます。

Copied!
1 2 3 4 5 6 7 8 9 10 11 import { Function, OntologyEditFunction, Edits } from "@foundry/functions-api"; import { Objects, ExampleDataFlight, ExampleFlightDelayEvent } from "@foundry/ontology-api"; export class MyFunctions { @Edits(ExampleDataFlight, ExampleFlightDelayEvent ) @OntologyEditFunction() public linkDelays(flight: ExampleDataFlight, delay: ExampleFlightDelayEvent): void { flight.flightDelayEvent.add(delay); } }

オブジェクトからのリンクの削除を検証する

.removesLink を使用して、オブジェクトからのリンクの削除を検証できます。以下に例を示します。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import { MyFunctions } from ".." import { Objects , ExampleDataFlight, ExampleFlightDelayEvent } from "@foundry/ontology-api"; import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib"; describe("example test suite", () => { const myFunctions = new MyFunctions(); test("test link removal", () => { const flight = Objects.create().exampleDataFlight("flightAnotherTest"); const delay = Objects.create().exampleFlightDelayEvent("new-flight-delay") flight.flightDelayEvent.add(delay); verifyOntologyEditFunction(() => myFunctions.removeAllDelays(flight)) .removesLink({link: flight.flightDelayEvent, unlinkedObject: delay }) .hasNoMoreEdits(); }); });

これを使用して、次の関数をテストできます。

Copied!
1 2 3 4 5 6 7 8 9 10 11 import { Function, OntologyEditFunction, Edits } from "@foundry/functions-api"; import { Objects, ExampleDataFlight, ExampleFlightDelayEvent } from "@foundry/ontology-api"; export class MyFunctions { @Edits(ExampleDataFlight, ExampleFlightDelayEvent) @OntologyEditFunction() public removeAllDelays(flight: ExampleDataFlight): void { flight.flightDelayEvent.clear(); } }

オブジェクトの削除を検証する

.deletesObject を使用して、オブジェクトの削除を検証できます。以下に例を示します。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import { MyFunctions } from ".." import { Objects , ExampleDataFlight } from "@foundry/ontology-api"; import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib"; describe("example test suite", () => { const myFunctions = new MyFunctions(); test("test object deletion", () => { const flight = Objects.create().exampleDataFlight("flightAnotherTest"); verifyOntologyEditFunction(() => myFunctions.deleteFlight(flight)) .deletesObject(flight) .hasNoMoreEdits(); }); });

これを使用して、次の関数をテストできます。

Copied!
1 2 3 4 5 6 7 8 9 10 11 import { Function, OntologyEditFunction, Edits } from "@foundry/functions-api"; import { Objects, ExampleDataFlight } from "@foundry/ontology-api"; export class MyFunctions { @Edits(ExampleDataFlight) @OntologyEditFunction() public deleteFlight(flight: ExampleDataFlight): void { flight.delete(); } }

複数のオブジェクトが作成されたことを検証する

.createsObjects メソッドを使用し、オブジェクトの仕様の配列を渡すことで、複数のオブジェクトが作成されたことを検証できます。以下に例を示します。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import { MyFunctions } from ".." import { Objects , ExampleDataFlight, ExampleFlightDelayEvent } from "@foundry/ontology-api"; import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib"; describe("example test suite", () => { const myFunctions = new MyFunctions(); test("single key with many created objects", () => { const flight = Objects.create().exampleDataFlight("flightTest"); verifyOntologyEditFunction(() => myFunctions.createAndLinkDelays(flight, 3)) .createsObjects( [0, 1, 2].map(i => ({ objectType: ExampleFlightDelayEvent, properties: { eventId: "new-flight-delay-" + i, }, })), ) .addsLinks(edits => edits.createdObjects.byObjectType(ExampleFlightDelayEvent).map(event => ({ link: flight.flightDelayEvent, linkedObject: event, })), ) .hasNoMoreEdits(); }); });

これを使用して、次の関数をテストできます。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import { Function, Integer, OntologyEditFunction, Edits } from "@foundry/functions-api"; import { Objects, ExampleDataFlight, ExampleFlightDelayEvent } from "@foundry/ontology-api"; export class MyFunctions { @Edits(ExampleDataFlight, ExampleFlightDelayEvent ) @OntologyEditFunction() public createAndLinkDelays(flight: ExampleDataFlight, numDelay: Integer): void { for (let n = 0; n < numDelay; n++) { const delay = Objects.create().exampleFlightDelayEvent(`new-flight-delay-${n}`); flight.flightDelayEvent.add(delay); } } }

非同期のオントロジー編集

非同期のオントロジー編集は、次のように検証できます。

Copied!
1 2 3 4 5 6 7 8 9 10 11 import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib"; test("test async edit function", async () => { const obj = Objects.create().objectWithAllPropertyTypes(1); (await verifyOntologyEditFunction(() => myFunctions.setDateAndTimestampToNow(obj))).modifiesObject({ object: obj, properties: { timestampProperty: makeTimestamp(), }, }); });

複数の検証

上記の例で示したように、検証をチェーン化できます。次のパターンは、その方法を示しています。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib"; import { Objects, ExampleDataObject } from "@foundry/ontology-api"; test("multiple action edit", () => { verifyOntologyEditFunction(() => myFunctions.multistageEdits("objectId", "objectName")) .createsObject({...}) .modifiesObjects([{...}]) .addsLinks([{...}]) .removesLinks([{...}]) .deletesObject(...) .hasNoMoreEdits(); });