Skip to content

Commit

Permalink
Add test for updating link property (#1118)
Browse files Browse the repository at this point in the history
  • Loading branch information
scotttrinh authored Oct 11, 2024
1 parent 227f125 commit 51eca53
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
47 changes: 47 additions & 0 deletions docs/update.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^

Expand Down
33 changes: 32 additions & 1 deletion integration-tests/lts/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => ({
Expand Down

0 comments on commit 51eca53

Please sign in to comment.