Skip to content
This repository was archived by the owner on Oct 9, 2023. It is now read-only.

Commit 547ae30

Browse files
authored
Add concept cast methods, and add all Concept types to typedb.client (#229)
## What is the goal of this PR? We added methods to cast concept types to each other: for example `concept.as_attribute()` will now cast a `Concept` to an `Attribute`, throwing an exception if it is not actually an `Attribute`. This aids type checking systems. Also, all the `Concept` types are now importable from `typedb.client` directly. ## What are the changes implemented in this PR? Add concept cast methods, and add all Concept types to typedb.client
1 parent d1d8222 commit 547ae30

File tree

32 files changed

+267
-136
lines changed

32 files changed

+267
-136
lines changed

dependencies/vaticle/artifacts.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ def vaticle_typedb_cluster_artifacts():
3939
artifact_name = "typedb-cluster-all-{platform}-{version}.{ext}",
4040
tag_source = deployment_private["artifact.release"],
4141
commit_source = deployment_private["artifact.snapshot"],
42-
commit = "25e45df9512604812f800218066b0f93435e44d2"
42+
commit = "8059e3aadaa46de8de0ccf57580dd0a3a61d06a4"
4343
)

tests/behaviour/background/cluster/environment.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
#
2121
import os
2222

23-
from typedb.api.connection.credential import TypeDBCredential
24-
from typedb.client import TypeDB
23+
from typedb.client import *
2524
from tests.behaviour.background import environment_base
2625
from tests.behaviour.context import Context
2726

tests/behaviour/background/core/environment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#
2121

2222
from tests.behaviour.background import environment_base
23-
from typedb.client import TypeDB
23+
from typedb.client import *
2424
from tests.behaviour.context import Context
2525

2626

tests/behaviour/background/environment_base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
# specific language governing permissions and limitations
1919
# under the License.
2020
#
21-
from behave import *
2221
from behave.model_core import Status
2322

23+
from typedb.client import *
24+
2425
from tests.behaviour.config.parameters import RootLabel
25-
from tests.behaviour.context import Context, ThingSubtype
26+
from tests.behaviour.context import Context
2627

2728
import time
2829

@@ -47,7 +48,7 @@ def before_scenario(context: Context, scenario):
4748
context.clear_answers = lambda: _clear_answers_impl(context)
4849

4950

50-
def _put_impl(context: Context, variable: str, thing: ThingSubtype):
51+
def _put_impl(context: Context, variable: str, thing: Thing):
5152
context.things[variable] = thing
5253

5354

tests/behaviour/concept/thing/attribute/attribute_steps.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
from behave import *
2424
from hamcrest import *
2525

26-
from typedb.api.concept.type.attribute_type import AttributeType
27-
from typedb.common.exception import TypeDBClientException
26+
from typedb.client import *
2827
from tests.behaviour.context import Context
2928

3029

@@ -40,17 +39,17 @@ def step_impl(context: Context, type_label: str):
4039

4140
@step("attribute {var1:Var} get owners contain: {var2:Var}")
4241
def step_impl(context: Context, var1: str, var2: str):
43-
assert_that(context.get(var2), is_in(context.get(var1).as_remote(context.tx()).get_owners()))
42+
assert_that(context.get(var2), is_in(context.get(var1).as_attribute().as_remote(context.tx()).get_owners()))
4443

4544

4645
@step("attribute {var1:Var} get owners do not contain: {var2:Var}")
4746
def step_impl(context: Context, var1: str, var2: str):
48-
assert_that(context.get(var2), not_(is_in(context.get(var1).as_remote(context.tx()).get_owners())))
47+
assert_that(context.get(var2), not_(is_in(context.get(var1).as_attribute().as_remote(context.tx()).get_owners())))
4948

5049

5150
@step("attribute {var:Var} has value type: {value_type:ValueType}")
5251
def step_impl(context: Context, var: str, value_type: AttributeType.ValueType):
53-
assert_that(context.get(var).get_type().get_value_type(), is_(value_type))
52+
assert_that(context.get(var).as_attribute().get_type().get_value_type(), is_(value_type))
5453

5554

5655
@step("attribute({type_label}) as(boolean) put: {value:Bool}; throws exception")
@@ -130,24 +129,24 @@ def step_impl(context: Context, var: str, type_label: str, value: datetime):
130129

131130
@step("attribute {var:Var} has boolean value: {value:Bool}")
132131
def step_impl(context: Context, var: str, value: bool):
133-
assert_that(context.get(var).get_value(), is_(value))
132+
assert_that(context.get(var).as_attribute().get_value(), is_(value))
134133

135134

136135
@step("attribute {var:Var} has long value: {value:Int}")
137136
def step_impl(context: Context, var: str, value: int):
138-
assert_that(context.get(var).get_value(), is_(value))
137+
assert_that(context.get(var).as_attribute().get_value(), is_(value))
139138

140139

141140
@step("attribute {var:Var} has double value: {value:Float}")
142141
def step_impl(context: Context, var: str, value: float):
143-
assert_that(context.get(var).get_value(), is_(value))
142+
assert_that(context.get(var).as_attribute().get_value(), is_(value))
144143

145144

146145
@step("attribute {var:Var} has string value: {value}")
147146
def step_impl(context: Context, var: str, value: str):
148-
assert_that(context.get(var).get_value(), is_(value))
147+
assert_that(context.get(var).as_attribute().get_value(), is_(value))
149148

150149

151150
@step("attribute {var:Var} has datetime value: {value:DateTime}")
152151
def step_impl(context: Context, var: str, value: datetime):
153-
assert_that(context.get(var).get_value(), is_(value))
152+
assert_that(context.get(var).as_attribute().get_value(), is_(value))

tests/behaviour/concept/thing/entity/entity_steps.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
from behave import *
2323
from hamcrest import *
2424

25-
from typedb.common.exception import TypeDBClientException
26-
from typedb.common.label import Label
25+
from typedb.client import *
2726
from tests.behaviour.context import Context
2827

2928

tests/behaviour/concept/thing/relation/relation_steps.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
from behave import *
2424
from hamcrest import *
2525

26-
from typedb.common.exception import TypeDBClientException
27-
from typedb.common.label import Label
26+
from typedb.client import *
2827
from tests.behaviour.config.parameters import parse_dict, parse_var
2928
from tests.behaviour.context import Context
3029

@@ -71,12 +70,14 @@ def step_impl(context: Context, type_label: str):
7170

7271
@step("relation {var1:Var} add player for role({role_label}): {var2:Var}")
7372
def step_impl(context: Context, var1: str, role_label: str, var2: str):
74-
context.get(var1).as_remote(context.tx()).add_player(context.get(var1).get_type().as_remote(context.tx()).get_relates(role_label), context.get(var2))
73+
relation = context.get(var1).as_relation()
74+
relation.as_remote(context.tx()).add_player(relation.get_type().as_remote(context.tx()).get_relates(role_label), context.get(var2))
7575

7676

7777
@step("relation {var1:Var} remove player for role({role_label}): {var2:Var}")
7878
def step_impl(context: Context, var1: str, role_label: str, var2: str):
79-
context.get(var1).as_remote(context.tx()).remove_player(context.get(var1).get_type().as_remote(context.tx()).get_relates(role_label), context.get(var2))
79+
relation = context.get(var1).as_relation()
80+
relation.as_remote(context.tx()).remove_player(relation.get_type().as_remote(context.tx()).get_relates(role_label), context.get(var2))
8081

8182

8283
@step("relation {var1:Var} add player for role({role_label}): {var2:Var}; throws exception")
@@ -85,9 +86,10 @@ def step_impl(context: Context, var1: str, role_label: str, var2: str):
8586

8687

8788
def adding_player_throws_exception(context: Context, var1: str, role_label: str, var2: str):
89+
relation = context.get(var1).as_relation()
8890
try:
89-
context.get(var1).as_remote(context.tx()).add_player(
90-
context.get(var1).get_type().as_remote(context.tx()).get_relates(role_label),
91+
relation.as_remote(context.tx()).add_player(
92+
relation.get_type().as_remote(context.tx()).get_relates(role_label),
9193
context.get(var2))
9294
assert False;
9395
except TypeDBClientException:
@@ -97,7 +99,7 @@ def adding_player_throws_exception(context: Context, var1: str, role_label: str,
9799
@step("relation {var:Var} get players contain")
98100
def step_impl(context: Context, var: str):
99101
players = parse_dict(context.table)
100-
relation = context.get(var)
102+
relation = context.get(var).as_relation()
101103
players_by_role_type = relation.as_remote(context.tx()).get_players_by_role_type()
102104
for (role_label, var2) in players.items():
103105
assert_that(players_by_role_type.get(relation.get_type().as_remote(context.tx()).get_relates(role_label)), has_item(context.get(parse_var(var2))))
@@ -106,31 +108,31 @@ def step_impl(context: Context, var: str):
106108
@step("relation {var:Var} get players do not contain")
107109
def step_impl(context: Context, var: str):
108110
players = parse_dict(context.table)
109-
relation = context.get(var)
111+
relation = context.get(var).as_relation()
110112
players_by_role_type = relation.as_remote(context.tx()).get_players_by_role_type()
111113
for (role_label, var2) in players.items():
112114
assert_that(players_by_role_type.get(relation.get_type().as_remote(context.tx()).get_relates(role_label)), not_(has_item(context.get(parse_var(var2)))))
113115

114116

115117
@step("relation {var1:Var} get players contain: {var2:Var}")
116118
def step_impl(context: Context, var1: str, var2: str):
117-
assert_that(context.get(var1).as_remote(context.tx()).get_players(), has_item(context.get(var2)))
119+
assert_that(context.get(var1).as_relation().as_remote(context.tx()).get_players(), has_item(context.get(var2)))
118120

119121

120122
@step("relation {var1:Var} get players do not contain: {var2:Var}")
121123
def step_impl(context: Context, var1: str, var2: str):
122-
assert_that(context.get(var1).as_remote(context.tx()).get_players(), not_(has_item(context.get(var2))))
124+
assert_that(context.get(var1).as_relation().as_remote(context.tx()).get_players(), not_(has_item(context.get(var2))))
123125

124126

125127
@step("relation {var1:Var} get players for role({role_label}) contain: {var2:Var}")
126128
def step_impl(context: Context, var1: str, role_label: str, var2: str):
127-
assert_that(context.get(var1).as_remote(context.tx()).get_players(
128-
role_types=[context.get(var1).get_type().as_remote(context.tx()).get_relates(role_label)]),
129+
relation = context.get(var1).as_relation()
130+
assert_that(relation.as_remote(context.tx()).get_players(role_types=[relation.get_type().as_remote(context.tx()).get_relates(role_label)]),
129131
has_item(context.get(var2)))
130132

131133

132134
@step("relation {var1:Var} get players for role({role_label}) do not contain: {var2:Var}")
133135
def step_impl(context: Context, var1: str, role_label: str, var2: str):
134-
assert_that(context.get(var1).as_remote(context.tx()).get_players(
135-
role_types=[context.get(var1).get_type().as_remote(context.tx()).get_relates(role_label)]),
136+
relation = context.get(var1).as_relation()
137+
assert_that(relation.as_remote(context.tx()).get_players(role_types=[relation.get_type().as_remote(context.tx()).get_relates(role_label)]),
136138
not_(has_item(context.get(var2))))

tests/behaviour/concept/thing/thing_steps.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
from behave import *
2323
from hamcrest import *
2424

25-
from typedb.common.exception import TypeDBClientException
26-
from typedb.common.label import Label
25+
from typedb.client import *
2726
from tests.behaviour.config.parameters import parse_bool, RootLabel
2827
from tests.behaviour.context import Context
2928

@@ -62,7 +61,7 @@ def step_impl(context: Context, var: str):
6261
@step("relation {var1:Var} set has: {var2:Var}; throws exception")
6362
def step_impl(context: Context, var1: str, var2: str):
6463
try:
65-
context.get(var1).as_remote(context.tx()).set_has(context.get(var2))
64+
context.get(var1).as_remote(context.tx()).set_has(context.get(var2).as_attribute())
6665
assert False
6766
except TypeDBClientException:
6867
pass
@@ -72,35 +71,35 @@ def step_impl(context: Context, var1: str, var2: str):
7271
@step("attribute {var1:Var} set has: {var2:Var}")
7372
@step("relation {var1:Var} set has: {var2:Var}")
7473
def step_impl(context: Context, var1: str, var2: str):
75-
context.get(var1).as_remote(context.tx()).set_has(context.get(var2))
74+
context.get(var1).as_remote(context.tx()).set_has(context.get(var2).as_attribute())
7675

7776

7877
@step("entity {var1:Var} unset has: {var2:Var}")
7978
@step("attribute {var1:Var} unset has: {var2:Var}")
8079
@step("relation {var1:Var} unset has: {var2:Var}")
8180
def step_impl(context: Context, var1: str, var2: str):
82-
context.get(var1).as_remote(context.tx()).unset_has(context.get(var2))
81+
context.get(var1).as_remote(context.tx()).unset_has(context.get(var2).as_attribute())
8382

8483

8584
@step("entity {var1:Var} get keys contain: {var2:Var}")
8685
@step("attribute {var1:Var} get keys contain: {var2:Var}")
8786
@step("relation {var1:Var} get keys contain: {var2:Var}")
8887
def step_impl(context: Context, var1: str, var2: str):
89-
assert_that(context.get(var1).as_remote(context.tx()).get_has(only_key=True), has_item(context.get(var2)))
88+
assert_that(context.get(var1).as_remote(context.tx()).get_has(only_key=True), has_item(context.get(var2).as_attribute()))
9089

9190

9291
@step("entity {var1:Var} get keys do not contain: {var2:Var}")
9392
@step("attribute {var1:Var} get keys do not contain: {var2:Var}")
9493
@step("relation {var1:Var} get keys do not contain: {var2:Var}")
9594
def step_impl(context: Context, var1: str, var2: str):
96-
assert_that(context.get(var1).as_remote(context.tx()).get_has(only_key=True), not_(has_item(context.get(var2))))
95+
assert_that(context.get(var1).as_remote(context.tx()).get_has(only_key=True), not_(has_item(context.get(var2).as_attribute())))
9796

9897

9998
@step("entity {var1:Var} get attributes contain: {var2:Var}")
10099
@step("attribute {var1:Var} get attributes contain: {var2:Var}")
101100
@step("relation {var1:Var} get attributes contain: {var2:Var}")
102101
def step_impl(context: Context, var1: str, var2: str):
103-
assert_that(context.get(var1).as_remote(context.tx()).get_has(), has_item(context.get(var2)))
102+
assert_that(context.get(var1).as_remote(context.tx()).get_has(), has_item(context.get(var2).as_attribute()))
104103

105104

106105
@step("entity {var1:Var} get attributes({type_label}) contain: {var2:Var}")
@@ -122,14 +121,14 @@ def step_impl(context: Context, var1: str, var2: str):
122121
@step("attribute {var1:Var} get attributes({type_label}) as(datetime) contain: {var2:Var}")
123122
@step("relation {var1:Var} get attributes({type_label}) as(datetime) contain: {var2:Var}")
124123
def step_impl(context: Context, var1: str, type_label: str, var2: str):
125-
assert_that(context.get(var1).as_remote(context.tx()).get_has(attribute_type=context.tx().concepts().get_attribute_type(type_label)), has_item(context.get(var2)))
124+
assert_that(context.get(var1).as_remote(context.tx()).get_has(attribute_type=context.tx().concepts().get_attribute_type(type_label)), has_item(context.get(var2).as_attribute()))
126125

127126

128127
@step("entity {var1:Var} get attributes do not contain: {var2:Var}")
129128
@step("attribute {var1:Var} get attributes do not contain: {var2:Var}")
130129
@step("relation {var1:Var} get attributes do not contain: {var2:Var}")
131130
def step_impl(context: Context, var1: str, var2: str):
132-
assert_that(context.get(var1).as_remote(context.tx()).get_has(), not_(has_item(context.get(var2))))
131+
assert_that(context.get(var1).as_remote(context.tx()).get_has(), not_(has_item(context.get(var2).as_attribute())))
133132

134133

135134
@step("entity {var1:Var} get attributes({type_label}) do not contain: {var2:Var}")
@@ -151,7 +150,8 @@ def step_impl(context: Context, var1: str, var2: str):
151150
@step("attribute {var1:Var} get attributes({type_label}) as(datetime) do not contain: {var2:Var}")
152151
@step("relation {var1:Var} get attributes({type_label}) as(datetime) do not contain: {var2:Var}")
153152
def step_impl(context: Context, var1: str, type_label: str, var2: str):
154-
assert_that(context.get(var1).as_remote(context.tx()).get_has(attribute_type=context.tx().concepts().get_attribute_type(type_label)), not_(has_item(context.get(var2))))
153+
assert_that(context.get(var1).as_remote(context.tx()).get_has(attribute_type=context.tx().concepts().get_attribute_type(type_label)),
154+
not_(has_item(context.get(var2).as_attribute())))
155155

156156

157157
@step("entity {var1:Var} get relations({label:ScopedLabel}) contain: {var2:Var}")

tests/behaviour/concept/type/attributetype/attribute_type_steps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from behave import *
2323
from hamcrest import *
2424

25-
from typedb.api.concept.type.attribute_type import AttributeType
25+
from typedb.client import *
2626
from tests.behaviour.config.parameters import parse_value_type, parse_list, parse_label
2727
from tests.behaviour.context import Context
2828

@@ -39,7 +39,7 @@ def step_impl(context: Context, type_label: str, value_type: str):
3939

4040
@step("attribute({type_label}) get supertype value type: {value_type}")
4141
def step_impl(context: Context, type_label: str, value_type: str):
42-
supertype = context.tx().concepts().get_attribute_type(type_label).as_remote(context.tx()).get_supertype()
42+
supertype = context.tx().concepts().get_attribute_type(type_label).as_remote(context.tx()).get_supertype().as_attribute_type()
4343
assert_that(supertype.get_value_type(), is_(parse_value_type(value_type)))
4444

4545

tests/behaviour/concept/type/relationtype/relation_type_steps.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
from behave import *
2323
from hamcrest import *
2424

25-
from typedb.common.exception import TypeDBClientException
26-
from typedb.common.label import Label
25+
from typedb.client import *
2726
from tests.behaviour.config.parameters import parse_list, parse_bool, parse_label
2827
from tests.behaviour.context import Context
2928

0 commit comments

Comments
 (0)