code
stringlengths 26
870k
| docstring
stringlengths 1
65.6k
| func_name
stringlengths 1
194
| language
stringclasses 1
value | repo
stringlengths 8
68
| path
stringlengths 5
194
| url
stringlengths 46
254
| license
stringclasses 4
values |
---|---|---|---|---|---|---|---|
def test_sql_parse_select_locking_04(self):
"""
SELECT id FROM a FOR UPDATE NOWAIT
""" | SELECT id FROM a FOR UPDATE NOWAIT | test_sql_parse_select_locking_04 | python | geldata/gel | tests/test_sql_parse.py | https://github.com/geldata/gel/blob/master/tests/test_sql_parse.py | Apache-2.0 |
def test_sql_parse_select_locking_05(self):
"""
SELECT id FROM a FOR UPDATE SKIP LOCKED
""" | SELECT id FROM a FOR UPDATE SKIP LOCKED | test_sql_parse_select_locking_05 | python | geldata/gel | tests/test_sql_parse.py | https://github.com/geldata/gel/blob/master/tests/test_sql_parse.py | Apache-2.0 |
def test_sql_parse_select_locking_06(self):
"""
SELECT id FROM a FOR UPDATE OF b
""" | SELECT id FROM a FOR UPDATE OF b | test_sql_parse_select_locking_06 | python | geldata/gel | tests/test_sql_parse.py | https://github.com/geldata/gel/blob/master/tests/test_sql_parse.py | Apache-2.0 |
async def do_basic_work(self):
"""Do a standardized set of DML operations.
We'll run this with different triggers and observe the results
"""
# This is multiple queries instead of one so that if one of them
# errors, it is immediately obvious which.
#
# ... I forgot this at some point and merged them, so now I am
# adding a note.
await self.con.execute('''
insert InsertTest { name := "a" };
''')
await self.con.execute('''
select {
(insert InsertTest { name := "b" }),
(update InsertTest filter .name = 'a'
set { name := 'a!' }),
};
''')
await self.con.execute('''
select {
(insert InsertTest { name := "c" }),
(insert DerivedTest { name := "d" }),
(update InsertTest filter .name = 'b'
set { name := 'b!' }),
(delete InsertTest filter .name = "a!"),
};
''')
await self.con.execute('''
select {
(for x in {'e', 'f'} union (insert DerivedTest { name := x })),
(delete InsertTest filter .name = "b!"),
};
''')
await self.con.execute('''
update InsertTest filter .name = 'd'
set { name := .name ++ '!' };
''')
await self.con.execute('''
for x in {'c', 'e'} union (
update InsertTest filter .name = x
set { name := x ++ '!' }
);
''')
await self.con.execute('''
select {
(update DerivedTest filter .name = 'f'
set { name := 'f!' }),
(delete DerivedTest filter .name = 'd!'),
};
''')
await self.con.execute('''
delete InsertTest;
''') | Do a standardized set of DML operations.
We'll run this with different triggers and observe the results | do_basic_work | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_insert_01(self):
await self.con.execute('''
alter type InsertTest {
create trigger log after insert for each do (
insert Note { name := "insert", note := __new__.name }
);
};
''')
await self.do_basic_work()
await self.assert_notes([
{'name': "insert", 'notes': set("abcdef")},
])
# We should still be able to insert Note normally
await self.con.execute('''
select {
(insert InsertTest { name := "foo" }),
(insert Note { name := "manual", note := "!" }),
};
''') | )
await self.do_basic_work()
await self.assert_notes([
{'name': "insert", 'notes': set("abcdef")},
])
# We should still be able to insert Note normally
await self.con.execute( | test_edgeql_triggers_insert_01 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_mixed_all_01(self):
# Install FOR ALL triggers for everything
await self.con.execute('''
alter type InsertTest {
create trigger log_del after delete for all do (
insert Note { name := "delete", notes := __old__.name }
);
create trigger log_ins after insert for all do (
insert Note { name := "insert", notes := __new__.name }
);
create trigger log_upd after update for all do (
insert Note {
name := "update",
notes := __old__.name,
subject := (insert DerivedNote {
name := "", notes := __new__.name
})
}
)
};
''')
await self.do_basic_work()
res = tb.bag([
{"name": "insert", "notes": ["a"], "subject": None},
{"name": "insert", "notes": ["b"], "subject": None},
{"name": "insert", "notes": {"c", "d"}, "subject": None},
{"name": "insert", "notes": {"e", "f"}, "subject": None},
{"name": "update", "notes": ["a"], "subject": {"notes": ["a!"]}},
{"name": "update", "notes": ["b"], "subject": {"notes": ["b!"]}},
{"name": "update", "notes": ["d"], "subject": {"notes": ["d!"]}},
{
"name": "update",
"notes": {"c", "e"},
"subject": {"notes": {"c!", "e!"}}
},
{"name": "update", "notes": ["f"], "subject": {"notes": ["f!"]}},
{"name": "delete", "notes": ["b!"], "subject": None},
{"name": "delete", "notes": ["d!"], "subject": None},
{"name": "delete", "notes": ["a!"], "subject": None},
{"name": "delete", "notes": {"c!", "e!", "f!"}, "subject": None},
])
await self.assert_query_result(
'''
select Note { name, notes, subject[is Note]: {notes} }
filter .name != "";
''',
res,
) | )
await self.do_basic_work()
res = tb.bag([
{"name": "insert", "notes": ["a"], "subject": None},
{"name": "insert", "notes": ["b"], "subject": None},
{"name": "insert", "notes": {"c", "d"}, "subject": None},
{"name": "insert", "notes": {"e", "f"}, "subject": None},
{"name": "update", "notes": ["a"], "subject": {"notes": ["a!"]}},
{"name": "update", "notes": ["b"], "subject": {"notes": ["b!"]}},
{"name": "update", "notes": ["d"], "subject": {"notes": ["d!"]}},
{
"name": "update",
"notes": {"c", "e"},
"subject": {"notes": {"c!", "e!"}}
},
{"name": "update", "notes": ["f"], "subject": {"notes": ["f!"]}},
{"name": "delete", "notes": ["b!"], "subject": None},
{"name": "delete", "notes": ["d!"], "subject": None},
{"name": "delete", "notes": ["a!"], "subject": None},
{"name": "delete", "notes": {"c!", "e!", "f!"}, "subject": None},
])
await self.assert_query_result( | test_edgeql_triggers_mixed_all_01 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_mixed_all_02(self):
# Install FOR ALL triggers for everything
await self.con.execute('''
alter type InsertTest {
create trigger log_new after insert, update for all do (
insert Note { name := "new", notes := __new__.name }
);
create trigger log_old after delete, update for all do (
insert Note { name := "old", notes := __old__.name }
);
create trigger log_all after delete, update, insert for all do (
insert Note { name := "all", notes := "." }
);
};
''')
await self.do_basic_work()
res = tb.bag([
{"name": "all", "notes": {"."}},
{"name": "all", "notes": {"."}},
{"name": "all", "notes": {"."}},
{"name": "all", "notes": {"."}},
{"name": "all", "notes": {"."}},
{"name": "all", "notes": {"."}},
{"name": "all", "notes": {"."}},
{"name": "all", "notes": {"."}},
{"name": "new", "notes": {"a"}},
{"name": "new", "notes": {"c!", "e!"}},
{"name": "new", "notes": {"d!"}},
{"name": "new", "notes": {"e", "f"}},
{"name": "new", "notes": {"f!"}},
{"name": "new", "notes": {"b", "a!"}},
{"name": "new", "notes": {"b!", "d", "c"}},
{"name": "old", "notes": {"f", "d!"}},
{"name": "old", "notes": {"a"}},
{"name": "old", "notes": {"b", "a!"}},
{"name": "old", "notes": {"b!"}},
{"name": "old", "notes": {"d"}},
{"name": "old", "notes": {"c", "e"}},
{"name": "old", "notes": {"c!", "e!", "f!"}},
])
await self.assert_query_result(
'''
select Note { name, notes } order by .name
''',
res,
) | )
await self.do_basic_work()
res = tb.bag([
{"name": "all", "notes": {"."}},
{"name": "all", "notes": {"."}},
{"name": "all", "notes": {"."}},
{"name": "all", "notes": {"."}},
{"name": "all", "notes": {"."}},
{"name": "all", "notes": {"."}},
{"name": "all", "notes": {"."}},
{"name": "all", "notes": {"."}},
{"name": "new", "notes": {"a"}},
{"name": "new", "notes": {"c!", "e!"}},
{"name": "new", "notes": {"d!"}},
{"name": "new", "notes": {"e", "f"}},
{"name": "new", "notes": {"f!"}},
{"name": "new", "notes": {"b", "a!"}},
{"name": "new", "notes": {"b!", "d", "c"}},
{"name": "old", "notes": {"f", "d!"}},
{"name": "old", "notes": {"a"}},
{"name": "old", "notes": {"b", "a!"}},
{"name": "old", "notes": {"b!"}},
{"name": "old", "notes": {"d"}},
{"name": "old", "notes": {"c", "e"}},
{"name": "old", "notes": {"c!", "e!", "f!"}},
])
await self.assert_query_result( | test_edgeql_triggers_mixed_all_02 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_enforce_errors_01(self):
# Simulate a global constraint that we can't do with constraints:
# ensure the *count* of subordinates in each InsertTest is unique
# This is woefully inefficient though.
# (A better approach would be to enforce that a _count field
# matches the count (using policies, triggers, or best of all
# rewrite rules), and then having an exclusive constraint on that.)
await self.con.execute('''
alter type InsertTest {
create trigger check_distinct after insert, update for all do (
assert_distinct(
(InsertTest { cnt := count(.subordinates) }.cnt),
message := "subordinate counts collide",
)
);
};
''')
await self.con.execute('''
insert InsertTest { name := "0" };
''')
async with self.assertRaisesRegexTx(
edgedb.ConstraintViolationError,
r"subordinate counts collide"):
await self.con.query('''
insert InsertTest { name := "1" };
''')
await self.con.query('''
insert InsertTest {
name := "1",
subordinates := (insert Subordinate { name := "a" }),
};
''')
async with self.assertRaisesRegexTx(
edgedb.ConstraintViolationError,
r"subordinate counts collide"):
await self.con.query('''
insert InsertTest {
name := "2",
subordinates := (insert Subordinate { name := "b" }),
};
''')
await self.con.query('''
insert InsertTest {
name := "2",
subordinates := {
(insert Subordinate { name := "b" }),
(insert Subordinate { name := "c" }),
}
}
''')
async with self.assertRaisesRegexTx(
edgedb.ConstraintViolationError,
r"subordinate counts collide"):
await self.con.query('''
update InsertTest filter .name = "0"
set { subordinates := (insert Subordinate { name := "d" }) }
''')
async with self.assertRaisesRegexTx(
edgedb.ConstraintViolationError,
r"subordinate counts collide"):
await self.con.query('''
update InsertTest filter .name = "0"
set { subordinates += (insert Subordinate { name := "d" }) }
''')
async with self.assertRaisesRegexTx(
edgedb.ConstraintViolationError,
r"subordinate counts collide"):
await self.con.query('''
update InsertTest filter .name = "1"
set { subordinates += (insert Subordinate { name := "d" }) }
''')
async with self.assertRaisesRegexTx(
edgedb.ConstraintViolationError,
r"subordinate counts collide"):
await self.con.query('''
update InsertTest filter .name = "1"
set { subordinates -= .subordinates }
''')
async with self.assertRaisesRegexTx(
edgedb.ConstraintViolationError,
r"subordinate counts collide"):
await self.con.query('''
update InsertTest filter .name = "1"
set { subordinates -= .subordinates }
''')
async with self.assertRaisesRegexTx(
edgedb.ConstraintViolationError,
r"subordinate counts collide"):
await self.con.query('''
update InsertTest filter .name = "2"
set { subordinates -= (select Subordinate filter .name = "b") }
''')
async with self.assertRaisesRegexTx(
edgedb.ConstraintViolationError,
r"subordinate counts collide"):
await self.con.query('''
update InsertTest filter .name = "2"
set { subordinates := (select Subordinate filter .name = "b") }
''')
async with self.assertRaisesRegexTx(
edgedb.ConstraintViolationError,
r"subordinate counts collide"):
await self.con.query('''
update InsertTest filter .name in {"1", "2"}
set { subordinates := Subordinate }
''')
await self.con.query('''
with c := (select Subordinate filter .name = "c"),
select {
(update InsertTest filter .name = "1"
set { subordinates += c }),
(update InsertTest filter .name = "2"
set { subordinates -= c }),
};
''')
await self.con.query('''
with c := (select Subordinate filter .name = "c"),
select {
(update InsertTest filter .name = "2"
set { subordinates -= c }),
(update InsertTest filter .name = "1"
set { subordinates += c }),
};
''') | )
await self.con.execute( | test_edgeql_triggers_enforce_errors_01 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_enforce_errors_02(self):
# Simulate a multi-table constraint that we can't do with constraints:
# ensure the *sum* of the val fields in subordinates is zero
# To do this we put triggers on both InsertTest for inserts and updates
# and Subordinate for updates.
await self.con.execute('''
alter type InsertTest {
create trigger check_subs after insert, update for each do (
select assert(
sum(__new__.subordinates.val) = 0,
message := "subordinate sum is not zero for "++__new__.name,
)
);
};
alter type Subordinate {
# use for all so that we semi-join deduplicate the InsertTests
# before checking
# Use a shape to drive the error check for fun (testing).
create trigger check_subs after update for all do (
(__new__.<subordinates[is InsertTest]) {
fail := assert(
sum(.subordinates.val) = 0,
message := "subordinate sum is not zero for " ++ .name,
)
}
);
};
create function sub(i: str) -> set of Subordinate using (
select Subordinate filter .name = i
);
''')
await self.con.query('''
for x in range_unpack(range(-10, 10)) union (
insert Subordinate { name := <str>x, val := x }
);
''')
await self.con.query('''
insert InsertTest { name := "a" }
''')
err = lambda name: self.assertRaisesRegexTx(
edgedb.InvalidValueError,
f"subordinate sum is not zero for {name}"
)
await self.con.query('''
insert InsertTest {
name := "b",
subordinates := assert_distinct(sub({"1", "2", "-3"}))
}
''')
async with err("c"):
await self.con.query('''
insert InsertTest {
name := "c", subordinates := assert_distinct(sub("1"))
}
''')
async with err("c"):
await self.con.query('''
insert InsertTest {
name := "c",
subordinates := assert_distinct(sub({"1", "-2"})),
}
''')
# Try some updates
async with err("b"):
await self.con.query('''
update InsertTest filter .name = "b" set {
subordinates := assert_distinct(sub({"1", "2", "3"}))
}
''')
async with err("b"):
await self.con.query('''
update InsertTest filter .name = "b" set {
subordinates += assert_distinct(sub({"4", "-2"}))
}
''')
async with err("b"):
await self.con.query('''
update InsertTest filter .name = "b" set {
subordinates -= assert_distinct(sub({"2"}))
}
''')
await self.con.query('''
update InsertTest filter .name = "b" set {
subordinates += assert_distinct(sub({"-4", "-2", "6", "2"}))
}
''')
await self.con.query('''
update InsertTest filter .name = "b" set {
subordinates -= assert_distinct(
sub({"1", "6", "-4", "-3", "5"}))
}
''')
await self.con.query('''
update InsertTest filter .name = "b" set {
subordinates := assert_distinct(sub({"-1", "-2", "3"}))
};
''')
# Now try updating Subordinate
async with err("b"):
await self.con.query('''
update Subordinate filter .name = "3"
set { val := -3 };
''')
await self.con.query('''
insert InsertTest {
name := "b2",
subordinates := assert_distinct(sub({"-1", "-2", "3"}))
}
''')
async with err("b"):
await self.con.query('''
update Subordinate filter .name = "3"
set { val := -3 };
''')
# This one *should* work, though, since the sums still work out
await self.con.query('''
update Subordinate filter .name in {"-1", "-2", "3"}
set { val := - .val };
''')
# ... and set it back
await self.con.query('''
update Subordinate filter .name in {"-1", "-2", "3"}
set { val := - .val };
''')
# Now create a new InsertTest that uses one of those vals
await self.con.query('''
insert InsertTest {
name := "d",
subordinates := assert_distinct(sub({"3", "-3"}))
}
''')
# And now it *shouldn't* work
async with err("d"):
await self.con.query('''
update Subordinate filter .name in {"-1", "-2", "3"}
set { val := - .val };
''')
# Make sure they fire with typename injection on
async with err("a"):
await self.con._fetchall('''
update InsertTest filter .name = "a"
set { subordinates := assert_distinct(sub("1")) };
''', __typenames__=True)
async with err("c"):
await self.con._fetchall('''
insert InsertTest {
name := "c", subordinates := assert_distinct(sub("1"))
}
''', __typenames__=True) | )
await self.con.query( | test_edgeql_triggers_enforce_errors_02 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_policies_01(self):
# It is OK to see the newly created object during a trigger,
# even if you shouldn't otherwise. (Much like with overlays
# normally.)
await self.con.execute('''
alter type InsertTest {
create access policy ins_ok allow insert;
create trigger log after insert for each do (
insert Note { name := "insert", note := __new__.name }
);
};
''')
await self.con.query('''
insert InsertTest {
name := "x",
}
''')
await self.assert_query_result(
'''
select Note { note }
''',
[{'note': "x"}],
) | )
await self.con.query( | test_edgeql_triggers_policies_01 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_policies_02(self):
# But you *can't* see things by accessing them through the
# "normal channels"
await self.con.execute('''
alter type InsertTest {
create access policy ins_ok allow insert;
create trigger log after insert for each do (
insert Note {
name := "insert", note := <str>count(InsertTest)
}
);
};
''')
await self.con.query('''
insert InsertTest {name := "x"};
insert InsertTest {name := "y"};
''')
await self.assert_query_result(
'''
select Note { note }
''',
tb.bag([
{'note': "0"},
{'note': "0"},
]),
) | )
await self.con.query( | test_edgeql_triggers_policies_02 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_policies_03(self):
await self.con.execute('''
alter type Note {
create access policy ok allow all;
create access policy no_x deny insert using (
(.note like 'x%') ?? false);
};
alter type InsertTest {
create trigger log after insert for each do (
insert Note { name := "insert", note := __new__.name }
);
};
''')
await self.con.query('''
insert InsertTest {name := "y"};
''')
async with self.assertRaisesRegexTx(edgedb.AccessPolicyError, ''):
await self.con.query('''
insert InsertTest {name := "x"};
''') | )
await self.con.query( | test_edgeql_triggers_policies_03 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_policies_04(self):
await self.con.execute('''
alter type InsertTest {
create access policy ok allow all;
create access policy no_x deny select
using (.name = 'xx');
create trigger log after update for all do (
insert Note {
name := "update",
note := <str>count(__old__)++"/"++<str>count(__new__)++"/"
++<str>count(InsertTest),
}
);
};
''')
await self.con.query('''
insert InsertTest {name := "x"};
insert InsertTest {name := "y"};
''')
await self.con.query('''
update InsertTest set {name := .name ++ "x"};
''')
await self.assert_query_result(
'''
select Note { note }
''',
tb.bag([
{'note': "2/2/1"},
]),
) | )
await self.con.query( | test_edgeql_triggers_policies_04 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_policies_05(self):
await self.con.execute('''
alter type Subordinate {
create access policy ok allow all;
create access policy no deny select using (
any(.<subordinates[is InsertTest].l2 < 0)
);
};
insert Subordinate { name := "foo" };
insert Subordinate { name := "bar" };
insert InsertTest { name := "x", subordinates := Subordinate };
alter type InsertTest {
create trigger log after update for each do (
insert Note {
name := "update",
note := <str>count(__old__.subordinates)++
"/"++<str>count(__new__.subordinates)++
"/"++<str>count(InsertTest.subordinates),
}
);
};
''')
await self.con.execute('''
update InsertTest set { l2 := -1 };
''')
await self.assert_query_result(
'''
select Note { note }
''',
[{'note': "2/2/0"}],
) | )
await self.con.execute( | test_edgeql_triggers_policies_05 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_policies_06(self):
await self.con.execute('''
alter type Subordinate {
create access policy ok allow all;
create access policy no deny select;
};
alter type InsertTest {
create trigger log after insert for each do (
insert Note {
name := "insert",
note := <str>count(__new__.subordinates)
}
);
};
''')
await self.con.execute('''
insert InsertTest {
name := "!",
subordinates := {
(insert Subordinate { name := "foo" }),
(insert Subordinate { name := "bar" }),
},
};
''')
await self.assert_query_result(
'''
select Note { note }
''',
[{'note': "2"}],
) | )
await self.con.execute( | test_edgeql_triggers_policies_06 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_policies_07(self):
# Do some weird unioning + source rvar forcing in the trigger
await self.con.execute('''
alter type Subordinate {
create access policy ok allow all;
create access policy no deny select using (
any(.<subordinates[is InsertTest].l2 < 0)
);
};
insert Subordinate { name := "foo" };
insert Subordinate { name := "bar" };
insert InsertTest { name := "x", subordinates := Subordinate };
alter type InsertTest {
create trigger log after update for each do (
insert Note {
name := "update",
note := <str>count(assert_exists((select {
__old__.subordinates,
__new__.subordinates,
InsertTest.subordinates,
} filter true)).name),
}
);
};
''')
await self.con.execute('''
update InsertTest set { l2 := -1 };
''')
await self.assert_query_result(
'''
select Note { note }
''',
[{'note': "4"}],
) | )
await self.con.execute( | test_edgeql_triggers_policies_07 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_policies_08(self):
# Do some weird unioning + source rvar forcing in the trigger
await self.con.execute('''
alter type Subordinate {
create access policy ok allow all;
create access policy no deny select using (
any(.<subordinates[is InsertTest].l2 < 0)
);
create access policy no_val deny select using (
.val ?= -1
)
};
insert Subordinate { name := "foo" };
insert Subordinate { name := "bar" };
insert InsertTest { name := "x", subordinates := Subordinate };
alter type InsertTest {
create trigger log after update for each do (
insert Note {
name := "update",
note := <str>count(assert_exists((select {
(insert Subordinate { name := "lol", val := -1 }),
__old__.subordinates,
__new__.subordinates,
} filter true)).name),
}
);
};
''')
await self.con.execute('''
update InsertTest set { l2 := -1 };
''')
await self.assert_query_result(
'''
select Note { note }
''',
[{'note': "5"}],
) | )
await self.con.execute( | test_edgeql_triggers_policies_08 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_chain_01(self):
await self.con.execute('''
alter type InsertTest {
create trigger log after insert for each do (
insert Note { name := "insert", note := __new__.name }
);
};
alter type Note {
create trigger log after insert for each do (
insert Subordinate { val := 1, name := __new__.note }
);
};
''')
await self.do_basic_work()
await self.assert_notes([
{'name': "insert", 'notes': set("abcdef")},
])
await self.assert_query_result(
'''
select Subordinate.name
''',
set("abcdef"),
) | )
await self.do_basic_work()
await self.assert_notes([
{'name': "insert", 'notes': set("abcdef")},
])
await self.assert_query_result( | test_edgeql_triggers_chain_01 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_chain_03(self):
await self.con.execute('''
alter type InsertTest {
create trigger log after insert for each do (
insert Note { name := "insert", note := __new__.name }
);
};
alter type Note {
create trigger log after insert for each do (
insert Subordinate { val := 1, name := __new__.note }
);
};
''')
async with self.assertRaisesRegexTx(
edgedb.QueryError,
'would need to be executed in multiple stages on default::Note '
'after insert'):
await self.con.execute('''
select {
(insert InsertTest { name := "foo" }),
(insert Note { name := "foo" }),
}
''') | )
async with self.assertRaisesRegexTx(
edgedb.QueryError,
'would need to be executed in multiple stages on default::Note '
'after insert'):
await self.con.execute( | test_edgeql_triggers_chain_03 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_chain_04(self):
await self.con.execute('''
alter type InsertTest {
create trigger log after update for each do (
insert InsertTest { name := __new__.name ++ "!" }
);
};
''')
await self.con.execute('''
insert InsertTest { name := "test" }
''')
await self.con.execute('''
update InsertTest
filter InsertTest.name = "test"
set { name := "updated" }
''')
await self.assert_query_result(
'''
select InsertTest { name }
''',
[{'name': 'updated'}, {'name': 'updated!'}],
) | )
await self.con.execute( | test_edgeql_triggers_chain_04 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_chain_05(self):
await self.con.execute('''
alter type InsertTest {
create trigger log after insert for each do (
insert Note { name := "insert", note := __new__.name }
);
};
''')
await self.con.execute('''
select {
(insert Note { name := "foo" }),
}
''')
await self.assert_notes([
{'name': "foo", 'notes': set()},
])
await self.con.execute('''
select {
(insert InsertTest { name := "foo_insert" }),
(
update Note
filter Note.name = "foo"
set { name := "foo_update" }
),
}
''')
await self.assert_notes([
{'name': "foo_update", 'notes': set()},
{'name': "insert", 'notes': set(["foo_insert"])},
]) | )
await self.con.execute( | test_edgeql_triggers_chain_05 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_tricky_01(self):
await self.con.execute('''
alter type InsertTest {
create trigger log after insert for each do (
with X := (insert Note{ name := "x", subject := __new__.sub }),
insert Note { name := "y", note := <str>count(X.subject) }
);
};
''')
await self.con.execute('''
insert InsertTest {
name := "test", sub := (insert Subordinate { name := "!" })
}
''')
await self.assert_query_result(
'''
select Note { name, note, subject } order by .name
''',
[
{'name': 'x', 'note': None, 'subject': {'id': str}},
{'name': 'y', 'note': "1", 'subject': None},
]
) | )
await self.con.execute( | test_edgeql_triggers_tricky_01 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_old_link_01(self):
await self.con.execute('''
alter type InsertTest {
create trigger log_upd after update for each do (
insert Note { name := "upd", note := __old__.__type__.name }
);
create trigger log_del after delete for each do (
insert Note { name := "del", note := __old__.__type__.name }
);
};
''')
await self.con.execute('''
insert InsertTest {
name := "test",
};
update InsertTest set {};
delete InsertTest;
''')
await self.assert_query_result(
'''
select Note { name, note } order by .name
''',
[
{'name': 'del', 'note': "default::InsertTest"},
{'name': 'upd', 'note': "default::InsertTest"},
]
) | )
await self.con.execute( | test_edgeql_triggers_old_link_01 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_old_link_02(self):
await self.con.execute('''
alter type InsertTest {
create trigger log_upd after update for each do (
insert Note {
name := "upd", note := <str>count(__old__.subordinates) }
);
create trigger log_del after delete for each do (
insert Note {
name := "del", note := <str>count(__old__.subordinates) }
);
};
''')
await self.con.execute('''
insert InsertTest {
name := "test",
subordinates := (insert Subordinate { name := "foo" }),
};
update InsertTest set {};
delete InsertTest;
''')
await self.assert_query_result(
'''
select Note { name, note } order by .name
''',
[
{'name': 'del', 'note': "1"},
{'name': 'upd', 'note': "1"},
]
) | )
await self.con.execute( | test_edgeql_triggers_old_link_02 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_when_01(self):
await self.con.execute('''
alter type InsertTest {
create trigger log_new after insert, update for each
when (__new__.name not in {'a', 'f!'})
do (
insert Note { name := "new", note := __new__.name }
);
};
''')
await self.do_basic_work()
await self.assert_query_result(
'''
select Note.note
''',
tb.bag(['a!', 'b', 'b!', 'c', 'c!', 'd', 'd!', 'e', 'e!', 'f']),
) | )
await self.do_basic_work()
await self.assert_query_result( | test_edgeql_triggers_when_01 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_when_02(self):
await self.con.execute('''
alter type InsertTest {
create trigger log_new after insert, update for each
when (__new__.name = {'a', 'f!'})
do (
insert Note { name := "new", note := __new__.name }
);
};
''')
await self.do_basic_work()
await self.assert_query_result(
'''
select Note.note
''',
tb.bag(['a', 'f!']),
) | )
await self.do_basic_work()
await self.assert_query_result( | test_edgeql_triggers_when_02 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_when_03(self):
# Install FOR ALL triggers for everything
await self.con.execute('''
alter type InsertTest {
create trigger log_new after insert, update for all
when (count(__new__) >= 2)
do (
insert Note { name := "new", notes := __new__.name }
);
create trigger log_old after delete, update for all
when (count(__old__) >= 2)
do (
insert Note { name := "old", notes := __old__.name }
);
};
''')
await self.do_basic_work()
res = tb.bag([
{"name": "new", "notes": {"c!", "e!"}},
{"name": "new", "notes": {"e", "f"}},
{"name": "new", "notes": {"b", "a!"}},
{"name": "new", "notes": {"b!", "d", "c"}},
{"name": "old", "notes": {"f", "d!"}},
{"name": "old", "notes": {"b", "a!"}},
{"name": "old", "notes": {"c", "e"}},
{"name": "old", "notes": {"c!", "e!", "f!"}},
])
await self.assert_query_result(
'''
select Note { name, notes } order by .name
''',
res,
) | )
await self.do_basic_work()
res = tb.bag([
{"name": "new", "notes": {"c!", "e!"}},
{"name": "new", "notes": {"e", "f"}},
{"name": "new", "notes": {"b", "a!"}},
{"name": "new", "notes": {"b!", "d", "c"}},
{"name": "old", "notes": {"f", "d!"}},
{"name": "old", "notes": {"b", "a!"}},
{"name": "old", "notes": {"c", "e"}},
{"name": "old", "notes": {"c!", "e!", "f!"}},
])
await self.assert_query_result( | test_edgeql_triggers_when_03 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_when_04(self):
await self.con.execute('''
alter type InsertTest {
create trigger log_new after insert, update for each
when (__new__.l2 < 0)
do (
insert Note { name := "new", note := __new__.name }
);
};
''')
await self.con.execute('''
insert InsertTest { name := "a" };
''')
await self.con.execute('''
insert InsertTest { name := "b", l2 := 10 };
''')
await self.con.execute('''
insert InsertTest { name := "c", l2 := -42 };
''')
await self.assert_query_result(
'''
select Note.note
''',
['c'],
) | )
await self.con.execute( | test_edgeql_triggers_when_04 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_triggers_cached_global_01(self):
# Install FOR ALL triggers for everything
await self.con.execute('''
create alias CA := count(InsertTest);
create global CG := count(InsertTest);
create type X {
create access policy asdf allow all using (global CG > 0)
};
alter type InsertTest {
create trigger log after insert for each
do (
insert Note {
name := <str>assert_single(CA),
note := <str>(global CG),
}
);
};
''')
await self.con.execute('''
insert InsertTest { name := <str>((global CG)) };
''')
await self.assert_query_result(
'''
select Note { name, note }
''',
[
{'name': '1', 'note': '1'},
],
) | )
await self.con.execute( | test_edgeql_triggers_cached_global_01 | python | geldata/gel | tests/test_edgeql_triggers.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_triggers.py | Apache-2.0 |
async def test_edgeql_igroup_result_alias_01(self):
await self.assert_query_result(
r'''
FOR GROUP Issue
USING _ := Issue.time_estimate
BY _
IN Issue
UNION _ := (
count := count(Issue.status.id),
te := array_agg(DISTINCT Issue.time_estimate > 0),
) ORDER BY _.te;
''',
[{'count': 2, 'te': []}, {'count': 1, 'te': [True]}]
)
await self.assert_query_result(
r'''
FOR GROUP Issue
USING _ := Issue.time_estimate
BY _
IN Issue
UNION _ := (
count := count(Issue.status.id),
te := array_agg(DISTINCT Issue.time_estimate > 0),
) ORDER BY _.te DESC;
''',
[{'count': 1, 'te': [True]}, {'count': 2, 'te': []}],
) | ,
[{'count': 2, 'te': []}, {'count': 1, 'te': [True]}]
)
await self.assert_query_result(
r | test_edgeql_igroup_result_alias_01 | python | geldata/gel | tests/test_edgeql_internal_group.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_internal_group.py | Apache-2.0 |
def test_model_group_00(self):
res = bag([
{
"el": "Air",
"cards": bag([
{"name": "Djinn"},
{"name": "Giant eagle"},
{"name": "Sprite"},
]),
},
{
"el": "Earth",
"cards": bag([{"name": "Dwarf"}, {"name": "Golem"}]),
},
{"el": "Fire", "cards": bag([
{"name": "Dragon"}, {"name": "Imp"}])},
{
"el": "Water",
"cards": bag([
{"name": "Bog monster"},
{"name": "Giant turtle"},
]),
},
])
# Two implementations of the same query
self.assert_test_query(
"""
FOR g in (GROUP Card {name} BY .element)
UNION g {
el := .key.element,
cards := .elements {name}
}
""",
res,
)
self.assert_test_query(
"""
SELECT (GROUP Card {name} BY .element) {
el := .key.element,
cards := (SELECT .elements {name} ORDER BY .name)
}
""",
res,
) | FOR g in (GROUP Card {name} BY .element)
UNION g {
el := .key.element,
cards := .elements {name}
}
""",
res,
)
self.assert_test_query( | test_model_group_00 | python | geldata/gel | tests/test_eval_model_group.py | https://github.com/geldata/gel/blob/master/tests/test_eval_model_group.py | Apache-2.0 |
def test_model_group_01(self):
res = [
{
"el": "Air",
"cards": [
{"name": "Djinn"},
{"name": "Giant eagle"},
{"name": "Sprite"},
],
},
{
"el": "Earth",
"cards": [{"name": "Dwarf"}, {"name": "Golem"}],
},
{"el": "Fire", "cards": [{"name": "Dragon"}, {"name": "Imp"}]},
{
"el": "Water",
"cards": [
{"name": "Bog monster"},
{"name": "Giant turtle"},
],
},
]
# Two implementations of the same query
self.assert_test_query(
"""
SELECT (
FOR g in (GROUP Card {name} BY .element)
UNION g {
el := .key.element,
cards := (SELECT .elements {name} ORDER BY .name)
}) ORDER BY .el
""",
res,
)
self.assert_test_query(
"""
SELECT (GROUP Card {name} BY .element) {
el := .key.element,
cards := (SELECT .elements {name} ORDER BY .name)
} ORDER BY .el
""",
res,
) | SELECT (
FOR g in (GROUP Card {name} BY .element)
UNION g {
el := .key.element,
cards := (SELECT .elements {name} ORDER BY .name)
}) ORDER BY .el
""",
res,
)
self.assert_test_query( | test_model_group_01 | python | geldata/gel | tests/test_eval_model_group.py | https://github.com/geldata/gel/blob/master/tests/test_eval_model_group.py | Apache-2.0 |
def test_model_group_02(self):
res = [
{"el": "Air", "avg_cost": 2.3333333333333335},
{"el": "Earth", "avg_cost": 2.0},
{"el": "Fire", "avg_cost": 3.0},
{"el": "Water", "avg_cost": 2.5},
]
# Two implementations of the same query
self.assert_test_query(
"""
SELECT (
FOR g in (GROUP Card {name} BY .element) UNION g {
el := .key.element,
avg_cost := sum(.elements.cost) / count(.elements)
}
) ORDER BY .el
""",
res,
)
self.assert_test_query(
"""
SELECT (GROUP Card {name} BY .element) {
el := .key.element,
avg_cost := sum(.elements.cost) / count(.elements)
} ORDER BY .el
""",
res,
) | SELECT (
FOR g in (GROUP Card {name} BY .element) UNION g {
el := .key.element,
avg_cost := sum(.elements.cost) / count(.elements)
}
) ORDER BY .el
""",
res,
)
self.assert_test_query( | test_model_group_02 | python | geldata/gel | tests/test_eval_model_group.py | https://github.com/geldata/gel/blob/master/tests/test_eval_model_group.py | Apache-2.0 |
async def test_edgeql_update_simple_01(self):
await self.assert_query_result(
r"""
UPDATE UpdateTest
# bad name doesn't exist, so no update is expected
FILTER .name = 'bad name'
SET {
status := (SELECT Status FILTER Status.name = 'Closed')
};
""",
[]
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
id,
name,
comment,
status: {
name
}
} ORDER BY .name;
""",
self.original,
) | ,
[]
)
await self.assert_query_result(
r | test_edgeql_update_simple_01 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_simple_02(self):
orig1, orig2, orig3 = self.original
await self.assert_query_result(
r"""
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
name := 'update-test1-updated',
status := (SELECT Status FILTER Status.name = 'Closed')
};
""",
[{}]
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
id,
name,
comment,
status: {
name
}
} ORDER BY .name;
""",
[
{
'id': orig1['id'],
'name': 'update-test1-updated',
'status': {
'name': 'Closed'
}
},
orig2,
orig3,
]
) | ,
[{}]
)
await self.assert_query_result(
r | test_edgeql_update_simple_02 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_simple_03(self):
orig1, orig2, orig3 = self.original
await self.assert_query_result(
r"""
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test2'
SET {
comment := 'updated ' ++ UpdateTest.comment
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
id,
name,
comment,
} ORDER BY .name;
""",
[
{
'id': orig1['id'],
'name': orig1['name'],
'comment': orig1['comment'],
}, {
'id': orig2['id'],
'name': 'update-test2',
'comment': 'updated second',
}, {
'id': orig3['id'],
'name': orig3['name'],
'comment': orig3['comment'],
},
]
) | ,
[{}],
)
await self.assert_query_result(
r | test_edgeql_update_simple_03 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_simple_04(self):
orig1, orig2, orig3 = self.original
await self.assert_query_result(
r"""
UPDATE UpdateTest
SET {
comment := UpdateTest.comment ++ "!",
status := (SELECT Status FILTER Status.name = 'Closed')
};
""",
[{}, {}, {}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
id,
name,
comment,
status: {
name
}
} ORDER BY .name;
""",
[
{
'id': orig1['id'],
'name': 'update-test1',
'comment': None,
'status': {
'name': 'Closed'
}
}, {
'id': orig2['id'],
'name': 'update-test2',
'comment': 'second!',
'status': {
'name': 'Closed'
}
}, {
'id': orig3['id'],
'name': 'update-test3',
'comment': 'third!',
'status': {
'name': 'Closed'
}
},
]
) | ,
[{}, {}, {}],
)
await self.assert_query_result(
r | test_edgeql_update_simple_04 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_simple_05(self):
orig1, orig2, orig3 = self.original
await self.assert_query_result(
r"""
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
name := 'update-test1-updated',
status := assert_single((
SELECT
Status
FILTER
.name = 'Closed' or .name = 'Foo'
))
};
""",
[{}]
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
id,
name,
comment,
status: {
name
}
} ORDER BY .name;
""",
[
{
'id': orig1['id'],
'name': 'update-test1-updated',
'status': {
'name': 'Closed'
}
},
orig2,
orig3,
]
) | ,
[{}]
)
await self.assert_query_result(
r | test_edgeql_update_simple_05 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_returning_05(self):
# test that plain INSERT and UPDATE return objects they have
# manipulated
try:
data = []
data.append(await self.con.query_single(r"""
INSERT UpdateTest {
name := 'ret5.1'
};
"""))
data.append(await self.con.query_single(r"""
INSERT UpdateTest {
name := 'ret5.2'
};
"""))
data = [str(o.id) for o in data]
await self.assert_query_result(
r"""
SELECT UpdateTest {
id,
name
}
FILTER .name LIKE '%ret5._'
ORDER BY .name;
""",
[
{
'id': data[0],
'name': 'ret5.1',
},
{
'id': data[1],
'name': 'ret5.2',
}
],
)
await self.assert_query_result(
r"""
UPDATE UpdateTest
FILTER UpdateTest.name LIKE '%ret5._'
SET {
name := 'new ' ++ UpdateTest.name
};
""",
[{'id': data_id} for data_id in sorted(data)],
sort=lambda x: x['id']
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
id,
name
}
FILTER .name LIKE '%ret5._'
ORDER BY .name;
""",
[
{
'id': data[0],
'name': 'new ret5.1',
},
{
'id': data[1],
'name': 'new ret5.2',
}
],
)
objs = await self.con._fetchall(
r"""
UPDATE UpdateTest
FILTER UpdateTest.name LIKE '%ret5._'
SET {
name := 'new ' ++ UpdateTest.name
};
""",
__typenames__=True,
__typeids__=True
)
self.assertTrue(hasattr(objs[0], '__tid__'))
self.assertEqual(objs[0].__tname__, 'default::UpdateTest')
finally:
await self.con.execute(r"""
DELETE (
SELECT UpdateTest
FILTER .name LIKE '%ret5._'
);
""") | ))
data.append(await self.con.query_single(r | test_edgeql_update_returning_05 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_generic_01(self):
status = await self.con.query_single(r"""
SELECT Status{id}
FILTER Status.name = 'Open'
LIMIT 1;
""")
status = str(status.id)
updated = await self.con.query(
r"""
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test3'
SET {
status := (
SELECT Status
FILTER Status.id = <uuid>$status
)
};
""",
status=status
)
self.assertGreater(len(updated), 0)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
status: {
name
}
} FILTER UpdateTest.name = 'update-test3';
""",
[
{
'name': 'update-test3',
'status': {
'name': 'Open',
},
},
]
) | )
status = str(status.id)
updated = await self.con.query(
r | test_edgeql_update_generic_01 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_filter_01(self):
await self.assert_query_result(
r"""
UPDATE (SELECT UpdateTest)
# this FILTER is trivial because UpdateTest is wrapped
# into a SET OF by SELECT
FILTER UpdateTest.name = 'update-test1'
SET {
comment := 'bad test'
};
""",
[{}, {}, {}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest.comment;
""",
['bad test'] * 3,
) | ,
[{}, {}, {}],
)
await self.assert_query_result(
r | test_edgeql_update_filter_01 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_filter_02(self):
await self.assert_query_result(
r"""
UPDATE (<UpdateTest>{} ?? UpdateTest)
# this FILTER is trivial because UpdateTest is wrapped
# into a SET OF by ??
FILTER UpdateTest.name = 'update-test1'
SET {
comment := 'bad test'
};
""",
[{}, {}, {}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest.comment;
""",
['bad test'] * 3,
) | ,
[{}, {}, {}],
)
await self.assert_query_result(
r | test_edgeql_update_filter_02 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_multiple_01(self):
await self.assert_query_result(
r"""
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
tags := (SELECT Tag)
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
tags: {
name
} ORDER BY .name
} FILTER UpdateTest.name = 'update-test1';
""",
[
{
'name': 'update-test1',
'tags': [{
'name': 'boring',
}, {
'name': 'fun',
}, {
'name': 'wow',
}],
},
]
) | ,
[{}],
)
await self.assert_query_result(
r | test_edgeql_update_multiple_01 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_multiple_02(self):
await self.assert_query_result(
r"""
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
tags := (SELECT Tag FILTER Tag.name = 'wow')
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
tags: {
name
} ORDER BY .name
} FILTER UpdateTest.name = 'update-test1';
""",
[
{
'name': 'update-test1',
'tags': [{
'name': 'wow',
}],
},
]
) | ,
[{}],
)
await self.assert_query_result(
r | test_edgeql_update_multiple_02 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_multiple_03(self):
await self.assert_query_result(
r"""
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
tags := (SELECT Tag FILTER Tag.name IN {'wow', 'fun'})
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
tags: {
name
} ORDER BY .name
} FILTER UpdateTest.name = 'update-test1';
""",
[
{
'name': 'update-test1',
'tags': [{
'name': 'fun',
}, {
'name': 'wow',
}],
},
]
) | ,
[{}],
)
await self.assert_query_result(
r | test_edgeql_update_multiple_03 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_multiple_04(self):
await self.assert_query_result(
r"""
# first add a tag to UpdateTest
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
tags := (
SELECT Tag
FILTER Tag.name = 'fun'
)
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
tags: {
name
} ORDER BY .name
} FILTER UpdateTest.name = 'update-test1';
""",
[{
'name': 'update-test1',
'tags': [{
'name': 'fun',
}],
}],
)
await self.assert_query_result(
r"""
# now add another tag, but keep the existing one, too
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
tags += (
SELECT Tag
FILTER Tag.name = 'wow'
)
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
tags: {
name
} ORDER BY .name
} FILTER UpdateTest.name = 'update-test1';
""",
[{
'name': 'update-test1',
'tags': [{
'name': 'fun',
}, {
'name': 'wow',
}],
}],
) | ,
[{}],
)
await self.assert_query_result(
r | test_edgeql_update_multiple_04 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_multiple_05(self):
await self.assert_query_result(
r"""
WITH
U2 := UpdateTest
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
related := (SELECT U2 FILTER U2.name != 'update-test1')
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
related: {
name
} ORDER BY .name
} FILTER UpdateTest.name = 'update-test1';
""",
[
{
'name': 'update-test1',
'related': [{
'name': 'update-test2',
}, {
'name': 'update-test3',
}],
},
]
) | ,
[{}],
)
await self.assert_query_result(
r | test_edgeql_update_multiple_05 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_multiple_06(self):
await self.assert_query_result(
r"""
WITH
U2 := UpdateTest
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
annotated_tests := (
SELECT U2 FILTER U2.name != 'update-test1'
)
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
annotated_tests: {
name,
@note
} ORDER BY .name
} FILTER UpdateTest.name = 'update-test1';
""",
[
{
'name': 'update-test1',
'annotated_tests': [{
'name': 'update-test2',
'@note': None,
}, {
'name': 'update-test3',
'@note': None,
}],
},
]
) | ,
[{}],
)
await self.assert_query_result(
r | test_edgeql_update_multiple_06 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_multiple_07(self):
await self.assert_query_result(
r"""
WITH
U2 := UpdateTest
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
annotated_tests := (
SELECT U2 {
@note := 'note' ++ U2.name[-1]
} FILTER U2.name != 'update-test1'
)
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
annotated_tests: {
name,
@note
} ORDER BY .name
} FILTER UpdateTest.name = 'update-test1';
""",
[
{
'name': 'update-test1',
'annotated_tests': [{
'name': 'update-test2',
'@note': 'note2',
}, {
'name': 'update-test3',
'@note': 'note3',
}],
},
]
) | ,
[{}],
)
await self.assert_query_result(
r | test_edgeql_update_multiple_07 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_multiple_08(self):
await self.con.execute("""
INSERT UpdateTest {
name := 'update-test-8-1',
};
INSERT UpdateTestSubType {
name := 'update-test-8-2',
};
INSERT UpdateTestSubSubType {
name := 'update-test-8-3',
};
""")
await self.assert_query_result(
r"""
# make tests related to the other 2
WITH
UT := (SELECT UpdateTest
FILTER .name LIKE 'update-test-8-%')
UPDATE UpdateTest
FILTER .name LIKE 'update-test-8-%'
SET {
related := (SELECT UT FILTER UT != UpdateTest)
};
""",
[
{'id': uuid.UUID},
{'id': uuid.UUID},
{'id': uuid.UUID},
],
)
await self.assert_query_result(
r"""
SELECT UpdateTest{
name,
related: {name} ORDER BY .name
}
FILTER .name LIKE 'update-test-8-%'
ORDER BY .name;
""",
[
{
'name': 'update-test-8-1',
'related': [
{'name': 'update-test-8-2'},
{'name': 'update-test-8-3'},
],
},
{
'name': 'update-test-8-2',
'related': [
{'name': 'update-test-8-1'},
{'name': 'update-test-8-3'},
],
},
{
'name': 'update-test-8-3',
'related': [
{'name': 'update-test-8-1'},
{'name': 'update-test-8-2'},
],
},
],
)
await self.assert_query_result(
r"""
# now update related tests based on existing related tests
WITH
UT := (SELECT UpdateTest
FILTER .name LIKE 'update-test-8-%')
UPDATE UpdateTest
FILTER .name LIKE 'update-test-8-%'
SET {
# since there are 2 tests in each FILTER, != is
# guaranteed to be TRUE for at least one of them
related := (SELECT UT FILTER UT != UpdateTest.related)
};
""",
[
{'id': uuid.UUID},
{'id': uuid.UUID},
{'id': uuid.UUID},
],
)
await self.assert_query_result(
r"""
SELECT UpdateTest{
name,
related: {name} ORDER BY .name
}
FILTER .name LIKE 'update-test-8-%'
ORDER BY .name;
""",
[
{
'name': 'update-test-8-1',
'related': [
{'name': 'update-test-8-1'},
{'name': 'update-test-8-2'},
{'name': 'update-test-8-3'},
],
},
{
'name': 'update-test-8-2',
'related': [
{'name': 'update-test-8-1'},
{'name': 'update-test-8-2'},
{'name': 'update-test-8-3'},
],
},
{
'name': 'update-test-8-3',
'related': [
{'name': 'update-test-8-1'},
{'name': 'update-test-8-2'},
{'name': 'update-test-8-3'},
],
},
],
) | )
await self.assert_query_result(
r | test_edgeql_update_multiple_08 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_multiple_09(self):
await self.con.execute("""
INSERT UpdateTest {
name := 'update-test-9-1',
};
INSERT UpdateTest {
name := 'update-test-9-2',
};
INSERT UpdateTest {
name := 'update-test-9-3',
};
""")
await self.assert_query_result(
r"""
# make tests related to the other 2
WITH
UT := (SELECT UpdateTest
FILTER .name LIKE 'update-test-9-%')
UPDATE UpdateTest
FILTER .name LIKE 'update-test-9-%'
SET {
related := (SELECT UT FILTER UT != UpdateTest)
};
""",
[
{'id': uuid.UUID},
{'id': uuid.UUID},
{'id': uuid.UUID},
],
)
await self.assert_query_result(
r"""
SELECT UpdateTest{
name,
related: {name} ORDER BY .name
}
FILTER .name LIKE 'update-test-9-%'
ORDER BY .name;
""",
[
{
'name': 'update-test-9-1',
'related': [
{'name': 'update-test-9-2'},
{'name': 'update-test-9-3'},
],
},
{
'name': 'update-test-9-2',
'related': [
{'name': 'update-test-9-1'},
{'name': 'update-test-9-3'},
],
},
{
'name': 'update-test-9-3',
'related': [
{'name': 'update-test-9-1'},
{'name': 'update-test-9-2'},
],
},
],
)
await self.assert_query_result(
r"""
# now update related tests based on existing related tests
WITH
UT := (SELECT UpdateTest
FILTER .name LIKE 'update-test-9-%')
UPDATE UpdateTest
FILTER .name LIKE 'update-test-9-%'
SET {
# this should make the related test be the same as parent
related := (SELECT UT FILTER UT NOT IN UpdateTest.related)
};
""",
[
{'id': uuid.UUID},
{'id': uuid.UUID},
{'id': uuid.UUID},
],
)
await self.assert_query_result(
r"""
SELECT UpdateTest{
name,
related: {name} ORDER BY .name
}
FILTER .name LIKE 'update-test-9-%'
ORDER BY .name;
""",
[
{
'name': 'update-test-9-1',
'related': [
{'name': 'update-test-9-1'},
],
},
{
'name': 'update-test-9-2',
'related': [
{'name': 'update-test-9-2'},
],
},
{
'name': 'update-test-9-3',
'related': [
{'name': 'update-test-9-3'},
],
},
],
) | )
await self.assert_query_result(
r | test_edgeql_update_multiple_09 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_multiple_10(self):
await self.con.execute("""
INSERT UpdateTest {
name := 'update-test-10-1',
};
INSERT UpdateTest {
name := 'update-test-10-2',
};
INSERT UpdateTest {
name := 'update-test-10-3',
};
""")
await self.assert_query_result(
r"""
# make each test related to 'update-test-10-1'
WITH
UT := (
SELECT UpdateTest FILTER .name = 'update-test-10-1'
)
UPDATE UpdateTest
FILTER .name LIKE 'update-test-10-%'
SET {
related := UT
};
""",
[
{'id': uuid.UUID},
{'id': uuid.UUID},
{'id': uuid.UUID},
],
)
await self.assert_query_result(
r"""
SELECT UpdateTest{
name,
related: {name} ORDER BY .name
}
FILTER .name LIKE 'update-test-10-%'
ORDER BY .name;
""",
[
{
'name': 'update-test-10-1',
'related': [
{'name': 'update-test-10-1'},
],
},
{
'name': 'update-test-10-2',
'related': [
{'name': 'update-test-10-1'},
],
},
{
'name': 'update-test-10-3',
'related': [
{'name': 'update-test-10-1'},
],
},
],
)
await self.assert_query_result(
r"""
# now update related tests
# there's only one item in the UPDATE set
UPDATE UpdateTest.related
FILTER .name LIKE 'update-test-10-%'
SET {
# every test is .<related to 'update-test1'
related := UpdateTest.related.<related[IS UpdateTest]
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest{
name,
related: {name} ORDER BY .name
}
FILTER .name LIKE 'update-test-10-%'
ORDER BY .name;
""",
[
{
'name': 'update-test-10-1',
'related': [
{'name': 'update-test-10-1'},
{'name': 'update-test-10-2'},
{'name': 'update-test-10-3'},
],
},
{
'name': 'update-test-10-2',
'related': [
{'name': 'update-test-10-1'},
],
},
{
'name': 'update-test-10-3',
'related': [
{'name': 'update-test-10-1'},
],
},
],
) | )
await self.assert_query_result(
r | test_edgeql_update_multiple_10 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_props_01(self):
await self.assert_query_result(
r"""
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
weighted_tags := (
SELECT Tag {
@weight :=
1 IF Tag.name = 'boring' ELSE
2 IF Tag.name = 'wow' ELSE
3
}
)
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
weighted_tags: {
name,
@weight
} ORDER BY @weight
} FILTER UpdateTest.name = 'update-test1';
""",
[
{
'name': 'update-test1',
'weighted_tags': [{
'name': 'boring',
'@weight': 1,
}, {
'name': 'wow',
'@weight': 2,
}, {
'name': 'fun',
'@weight': 3,
}],
},
]
) | ,
[{}],
)
await self.assert_query_result(
r | test_edgeql_update_props_01 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_props_02(self):
await self.assert_query_result(
r"""
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
weighted_tags := (
SELECT Tag {@weight := 1} FILTER Tag.name = 'wow')
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
weighted_tags: {
name,
@weight
} ORDER BY @weight
} FILTER UpdateTest.name = 'update-test1';
""",
[
{
'name': 'update-test1',
'weighted_tags': [{
'name': 'wow',
'@weight': 1,
}],
},
]
) | ,
[{}],
)
await self.assert_query_result(
r | test_edgeql_update_props_02 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_props_03(self):
await self.assert_query_result(
r"""
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
weighted_tags := (
SELECT Tag {
@weight := len(Tag.name) % 2 + 1,
@note := Tag.name ++ '!',
} FILTER Tag.name IN {'wow', 'boring'}
)
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
weighted_tags: {
name,
@weight,
@note,
} ORDER BY @weight
} FILTER UpdateTest.name = 'update-test1';
""",
[
{
'name': 'update-test1',
'weighted_tags': [{
'name': 'boring',
'@weight': 1,
'@note': 'boring!',
}, {
'name': 'wow',
'@weight': 2,
'@note': 'wow!',
}],
},
]
)
# Check that reassignment erases the link properties.
await self.assert_query_result(
r"""
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
weighted_tags := .weighted_tags {
@weight := len(.name) % 2 + 1,
},
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
weighted_tags: {
name,
@weight,
@note,
} ORDER BY @weight
} FILTER UpdateTest.name = 'update-test1';
""",
[
{
'name': 'update-test1',
'weighted_tags': [{
'name': 'boring',
'@weight': 1,
'@note': None,
}, {
'name': 'wow',
'@weight': 2,
'@note': None,
}],
},
]
) | ,
[{}],
)
await self.assert_query_result(
r | test_edgeql_update_props_03 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_props_05(self):
await self.assert_query_result(
r"""
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
annotated_status := (
SELECT Status {
@note := 'Victor'
} FILTER Status.name = 'Closed'
)
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
annotated_status: {
name,
@note
}
} FILTER UpdateTest.name = 'update-test1';
""",
[
{
'name': 'update-test1',
'annotated_status': {
'name': 'Closed',
'@note': 'Victor',
},
},
]
) | ,
[{}],
)
await self.assert_query_result(
r | test_edgeql_update_props_05 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_props_06(self):
await self.assert_query_result(
r"""
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
annotated_status := (
SELECT Status {
@note := 'Victor'
} FILTER Status = UpdateTest.status
)
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
annotated_status: {
name,
@note
}
} FILTER UpdateTest.name = 'update-test1';
""",
[
{
'name': 'update-test1',
'annotated_status': {
'name': 'Open',
'@note': 'Victor',
},
},
]
) | ,
[{}],
)
await self.assert_query_result(
r | test_edgeql_update_props_06 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_props_07(self):
await self.assert_query_result(
r"""
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
annotated_status := (
SELECT Status FILTER Status.name = 'Open'
)
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
annotated_status: {
name,
@note
}
} FILTER UpdateTest.name = 'update-test1';
""",
[
{
'name': 'update-test1',
'annotated_status': {
'name': 'Open',
'@note': None,
},
},
]
) | ,
[{}],
)
await self.assert_query_result(
r | test_edgeql_update_props_07 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_props_08(self):
await self.assert_query_result(
r"""
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
annotated_status := (
SELECT Status {
@note := 'Victor'
} FILTER Status.name = 'Open'
)
};
""",
[{}],
)
await self.assert_query_result(
r"""
# update again, erasing the 'note' value
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
annotated_status := .annotated_status {
@note := <str>{}
}
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
annotated_status: {
name,
@note
}
} FILTER UpdateTest.name = 'update-test1';
""",
[
{
'name': 'update-test1',
'annotated_status': {
'name': 'Open',
'@note': None,
},
},
]
) | ,
[{}],
)
await self.assert_query_result(
r | test_edgeql_update_props_08 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_props_09(self):
# Check that we can update a link property on a specific link.
# Setup some multi links
await self.con.execute(
r"""
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
weighted_tags := (SELECT Tag FILTER .name != 'boring')
};
""",
)
# Update the @weight for Tag 'wow'
await self.con.execute(
r"""
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
weighted_tags += (
SELECT .weighted_tags {@weight := 1}
FILTER .name = 'wow'
)
};
""",
)
# Update the @weight for Tag 'boring', which should do nothing
# because that Tag is not actually linked.
await self.con.execute(
r"""
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
weighted_tags += (
SELECT .weighted_tags {@weight := 2}
FILTER .name = 'boring'
)
};
""",
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
weighted_tags: {
name,
@weight
} ORDER BY .name
} FILTER UpdateTest.name = 'update-test1';
""",
[
{
'name': 'update-test1',
'weighted_tags': [{
'name': 'fun',
'@weight': None,
}, {
'name': 'wow',
'@weight': 1,
}],
},
]
) | ,
)
# Update the @weight for Tag 'wow'
await self.con.execute(
r | test_edgeql_update_props_09 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_props_10(self):
# Check that we can update a link property on a specific link.
# Setup some multi links on several objects
await self.con.execute(
r"""
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test1'
SET {
weighted_tags := (
SELECT Tag {@weight := 2}
FILTER .name != 'boring'
)
};
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test2'
SET {
weighted_tags := (
SELECT Tag {@weight := len(.name)}
FILTER .name != 'wow'
)
};
UPDATE UpdateTest
FILTER UpdateTest.name = 'update-test3'
SET {
weighted_tags := (
SELECT Tag {
@weight := 10,
@note := 'original'
}
FILTER .name = 'fun'
)
};
""",
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
weighted_tags: {
name,
@weight,
@note,
} ORDER BY .name
} ORDER BY .name;
""",
[
{
'name': 'update-test1',
'weighted_tags': [{
'name': 'fun',
'@weight': 2,
'@note': None,
}, {
'name': 'wow',
'@weight': 2,
'@note': None,
}],
},
{
'name': 'update-test2',
'weighted_tags': [{
'name': 'boring',
'@weight': 6,
'@note': None,
}, {
'name': 'fun',
'@weight': 3,
'@note': None,
}],
},
{
'name': 'update-test3',
'weighted_tags': [{
'name': 'fun',
'@weight': 10,
'@note': 'original',
}],
},
]
)
# Update the @weight, @note for some tags on all of the
# UpdateTest objects.
await self.con.execute(
r"""
UPDATE UpdateTest
SET {
weighted_tags += (
FOR x IN {
(
name := 'fun',
weight_adj := -1,
empty_note := 'new fun'
),
(
name := 'wow',
weight_adj := 5,
empty_note := 'new wow'
),
(
name := 'boring',
weight_adj := -2,
empty_note := 'new boring'
),
} UNION (
SELECT .weighted_tags {
@weight := @weight + x.weight_adj,
@note := @note ?? x.empty_note,
}
FILTER .name = x.name
)
)
};
""",
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
weighted_tags: {
name,
@weight,
@note,
} ORDER BY .name
} ORDER BY .name;
""",
[
{
'name': 'update-test1',
'weighted_tags': [{
'name': 'fun',
'@weight': 1,
'@note': 'new fun',
}, {
'name': 'wow',
'@weight': 7,
'@note': 'new wow',
}],
},
{
'name': 'update-test2',
'weighted_tags': [{
'name': 'boring',
'@weight': 4,
'@note': 'new boring',
}, {
'name': 'fun',
'@weight': 2,
'@note': 'new fun',
}],
},
{
'name': 'update-test3',
'weighted_tags': [{
'name': 'fun',
'@weight': 9,
'@note': 'original',
}],
},
]
) | ,
)
await self.assert_query_result(
r | test_edgeql_update_props_10 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_for_01(self):
await self.assert_query_result(
r"""
FOR x IN {
(name := 'update-test1', comment := 'foo'),
(name := 'update-test2', comment := 'bar')
}
UNION (
UPDATE UpdateTest
FILTER UpdateTest.name = x.name
SET {
comment := x.comment
}
);
""",
[{}, {}], # since updates are in FOR they return objects
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
comment
} ORDER BY UpdateTest.name;
""",
[
{
'name': 'update-test1',
'comment': 'foo'
},
{
'name': 'update-test2',
'comment': 'bar'
},
{
'name': 'update-test3',
'comment': 'third'
},
]
) | ,
[{}, {}], # since updates are in FOR they return objects
)
await self.assert_query_result(
r | test_edgeql_update_for_01 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_for_02(self):
await self.assert_query_result(
r"""
FOR x IN {
'update-test1',
'update-test2',
}
UNION (
UPDATE UpdateTest
FILTER UpdateTest.name = x
SET {
comment := x ++ "!"
}
);
""",
[{}, {}], # since updates are in FOR they return objects
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
comment
} ORDER BY UpdateTest.name;
""",
[
{
'name': 'update-test1',
'comment': 'update-test1!'
},
{
'name': 'update-test2',
'comment': 'update-test2!'
},
{
'name': 'update-test3',
'comment': 'third'
},
]
) | ,
[{}, {}], # since updates are in FOR they return objects
)
await self.assert_query_result(
r | test_edgeql_update_for_02 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_for_03(self):
await self.assert_query_result(
r"""
FOR x IN {
'update-test1',
'update-test2',
}
UNION (
UPDATE UpdateTest
FILTER UpdateTest.name = x
SET {
str_tags := x
}
);
""",
[{}, {}], # since updates are in FOR they return objects
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
str_tags,
}
FILTER
.name IN {'update-test1', 'update-test2', 'update-test3'}
ORDER BY
.name;
""",
[
{
'name': 'update-test1',
'str_tags': ['update-test1'],
},
{
'name': 'update-test2',
'str_tags': ['update-test2'],
},
{
'name': 'update-test3',
'str_tags': [],
},
]
) | ,
[{}, {}], # since updates are in FOR they return objects
)
await self.assert_query_result(
r | test_edgeql_update_for_03 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_empty_01(self):
await self.assert_query_result(
r"""
# just clear all the comments
UPDATE UpdateTest
SET {
comment := {}
};
""",
[{}, {}, {}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest.comment;
""",
[],
) | ,
[{}, {}, {}],
)
await self.assert_query_result(
r | test_edgeql_update_empty_01 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_empty_04(self):
await self.assert_query_result(
r"""
# just clear all the statuses
UPDATE UpdateTest
SET {
status := {}
};
""",
[{}, {}, {}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest.status;
""",
[],
) | ,
[{}, {}, {}],
)
await self.assert_query_result(
r | test_edgeql_update_empty_04 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_new_01(self):
# test and UPDATE with a new object
await self.assert_query_result(
r"""
UPDATE UpdateTest
FILTER .name = 'update-test1'
SET {
tags := (
INSERT Tag {
name := 'new tag'
}
)
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
tags: {
name
}
} FILTER .name = 'update-test1';
""",
[
{
'name': 'update-test1',
'tags': [{
'name': 'new tag',
}],
},
]
) | ,
[{}],
)
await self.assert_query_result(
r | test_edgeql_update_new_01 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_new_02(self):
# test and UPDATE with a new object
await self.assert_query_result(
r"""
UPDATE UpdateTest
FILTER .name = 'update-test1'
SET {
status := (
INSERT Status {
name := 'new status'
}
)
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
status: {
name
}
} FILTER .name = 'update-test1';
""",
[
{
'name': 'update-test1',
'status': {
'name': 'new status',
},
},
]
) | ,
[{}],
)
await self.assert_query_result(
r | test_edgeql_update_new_02 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_collection_01(self):
# test and UPDATE with a collection
await self.con.execute(
r"""
UPDATE CollectionTest
FILTER .name = 'collection-test1'
SET {
some_tuple := ('coll_01', 1)
};
"""
)
await self.assert_query_result(
r"""
SELECT CollectionTest {
name,
some_tuple,
} FILTER .name = 'collection-test1';
""",
[
{
'name': 'collection-test1',
'some_tuple': ['coll_01', 1],
},
]
) | )
await self.assert_query_result(
r | test_edgeql_update_collection_01 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_collection_02(self):
# test and UPDATE with a collection
await self.con.execute(
r"""
UPDATE CollectionTest
FILTER .name = 'collection-test1'
SET {
str_array := ['coll_02', '2']
};
"""
)
await self.assert_query_result(
r"""
SELECT CollectionTest {
name,
str_array,
} FILTER .name = 'collection-test1';
""",
[
{
'name': 'collection-test1',
'str_array': ['coll_02', '2'],
},
]
) | )
await self.assert_query_result(
r | test_edgeql_update_collection_02 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_append_01(self):
await self.con.execute("""
INSERT UpdateTest {
name := 'update-test-append-1',
};
INSERT UpdateTest {
name := 'update-test-append-2',
};
INSERT UpdateTest {
name := 'update-test-append-3',
};
""")
await self.con.execute("""
WITH
U2 := UpdateTest
UPDATE UpdateTest
FILTER .name = 'update-test-append-1'
SET {
annotated_tests := (
SELECT U2 FILTER .name = 'update-test-append-2'
)
};
""")
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
annotated_tests: {
name,
@note
} ORDER BY .name
} FILTER UpdateTest.name = 'update-test-append-1';
""",
[
{
'name': 'update-test-append-1',
'annotated_tests': [{
'name': 'update-test-append-2',
'@note': None,
}],
},
]
)
await self.con.execute("""
WITH
U2 := UpdateTest
UPDATE UpdateTest
FILTER .name = 'update-test-append-1'
SET {
annotated_tests += (
SELECT U2 { @note := 'foo' }
FILTER .name = 'update-test-append-3'
)
};
""")
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
annotated_tests: {
name,
@note
} ORDER BY .name
} FILTER UpdateTest.name = 'update-test-append-1';
""",
[
{
'name': 'update-test-append-1',
'annotated_tests': [{
'name': 'update-test-append-2',
'@note': None,
}, {
'name': 'update-test-append-3',
'@note': 'foo',
}],
},
]
) | )
await self.con.execute( | test_edgeql_update_append_01 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_subtract_01(self):
await self.con.execute("""
INSERT UpdateTest {
name := 'update-test-subtract-1',
};
INSERT UpdateTest {
name := 'update-test-subtract-2',
};
INSERT UpdateTest {
name := 'update-test-subtract-3',
};
""")
await self.con.execute("""
WITH
U2 := UpdateTest
UPDATE UpdateTest
FILTER .name = 'update-test-subtract-1'
SET {
annotated_tests := DISTINCT(
FOR v IN {
('update-test-subtract-2', 'one'),
('update-test-subtract-3', 'two'),
}
UNION (
SELECT U2 {
@note := v.1,
} FILTER .name = v.0
)
)
};
""")
await self.con.execute("""
WITH
U2 := UpdateTest
UPDATE UpdateTest
FILTER .name = 'update-test-subtract-3'
SET {
annotated_tests := (
FOR v IN {
('update-test-subtract-2', 'one'),
}
UNION (
SELECT U2 {
@note := v.1,
} FILTER .name = v.0
)
)
};
""")
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
annotated_tests: {
name,
@note
} ORDER BY .name
} FILTER
.name LIKE 'update-test-subtract-%'
ORDER BY .name;
""",
[
{
'name': 'update-test-subtract-1',
'annotated_tests': [{
'name': 'update-test-subtract-2',
'@note': 'one',
}, {
'name': 'update-test-subtract-3',
'@note': 'two',
}],
},
{
'name': 'update-test-subtract-2',
'annotated_tests': [],
},
{
'name': 'update-test-subtract-3',
'annotated_tests': [{
'name': 'update-test-subtract-2',
'@note': 'one',
}],
},
]
)
await self.con.execute("""
WITH
U2 := UpdateTest
UPDATE UpdateTest
FILTER .name = 'update-test-subtract-1'
SET {
annotated_tests -= (
SELECT U2
FILTER .name = 'update-test-subtract-2'
)
};
""")
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
annotated_tests: {
name,
@note
} ORDER BY .name
} FILTER
.name LIKE 'update-test-subtract-%'
ORDER BY .name;
""",
[
{
'name': 'update-test-subtract-1',
'annotated_tests': [{
'name': 'update-test-subtract-3',
'@note': 'two',
}],
},
{
'name': 'update-test-subtract-2',
'annotated_tests': [],
},
{
'name': 'update-test-subtract-3',
'annotated_tests': [{
'name': 'update-test-subtract-2',
'@note': 'one',
}],
},
]
) | )
await self.con.execute( | test_edgeql_update_subtract_01 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_subtract_02(self):
await self.con.execute("""
INSERT UpdateTest {
name := 'update-test-subtract-various',
annotated_status := (
SELECT Status {
@note := 'forever',
} FILTER .name = 'Closed'
),
comment := 'to remove',
str_tags := {'1', '2', '3'},
};
""")
await self.assert_query_result(
r"""
SELECT UpdateTest {
annotated_status: {
name,
@note
},
comment,
str_tags ORDER BY UpdateTest.str_tags
} FILTER
.name = 'update-test-subtract-various';
""",
[
{
'annotated_status': {
'name': 'Closed',
'@note': 'forever',
},
'comment': 'to remove',
'str_tags': ['1', '2', '3'],
},
],
)
# Check that singleton links work.
await self.con.execute("""
UPDATE UpdateTest
FILTER .name = 'update-test-subtract-various'
SET {
annotated_status -= (SELECT Status FILTER .name = 'Closed')
};
""")
await self.assert_query_result(
r"""
SELECT UpdateTest {
annotated_status: {
name,
@note
},
} FILTER
.name = 'update-test-subtract-various';
""",
[
{
'annotated_status': None,
},
],
)
# And singleton properties too.
await self.con.execute("""
UPDATE UpdateTest
FILTER .name = 'update-test-subtract-various'
SET {
comment -= 'to remove'
};
""")
await self.assert_query_result(
r"""
SELECT UpdateTest {
comment,
} FILTER
.name = 'update-test-subtract-various';
""",
[
{
'comment': None,
},
],
)
# And multi properties as well.
await self.con.execute("""
UPDATE UpdateTest
FILTER .name = 'update-test-subtract-various'
SET {
str_tags -= '2'
};
""")
await self.assert_query_result(
r"""
SELECT UpdateTest {
str_tags,
} FILTER
.name = 'update-test-subtract-various';
""",
[
{
'str_tags': {'1', '3'},
},
],
) | )
await self.assert_query_result(
r | test_edgeql_update_subtract_02 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_subtract_non_distinct(self):
await self.con.execute("""
INSERT UpdateTest {
name := 'update-test-subtract-non-distinct-1',
str_tags := {'1', '1', '2', '3', '2', '2', '3', '4'},
};
INSERT UpdateTest {
name := 'update-test-subtract-non-distinct-2',
str_tags := {'1', '2', '2', '3', '4', '5'},
};
""")
await self.assert_query_result(
r"""
SELECT
UpdateTest {
str_tags ORDER BY UpdateTest.str_tags
}
FILTER
.name LIKE 'update-test-subtract-non-distinct-%'
ORDER BY
.name
""",
[
{
'str_tags': ['1', '1', '2', '2', '2', '3', '3', '4'],
},
{
'str_tags': ['1', '2', '2', '3', '4', '5'],
},
],
)
await self.con.execute("""
UPDATE UpdateTest
FILTER .name LIKE 'update-test-subtract-non-distinct-%'
SET {
str_tags -= {'1', '2', '2', '3', '5'}
};
""")
await self.assert_query_result(
r"""
SELECT
UpdateTest {
str_tags ORDER BY UpdateTest.str_tags,
}
FILTER
.name LIKE 'update-test-subtract-non-distinct-%'
ORDER BY
.name
""",
[
{
'str_tags': {'1', '2', '3', '4'},
},
{
'str_tags': {'4'},
},
],
)
await self.con.execute("""
UPDATE UpdateTest
FILTER .name LIKE 'update-test-subtract-non-distinct-%'
SET {
str_tags -= <str>{}
};
""")
await self.assert_query_result(
r"""
SELECT
UpdateTest {
str_tags ORDER BY UpdateTest.str_tags,
}
FILTER
.name LIKE 'update-test-subtract-non-distinct-%'
ORDER BY
.name
""",
[
{
'str_tags': {'1', '2', '3', '4'},
},
{
'str_tags': {'4'},
},
],
)
await self.con.execute("""
UPDATE UpdateTest
FILTER .name LIKE 'update-test-subtract-non-distinct-%'
SET {
str_tags -= {'10'}
};
""")
await self.assert_query_result(
r"""
SELECT
UpdateTest {
str_tags ORDER BY UpdateTest.str_tags,
}
FILTER
.name LIKE 'update-test-subtract-non-distinct-%'
ORDER BY
.name
""",
[
{
'str_tags': {'1', '2', '3', '4'},
},
{
'str_tags': {'4'},
},
],
) | )
await self.assert_query_result(
r | test_edgeql_update_subtract_non_distinct | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_subtract_required(self):
await self.con.execute("""
INSERT MultiRequiredTest {
name := 'update-test-subtract-required',
prop := {'one', 'two'},
tags := (SELECT Tag FILTER .name IN {'fun', 'wow'}),
};
""")
await self.con.execute("""
UPDATE MultiRequiredTest
FILTER .name = 'update-test-subtract-required'
SET {
prop -= 'one'
};
""")
await self.assert_query_result(
r"""
SELECT MultiRequiredTest {
prop,
} FILTER
.name = 'update-test-subtract-required';
""",
[
{
'prop': {'two'},
},
],
)
async with self.assertRaisesRegexTx(
edgedb.MissingRequiredError,
r"missing value for required property 'prop'",
):
await self.con.execute("""
UPDATE MultiRequiredTest
FILTER .name = 'update-test-subtract-required'
SET {
prop -= 'two'
};
""")
await self.con.execute("""
UPDATE MultiRequiredTest
FILTER .name = 'update-test-subtract-required'
SET {
tags -= (SELECT Tag FILTER .name = 'fun')
};
""")
await self.assert_query_result(
r"""
SELECT MultiRequiredTest {
tags: {name}
} FILTER
.name = 'update-test-subtract-required';
""",
[
{
'tags': [{'name': 'wow'}],
},
],
)
async with self.assertRaisesRegexTx(
edgedb.MissingRequiredError,
r"missing value for required link 'tags'",
):
await self.con.execute("""
UPDATE MultiRequiredTest
FILTER .name = 'update-test-subtract-required'
SET {
tags -= (SELECT Tag FILTER .name = 'wow')
};
""")
async with self.assertRaisesRegexTx(
edgedb.MissingRequiredError,
r"missing value for required link 'tags'",
):
await self.con.execute("""
SELECT (
UPDATE MultiRequiredTest
FILTER .name = 'update-test-subtract-required'
SET {
tags -= (SELECT Tag FILTER .name = 'wow')
}
) FILTER false;
""") | )
await self.con.execute( | test_edgeql_update_subtract_required | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_insert_01(self):
await self.con.execute('''
UPDATE UpdateTest SET {
tags := (INSERT Tag { name := <str>random() })
};
''')
await self.assert_query_result(
r"""
SELECT count(UpdateTest.tags);
""",
[3],
) | )
await self.assert_query_result(
r | test_edgeql_update_insert_01 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_insert_02(self):
await self.con.execute('''
UPDATE UpdateTest SET {
status := (INSERT Status { name := <str>random() })
};
''')
await self.assert_query_result(
r"""
SELECT count(UpdateTest.status);
""",
[3],
) | )
await self.assert_query_result(
r | test_edgeql_update_insert_02 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_with_self_insert_01(self):
await self.con.execute('''
WITH new_test := (INSERT UpdateTest { name := "new-test" })
UPDATE new_test
SET {
related := (new_test)
};
''')
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
related: {
name
}
}
FILTER .name = "new-test";
""",
[
{
'name': 'new-test',
'related': [{
'name': 'new-test'
}]
},
],
) | )
await self.assert_query_result(
r | test_edgeql_update_with_self_insert_01 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_inheritance_01(self):
await self.con.execute('''
INSERT UpdateTest {
name := 'update-test-inh-supertype-1',
related := (
SELECT (DETACHED UpdateTest)
FILTER .name = 'update-test1'
)
};
INSERT UpdateTestSubType {
name := 'update-test-inh-subtype-1',
related := (
SELECT (DETACHED UpdateTest)
FILTER .name = 'update-test1'
)
};
''')
await self.assert_query_result(
r"""
UPDATE UpdateTest
FILTER .name = 'update-test-inh-subtype-1'
SET {
comment := 'updated',
related := (
SELECT (DETACHED UpdateTest)
FILTER .name = 'update-test2'
),
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
comment,
related: {
name
}
}
FILTER .name LIKE 'update-test-inh-%'
ORDER BY .name
""",
[
{
'name': 'update-test-inh-subtype-1',
'comment': 'updated',
'related': [{
'name': 'update-test2'
}]
},
{
'name': 'update-test-inh-supertype-1',
'comment': None,
'related': [{
'name': 'update-test1'
}]
},
]
) | )
await self.assert_query_result(
r | test_edgeql_update_inheritance_01 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_inheritance_02(self):
await self.con.execute('''
INSERT UpdateTest {
name := 'update-test-inh-supertype-2',
related := (
SELECT (DETACHED UpdateTest)
FILTER .name = 'update-test2'
)
};
INSERT UpdateTestSubType {
name := 'update-test-inh-subtype-2',
comment := 'update-test-inh-02',
related := (
SELECT (DETACHED UpdateTest)
FILTER .name = 'update-test2'
)
};
INSERT UpdateTestSubSubType {
name := 'update-test-inh-subsubtype-2',
comment := 'update-test-inh-02',
related := (
SELECT (DETACHED UpdateTest)
FILTER .name = 'update-test2'
)
};
''')
await self.assert_query_result(
r"""
UPDATE UpdateTest
FILTER .comment = 'update-test-inh-02'
SET {
comment := 'updated',
related := (
SELECT (DETACHED UpdateTest)
FILTER .name = 'update-test2'
),
};
""",
[{}, {}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
name,
comment,
related: {
name
}
}
FILTER .name LIKE 'update-test-inh-%'
ORDER BY .name
""",
[
{
'name': 'update-test-inh-subsubtype-2',
'comment': 'updated',
'related': [{
'name': 'update-test2'
}]
},
{
'name': 'update-test-inh-subtype-2',
'comment': 'updated',
'related': [{
'name': 'update-test2'
}]
},
{
'name': 'update-test-inh-supertype-2',
'comment': None,
'related': [{
'name': 'update-test2'
}]
},
]
) | )
await self.assert_query_result(
r | test_edgeql_update_inheritance_02 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_inheritance_03(self):
await self.con.execute('''
INSERT UpdateTestSubSubType {
name := 'update-test-w-insert',
};
''')
await self.assert_query_result(
r"""
UPDATE UpdateTestSubType
FILTER .name = 'update-test-w-insert'
SET {
tags := (INSERT Tag { name := "new tag" })
};
""",
[{}],
)
await self.assert_query_result(
r"""
SELECT UpdateTest {
tags: {name},
}
FILTER .name = 'update-test-w-insert'
""",
[
{
'tags': [{
'name': 'new tag'
}]
},
]
) | )
await self.assert_query_result(
r | test_edgeql_update_inheritance_03 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_add_dupes_01a(self):
await self.con.execute("""
INSERT UpdateTestSubSubType {
name := 'add-dupes-scalar',
str_tags := {'foo', 'bar'},
};
""")
await self.assert_query_result(
r"""
WITH _ := (
UPDATE UpdateTestSubSubType
FILTER .name = 'add-dupes-scalar'
SET {
str_tags += 'foo'
}
)
SELECT _ { name, str_tags ORDER BY _.str_tags }
""",
[{
'name': 'add-dupes-scalar',
'str_tags': ['bar', 'foo', 'foo'],
}]
) | )
await self.assert_query_result(
r | test_edgeql_update_add_dupes_01a | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_add_dupes_01b(self):
await self.con.execute("""
INSERT UpdateTestSubSubType {
name := 'add-dupes-scalar-foo',
str_tags := {'foo', 'baz'},
};
INSERT UpdateTestSubSubType {
name := 'add-dupes-scalar-bar',
str_tags := {'bar', 'baz'},
};
""")
await self.assert_query_result(
r"""
WITH _ := (
FOR x in {'foo', 'bar'} UNION (
UPDATE UpdateTestSubSubType
FILTER .name = 'add-dupes-scalar-' ++ x
SET {
str_tags += x
}
)
)
SELECT _ {
name,
str_tags ORDER BY _.str_tags
};
""",
[
{
'name': 'add-dupes-scalar-foo',
'str_tags': ['baz', 'foo', 'foo'],
},
{
'name': 'add-dupes-scalar-bar',
'str_tags': ['bar', 'bar', 'baz'],
},
]
)
await self.assert_query_result(
r"""
WITH _ := (
FOR x in {'foo', 'bar'} UNION (
UPDATE UpdateTestSubSubType
FILTER .name = 'add-dupes-scalar-' ++ x
SET {
str_tags += {x, ('baz' if x = 'foo' else <str>{})}
}
)
)
SELECT _ {
name,
str_tags ORDER BY _.str_tags
};
""",
[
{
'name': 'add-dupes-scalar-foo',
'str_tags': ['baz', 'baz', 'foo', 'foo', 'foo'],
},
{
'name': 'add-dupes-scalar-bar',
'str_tags': ['bar', 'bar', 'bar', 'baz'],
},
]
)
await self.assert_query_result(
r"""
WITH _ := (
FOR x in {'foo', 'bar'} UNION (
UPDATE UpdateTestSubSubType
FILTER .name = 'add-dupes-scalar-' ++ x
SET {
str_tags += x
}
)
)
SELECT _ {
name,
str_tags ORDER BY _.str_tags
}
ORDER BY .name;
""",
[
{
'name': 'add-dupes-scalar-bar',
'str_tags': ['bar', 'bar', 'bar', 'bar', 'baz'],
},
{
'name': 'add-dupes-scalar-foo',
'str_tags': ['baz', 'baz', 'foo', 'foo', 'foo', 'foo'],
},
]
) | )
await self.assert_query_result(
r | test_edgeql_update_add_dupes_01b | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_add_dupes_02(self):
await self.con.execute("""
INSERT UpdateTestSubSubType {
name := 'add-dupes',
tags := (SELECT Tag FILTER .name IN {'fun', 'wow'})
};
""")
await self.assert_query_result(
r"""
SELECT (
UPDATE UpdateTestSubSubType
FILTER .name = 'add-dupes'
SET {
tags += {
(SELECT Tag FILTER .name = 'fun'),
}
}
) {
name,
tags: { name } ORDER BY .name
}
""",
[{
'name': 'add-dupes',
'tags': [
{'name': 'fun'},
{'name': 'wow'},
],
}]
) | )
await self.assert_query_result(
r | test_edgeql_update_add_dupes_02 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_add_dupes_03(self):
await self.con.execute("""
INSERT UpdateTestSubSubType {
name := 'add-dupes',
tags := (SELECT Tag FILTER .name IN {'fun', 'wow'})
};
""")
await self.assert_query_result(
r"""
SELECT (
UPDATE UpdateTestSubSubType
FILTER .name = 'add-dupes'
SET {
tags += {
(UPDATE Tag FILTER .name = 'fun' SET {flag := 1}),
(UPDATE Tag FILTER .name = 'wow' SET {flag := 2}),
}
}
) {
name,
tags: { name, flag } ORDER BY .name
}
""",
[{
'name': 'add-dupes',
'tags': [
{'name': 'fun', 'flag': 1},
{'name': 'wow', 'flag': 2},
],
}]
) | )
await self.assert_query_result(
r | test_edgeql_update_add_dupes_03 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_add_dupes_04(self):
await self.con.execute("""
INSERT UpdateTestSubSubType {
name := 'add-dupes-lprop',
weighted_tags := (
SELECT Tag { @weight := 10 }
FILTER .name IN {'fun', 'wow'})
};
""")
await self.assert_query_result(
r"""
SELECT (
UPDATE UpdateTestSubSubType
FILTER .name = 'add-dupes-lprop'
SET {
weighted_tags += (SELECT Tag {@weight := 20}
FILTER .name = 'fun'),
}
) {
name,
weighted_tags: { name, @weight } ORDER BY .name
}
""",
[{
'name': 'add-dupes-lprop',
'weighted_tags': [
{'name': 'fun', '@weight': 20},
{'name': 'wow', '@weight': 10},
],
}]
) | )
await self.assert_query_result(
r | test_edgeql_update_add_dupes_04 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_covariant_01(self):
await self.con.execute("""
INSERT UpdateTestSubSubType {
name := 'update-covariant',
};
""")
# Covariant updates should work if the types actually work at
# runtime
await self.con.execute("""
UPDATE UpdateTestSubType
FILTER .name = "update-covariant"
SET {
status := (SELECT Status FILTER .name = "Broke a Type System")
}
""")
# But fail if they don't
async with self.assertRaisesRegexTx(
edgedb.InvalidLinkTargetError,
r"invalid target for link 'status' of object type "
r"'default::UpdateTestSubSubType': 'default::Status' "
r"\(expecting 'default::MajorLifeEvent'\)"):
await self.con.execute("""
UPDATE UpdateTestSubType
FILTER .name = "update-covariant"
SET {
status := (SELECT Status FILTER .name = "Open")
}
""")
async with self.assertRaisesRegexTx(
edgedb.InvalidLinkTargetError,
r"invalid target for link 'status' of object type "
r"'default::UpdateTestSubSubType': 'default::Status' "
r"\(expecting 'default::MajorLifeEvent'\)"):
await self.con.execute("""
UPDATE UpdateTestSubType
FILTER .name = "update-covariant"
SET {
status := (INSERT Status { name := "Yolo" })
}
""") | )
# Covariant updates should work if the types actually work at
# runtime
await self.con.execute( | test_edgeql_update_covariant_01 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_covariant_02(self):
await self.con.execute("""
INSERT UpdateTestSubSubType {
name := 'update-covariant',
};
""")
# Covariant updates should work if the types actually work at
# runtime
await self.con.execute("""
UPDATE UpdateTestSubType
FILTER .name = "update-covariant"
SET {
statuses := (
SELECT Status FILTER .name = "Broke a Type System")
}
""")
# But fail if they don't
async with self.assertRaisesRegexTx(
edgedb.InvalidLinkTargetError,
r"invalid target for link 'statuses' of object type "
r"'default::UpdateTestSubSubType': 'default::Status' "
r"\(expecting 'default::MajorLifeEvent'\)"):
await self.con.execute("""
UPDATE UpdateTestSubType
FILTER .name = "update-covariant"
SET {
statuses := (SELECT Status FILTER .name = "Open")
}
""")
async with self.assertRaisesRegexTx(
edgedb.InvalidLinkTargetError,
r"invalid target for link 'statuses' of object type "
r"'default::UpdateTestSubSubType': 'default::Status' "
r"\(expecting 'default::MajorLifeEvent'\)"):
await self.con.execute("""
UPDATE UpdateTestSubType
FILTER .name = "update-covariant"
SET {
statuses := (INSERT Status { name := "Yolo" })
}
""") | )
# Covariant updates should work if the types actually work at
# runtime
await self.con.execute( | test_edgeql_update_covariant_02 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_covariant_03(self):
await self.con.execute("""
INSERT UpdateTestSubSubType {
name := 'update-covariant',
};
""")
# Tests with a multi link, actually using multiple things
# Covariant updates should work if the types actually work at
# runtime
await self.con.execute("""
UPDATE UpdateTestSubType
FILTER .name = "update-covariant"
SET {
statuses := (SELECT Status FILTER .name IN {
"Broke a Type System",
"Downloaded a Car",
})
}
""")
async with self.assertRaisesRegexTx(
edgedb.InvalidLinkTargetError,
r"invalid target for link 'statuses' of object type "
r"'default::UpdateTestSubSubType': 'default::Status' "
r"\(expecting 'default::MajorLifeEvent'\)"):
await self.con.execute("""
UPDATE UpdateTestSubType
FILTER .name = "update-covariant"
SET {
statuses := (SELECT Status FILTER .name IN {
"Broke a Type System",
"Downloaded a Car",
"Open",
})
}
""")
async with self.assertRaisesRegexTx(
edgedb.InvalidLinkTargetError,
r"invalid target for link 'statuses' of object type "
r"'default::UpdateTestSubSubType': 'default::Status' "
r"\(expecting 'default::MajorLifeEvent'\)"):
await self.con.execute("""
UPDATE UpdateTestSubType
FILTER .name = "update-covariant"
SET {
statuses := (FOR x in {"Foo", "Bar"} UNION (
INSERT Status {name := x}))
}
""") | )
# Tests with a multi link, actually using multiple things
# Covariant updates should work if the types actually work at
# runtime
await self.con.execute( | test_edgeql_update_covariant_03 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_subtract_backlink_overload_01(self):
await self.con.execute(r"""
CREATE TYPE Clash { CREATE LINK statuses -> Status; };
""")
await self.con.execute(r"""
UPDATE UpdateTest FILTER .name = 'update-test1'
SET { statuses := MajorLifeEvent };
""")
await self.assert_query_result(
r"""
SELECT (
UPDATE UpdateTest FILTER .name = "update-test1" SET {
statuses -= (SELECT Status
FILTER .name = "Downloaded a Car")
}
) {
statuses: {
name,
backlinks := .<statuses[IS UpdateTest].name
}
};
""",
[
{
"statuses": [
{
"backlinks": ["update-test1"],
"name": "Broke a Type System"
}
]
}
]
) | )
await self.con.execute(r | test_edgeql_update_subtract_backlink_overload_01 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_inject_intersection_01(self):
await self.con.execute(r"""
CREATE ABSTRACT TYPE default::I;
CREATE TYPE default::S EXTENDING default::I {
CREATE REQUIRED PROPERTY Depth -> std::float64;
};
CREATE TYPE default::P {
CREATE REQUIRED MULTI LINK Items -> default::I;
CREATE PROPERTY name -> std::str;
};
""")
await self.con._fetchall(
r"""
with
obj1 := (select P FILTER .name = 'foo'),
obj3 := (select obj1.Items limit 1)[is S]
UPDATE obj3
SET {
Depth := 11.3781298010066
};
""",
__typenames__=True
)
await self.con._fetchall(
r"""
with
obj1 := (select P FILTER .name = 'foo'),
obj3 := (select obj1.Items limit 1)[is S]
DELETE obj3
""",
__typenames__=True
) | )
await self.con._fetchall(
r | test_edgeql_update_inject_intersection_01 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_inject_intersection_02(self):
await self.con.execute(r"""
create alias UpdateTestCommented :=
(select UpdateTest filter exists .comment);
""")
await self.assert_query_result(
r"""
update UpdateTestCommented[is UpdateTestSubType]
set { str_tags += 'lol' };
""",
[],
)
await self.assert_query_result(
r"""
delete UpdateTestCommented[is UpdateTestSubType]
""",
[],
) | )
await self.assert_query_result(
r | test_edgeql_update_inject_intersection_02 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_volatility_01(self):
# random should be executed once for each object
await self.con.execute(r"""
update UpdateTest set { comment := <str>random() };
""")
await self.assert_query_result(
r"""
select count(distinct UpdateTest.comment) = count(UpdateTest)
""",
[True],
) | )
await self.assert_query_result(
r | test_edgeql_update_volatility_01 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_volatility_02(self):
# random should be executed once for each object
await self.con.execute(r"""
update UpdateTest set { str_tags := <str>random() };
""")
await self.assert_query_result(
r"""
select count(distinct UpdateTest.str_tags) = count(UpdateTest)
""",
[True],
) | )
await self.assert_query_result(
r | test_edgeql_update_volatility_02 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_poly_overlay_01(self):
await self.con.execute(r"""
insert UpdateTestSubType { name := 'update-test4' };
""")
await self.assert_query_result(
r"""
select (
update UpdateTest filter .name = 'update-test4'
set { name := '!' }
) { c1 := .name, c2 := [is UpdateTestSubType].name };
""",
[{"c1": "!", "c2": "!"}]
) | )
await self.assert_query_result(
r | test_edgeql_update_poly_overlay_01 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_poly_overlay_02(self):
await self.con.execute(r"""
insert UpdateTestSubType { name := 'update-test4' };
""")
await self.assert_query_result(
r"""
with X := (
update UpdateTest filter .name = 'update-test4'
set { name := '!' }
),
select X[is UpdateTestSubType] { name };
""",
[{"name": "!"}]
) | )
await self.assert_query_result(
r | test_edgeql_update_poly_overlay_02 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_where_order_dml(self):
async with self.assertRaisesRegexTx(
edgedb.QueryError,
"INSERT statements cannot be used in a FILTER clause"):
await self.con.query('''
update UpdateTest
filter (INSERT UpdateTest {
name := 't1',
})
set { name := '!' }
''')
async with self.assertRaisesRegexTx(
edgedb.QueryError,
"UPDATE statements cannot be used in a FILTER clause"):
await self.con.query('''
update UpdateTest
filter (UPDATE UpdateTest set {
name := 't1',
})
set { name := '!' }
''')
async with self.assertRaisesRegexTx(
edgedb.QueryError,
"DELETE statements cannot be used in a FILTER clause"):
await self.con.query('''
update UpdateTest
filter (DELETE UpdateTest filter .name = 't1')
set { name := '!' }
''') | )
async with self.assertRaisesRegexTx(
edgedb.QueryError,
"UPDATE statements cannot be used in a FILTER clause"):
await self.con.query( | test_edgeql_update_where_order_dml | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_dunder_default_01(self):
await self.con.execute(r"""
INSERT DunderDefaultTest01 { a := 1, b := 2, c := 3 };
""")
async with self.assertRaisesRegexTx(
edgedb.InvalidReferenceError,
r"__default__ cannot be used in this expression",
_hint='No default expression exists',
):
await self.con.execute(r'''
UPDATE DunderDefaultTest01 set { a := __default__ };
''')
async with self.assertRaisesRegexTx(
edgedb.InvalidReferenceError,
r"__default__ cannot be used in this expression",
_hint='Default expression uses __source__',
):
await self.con.execute(r'''
UPDATE DunderDefaultTest01 set { b := __default__ };
''')
await self.con.execute(r"""
UPDATE DunderDefaultTest01 set { c := __default__ };
""")
await self.assert_query_result(
r'''
SELECT DunderDefaultTest01 { a, b, c };
''',
[
{'a': 1, 'b': 2, 'c': 1},
]
) | )
async with self.assertRaisesRegexTx(
edgedb.InvalidReferenceError,
r"__default__ cannot be used in this expression",
_hint='No default expression exists',
):
await self.con.execute(r | test_edgeql_update_dunder_default_01 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_update_dunder_default_02(self):
await self.con.execute(r'''
INSERT DunderDefaultTest02_A { a := 1 };
INSERT DunderDefaultTest02_A { a := 2 };
INSERT DunderDefaultTest02_A { a := 3 };
INSERT DunderDefaultTest02_A { a := 4 };
INSERT DunderDefaultTest02_A { a := 5 };
INSERT DunderDefaultTest02_B {
default_with_insert := (
select DunderDefaultTest02_A
filter DunderDefaultTest02_A.a = 1
),
default_with_update := (
select DunderDefaultTest02_A
filter DunderDefaultTest02_A.a = 2
),
default_with_delete := (
select DunderDefaultTest02_A
filter DunderDefaultTest02_A.a = 3
),
default_with_select := (
select DunderDefaultTest02_A
filter DunderDefaultTest02_A.a = 5
),
};
''')
async with self.assertRaisesRegexTx(
edgedb.InvalidReferenceError,
r"__default__ cannot be used in this expression",
_hint='Default expression uses DML',
):
await self.con.execute(r'''
UPDATE DunderDefaultTest02_B set {
default_with_insert := __default__
};
''')
async with self.assertRaisesRegexTx(
edgedb.InvalidReferenceError,
r"__default__ cannot be used in this expression",
_hint='Default expression uses DML',
):
await self.con.execute(r'''
UPDATE DunderDefaultTest02_B set {
default_with_update := __default__
};
''')
async with self.assertRaisesRegexTx(
edgedb.InvalidReferenceError,
r"__default__ cannot be used in this expression",
_hint='Default expression uses DML',
):
await self.con.execute(r'''
UPDATE DunderDefaultTest02_B set {
default_with_delete := __default__
};
''')
await self.con.execute(r'''
UPDATE DunderDefaultTest02_B set {
default_with_select := __default__
};
''')
await self.assert_query_result(
r'''
SELECT DunderDefaultTest02_B {
a := .default_with_select.a
};
''',
[
{'a': [4]},
]
) | )
async with self.assertRaisesRegexTx(
edgedb.InvalidReferenceError,
r"__default__ cannot be used in this expression",
_hint='Default expression uses DML',
):
await self.con.execute(r | test_edgeql_update_dunder_default_02 | python | geldata/gel | tests/test_edgeql_update.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_update.py | Apache-2.0 |
async def test_edgeql_group_grouping_sets_01(self):
res = [
{"grouping": [], "num": 9},
{"grouping": ["element"], "num": int},
{"grouping": ["element"], "num": int},
{"grouping": ["element"], "num": int},
{"grouping": ["element"], "num": int},
{"grouping": ["element", "nowners"], "num": int},
{"grouping": ["element", "nowners"], "num": int},
{"grouping": ["element", "nowners"], "num": int},
{"grouping": ["element", "nowners"], "num": int},
{"grouping": ["element", "nowners"], "num": int},
{"grouping": ["element", "nowners"], "num": int},
{"grouping": ["nowners"], "num": int},
{"grouping": ["nowners"], "num": int},
{"grouping": ["nowners"], "num": int},
{"grouping": ["nowners"], "num": int},
]
await self.assert_query_result(
r'''
WITH MODULE cards
SELECT (
GROUP Card
USING nowners := count(.owners)
BY CUBE (.element, nowners)
) {
num := count(.elements),
grouping
} ORDER BY array_agg((SELECT _ := .grouping ORDER BY _))
''',
res
)
# With an extra SELECT
await self.assert_query_result(
r'''
WITH MODULE cards
SELECT (SELECT (
GROUP Card
USING nowners := count(.owners)
BY CUBE (.element, nowners)
) {
num := count(.elements),
grouping
}) ORDER BY array_agg((SELECT _ := .grouping ORDER BY _))
''',
res
)
await self.assert_query_result(
r'''
WITH MODULE cards
SELECT (
GROUP Card
USING x := count(.owners), nowners := x,
BY CUBE (.element, nowners)
) {
num := count(.elements),
grouping
} ORDER BY array_agg((SELECT _ := .grouping ORDER BY _))
''',
res
) | ,
res
)
# With an extra SELECT
await self.assert_query_result(
r | test_edgeql_group_grouping_sets_01 | python | geldata/gel | tests/test_edgeql_group.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_group.py | Apache-2.0 |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.