From 51eca531d0497d0a48cf73289c4c2b6bde41ce86 Mon Sep 17 00:00:00 2001 From: Scott Trinh Date: Fri, 11 Oct 2024 11:01:50 -0400 Subject: [PATCH] Add test for updating link property (#1118) --- docs/update.rst | 47 ++++++++++++++++++++++++++++ integration-tests/lts/update.test.ts | 33 ++++++++++++++++++- 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/docs/update.rst b/docs/update.rst index 979bfc2a6..b3d9a13d9 100644 --- a/docs/update.rst +++ b/docs/update.rst @@ -117,6 +117,53 @@ In the query builder this is represented with the following syntax. } })) +**Updating a single link property** + +.. code-block:: typescript + + e.update(e.Movie, (movie) => ({ + filter_single: { title: "The Eternals" }, + set: { + actors: { + "+=": e.select(movie.actors, (actor) => ({ + "@character_name": e.str("Sersi"), + filter: e.op(actor.name, "=", "Gemma Chan") + })) + } + } + })); + +**Updating many link properties** + +.. code-block:: typescript + + const q = e.params( + { + cast: e.array(e.tuple({ name: e.str, character_name: e.str })), + }, + (params) => + e.update(e.Movie, (movie) => ({ + filter_single: { title: "The Eternals" }, + set: { + actors: { + "+=": e.for(e.array_unpack(params.cast), (cast) => + e.select(movie.characters, (character) => ({ + "@character_name": cast.character_name, + filter: e.op(cast.name, "=", character.name), + })), + ), + }, + }, + })), + ).run(client, { + cast: [ + { name: "Gemma Chan", character_name: "Sersi" }, + { name: "Richard Madden", character_name: "Ikaris" }, + { name: "Angelina Jolie", character_name: "Thena" }, + { name: "Salma Hayek", character_name: "Ajak" }, + ], + }); + Bulk updates ^^^^^^^^^^^^ diff --git a/integration-tests/lts/update.test.ts b/integration-tests/lts/update.test.ts index 653d00358..a8df1fb79 100644 --- a/integration-tests/lts/update.test.ts +++ b/integration-tests/lts/update.test.ts @@ -132,10 +132,41 @@ describe("update", () => { })); await q2.run(client); + const theAvengersCast: { name: string; character_name: string }[] = [ + { name: data.iron_man.name, character_name: "Tony Stark!" }, + { name: data.cap.name, character_name: "Steve Rogers!" }, + { name: data.thanos.name, character_name: "Thanos!" }, + ]; + + const q2CharName = e.params( + { cast: e.array(e.tuple({ name: e.str, character_name: e.str })) }, + (params) => + e.update(theAvengers, (m) => ({ + set: { + characters: { + "+=": e.for(e.array_unpack(params.cast), (cast) => + e.select(m.characters, (c) => ({ + "@character_name": cast.character_name, + filter: e.op(c.name, "=", cast.name), + })), + ), + }, + }, + })), + ); + await q2CharName.run(client, { cast: theAvengersCast }); + const t2 = await e - .select(theAvengers, () => ({ id: true, characters: true })) + .select(theAvengers, () => ({ + id: true, + characters: () => ({ "@character_name": true, name: true }), + })) .run(client); assert.equal(t2?.characters.length, 3); + const charSet = new Set(t2.characters.map((c) => c["@character_name"])); + assert.ok(charSet.has("Thanos!")); + assert.ok(charSet.has("Tony Stark!")); + assert.ok(charSet.has("Steve Rogers!")); await e .update(theAvengers, () => ({