Skip to content

Commit

Permalink
Upd set relation check. Strict check for Id
Browse files Browse the repository at this point in the history
  • Loading branch information
joente committed Feb 9, 2024
1 parent f2d437d commit 8c5edd6
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* Added bit-wise NOT (`~`) operator.
* Return `nil` instead of success error when a repeating task is successful.
* Corrected a spelling mistake in error message for integer range values.
* No longer allow a relation between a none-stored set and a none-stored value.

# v1.4.16

Expand Down
98 changes: 92 additions & 6 deletions itest/test_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ async def test_errors(self, client):
''')

res = await client.query(r'''
f = F{};
.f = F{};
f2 = F{};
f3 = F{};
f.fff.add(f);
f.fff.add(f2);
.f.fff.add(.f);
.f.fff.add(f2);
mod_type('F', 'ren', 'fff', 'others');
f.others.add(f3);
[f.others.len(), f2.others.len(), f3.others.len()];
.f.others.add(f3);
[.f.others.len(), f2.others.len(), f3.others.len()];
''')
self.assertEqual(res, [3, 1, 1])

Expand Down Expand Up @@ -822,7 +822,7 @@ async def test_set_to_set(self, client):
'''), 'OK')

self.assertEqual(await client.query(r'''
c = C{};
.c = c = C{};
cc = C{};
c.c.add(c);
c.c.add(cc);
Expand Down Expand Up @@ -1545,6 +1545,92 @@ async def test_set_operation_complex(self, client0):
assert (.sp.p == set(.Iris));
""")

async def test_strict_rel(self, client):
await client.query(r"""//ti
new_type("A");
new_type("B");
new_type("C");
set_type("A", {one: 'A?'});
set_type("B", {one: 'B?', many: '{B}'});
set_type("C", {many: '{C}'});
mod_type("A", "rel", "one", "one");
mod_type("B", "rel", "one", "many");
mod_type("C", "rel", "many", "many");
""")

with self.assertRaisesRegex(
TypeError,
r'mismatch in type `A` on property `one`; '
r'relations must be created using a property on '
r'a stored thing \(a thing with an Id\)'):
await client.query(r"""//ti
a = A{};
a.one = a;
""")

with self.assertRaisesRegex(
TypeError,
r'mismatch in type `A` on property `one`; '
r'relations must be created using a property on '
r'a stored thing \(a thing with an Id\)'):
await client.query(r"""//ti
a = A{};
a.set("one", a);
""")

with self.assertRaisesRegex(
TypeError,
r'mismatch in type `B` on property `one`; '
r'relations must be created using a property on '
r'a stored thing \(a thing with an Id\)'):
await client.query(r"""//ti
b = B{};
b.one = b;
""")

with self.assertRaisesRegex(
TypeError,
r'mismatch in type `B` on property `many`; '
r'relations must be created using a property on '
r'a stored thing \(a thing with an Id\)'):
await client.query(r"""//ti
b = B{};
b.many |= set(b);
""")

with self.assertRaisesRegex(
TypeError,
r'relations must be created using a property on '
r'a stored thing \(a thing with an Id\)'):
await client.query(r"""//ti
c = C{};
c.many.add(c);
""")

await client.query(r"""//ti
a = .a = A{};
a.set("one", a);
assert(a.one == a);
""")

await client.query(r"""//ti
b = .b = B{};
b.set("one", b);
assert(b.many.has(b));
""")

await client.query(r"""//ti
b = .b = B{};
b.many |= set(b);
assert(b.one == b);
""")

await client.query(r"""//ti
c = .c = C{};
c.many.add(c);
assert(c.many.has(c));
""")


if __name__ == '__main__':
run_test(TestRelations())
4 changes: 2 additions & 2 deletions src/ti/field.c
Original file line number Diff line number Diff line change
Expand Up @@ -2582,8 +2582,8 @@ static int field__non_id_chk(ti_thing_t * thing, field__set_rel_chk_t * w)

ex_set(w->e, EX_TYPE_ERROR,
"failed to create relation; "
"relations must be created using a property on a stored thing "
"(a thing with an Id)");
"relations must be created using a property "
"on a stored thing (a thing with an Id)");
return w->e->nr;
}

Expand Down
18 changes: 9 additions & 9 deletions src/ti/vset.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,18 +294,18 @@ int ti_vset_add_val(ti_vset_t * vset, ti_val_t * val, ex_t * e)

assert(vset->parent);

if (!vset->parent->id && thing->id)
{
ex_set(e, EX_TYPE_ERROR,
"relations between stored and non-stored things must be "
"created using the property on the the stored thing "
"(the thing with an ID)");
return e->nr;
}

if (field->condition.rel)
{
ti_field_t * ofield = field->condition.rel->field;

if (!vset->parent->id)
{
ex_set(e, EX_TYPE_ERROR,
"relations must be created using a property "
"on a stored thing (a thing with an Id)");
return e->nr;
}

if (ofield->spec == TI_SPEC_SET)
{
if (field != ofield || thing != vset->parent)
Expand Down

0 comments on commit 8c5edd6

Please sign in to comment.