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 |
---|---|---|---|---|---|---|---|
async def test_edgeql_ddl_duplicates_02(self):
await self.con.execute(r"""
CREATE TYPE Foo {
CREATE PROPERTY foo -> str;
}
""")
with self.assertRaisesRegex(
edgedb.errors.SchemaError,
r"property 'foo' of "
r"object type 'default::Foo' already exists"):
await self.con.execute(r"""
ALTER TYPE Foo {
CREATE PROPERTY foo -> str;
}
""") | )
with self.assertRaisesRegex(
edgedb.errors.SchemaError,
r"property 'foo' of "
r"object type 'default::Foo' already exists"):
await self.con.execute(r | test_edgeql_ddl_duplicates_02 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_duplicates_03(self):
await self.con.execute(r"""
CREATE TYPE Foo;
CREATE TYPE Bar;
""")
with self.assertRaisesRegex(
edgedb.errors.SchemaError,
r"object type 'default::Foo' already exists"):
await self.con.execute(r"""
ALTER TYPE Bar RENAME TO Foo;
""") | )
with self.assertRaisesRegex(
edgedb.errors.SchemaError,
r"object type 'default::Foo' already exists"):
await self.con.execute(r | test_edgeql_ddl_duplicates_03 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_duplicates_04(self):
await self.con.execute(r"""
CREATE TYPE Foo {
CREATE PROPERTY foo -> str;
CREATE PROPERTY bar -> str;
}
""")
with self.assertRaisesRegex(
edgedb.errors.SchemaError,
r"property 'foo' of "
r"object type 'default::Foo' already exists"):
await self.con.execute(r"""
ALTER TYPE Foo {
ALTER PROPERTY bar RENAME TO foo;
}
""") | )
with self.assertRaisesRegex(
edgedb.errors.SchemaError,
r"property 'foo' of "
r"object type 'default::Foo' already exists"):
await self.con.execute(r | test_edgeql_ddl_duplicates_04 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_alias_in_computable_01(self):
await self.con.execute(r"""
CREATE ALIAS Alias := {0, 1, 2, 3};
""")
# We don't want to prohibit this forever, but we need to for now.
async with self.assertRaisesRegexTx(
edgedb.errors.UnsupportedFeatureError,
r"referring to alias 'default::Alias' from computed property"):
await self.con.execute(r"""
CREATE TYPE Foo {
CREATE PROPERTY bar := Alias;
};
""")
async with self.assertRaisesRegexTx(
edgedb.errors.UnsupportedFeatureError,
r"referring to alias 'default::Alias' from computed property"):
await self.con.execute(r"""
CREATE TYPE Foo {
CREATE PROPERTY bar := {Alias, Alias};
};
""") | )
# We don't want to prohibit this forever, but we need to for now.
async with self.assertRaisesRegexTx(
edgedb.errors.UnsupportedFeatureError,
r"referring to alias 'default::Alias' from computed property"):
await self.con.execute(r | test_edgeql_ddl_alias_in_computable_01 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_drop_parent_multi_link(self):
await self.con.execute(r"""
CREATE TYPE C;
CREATE TYPE D {
CREATE MULTI LINK multi_link -> C;
};
CREATE TYPE E EXTENDING D;
INSERT C;
""")
await self.con.execute(r"""
ALTER TYPE D {
DROP LINK multi_link;
};
""")
await self.con.execute(r"""
DELETE C;
""") | )
await self.con.execute(r | test_edgeql_ddl_drop_parent_multi_link | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_drop_multi_parent_multi_link(self):
await self.con.execute(r"""
CREATE TYPE C;
INSERT C;
CREATE TYPE D {
CREATE MULTI LINK multi_link -> C;
};
CREATE TYPE E {
CREATE MULTI LINK multi_link -> C;
};
CREATE TYPE F EXTENDING D, E;
""")
await self.con.execute(r"""
ALTER TYPE D {
DROP LINK multi_link;
};
""")
await self.con.execute(r"""
DELETE C;
""") | )
await self.con.execute(r | test_edgeql_ddl_drop_multi_parent_multi_link | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_switch_link_to_computed(self):
await self.con.execute(r"""
create type Identity;
create type User {
create required property name -> str {
create constraint exclusive;
};
create multi link identities -> Identity {
create constraint exclusive;
};
};
alter type Identity {
create link user -> User {
on target delete delete source;
};
};
""")
await self.con.execute(r"""
alter type User {
alter link identities {
drop constraint exclusive;
};
alter link identities {
using (.<user[IS Identity]);
};
};
""")
await self.con.execute(r"""
insert Identity { user := (insert User { name := 'foo' }) }
""")
await self.con.execute(r"""
delete User filter true
""") | )
await self.con.execute(r | test_edgeql_ddl_switch_link_to_computed | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_switch_link_target(self):
await self.con.execute(r"""
create type Foo;
create type Bar;
create type Ptr { create link p -> Foo; };
alter type Ptr { alter link p set type Bar using (<Bar>{}); };
insert Ptr { p := (insert Bar) };
""")
async with self.assertRaisesRegexTx(
edgedb.ConstraintViolationError,
"prohibited by link target policy",
):
await self.con.execute("""
delete Bar;
""")
await self.con.execute(r"""
drop type Ptr;
""")
await self.con.execute(r"""
insert Foo;
delete Foo;
""") | )
async with self.assertRaisesRegexTx(
edgedb.ConstraintViolationError,
"prohibited by link target policy",
):
await self.con.execute( | test_edgeql_ddl_switch_link_target | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_switch_link_computed_01(self):
await self.con.execute(r"""
create type Tgt;
create type Src {
create multi link l1 -> Tgt { create property s1 -> str; };
create multi link l2 -> Tgt { create property s2 -> str; };
create link c := (.l1);
};
""")
await self.con.execute(r"""
select Src { c: {@s1} };
""")
await self.con.execute(r"""
alter type Src alter link c using (.l2);
""")
await self.con.execute(r"""
select Src { c: {@s2} };
""") | )
await self.con.execute(r | test_edgeql_ddl_switch_link_computed_01 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_set_abs_linkprop_type(self):
await self.con.execute(r"""
CREATE ABSTRACT LINK orderable {
CREATE PROPERTY orderVal -> str {
CREATE DELEGATED CONSTRAINT exclusive;
};
};
CREATE ABSTRACT TYPE Entity;
CREATE TYPE Video EXTENDING Entity;
CREATE TYPE Topic EXTENDING Entity {
CREATE MULTI LINK videos EXTENDING orderable -> Video;
};
""")
await self.con.execute(r"""
ALTER ABSTRACT LINK orderable {
ALTER PROPERTY orderVal {
SET TYPE decimal using (<decimal>@orderVal);
};
};
""") | )
await self.con.execute(r | test_edgeql_ddl_set_abs_linkprop_type | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_set_multi_with_children_01(self):
await self.con.execute(r"""
create type Person { create link lover -> Person; };
create type NPC extending Person;
alter type Person { alter link lover { set multi; }; };
""")
await self.con.execute(r"""
drop type NPC;
drop type Person;
""") | )
await self.con.execute(r | test_edgeql_ddl_set_multi_with_children_01 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_set_multi_with_children_02(self):
await self.con.execute(r"""
create abstract type Person { create link lover -> Person; };
create type NPC extending Person;
alter type Person { alter link lover { set multi; }; };
""")
await self.con.execute(r"""
drop type NPC;
drop type Person;
""") | )
await self.con.execute(r | test_edgeql_ddl_set_multi_with_children_02 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_set_multi_with_children_03(self):
await self.con.execute(r"""
create type Person { create property foo -> str; };
create type NPC extending Person;
alter type Person { alter property foo { set multi; }; };
""")
await self.con.execute(r"""
drop type NPC;
drop type Person;
""") | )
await self.con.execute(r | test_edgeql_ddl_set_multi_with_children_03 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_set_multi_with_children_04(self):
await self.con.execute(r"""
create abstract type Person { create property foo -> str; };
create type NPC extending Person;
alter type Person { alter property foo { set multi; }; };
""")
await self.con.execute(r"""
drop type NPC;
drop type Person;
""") | )
await self.con.execute(r | test_edgeql_ddl_set_multi_with_children_04 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_set_single_with_children_01(self):
await self.con.execute(r"""
create abstract type Person { create multi link foo -> Person; };
create type NPC extending Person;
alter type Person alter link foo {
set single USING (SELECT .foo LIMIT 1);
};
""")
await self.con.execute(r"""
drop type NPC;
drop type Person;
""") | )
await self.con.execute(r | test_edgeql_ddl_set_single_with_children_01 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_set_single_with_children_02(self):
await self.con.execute(r"""
create abstract type Person { create multi property foo -> str; };
create type NPC extending Person;
alter type Person alter property foo {
set single USING (SELECT .foo LIMIT 1);
};
""")
await self.con.execute(r"""
drop type NPC;
drop type Person;
""") | )
await self.con.execute(r | test_edgeql_ddl_set_single_with_children_02 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_drop_multi_child_01(self):
await self.con.execute(r"""
create abstract type Person { create multi property foo -> str; };
create type NPC extending Person;
""")
await self.con.execute(r"""
drop type NPC;
drop type Person;
""") | )
await self.con.execute(r | test_edgeql_ddl_drop_multi_child_01 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_drop_multi_child_02(self):
await self.con.execute(r"""
create abstract type Person { create multi link foo -> Person; };
create type NPC extending Person;
""")
await self.con.execute(r"""
drop type NPC;
drop type Person;
""") | )
await self.con.execute(r | test_edgeql_ddl_drop_multi_child_02 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_set_abstract_bogus_01(self):
await self.con.execute(r"""
create type Foo;
insert Foo;
""")
with self.assertRaisesRegex(
edgedb.ConstraintViolationError,
r"may not make non-empty object type 'default::Foo' abstract"):
await self.con.execute(r"""
alter type Foo set abstract;
""") | )
with self.assertRaisesRegex(
edgedb.ConstraintViolationError,
r"may not make non-empty object type 'default::Foo' abstract"):
await self.con.execute(r | test_edgeql_ddl_set_abstract_bogus_01 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_no_type_intro_in_default(self):
await self.con.execute(r"""
create scalar type Foo extending sequence;
create type Project {
create required property number -> Foo {
set default := sequence_next(introspect Foo);
}
};
""")
await self.con.execute(r"""
insert Project;
insert Project;
""") | )
await self.con.execute(r | test_edgeql_no_type_intro_in_default | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_no_shapes_in_using(self):
await self.con.execute(r"""
create type Foo;
create type Bar extending Foo;
create type Baz {
create multi link foo -> Foo;
};
""")
q = 'select Bar { x := "oops" } limit 1'
alters = [
f'set required using ({q})',
f'set single using ({q})',
f'set default := ({q})',
f'set type Bar using ({q})',
]
for alter in alters:
async with self.assertRaisesRegexTx(
(edgedb.SchemaError, edgedb.SchemaDefinitionError),
r"may not include a shape"):
await self.con.execute(fr"""
alter type Baz {{
alter link foo {{
{alter}
}}
}};
""") | )
q = 'select Bar { x := "oops" } limit 1'
alters = [
f'set required using ({q})',
f'set single using ({q})',
f'set default := ({q})',
f'set type Bar using ({q})',
]
for alter in alters:
async with self.assertRaisesRegexTx(
(edgedb.SchemaError, edgedb.SchemaDefinitionError),
r"may not include a shape"):
await self.con.execute(fr | test_edgeql_ddl_no_shapes_in_using | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_uuid_array_01(self):
await self.con.execute(r"""
create type Foo {
create property uuid_array_prop -> array<uuid>
}
""")
await self.assert_query_result(
r"""
select schema::Property {target: {name}}
filter .name = 'uuid_array_prop';
""",
[{'target': {'name': 'array<std::uuid>'}}]
) | )
await self.assert_query_result(
r | test_edgeql_ddl_uuid_array_01 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_computed_intersection_lprop_01(self):
await self.con.execute(r"""
create type Pointer;
create type Property extending Pointer;
create type Link extending Pointer;
create type ObjectType {
create multi link pointers -> Pointer {
create property owned -> bool;
};
create link links := .pointers[is Link];
};
""")
await self.con.query(r"""
select ObjectType {links: {@owned}};
""") | )
await self.con.query(r | test_edgeql_ddl_computed_intersection_lprop_01 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_union_link_target_alter_01(self):
await self.con.execute(r"""
create type X;
create type Y;
create type T { create link xy -> X | Y; };
""")
await self.con.query(r"""
alter type X create required property foo -> str;
""")
await self.con.query(r"""
alter type Y create required property foo -> str;
""")
await self.con.query(r"""
alter type X alter property foo set multi;
""") | )
await self.con.query(r | test_edgeql_ddl_union_link_target_alter_01 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_rebase_views_01(self):
await self.con.execute(r"""
CREATE TYPE default::Foo {
CREATE PROPERTY x -> std::str {
CREATE CONSTRAINT std::exclusive;
};
};
CREATE TYPE default::Bar EXTENDING default::Foo;
CREATE TYPE default::Baz EXTENDING default::Foo;
""")
await self.con.execute(r"""
CREATE TYPE default::Foo2 EXTENDING default::Foo;
ALTER TYPE default::Bar {
DROP EXTENDING default::Foo;
EXTENDING default::Foo2 LAST;
};
INSERT Bar;
""")
# should still be in the view
await self.assert_query_result(
'select Foo',
[{}],
)
await self.assert_query_result(
'select Object',
[{}],
) | )
await self.con.execute(r | test_edgeql_ddl_rebase_views_01 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_rebase_views_02(self):
await self.con.execute(r"""
CREATE TYPE default::Foo {
CREATE PROPERTY x -> std::str {
CREATE CONSTRAINT std::exclusive;
};
};
CREATE TYPE default::Bar EXTENDING default::Foo;
CREATE TYPE default::Baz EXTENDING default::Foo;
""")
await self.con.execute(r"""
CREATE TYPE default::Foo2 {
CREATE PROPERTY x -> std::str {
CREATE CONSTRAINT std::exclusive;
};
};
ALTER TYPE default::Bar {
DROP EXTENDING default::Foo;
EXTENDING default::Foo2 LAST;
};
INSERT Bar;
""")
# should *not* still be in the view
await self.assert_query_result(
'select Foo',
[],
)
await self.assert_query_result(
'select Object',
[{}],
) | )
await self.con.execute(r | test_edgeql_ddl_rebase_views_02 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_schema_repair(self):
await self.con.execute('''
create type Tgt {
create property lol := count(Object)
}
''')
await self.con.execute('''
administer schema_repair()
''') | )
await self.con.execute( | test_edgeql_ddl_schema_repair | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_consecutive_create_migration_01(self):
# A regression test for https://github.com/edgedb/edgedb/issues/2085.
await self.con.execute('''
CREATE MIGRATION m1dpxyvsejl6b2tqe5nzpy6wpk5zzjhm7gwky7jn5vmnqrqoujxn6q
ONTO initial
{
CREATE TYPE default::A;
};
''')
await self.con.query('''
CREATE MIGRATION m1xuduby4e6u2sraygw352y553ltcj4cyz4dijuwlbqqq34ap43yca
ONTO m1dpxyvsejl6b2tqe5nzpy6wpk5zzjhm7gwky7jn5vmnqrqoujxn6q
{
CREATE TYPE default::B;
};
''') | )
await self.con.query( | test_edgeql_ddl_consecutive_create_migration_01 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_no_tx_mig_error_01(self):
await self.con.execute('''
create type Mig01;
insert Mig01;
''')
with self.assertRaisesRegex(
edgedb.MissingRequiredError,
r"missing value for required property 'n'"):
# Test it standalone
await self.con.query('''
alter type Mig01 create required property n -> int64;
''') | )
with self.assertRaisesRegex(
edgedb.MissingRequiredError,
r"missing value for required property 'n'"):
# Test it standalone
await self.con.query( | test_edgeql_ddl_no_tx_mig_error_01 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_no_tx_mig_error_02(self):
await self.con.execute('''
create type Mig02;
insert Mig02;
''')
with self.assertRaisesRegex(
edgedb.MissingRequiredError,
r"missing value for required property 'n'"):
# Test it in a script
await self.con.query('''
alter type Mig02 create required property n -> int64;
create type Mig02b;
''') | )
with self.assertRaisesRegex(
edgedb.MissingRequiredError,
r"missing value for required property 'n'"):
# Test it in a script
await self.con.query( | test_edgeql_ddl_no_tx_mig_error_02 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_no_tx_mig_error_03(self):
await self.con.execute('''
create type Mig03;
''')
with self.assertRaisesRegex(
edgedb.MissingRequiredError,
r"missing value for required property 'n'"):
# Test a non-DDL failure in the script where prop was created
await self.con.query('''
alter type Mig03 create required property n -> int64;
insert Mig03 { n := <int64>{} };
''') | )
with self.assertRaisesRegex(
edgedb.MissingRequiredError,
r"missing value for required property 'n'"):
# Test a non-DDL failure in the script where prop was created
await self.con.query( | test_edgeql_ddl_no_tx_mig_error_03 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_reindex(self):
await self.con.execute('''
create type Tgt;
create type Foo {
create property foo -> str {
create constraint exclusive;
};
create property bar -> str;
create index on (.foo);
create constraint exclusive on ((.foo, .bar));
create link tgt -> Tgt {
create property foo -> str;
};
create link tgts -> Tgt {
create property foo -> str;
};
};
create module test;
create type test::Bar extending Foo;
''')
try:
await self.con.execute('''
administer reindex(Foo)
''')
await self.con.execute('''
administer reindex(Foo.foo)
''')
await self.con.execute('''
administer reindex(Foo.bar)
''')
await self.con.execute('''
administer reindex(Foo.tgt)
''')
await self.con.execute('''
administer reindex(Foo.tgts)
''')
await self.con.execute('''
administer reindex(test::Bar)
''')
await self.con.execute('''
administer reindex(Object)
''')
finally:
await self.con.execute('''
drop type test::Bar;
drop type Foo;
drop type Tgt;
drop module test;
''') | )
try:
await self.con.execute( | test_edgeql_ddl_reindex | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def _deadlock_tester(self, setup, teardown, modification, query):
"""Deadlock test helper.
Interleave a long running query, some DDL, and a short running query
in a way that has triggered deadlock in the past.
(See #6304.)
"""
cons = []
con1 = self.con
await con1.execute(setup)
try:
for _ in range(2):
con = await self.connect(database=self.con.dbname)
await con.query('select 1')
cons.append(con)
con2, con3 = cons
long_call = asyncio.create_task(con1.query_single(f'''
select (count({query}), sys::_sleep(3));
'''))
await asyncio.sleep(0.5)
ddl = asyncio.create_task(con2.execute(modification))
await asyncio.sleep(0.5)
short_call = con3.query(f'''
select {query}
''')
return await asyncio.gather(long_call, ddl, short_call)
finally:
await self.con.execute(teardown)
for con in cons:
await con.aclose() | Deadlock test helper.
Interleave a long running query, some DDL, and a short running query
in a way that has triggered deadlock in the past.
(See #6304.) | _deadlock_tester | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_deadlock_01(self):
((cnt, _), _, objs) = await self._deadlock_tester(
setup='''
create type X;
insert X;
''',
teardown='''
drop type X;
''',
modification='''
alter type X create property foo -> str;
''',
query='X',
)
self.assertEqual(cnt, 1)
self.assertEqual(len(objs), 1) | ,
teardown= | test_edgeql_ddl_deadlock_01 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_deadlock_02(self):
((cnt, _), _, objs) = await self._deadlock_tester(
setup='''
create type Y;
create type X { create multi link t -> Y; };
insert X { t := (insert Y) };
''',
teardown='''
drop type X;
drop type Y;
''',
modification='''
alter type X alter link t create property foo -> str;
''',
query='(Y, Y.<t[is X])',
)
self.assertEqual(cnt, 1)
self.assertEqual(len(objs), 1) | ,
teardown= | test_edgeql_ddl_deadlock_02 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_single_index(self):
# Test that types only have a single index for id
await self.con.execute('''
create type DDLSingleIndex;
''')
objid = await self.con.query_single('''
select (introspect DDLSingleIndex).id
''')
async with self.with_backend_sql_connection() as scon:
res = await scon.fetch(
f'''
select indexname, tablename, indexdef from pg_indexes
where tablename = $1::text
''',
str(objid),
)
self.assertEqual(
len(res),
1,
f"Too many indexes on .id: {res}",
) | )
objid = await self.con.query_single( | test_edgeql_ddl_single_index | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_function_drop_tuple_cache(self):
await self.con.execute('''
create function lol() -> SET OF tuple<str, str> using (('x', 'y'));
''')
# Run many times to wait for the func cache creation
for _ in range(64):
await self.assert_query_result(
'select lol()',
[('x', 'y')]
)
# This drop should succeed, even when the func cache depends on the
# returning tuple type; the cache should be evicted.
await self.con.execute('''
drop function lol();
''') | )
# Run many times to wait for the func cache creation
for _ in range(64):
await self.assert_query_result(
'select lol()',
[('x', 'y')]
)
# This drop should succeed, even when the func cache depends on the
# returning tuple type; the cache should be evicted.
await self.con.execute( | test_edgeql_ddl_function_drop_tuple_cache | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_ddl_rollback_enum_01(self):
await self.con.query("START TRANSACTION")
await self.con.execute('''
CREATE SCALAR TYPE Color
EXTENDING enum<Red, Green, Blue>;
''')
await self.con.query('DECLARE SAVEPOINT t0')
with self.assertRaisesRegex(
edgedb.SchemaError,
'cannot DROP EXTENDING enum'):
await self.con.execute('''
ALTER SCALAR TYPE Color
DROP EXTENDING enum<Red, Green, Blue>;
''')
# Recover.
await self.con.query('ROLLBACK TO SAVEPOINT t0;')
with self.assertRaisesRegex(
edgedb.SchemaError,
"cannot add supertype scalar type 'std::str' to enum type "
"default::Color"):
await self.con.execute('''
ALTER SCALAR TYPE Color EXTENDING str FIRST;
''')
# Recover.
await self.con.query('ROLLBACK TO SAVEPOINT t0;')
with self.assertRaisesRegex(
edgedb.SchemaError,
'cannot add supertype enum<Bad> to enum type default::Color'):
await self.con.execute('''
ALTER SCALAR TYPE Color
EXTENDING enum<Bad> LAST;
''')
# Recover.
await self.con.query('ROLLBACK TO SAVEPOINT t0;')
with self.assertRaisesRegex(
edgedb.SchemaError,
'enum default::Color may not have multiple supertypes'):
await self.con.execute('''
ALTER SCALAR TYPE Color
EXTENDING enum<Bad>, enum<AlsoBad>;
''')
# Recover.
await self.con.query('ROLLBACK TO SAVEPOINT t0;')
with self.assertRaisesRegex(
edgedb.SchemaError,
'enums cannot contain duplicate values'):
await self.con.execute('''
ALTER SCALAR TYPE Color
EXTENDING enum<Red, Green, Blue, Red>;
''')
# Recover.
await self.con.query('ROLLBACK TO SAVEPOINT t0;')
await self.con.execute(r'''
ALTER SCALAR TYPE Color
EXTENDING enum<Red, Green, Blue, Magic>;
''')
# Commit the changes and before continuing testing.
await self.con.query("COMMIT")
await self.assert_query_result(
r"""
SELECT <Color>'Magic' >
<Color>'Red';
""",
[True],
) | )
await self.con.query('DECLARE SAVEPOINT t0')
with self.assertRaisesRegex(
edgedb.SchemaError,
'cannot DROP EXTENDING enum'):
await self.con.execute( | test_edgeql_ddl_rollback_enum_01 | python | geldata/gel | tests/test_edgeql_ddl.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ddl.py | Apache-2.0 |
async def test_edgeql_tutorial(self):
await self.con.execute(r'''
START MIGRATION TO {
module default {
type Movie {
required property title -> str;
# the year of release
property year -> int64;
required link director -> Person;
multi link cast -> Person;
}
type Person {
required property first_name -> str;
required property last_name -> str;
}
}
};
POPULATE MIGRATION;
COMMIT MIGRATION;
INSERT Movie {
title := 'Blade Runner 2049',
year := 2017,
director := (
INSERT Person {
first_name := 'Denis',
last_name := 'Villeneuve',
}
),
cast := {
(INSERT Person {
first_name := 'Harrison',
last_name := 'Ford',
}),
(INSERT Person {
first_name := 'Ryan',
last_name := 'Gosling',
}),
(INSERT Person {
first_name := 'Ana',
last_name := 'de Armas',
}),
}
};
INSERT Movie {
title := 'Dune',
director := (
SELECT Person
FILTER
# the last name is sufficient
# to identify the right person
.last_name = 'Villeneuve'
# the LIMIT is needed to satisfy the single
# link requirement validation
LIMIT 1
)
};
''')
await self.assert_query_result(
r'''
SELECT Movie {
title,
year
} ORDER BY .title;
''',
[
{'title': 'Blade Runner 2049', 'year': 2017},
{'title': 'Dune', 'year': None},
],
)
await self.assert_query_result(
r'''
SELECT Movie {
title,
year
}
FILTER .title ILIKE 'blade runner%';
''',
[
{'title': 'Blade Runner 2049', 'year': 2017},
],
)
await self.assert_query_result(
r'''
SELECT Movie {
title,
year,
director: {
first_name,
last_name
},
cast: {
first_name,
last_name
} ORDER BY .last_name
}
FILTER .title ILIKE 'blade runner%';
''',
[{
'title': 'Blade Runner 2049',
'year': 2017,
'director': {
'first_name': 'Denis',
'last_name': 'Villeneuve',
},
'cast': [{
'first_name': 'Harrison',
'last_name': 'Ford',
}, {
'first_name': 'Ryan',
'last_name': 'Gosling',
}, {
'first_name': 'Ana',
'last_name': 'de Armas',
}],
}],
)
await self.assert_query_result(
r'''
SELECT Movie {
title,
num_actors := count(Movie.cast)
} ORDER BY .title;
''',
[
{'title': 'Blade Runner 2049', 'num_actors': 3},
{'title': 'Dune', 'num_actors': 0},
],
)
await self.con.execute(r'''
INSERT Person {
first_name := 'Jason',
last_name := 'Momoa'
};
INSERT Person {
first_name := 'Oscar',
last_name := 'Isaac'
};
''')
await self.con.execute(r'''
ALTER TYPE Person {
ALTER PROPERTY first_name {
SET OPTIONAL;
}
};
''')
await self.con.execute(r'''
INSERT Person {
last_name := 'Zendaya'
};
''')
await self.con.execute(r'''
UPDATE Movie
FILTER Movie.title = 'Dune'
SET {
cast := (
SELECT Person
FILTER .last_name IN {
'Momoa',
'Zendaya',
'Isaac'
}
)
};
''')
await self.con.execute(r'''
START MIGRATION TO {
module default {
type Movie {
required property title -> str;
# the year of release
property year -> int64;
required link director -> Person;
multi link cast -> Person;
}
type Person {
property first_name -> str;
required property last_name -> str;
property name :=
.first_name ++ ' ' ++ .last_name
IF EXISTS .first_name
ELSE .last_name;
}
}
};
POPULATE MIGRATION;
COMMIT MIGRATION;
''')
await self.assert_query_result(
r'''
SELECT Movie {
title,
year,
director: { name },
cast: { name } ORDER BY .name
}
FILTER .title = 'Dune';
''',
[{
'title': 'Dune',
'year': None,
'director': {
'name': 'Denis Villeneuve',
},
'cast': [{
'name': 'Jason Momoa',
}, {
'name': 'Oscar Isaac',
}, {
'name': 'Zendaya',
}],
}],
) | )
await self.assert_query_result(
r | test_edgeql_tutorial | python | geldata/gel | tests/test_edgeql_tutorial.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_tutorial.py | Apache-2.0 |
async def test_edgeql_aliases_basic_02(self):
await self.con.execute('''
CREATE ALIAS expert_map := (
SELECT {
('Alice', 'pro'),
('Bob', 'noob'),
('Carol', 'noob'),
('Dave', 'casual'),
}
);
''')
await self.assert_query_result(
r'''
SELECT expert_map
ORDER BY expert_map;
''',
[
['Alice', 'pro'],
['Bob', 'noob'],
['Carol', 'noob'],
['Dave', 'casual'],
],
)
await self.con.execute('''
DROP ALIAS expert_map;
''') | )
await self.assert_query_result(
r | test_edgeql_aliases_basic_02 | python | geldata/gel | tests/test_edgeql_expr_aliases.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_expr_aliases.py | Apache-2.0 |
async def test_edgeql_aliases_basic_03(self):
await self.con.execute('''
CREATE ALIAS scores := (
SELECT {
(name := 'Alice', score := 100, games := 10),
(name := 'Bob', score := 11, games := 2),
(name := 'Carol', score := 31, games := 5),
(name := 'Dave', score := 78, games := 10),
}
);
''')
await self.assert_query_result(
r'''
SELECT scores ORDER BY scores.name;
''',
[
{'name': 'Alice', 'score': 100, 'games': 10},
{'name': 'Bob', 'score': 11, 'games': 2},
{'name': 'Carol', 'score': 31, 'games': 5},
{'name': 'Dave', 'score': 78, 'games': 10},
],
)
await self.assert_query_result(
r'''
SELECT <tuple<str, int64, int64>>scores
ORDER BY .0;
''',
[
['Alice', 100, 10],
['Bob', 11, 2],
['Carol', 31, 5],
['Dave', 78, 10],
],
)
await self.assert_query_result(
r'''
SELECT <tuple<name: str, points: int64, plays: int64>>scores
ORDER BY .name;
''',
[
{'name': 'Alice', 'points': 100, 'plays': 10},
{'name': 'Bob', 'points': 11, 'plays': 2},
{'name': 'Carol', 'points': 31, 'plays': 5},
{'name': 'Dave', 'points': 78, 'plays': 10},
],
)
await self.con.execute('''
DROP ALIAS scores;
''') | )
await self.assert_query_result(
r | test_edgeql_aliases_basic_03 | python | geldata/gel | tests/test_edgeql_expr_aliases.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_expr_aliases.py | Apache-2.0 |
async def test_edgeql_aliases_basic_04(self):
await self.con.execute('''
CREATE ALIAS levels := {'pro', 'casual', 'noob'};
''')
await self.assert_query_result(
r'''
SELECT levels;
''',
{'pro', 'casual', 'noob'},
) | )
await self.assert_query_result(
r | test_edgeql_aliases_basic_04 | python | geldata/gel | tests/test_edgeql_expr_aliases.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_expr_aliases.py | Apache-2.0 |
async def test_edgeql_aliases_create_01(self):
await self.con.execute(r'''
CREATE ALIAS DCard := (
SELECT Card {
# This is an identical computable to the one
# present in the type, but it must be legal to
# override the link with any compatible
# expression.
owners := (
SELECT Card.<deck[IS User] {
name_upper := str_upper(.name)
}
)
} FILTER Card.name LIKE 'D%'
);
''')
await self.assert_query_result(
r'''
SELECT DCard {
name,
owners: {
name_upper,
} ORDER BY .name
} ORDER BY DCard.name;
''',
[
{
'name': 'Djinn',
'owners': [{'name_upper': 'CAROL'},
{'name_upper': 'DAVE'}],
},
{
'name': 'Dragon',
'owners': [{'name_upper': 'ALICE'},
{'name_upper': 'DAVE'}],
},
{
'name': 'Dwarf',
'owners': [{'name_upper': 'BOB'},
{'name_upper': 'CAROL'}],
}
],
)
await self.con.execute('DROP ALIAS DCard;')
# Check that we can recreate the alias.
await self.con.execute(r'''
CREATE ALIAS DCard := (
SELECT Card {
owners := (
SELECT Card.<deck[IS User] {
name_upper := str_upper(.name)
}
)
} FILTER Card.name LIKE 'D%'
);
''')
await self.assert_query_result(
r'''
WITH
MODULE schema,
DCardT := (SELECT ObjectType
FILTER .name = 'default::DCard'),
DCardOwners := (SELECT DCardT.links
FILTER .name = 'owners')
SELECT
DCardOwners {
target[IS ObjectType]: {
name,
pointers: {
name
} FILTER .name = 'name_upper'
}
}
''',
[{
'target': {
'name': 'default::__DCard__owners',
'pointers': [
{
'name': 'name_upper',
}
]
}
}]
) | )
await self.assert_query_result(
r | test_edgeql_aliases_create_01 | python | geldata/gel | tests/test_edgeql_expr_aliases.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_expr_aliases.py | Apache-2.0 |
async def test_edgeql_aliases_if_else_02(self):
await self.assert_query_result(
r"""
# working with singletons
SELECT
_ := (
for u in User
select 'ok' IF u.deck_cost < 19 ELSE u.deck.name
)
ORDER BY _;
""",
[
'Bog monster',
'Djinn',
'Dragon',
'Giant eagle',
'Giant turtle',
'Golem',
'Sprite',
'ok',
'ok',
'ok',
],
)
await self.assert_query_result(
r"""
# either result is a set, but the condition is a singleton
SELECT
_ := (
for u in User
select u.deck.element IF u.deck_cost < 19
ELSE u.deck.name
)
ORDER BY _;
""",
[
'Air',
'Air',
'Air',
'Bog monster',
'Djinn',
'Dragon',
'Earth',
'Earth',
'Earth',
'Earth',
'Fire',
'Fire',
'Giant eagle',
'Giant turtle',
'Golem',
'Sprite',
'Water',
'Water',
'Water',
'Water',
'Water',
'Water',
],
) | ,
[
'Bog monster',
'Djinn',
'Dragon',
'Giant eagle',
'Giant turtle',
'Golem',
'Sprite',
'ok',
'ok',
'ok',
],
)
await self.assert_query_result(
r | test_edgeql_aliases_if_else_02 | python | geldata/gel | tests/test_edgeql_expr_aliases.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_expr_aliases.py | Apache-2.0 |
async def test_edgeql_aliases_if_else_03(self):
res = [
['Air', 'Air', 'Air', 'Earth', 'Earth', 'Fire', 'Fire', 'Water',
'Water'],
['1', '1', '1', '2', '2', '3', '3', '4', '5'],
[False, False, False, True, True],
]
await self.assert_query_result(
r"""
# get the data that this test relies upon in a format
# that's easy to analyze
SELECT _ := User.deck.element
ORDER BY _;
""",
res[0]
)
await self.assert_query_result(
r"""
SELECT _ := <str>User.deck.cost
ORDER BY _;
""",
res[1]
)
await self.assert_query_result(
r"""
SELECT _ := {User.name[0] = 'A', EXISTS User.friends}
ORDER BY _;
""",
res[2]
)
await self.assert_query_result(
r"""
# results and conditions are sets
SELECT _ :=
User.deck.element
# because the elements of {} are treated as SET OF,
# all of the paths in this expression are independent sets
IF {User.name[0] = 'A', EXISTS User.friends} ELSE
<str>User.deck.cost
ORDER BY _;
""",
sorted(res[1] + res[1] + res[1] + res[0] + res[0]),
) | ,
res[0]
)
await self.assert_query_result(
r | test_edgeql_aliases_if_else_03 | python | geldata/gel | tests/test_edgeql_expr_aliases.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_expr_aliases.py | Apache-2.0 |
async def test_edgeql_aliases_if_else_04(self):
await self.assert_query_result(
r"""
FOR User in User
SELECT
1 IF User.name[0] = 'A' ELSE
10 IF User.name[0] = 'B' ELSE
100 IF User.name[0] = 'C' ELSE
0;
""",
{1, 10, 100, 0},
)
await self.assert_query_result(
r"""
FOR User in User
SELECT (
User.name,
sum((
FOR f in User.friends SELECT
1 IF f.name[0] = 'A' ELSE
10 IF f.name[0] = 'B' ELSE
100 IF f.name[0] = 'C' ELSE
0
)),
) ORDER BY .0;
""",
[['Alice', 110], ['Bob', 0], ['Carol', 0], ['Dave', 10]],
) | ,
{1, 10, 100, 0},
)
await self.assert_query_result(
r | test_edgeql_aliases_if_else_04 | python | geldata/gel | tests/test_edgeql_expr_aliases.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_expr_aliases.py | Apache-2.0 |
async def test_edgeql_aliases_if_else_05(self):
await self.assert_query_result(
r"""
SELECT (
FOR Card in Card
SELECT
(Card.name, 'yes' IF Card.cost > 4 ELSE 'no')
)
ORDER BY .0;
""",
[
['Bog monster', 'no'],
['Djinn', 'no'],
['Dragon', 'yes'],
['Dwarf', 'no'],
['Giant eagle', 'no'],
['Giant turtle', 'no'],
['Golem', 'no'],
['Imp', 'no'],
['Sprite', 'no'],
],
)
await self.assert_query_result(
r"""
SELECT (
FOR Card in Card
SELECT
(Card.name, 'yes') IF Card.cost > 4 ELSE (Card.name, 'no')
)
ORDER BY .0;
""",
[
['Bog monster', 'no'],
['Djinn', 'no'],
['Dragon', 'yes'],
['Dwarf', 'no'],
['Giant eagle', 'no'],
['Giant turtle', 'no'],
['Golem', 'no'],
['Imp', 'no'],
['Sprite', 'no'],
],
) | ,
[
['Bog monster', 'no'],
['Djinn', 'no'],
['Dragon', 'yes'],
['Dwarf', 'no'],
['Giant eagle', 'no'],
['Giant turtle', 'no'],
['Golem', 'no'],
['Imp', 'no'],
['Sprite', 'no'],
],
)
await self.assert_query_result(
r | test_edgeql_aliases_if_else_05 | python | geldata/gel | tests/test_edgeql_expr_aliases.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_expr_aliases.py | Apache-2.0 |
async def test_edgeql_aliases_deep_01(self):
# fetch the result we will compare to
res = await self.con.query_json(r"""
SELECT AwardAlias {
winner: {
deck: {
owners
}
}
}
FILTER .name = '1st'
LIMIT 1;
""")
res = json.loads(res)
# fetch the same data via a different alias, that should be
# functionally identical
await self.assert_query_result(
r"""
SELECT AwardAlias2 {
winner: {
deck: {
owners
}
}
}
FILTER .name = '1st';
""",
res
) | )
res = json.loads(res)
# fetch the same data via a different alias, that should be
# functionally identical
await self.assert_query_result(
r | test_edgeql_aliases_deep_01 | python | geldata/gel | tests/test_edgeql_expr_aliases.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_expr_aliases.py | Apache-2.0 |
async def test_edgeql_aliases_clauses_01(self):
# fetch the result we will compare to
res = await self.con.query_json(r"""
SELECT User {
deck: {
id
} ORDER BY User.deck.cost DESC
LIMIT 1,
}
FILTER .name = 'Alice';
""")
res = json.loads(res)
# fetch the same data via an alias, that should be
# functionally identical
await self.assert_query_result(
r"""
SELECT UserAlias {
deck,
}
FILTER .name = 'Alice';
""",
res
) | )
res = json.loads(res)
# fetch the same data via an alias, that should be
# functionally identical
await self.assert_query_result(
r | test_edgeql_aliases_clauses_01 | python | geldata/gel | tests/test_edgeql_expr_aliases.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_expr_aliases.py | Apache-2.0 |
async def test_edgeql_aliases_limit_01(self):
# Test interaction of aliases and the LIMIT clause
await self.con.execute("""
CREATE ALIAS FirstUser := (
SELECT User {
name_upper := str_upper(User.name)
}
ORDER BY .name
LIMIT 1
);
""")
await self.assert_query_result(
r"""
SELECT FirstUser {
name_upper,
}
""",
[
{
'name_upper': 'ALICE',
},
],
) | )
await self.assert_query_result(
r | test_edgeql_aliases_limit_01 | python | geldata/gel | tests/test_edgeql_expr_aliases.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_expr_aliases.py | Apache-2.0 |
async def test_edgeql_aliases_ignore_alias(self):
await self.con.execute('''
CREATE ALIAS UserAlias2 := (
SELECT User {
deck: {
id
} ORDER BY User.deck.cost DESC
LIMIT 1,
}
);
''')
# Explicitly reset the default module alias to test
# that aliases don't care.
await self.con.execute('''
SET MODULE std;
''')
await self.assert_query_result(
r"""
SELECT default::UserAlias2 {
deck,
}
FILTER .name = 'Alice';
""",
[{
'deck': [
{}
]
}]
) | )
# Explicitly reset the default module alias to test
# that aliases don't care.
await self.con.execute( | test_edgeql_aliases_ignore_alias | python | geldata/gel | tests/test_edgeql_expr_aliases.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_expr_aliases.py | Apache-2.0 |
async def test_edgeql_aliases_esdl_01(self):
await self.assert_query_result(
r"""
SELECT WaterOrEarthCard {
name,
owned_by_alice,
}
FILTER any(.name ILIKE {'%turtle%', 'dwarf'})
ORDER BY .name;
""",
[
{
'name': 'Dwarf',
'owned_by_alice': True,
},
{
'name': 'Giant turtle',
'owned_by_alice': True,
},
]
)
await self.assert_query_result(
r"""
SELECT EarthOrFireCard {
name,
}
FILTER .name IN {'Imp', 'Dwarf'}
ORDER BY .name;
""",
[
{
'name': 'Dwarf'
},
{
'name': 'Imp'
},
]
) | ,
[
{
'name': 'Dwarf',
'owned_by_alice': True,
},
{
'name': 'Giant turtle',
'owned_by_alice': True,
},
]
)
await self.assert_query_result(
r | test_edgeql_aliases_esdl_01 | python | geldata/gel | tests/test_edgeql_expr_aliases.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_expr_aliases.py | Apache-2.0 |
async def test_edgeql_aliases_introspection(self):
await self.assert_query_result(
r"""
WITH MODULE schema
SELECT Type {
name
}
FILTER .from_alias AND .name LIKE 'default::Air%'
ORDER BY .name
""",
[{
'name': 'default::AirCard',
}]
)
await self.con.execute('''
CREATE ALIAS tuple_alias := ('foo', 10);
''')
await self.assert_query_result(
r"""
WITH MODULE schema
SELECT Tuple {
name,
element_types: {
name := .type.name
} ORDER BY @index
}
FILTER
.from_alias
AND .name = 'default::tuple_alias'
ORDER BY .name
""",
[{
'name': 'default::tuple_alias',
'element_types': [{
'name': 'std::str',
}, {
'name': 'std::int64',
}]
}]
)
await self.assert_query_result(
r"""
select schema::Pointer {name, target: {from_alias}}
filter .name = 'winner'
and .source.name = 'default::AwardAlias'
""",
[{"name": "winner", "target": {"from_alias": True}}]
) | ,
[{
'name': 'default::AirCard',
}]
)
await self.con.execute( | test_edgeql_aliases_introspection | python | geldata/gel | tests/test_edgeql_expr_aliases.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_expr_aliases.py | Apache-2.0 |
async def test_edgeql_aliases_schema_types_01(self):
# Scalar alias adds a type
await self.con.execute('''
create alias best_card := 'Dragon';
''')
await self.assert_query_result(
r'''
with module schema select Type { name }
filter .name ilike "%best_card%";
''',
[{'name': 'default::best_card'}]
)
await self.con.execute('''
drop alias best_card;
''')
await self.assert_query_result(
r'''
with module schema select Type { name }
filter .name ilike "%best_card%";
''',
[]
)
await self.con.execute('''
create module my_mod;
create alias my_mod::best_card := 'Dragon';
''')
await self.assert_query_result(
r'''
with module schema select Type { name }
filter .name ilike "%best_card%";
''',
[{'name': 'my_mod::best_card'}]
)
await self.con.execute('''
drop alias my_mod::best_card;
''')
await self.assert_query_result(
r'''
with module schema select Type { name }
filter .name ilike "%best_card%";
''',
[]
) | )
await self.assert_query_result(
r | test_edgeql_aliases_schema_types_01 | python | geldata/gel | tests/test_edgeql_expr_aliases.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_expr_aliases.py | Apache-2.0 |
async def test_edgeql_aliases_schema_types_02(self):
# Object alias adds a type
await self.con.execute('''
create alias best_card := (
select Card filter .name = 'Dragon' limit 1
);
''')
await self.assert_query_result(
r'''
with module schema select Type { name }
filter .name ilike "%best_card%";
''',
[{'name': 'default::best_card'}]
)
await self.con.execute('''
drop alias best_card;
''')
await self.assert_query_result(
r'''
with module schema select Type { name }
filter .name ilike "%best_card%";
''',
[]
)
await self.con.execute('''
create module my_mod;
create alias my_mod::best_card := (
select Card filter .name = 'Dragon' limit 1
);
''')
await self.assert_query_result(
r'''
with module schema select Type { name }
filter .name ilike "%best_card%";
''',
[{'name': 'my_mod::best_card'}]
)
await self.con.execute('''
drop alias my_mod::best_card;
''')
await self.assert_query_result(
r'''
with module schema select Type { name }
filter .name ilike "%best_card%";
''',
[]
) | )
await self.assert_query_result(
r | test_edgeql_aliases_schema_types_02 | python | geldata/gel | tests/test_edgeql_expr_aliases.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_expr_aliases.py | Apache-2.0 |
async def test_edgeql_aliases_schema_types_03(self):
# Object alias with shape adds two types:
# - one for the alias
# - one for the shape
await self.con.execute('''
create alias best_card := (
select Card {name}
filter .name = 'Dragon' limit 1
);
''')
await self.assert_query_result(
r'''
with module schema select Type { name }
filter .name ilike "%best_card%"
order by .name;
''',
[
{'name': 'default::__best_card__Card'},
{'name': 'default::best_card'},
]
)
await self.con.execute('''
drop alias best_card;
''')
await self.assert_query_result(
r'''
with module schema select Type { name }
filter .name ilike "%best_card%";
''',
[]
)
await self.con.execute('''
create module my_mod;
create alias my_mod::best_card := (
select Card {name}
filter .name = 'Dragon' limit 1
);
''')
await self.assert_query_result(
r'''
with module schema select Type { name }
filter .name ilike "%best_card%"
order by .name;
''',
[
{'name': 'my_mod::__best_card__Card'},
{'name': 'my_mod::best_card'},
]
)
await self.con.execute('''
drop alias my_mod::best_card;
''')
await self.assert_query_result(
r'''
with module schema select Type { name }
filter .name ilike "%best_card%";
''',
[]
) | )
await self.assert_query_result(
r | test_edgeql_aliases_schema_types_03 | python | geldata/gel | tests/test_edgeql_expr_aliases.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_expr_aliases.py | Apache-2.0 |
async def _test_proto_state_change_in_tx(self):
# Collect states
await self._execute('''
SET MODULE TestStateChangeInTx
''')
cc = await self.con.recv_match(protocol.CommandComplete)
await self.con.recv_match(protocol.ReadyForCommand)
await self._execute('''
CONFIGURE SESSION SET allow_user_specified_id := true
''', cc=cc)
cc_true = await self.con.recv_match(protocol.CommandComplete)
await self.con.recv_match(protocol.ReadyForCommand)
await self._execute('''
CONFIGURE SESSION SET allow_user_specified_id := false
''')
cc_false = await self.con.recv_match(protocol.CommandComplete)
await self.con.recv_match(protocol.ReadyForCommand)
# Start a transaction that doesn't allow_user_specified_id
await self._execute('START TRANSACTION', cc=cc_false)
await self.con.recv_match(protocol.CommandComplete)
await self.con.recv_match(protocol.ReadyForCommand)
# But insert with session that does allow_user_specified_id
await self._execute('''
INSERT StateChangeInTx {
id := <uuid>'a768e9d5-d908-4072-b370-865b450216ff'
};
''', cc=cc_true)
await self.con.recv_match(
protocol.CommandComplete,
status='INSERT'
)
await self.con.recv_match(protocol.ReadyForCommand) | )
cc = await self.con.recv_match(protocol.CommandComplete)
await self.con.recv_match(protocol.ReadyForCommand)
await self._execute( | _test_proto_state_change_in_tx | python | geldata/gel | tests/test_protocol.py | https://github.com/geldata/gel/blob/master/tests/test_protocol.py | Apache-2.0 |
def test_edgeql_ir_scope_tree_bad_01(self):
"""
SELECT User.deck
FILTER User.name
""" | SELECT User.deck
FILTER User.name | test_edgeql_ir_scope_tree_bad_01 | python | geldata/gel | tests/test_edgeql_ir_scopetree.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ir_scopetree.py | Apache-2.0 |
def test_edgeql_ir_scope_tree_bad_02(self):
"""
SELECT User.deck
FILTER User.deck@count
""" | SELECT User.deck
FILTER User.deck@count | test_edgeql_ir_scope_tree_bad_02 | python | geldata/gel | tests/test_edgeql_ir_scopetree.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ir_scopetree.py | Apache-2.0 |
def test_edgeql_ir_scope_tree_bad_03(self):
"""
SELECT User.deck { foo := User }
""" | SELECT User.deck { foo := User } | test_edgeql_ir_scope_tree_bad_03 | python | geldata/gel | tests/test_edgeql_ir_scopetree.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ir_scopetree.py | Apache-2.0 |
def test_edgeql_ir_scope_tree_bad_04(self):
"""
UPDATE User.deck SET { name := User.name }
""" | UPDATE User.deck SET { name := User.name } | test_edgeql_ir_scope_tree_bad_04 | python | geldata/gel | tests/test_edgeql_ir_scopetree.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ir_scopetree.py | Apache-2.0 |
def test_edgeql_ir_scope_tree_bad_05(self):
"""
WITH
U := User {id, r := random()}
SELECT
(
users := array_agg((SELECT U.id ORDER BY U.r LIMIT 10))
)
""" | WITH
U := User {id, r := random()}
SELECT
(
users := array_agg((SELECT U.id ORDER BY U.r LIMIT 10))
) | test_edgeql_ir_scope_tree_bad_05 | python | geldata/gel | tests/test_edgeql_ir_scopetree.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ir_scopetree.py | Apache-2.0 |
def test_edgeql_ir_scope_tree_bad_06(self):
"""
UPDATE User SET { avatar := (UPDATE .avatar SET { text := "foo" }) }
""" | UPDATE User SET { avatar := (UPDATE .avatar SET { text := "foo" }) } | test_edgeql_ir_scope_tree_bad_06 | python | geldata/gel | tests/test_edgeql_ir_scopetree.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_ir_scopetree.py | Apache-2.0 |
async def test_edgeql_policies_01(self):
await self.con.execute('''
set global cur_owner_active := false;
''')
await self.con.execute('''
set global watchers_active := false;
''')
await self.con.execute('''
set global filter_owned := True;
''')
await self.assert_query_result(
r'''
select Owned { [IS Named].name }
''',
[]
)
await self.assert_query_result(
r'''
select Issue { name }
''',
[]
) | )
await self.con.execute( | test_edgeql_policies_01 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_02a(self):
await self.con.execute('''
set global cur_user := 'Yury';
''')
await self.con.execute('''
set global filter_owned := True;
''')
await self.assert_query_result(
r'''
select Owned { [IS Named].name }
''',
tb.bag([
{"name": "Release EdgeDB"},
{"name": "Improve EdgeDB repl output rendering."},
{"name": "Repl tweak."},
])
)
await self.assert_query_result(
r'''
select Issue { name }
''',
tb.bag([
{"name": "Release EdgeDB"},
{"name": "Improve EdgeDB repl output rendering."},
{"name": "Repl tweak."},
])
) | )
await self.con.execute( | test_edgeql_policies_02a | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_02b(self):
await self.con.execute('''
alter type Owned reset abstract;
''')
await self.con.execute('''
set global cur_user := 'Yury';
''')
await self.con.execute('''
set global filter_owned := True;
''')
await self.assert_query_result(
r'''
select Owned { [IS Named].name }
''',
tb.bag([
{"name": "Release EdgeDB"},
{"name": "Improve EdgeDB repl output rendering."},
{"name": "Repl tweak."},
])
)
await self.assert_query_result(
r'''
select Issue { name }
''',
tb.bag([
{"name": "Release EdgeDB"},
{"name": "Improve EdgeDB repl output rendering."},
{"name": "Repl tweak."},
])
) | )
await self.con.execute( | test_edgeql_policies_02b | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_03(self):
vals = await self.con.query('''
select Object.id
''')
self.assertEqual(len(vals), len(set(vals)))
await self.con.execute('''
create alias foo := Issue;
''')
vals = await self.con.query('''
select BaseObject.id
''')
self.assertEqual(len(vals), len(set(vals))) | )
self.assertEqual(len(vals), len(set(vals)))
await self.con.execute( | test_edgeql_policies_03 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_04(self):
await self.con.execute('''
set global cur_user := 'Phil';
''')
await self.con.execute('''
set global filter_owned := True;
''')
await self.assert_query_result(
r'''
select URL { src := .<references[IS User] }
''',
tb.bag([
{"src": []}
])
)
await self.assert_query_result(
r'''
select URL { src := .<references }
''',
tb.bag([
{"src": []}
])
) | )
await self.con.execute( | test_edgeql_policies_04 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_05a(self):
await self.con.execute('''
CREATE TYPE Tgt {
CREATE REQUIRED PROPERTY b -> bool;
CREATE ACCESS POLICY redact
ALLOW SELECT USING (not global filter_owned);
CREATE ACCESS POLICY dml_always
ALLOW UPDATE, INSERT, DELETE;
};
CREATE TYPE Ptr {
CREATE REQUIRED LINK tgt -> Tgt;
CREATE PROPERTY tb := .tgt.b;
};
''')
await self.con.query('''
insert Ptr { tgt := (insert Tgt { b := True }) };
''')
await self.con.execute('''
set global filter_owned := True;
''')
async with self.assertRaisesRegexTx(
edgedb.CardinalityViolationError,
r"is hidden by access policy"):
await self.con.query('''
select Ptr { tgt }
''')
async with self.assertRaisesRegexTx(
edgedb.CardinalityViolationError,
r"is hidden by access policy"):
await self.con.query('''
select Ptr { z := .tgt.b }
''')
async with self.assertRaisesRegexTx(
edgedb.CardinalityViolationError,
r"is hidden by access policy"):
await self.con.query('''
select Ptr { z := .tgt.id }
''')
async with self.assertRaisesRegexTx(
edgedb.CardinalityViolationError,
r"required link 'tgt' of object type 'default::Ptr' is "
r"hidden by access policy \(while evaluating computed "
r"property 'tb' of object type 'default::Ptr'\)"):
await self.con.query('''
select Ptr { tb }
''')
async with self.assertRaisesRegexTx(
edgedb.CardinalityViolationError,
r"is hidden by access policy"):
await self.con.query('''
select Ptr.tgt
''')
async with self.assertRaisesRegexTx(
edgedb.CardinalityViolationError,
r"is hidden by access policy"):
await self.con.query('''
select Ptr.tgt.b
''')
await self.con.query('''
delete Ptr
''')
await self.assert_query_result(
r''' select Ptr { tgt }''',
[],
)
await self.assert_query_result(
r''' select Ptr.tgt''',
[],
)
await self.assert_query_result(
r''' select Ptr.tgt.b''',
[],
) | )
await self.con.query( | test_edgeql_policies_05a | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_05b(self):
await self.con.execute('''
CREATE TYPE Tgt {
CREATE REQUIRED PROPERTY b -> bool;
CREATE ACCESS POLICY redact
ALLOW SELECT USING (not global filter_owned);
CREATE ACCESS POLICY dml_always
ALLOW UPDATE, INSERT, DELETE;
};
CREATE TYPE Ptr {
CREATE REQUIRED LINK tgt -> Tgt;
CREATE PROPERTY tb := .tgt.b;
CREATE ACCESS POLICY redact
ALLOW SELECT USING (.tgt.b);
CREATE ACCESS POLICY dml_always
ALLOW UPDATE, INSERT, DELETE;
};
''')
await self.con.query('''
insert Ptr { tgt := (insert Tgt { b := True }) };
''')
await self.con.execute('''
set global filter_owned := True;
''')
async with self.assertRaisesRegexTx(
edgedb.CardinalityViolationError,
r"is hidden by access policy"):
await self.con.query('''
select Ptr { tgt }
''') | )
await self.con.query( | test_edgeql_policies_05b | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_06(self):
await self.con.execute('''
CREATE TYPE Tgt {
CREATE REQUIRED PROPERTY b -> bool;
CREATE ACCESS POLICY redact
ALLOW SELECT USING (not global filter_owned);
CREATE ACCESS POLICY dml_always
ALLOW UPDATE, INSERT, DELETE;
};
CREATE TYPE BadTgt;
CREATE TYPE Ptr {
CREATE REQUIRED LINK tgt -> Tgt | BadTgt;
};
''')
await self.con.query('''
insert Ptr { tgt := (insert Tgt { b := True }) };
''')
await self.con.execute('''
set global filter_owned := True;
''')
async with self.assertRaisesRegexTx(
edgedb.CardinalityViolationError,
r"is hidden by access policy"):
await self.con.query('''
select Ptr { tgt }
''') | )
await self.con.query( | test_edgeql_policies_06 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_07(self):
# test update policies
await self.con.execute('''
set global filter_owned := True;
''')
await self.con.execute('''
set global cur_user := 'Yury';
''')
await self.assert_query_result(
'''
select Issue { name } filter .number = '1'
''',
[{"name": "Release EdgeDB"}],
)
# Shouldn't work
await self.assert_query_result(
'''
update Issue filter .number = '1' set { name := "!" }
''',
[],
)
await self.assert_query_result(
'''
delete Issue filter .number = '1'
''',
[],
)
await self.assert_query_result(
'''
select Issue { name } filter .number = '1'
''',
[{"name": "Release EdgeDB"}],
)
async with self.assertRaisesRegexTx(
edgedb.InvalidValueError,
r"access policy violation on update of default::Issue"):
await self.con.query('''
update Issue filter .number = "2"
set { owner := (select User filter .name = 'Elvis') };
''')
# This update *should* work, though
await self.assert_query_result(
'''
update Issue filter .number = '2' set { name := "!" }
''',
[{}],
)
await self.assert_query_result(
'''
select Issue { name } filter .number = '2'
''',
[{"name": "!"}],
)
# Now try updating Named, based on name
# This one should work
await self.assert_query_result(
'''
update Named filter .name = '!' set { name := "Fix bug" }
''',
[{}],
)
await self.assert_query_result(
'''
select Issue { name } filter .number = '2'
''',
[{"name": "Fix bug"}],
)
# This shouldn't work
await self.assert_query_result(
'''
update Named filter .name = 'Release EdgeDB'
set { name := "?" }
''',
[],
)
await self.assert_query_result(
'''
select Issue { name } filter .number = '1'
''',
[{"name": "Release EdgeDB"}],
)
async with self.assertRaisesRegexTx(
edgedb.InvalidValueError,
"access policy violation"):
await self.con.query('''
INSERT Issue {
number := '4',
name := 'Regression.',
body := 'Fix regression introduced by lexer tweak.',
owner := (SELECT User FILTER User.name = 'Elvis'),
status := (SELECT Status FILTER Status.name = 'Closed'),
} UNLESS CONFLICT ON (.number) ELSE Issue;
''') | )
await self.con.execute( | test_edgeql_policies_07 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_09(self):
# Create a type that we can write but not view
await self.con.execute('''
create type X extending Dictionary {
create access policy can_insert allow insert;
};
insert X { name := "!" };
''')
# We need to raise a constraint violation error even though
# we are trying to do unless conflict, because we can't see
# the conflicting object!
async with self.assertRaisesRegexTx(
edgedb.ConstraintViolationError,
r"name violates exclusivity constraint"):
await self.con.query('''
insert X { name := "!" }
unless conflict on (.name) else (select X)
''') | )
# We need to raise a constraint violation error even though
# we are trying to do unless conflict, because we can't see
# the conflicting object!
async with self.assertRaisesRegexTx(
edgedb.ConstraintViolationError,
r"name violates exclusivity constraint"):
await self.con.query( | test_edgeql_policies_09 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_12(self):
await self.con.query('''
create global cur_user_obj := (
select User filter .name = global cur_user);
alter type User {
create access policy allow_self_obj
allow all
using (__subject__ ?= global cur_user_obj);
}
''')
async with self.assertRaisesRegexTx(
edgedb.InvalidValueError,
r"access policy violation on insert of default::User"):
await self.con.query('''
insert User { name := 'whatever' }
''') | )
async with self.assertRaisesRegexTx(
edgedb.InvalidValueError,
r"access policy violation on insert of default::User"):
await self.con.query( | test_edgeql_policies_12 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_order_01(self):
await self.con.execute('''
insert CurOnly { name := "!" }
''')
await self.con.execute('''
set global cur_user := "?"
''')
async with self.assertRaisesRegexTx(
edgedb.InvalidValueError,
r"access policy violation on insert of default::CurOnly"):
await self.con.query('''
insert CurOnly { name := "!" }
''') | )
await self.con.execute( | test_edgeql_policies_order_01 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_order_02(self):
await self.con.execute('''
insert CurOnly { name := "!" };
insert CurOnly { name := "?" };
''')
await self.con.execute('''
set global cur_user := "?"
''')
async with self.assertRaisesRegexTx(
edgedb.InvalidValueError,
r"access policy violation on update of default::CurOnly"):
await self.con.query('''
update CurOnly set { name := "!" }
''') | )
await self.con.execute( | test_edgeql_policies_order_02 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_order_03(self):
await self.con.execute('''
insert CurOnlyM { name := "!" }
''')
await self.con.execute('''
set global cur_user := "?"
''')
async with self.assertRaisesRegexTx(
edgedb.InvalidValueError,
r"access policy violation on insert of default::CurOnlyM"):
await self.con.query('''
insert CurOnlyM { name := "!" }
''') | )
await self.con.execute( | test_edgeql_policies_order_03 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_order_04(self):
await self.con.execute('''
insert CurOnlyM { name := "!" };
insert CurOnlyM { name := "?" };
''')
await self.con.execute('''
set global cur_user := "?"
''')
async with self.assertRaisesRegexTx(
edgedb.InvalidValueError,
r"access policy violation on update of default::CurOnlyM"):
await self.con.query('''
update CurOnlyM set { name := "!" }
''') | )
await self.con.execute( | test_edgeql_policies_order_04 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_scope_01(self):
await self.con.execute('''
create type Foo {
create required property val -> int64;
};
''')
async with self.assertRaisesRegexTx(
edgedb.SchemaDefinitionError,
r'possibly an empty set returned'):
await self.con.execute('''
alter type Foo {
create access policy pol allow all using(Foo.val > 5);
};
''') | )
async with self.assertRaisesRegexTx(
edgedb.SchemaDefinitionError,
r'possibly an empty set returned'):
await self.con.execute( | test_edgeql_policies_scope_01 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_binding_01(self):
await self.con.execute('''
CREATE TYPE Foo {
CREATE REQUIRED PROPERTY val -> int64;
};
CREATE TYPE Bar EXTENDING Foo;
ALTER TYPE Foo {
CREATE ACCESS POLICY ap0
ALLOW ALL USING ((count(Bar) = 0));
};
''')
await self.con.execute('''
insert Foo { val := 0 };
insert Foo { val := 1 };
insert Bar { val := 10 };
''')
await self.assert_query_result(
r'''
select Foo
''',
[]
)
await self.assert_query_result(
r'''
select Bar
''',
[]
) | )
await self.con.execute( | test_edgeql_policies_binding_01 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_binding_02(self):
await self.con.execute('''
CREATE TYPE Foo {
CREATE REQUIRED PROPERTY val -> int64;
};
CREATE TYPE Bar EXTENDING Foo;
ALTER TYPE Foo {
CREATE ACCESS POLICY ins ALLOW INSERT;
CREATE ACCESS POLICY ap0
ALLOW ALL USING (
not exists (select Foo filter .val = 1));
};
''')
await self.con.execute('''
insert Foo { val := 0 };
insert Foo { val := 1 };
insert Bar { val := 10 };
''')
await self.assert_query_result(
r'''
select Foo
''',
[]
)
await self.assert_query_result(
r'''
select Bar
''',
[]
) | )
await self.con.execute( | test_edgeql_policies_binding_02 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_binding_03(self):
await self.con.execute('''
CREATE TYPE Foo {
CREATE REQUIRED PROPERTY val -> int64;
};
CREATE TYPE Bar EXTENDING Foo;
ALTER TYPE Foo {
CREATE MULTI LINK bar -> Bar;
};
insert Foo { val := 0 };
insert Foo { val := 1 };
insert Bar { val := 10 };
update Foo set { bar := Bar };
ALTER TYPE Foo {
CREATE ACCESS POLICY ap0
ALLOW ALL USING (not exists .bar);
};
''')
await self.assert_query_result(
r'''
select Foo
''',
[]
)
await self.assert_query_result(
r'''
select Bar
''',
[]
) | )
await self.assert_query_result(
r | test_edgeql_policies_binding_03 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_binding_04(self):
await self.con.execute('''
CREATE TYPE Foo {
CREATE REQUIRED PROPERTY val -> int64;
CREATE MULTI LINK foo -> Foo;
};
CREATE TYPE Bar EXTENDING Foo;
insert Foo { val := 0 };
insert Foo { val := 1 };
insert Bar { val := 10 };
update Foo set { foo := Foo };
ALTER TYPE Foo {
CREATE ACCESS POLICY ap0
ALLOW ALL USING (not exists .foo);
};
''')
await self.assert_query_result(
r'''
select Foo
''',
[]
)
await self.assert_query_result(
r'''
select Bar
''',
[]
) | )
await self.assert_query_result(
r | test_edgeql_policies_binding_04 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_missing_prop_01(self):
await self.con.execute('''
CREATE TYPE A {
CREATE PROPERTY ts -> datetime;
CREATE ACCESS POLICY soft_delete
ALLOW SELECT, UPDATE READ, INSERT
USING (NOT (EXISTS (.ts)));
CREATE ACCESS POLICY update_write
ALLOW UPDATE WRITE;
}
''')
await self.con.execute('''
insert A;
''') | )
await self.con.execute( | test_edgeql_policies_missing_prop_01 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_missing_prop_02(self):
await self.con.execute('''
CREATE TYPE A {
CREATE MULTI PROPERTY ts -> datetime;
CREATE ACCESS POLICY soft_delete
ALLOW SELECT, UPDATE READ, INSERT
USING (NOT (EXISTS (.ts)));
CREATE ACCESS POLICY update_write
ALLOW UPDATE WRITE;
}
''')
await self.con.execute('''
insert A;
''') | )
await self.con.execute( | test_edgeql_policies_missing_prop_02 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_delete_union_01(self):
await self.con.execute('''
create type T {
create access policy insert_select
allow insert, select;
};
create type S;
insert T;
''')
await self.assert_query_result(
r'''
delete {T, S};
''',
[]
) | )
await self.assert_query_result(
r | test_edgeql_policies_delete_union_01 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_multi_object_01(self):
await self.con.execute('''
create global bypassAccessPolicies -> bool;
create type User2 {
create access policy bypass_access_policies
allow all using (
(global bypassAccessPolicies ?? false));
create required property username -> str {
create constraint exclusive;
};
};
create global sessionToken -> str;
create type Session {
create required property token -> str;
create required link user -> User2;
};
create global session := (select
Session filter
(.token ?= global sessionToken)
limit
1
);
alter type User2 {
create access policy admin_has_full_access
allow all using (
((global session).user.username ?= 'admin'));
};
''')
await self.con.execute('''
set global bypassAccessPolicies := true;
''')
await self.con.execute('''
insert User2 { username := "admin" }
''')
await self.con.execute('''
insert User2 { username := "admin" } unless conflict
''')
await self.assert_query_result(
r'''
select User2 { username }
''',
[{'username': 'admin'}]
) | )
await self.con.execute( | test_edgeql_policies_multi_object_01 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_internal_shape_01(self):
await self.con.execute('''
alter type Issue {
create access policy foo_1 deny all using (
not exists (select .watchers { foo := .todo }
filter "x" in .foo.name));
create access policy foo_2 deny all using (
not exists (select .watchers { todo }
filter "x" in .todo.name));
};
''')
await self.assert_query_result(
r'''
select Issue
''',
[],
)
async with self.assertRaisesRegexTx(
edgedb.InvalidValueError,
"access policy violation on insert",
):
await self.con.execute('''
insert Issue {
name := '', body := '', status := {}, number := '',
owner := {}};
''') | )
await self.assert_query_result(
r | test_edgeql_policies_internal_shape_01 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_volatile_01(self):
await self.con.execute('''
create type Bar {
create required property r -> float64;
create access policy ok allow all;
create access policy no deny
update write, insert using (.r <= 0.5);
};
''')
for _ in range(10):
async with self._run_and_rollback():
try:
await self.con.execute('''
insert Bar { r := random() };
''')
except edgedb.AccessPolicyError:
# If it failed, nothing to do, keep trying
pass
else:
r = (await self.con.query('''
select Bar.r
'''))[0]
self.assertGreater(r, 0.5)
await self.con.execute('''
insert Bar { r := 1.0 };
''')
for _ in range(10):
async with self._run_and_rollback():
try:
await self.con.execute('''
update Bar set { r := random() };
''')
except edgedb.AccessPolicyError:
# If it failed, nothing to do, keep trying
pass
else:
r = (await self.con.query('''
select Bar.r
'''))[0]
self.assertGreater(r, 0.5) | )
for _ in range(10):
async with self._run_and_rollback():
try:
await self.con.execute( | test_edgeql_policies_volatile_01 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_volatile_02(self):
# Same as above but multi
await self.con.execute('''
create type Bar {
create required multi property r -> float64;
create access policy ok allow all;
create access policy no deny
update write, insert using (all(.r <= 0.5));
};
''')
for _ in range(10):
async with self._run_and_rollback():
try:
await self.con.execute('''
insert Bar { r := random() };
''')
except edgedb.AccessPolicyError:
# If it failed, nothing to do, keep trying
pass
else:
r = (await self.con.query('''
select Bar.r
'''))[0]
self.assertGreater(r, 0.5)
await self.con.execute('''
insert Bar { r := 1.0 };
''')
for _ in range(10):
async with self._run_and_rollback():
try:
await self.con.execute('''
update Bar set { r := random() };
''')
except edgedb.AccessPolicyError:
# If it failed, nothing to do, keep trying
pass
else:
r = (await self.con.query('''
select Bar.r
'''))[0]
self.assertGreater(r, 0.5) | )
for _ in range(10):
async with self._run_and_rollback():
try:
await self.con.execute( | test_edgeql_policies_volatile_02 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_messages(self):
await self.con.execute(
'''
create type NoAllows {
create access policy allow_select
allow select;
};
create type TwoAllows {
create required property val -> str;
create access policy allow_insert_of_a
allow insert using (.val = 'a')
{ set errmessage := 'you can insert a' };
create access policy allow_insert_of_b
allow insert using (.val = 'b');
};
create type ThreeDenies {
create required property val -> str;
create access policy allow_insert
allow insert;
create access policy deny_starting_with_f
deny insert using (.val[0] = 'f')
{ set errmessage := 'val cannot start with f' };
create access policy deny_foo
deny insert using (.val = 'foo')
{ set errmessage := 'val cannot be foo' };
create access policy deny_bar
deny insert using (.val = 'bar');
};
'''
)
await self.con.execute("insert TwoAllows { val := 'a' };")
async with self.assertRaisesRegexTx(
edgedb.InvalidValueError,
r"access policy violation on insert of default::NoAllows$",
):
await self.con.query('insert NoAllows')
async with self.assertRaisesRegexTx(
edgedb.InvalidValueError, r"\(you can insert a\)$"
):
await self.con.query("insert TwoAllows { val := 'c' }")
async with self.assertRaisesRegexTx(
edgedb.InvalidValueError,
"access policy violation.*val cannot.*val cannot",
):
await self.con.query("insert ThreeDenies { val := 'foo' }")
async with self.assertRaisesRegexTx(
edgedb.InvalidValueError,
"access policy violation on insert of default::ThreeDenies$",
):
await self.con.query("insert ThreeDenies { val := 'bar' }") | create type NoAllows {
create access policy allow_select
allow select;
};
create type TwoAllows {
create required property val -> str;
create access policy allow_insert_of_a
allow insert using (.val = 'a')
{ set errmessage := 'you can insert a' };
create access policy allow_insert_of_b
allow insert using (.val = 'b');
};
create type ThreeDenies {
create required property val -> str;
create access policy allow_insert
allow insert;
create access policy deny_starting_with_f
deny insert using (.val[0] = 'f')
{ set errmessage := 'val cannot start with f' };
create access policy deny_foo
deny insert using (.val = 'foo')
{ set errmessage := 'val cannot be foo' };
create access policy deny_bar
deny insert using (.val = 'bar');
}; | test_edgeql_policies_messages | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_namespace(self):
# ... we were accidentally skipping some important fixups in
# access policy compilation
await self.con.execute(
'''
create type X {
create access policy foo
allow all using (
count((
WITH X := {1, 2}
SELECT (X, (FOR x in {X} UNION (SELECT x)))
)) = 2);
};
insert X;
'''
)
await self.assert_query_result(
r'''select X''',
[{}],
) | create type X {
create access policy foo
allow all using (
count((
WITH X := {1, 2}
SELECT (X, (FOR x in {X} UNION (SELECT x)))
)) = 2);
};
insert X; | test_edgeql_policies_namespace | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_function_01(self):
await self.con.execute('''
set global filter_owned := true;
''')
await self.assert_query_result(
r'''select (count(Issue), count_Issue())''',
[(0, 0)],
)
await self.con.execute('''
configure session set apply_access_policies := false;
''')
await self.assert_query_result(
r'''select (count(Issue), count_Issue())''',
[(4, 4)],
) | )
await self.assert_query_result(
r'''select (count(Issue), count_Issue())''',
[(0, 0)],
)
await self.con.execute( | test_edgeql_policies_function_01 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_complex_01(self):
await self.migrate(
"""
abstract type Auditable {
access policy auditable_default
allow all ;
access policy auditable_prohibit_hard_deletes
deny delete {
errmessage := 'hard deletes are disallowed';
};
delegated constraint std::expression on
((.updated_at >= .created_at))
except (NOT (EXISTS (.updated_at)));
delegated constraint std::expression on
((.deleted_at > .created_at))
except (NOT (EXISTS (.deleted_at)));
required property created_at: std::datetime {
default := (std::datetime_of_statement());
readonly := true;
};
property deleted_at: std::datetime;
required property uid: std::str {
default := <str>random();
readonly := true;
constraint std::exclusive;
};
property updated_at: std::datetime {
rewrite
update
using (std::datetime_of_statement());
};
};
type Avatar extending default::Auditable {
link owner := (.<avatar[is default::Member]);
required property url: std::str;
};
type Member extending default::Auditable {
link avatar: default::Avatar {
on source delete delete target if orphan;
on target delete allow;
constraint std::exclusive;
};
};
"""
)
await self.con.execute(
'''
update Avatar set {deleted_at:=datetime_of_statement()};
'''
) | abstract type Auditable {
access policy auditable_default
allow all ;
access policy auditable_prohibit_hard_deletes
deny delete {
errmessage := 'hard deletes are disallowed';
};
delegated constraint std::expression on
((.updated_at >= .created_at))
except (NOT (EXISTS (.updated_at)));
delegated constraint std::expression on
((.deleted_at > .created_at))
except (NOT (EXISTS (.deleted_at)));
required property created_at: std::datetime {
default := (std::datetime_of_statement());
readonly := true;
};
property deleted_at: std::datetime;
required property uid: std::str {
default := <str>random();
readonly := true;
constraint std::exclusive;
};
property updated_at: std::datetime {
rewrite
update
using (std::datetime_of_statement());
};
};
type Avatar extending default::Auditable {
link owner := (.<avatar[is default::Member]);
required property url: std::str;
};
type Member extending default::Auditable {
link avatar: default::Avatar {
on source delete delete target if orphan;
on target delete allow;
constraint std::exclusive;
};
}; | test_edgeql_policies_complex_01 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_optional_leakage_01(self):
await self.con.execute(
'''
CREATE GLOBAL current_user -> uuid;
CREATE TYPE Org {
CREATE REQUIRED PROPERTY domain -> str {
CREATE CONSTRAINT exclusive;
};
CREATE PROPERTY name -> str;
};
CREATE TYPE User2 {
CREATE REQUIRED LINK org -> Org;
CREATE REQUIRED PROPERTY email -> str {
CREATE CONSTRAINT exclusive;
};
};
CREATE GLOBAL current_user_object := (SELECT
User2
FILTER
(.id = GLOBAL current_user)
);
CREATE TYPE Src {
CREATE REQUIRED SINGLE LINK org -> Org;
CREATE SINGLE LINK user -> User2;
CREATE ACCESS POLICY deny_no
DENY ALL USING (
(((GLOBAL current_user_object).org != .org) ?? true));
CREATE ACCESS POLICY yes
ALLOW ALL USING (SELECT
((GLOBAL current_user = .user.id) ?? false)
);
};
'''
)
await self.con.execute('''
configure session set apply_access_policies := false;
''')
await self.con.execute('''
insert User2 {
email:= "[email protected]",
org:=(insert Org {domain:="a.com"}),
};
''')
res = await self.con.query_single('''
insert User2 {
email:= "[email protected]",
org:=(insert Org {domain:="b.com"}),
};
''')
await self.con.execute('''
insert Src {
org := (select Org filter .domain = "a.com")
};
''')
await self.con.execute('''
configure session set apply_access_policies := true;
''')
await self.con.execute(f'''
set global current_user := <uuid>'{res.id}';
''')
await self.assert_query_result(
r'''select Src''',
[],
) | CREATE GLOBAL current_user -> uuid;
CREATE TYPE Org {
CREATE REQUIRED PROPERTY domain -> str {
CREATE CONSTRAINT exclusive;
};
CREATE PROPERTY name -> str;
};
CREATE TYPE User2 {
CREATE REQUIRED LINK org -> Org;
CREATE REQUIRED PROPERTY email -> str {
CREATE CONSTRAINT exclusive;
};
};
CREATE GLOBAL current_user_object := (SELECT
User2
FILTER
(.id = GLOBAL current_user)
);
CREATE TYPE Src {
CREATE REQUIRED SINGLE LINK org -> Org;
CREATE SINGLE LINK user -> User2;
CREATE ACCESS POLICY deny_no
DENY ALL USING (
(((GLOBAL current_user_object).org != .org) ?? true));
CREATE ACCESS POLICY yes
ALLOW ALL USING (SELECT
((GLOBAL current_user = .user.id) ?? false)
);
}; | test_edgeql_policies_optional_leakage_01 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_parent_update_01(self):
await self.con.execute('''
CREATE ABSTRACT TYPE Base {
CREATE PROPERTY name: std::str;
CREATE ACCESS POLICY sel_ins
ALLOW SELECT, INSERT USING (true);
};
CREATE TYPE Child EXTENDING Base;
INSERT Child;
''')
await self.assert_query_result(
'''
update Base set { name := '!!!' }
''',
[],
)
await self.assert_query_result(
'''
delete Base
''',
[],
)
await self.con.execute('''
ALTER TYPE Base {
CREATE ACCESS POLICY upd_read
ALLOW UPDATE READ USING (true);
};
''')
async with self.assertRaisesRegexTx(
edgedb.InvalidValueError,
r"access policy violation on update"):
await self.con.query('''
update Base set { name := '!!!' }
''') | )
await self.assert_query_result( | test_edgeql_policies_parent_update_01 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_global_01(self):
# GH issue #6404
clan_and_global = '''
type Clan {
access policy allow_select_players
allow select
using (
global current_player.clan.id ?= .id
);
};
global current_player_id: uuid;
global current_player := (
select Player filter .id = global current_player_id
);
'''
await self.migrate(
'''
type Principal;
type Player extending Principal {
required link clan: Clan;
}
''' + clan_and_global
)
await self.migrate(
'''
type Player {
required link clan: Clan;
}
''' + clan_and_global
) | await self.migrate( | test_edgeql_policies_global_01 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
async def test_edgeql_policies_global_02(self):
await self.con.execute('''
create type T {
create access policy ok allow all;
create access policy no deny select;
};
insert T;
create global foo := (select T limit 1);
create type S {
create access policy ok allow all using (exists global foo)
};
''')
await self.assert_query_result(
r'''
select { s := S, foo := global foo };
''',
[{"s": [], "foo": None}]
) | )
await self.assert_query_result(
r | test_edgeql_policies_global_02 | python | geldata/gel | tests/test_edgeql_policies.py | https://github.com/geldata/gel/blob/master/tests/test_edgeql_policies.py | Apache-2.0 |
def get_setup_script(cls):
res = super().get_setup_script()
# HACK: As a debugging cycle hack, when RELOAD is true, we reload the
# extension package from the file, so we can test without a bootstrap.
RELOAD = False
if RELOAD:
root = pathlib.Path(__file__).parent.parent
with open(root / 'edb/lib/ext/ai.edgeql') as f:
contents = f.read()
to_add = (
'''
drop extension package ai version '1.0';
create extension ai;
'''
+ contents
)
splice = '__internal_testmode := true;'
res = res.replace(splice, splice + to_add)
return res | drop extension package ai version '1.0';
create extension ai; | get_setup_script | python | geldata/gel | tests/test_ext_ai.py | https://github.com/geldata/gel/blob/master/tests/test_ext_ai.py | Apache-2.0 |
async def test_ext_ai_indexing_01(self):
try:
await self.con.execute(
"""
insert Astronomy {
content := 'Skies on Mars are red'
};
insert Astronomy {
content := 'Skies on Earth are blue'
};
""",
)
await self.assert_query_result(
'''
select _ := ext::ai::to_context((select Astronomy))
order by _
''',
[
'Skies on Earth are blue',
'Skies on Mars are red',
],
)
async for tr in self.try_until_succeeds(
ignore=(AssertionError,),
timeout=30.0,
):
async with tr:
await self.assert_query_result(
r'''
with
result := ext::ai::search(
Astronomy, <array<float32>>$qv)
select
result.object {
content,
distance := result.distance,
}
order by
result.distance asc empty last
then result.object.content
''',
[
{
'content': 'Skies on Earth are blue',
'distance': 0.3675444679663241,
},
{
'content': 'Skies on Mars are red',
'distance': 0.4284523933505918,
},
],
variables={
"qv": [1 for i in range(10)],
}
)
finally:
await self.con.execute('''
delete Astronomy;
''') | insert Astronomy {
content := 'Skies on Mars are red'
};
insert Astronomy {
content := 'Skies on Earth are blue'
};
""",
)
await self.assert_query_result( | test_ext_ai_indexing_01 | python | geldata/gel | tests/test_ext_ai.py | https://github.com/geldata/gel/blob/master/tests/test_ext_ai.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.