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

Commit 6fc3bd8

Browse files
author
Alex Walker
authored
Add basic BDD infrastructure (#139)
## What is the goal of this PR? Add BDD infrastructure.
1 parent 774ba4a commit 6fc3bd8

File tree

31 files changed

+962
-16
lines changed

31 files changed

+962
-16
lines changed

.grabl/automation.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ build:
4646
bazel run @graknlabs_dependencies//distribution/artifact:create-netrc
4747
bazel build //...
4848
bazel run @graknlabs_dependencies//tool/checkstyle:test-coverage
49-
bazel test $(bazel query 'kind(checkstyle_test, //...)')
49+
bazel test $(bazel query 'kind(checkstyle_test, //...)') --test_output=errors
5050
# test-concept:
5151
# image: graknlabs-ubuntu-20.04
5252
# type: foreground

BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ py_library(
4343
graknlabs_client_python_requirement("grpcio"),
4444
graknlabs_client_python_requirement("six"),
4545
],
46-
visibility =["//visibility:public"]
46+
visibility = ["//visibility:public"]
4747
)
4848

4949
checkstyle_test(

WORKSPACE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ github_deps()
9292
# Load @graknlabs dependencies #
9393
################################
9494

95-
load("//dependencies/graknlabs:repositories.bzl", "graknlabs_common")
95+
load("//dependencies/graknlabs:repositories.bzl", "graknlabs_common", "graknlabs_behaviour")
9696
graknlabs_common()
97+
graknlabs_behaviour()
9798

9899
# Load artifacts
99100
load("//dependencies/graknlabs:artifacts.bzl", "graknlabs_grakn_core_artifacts")

dependencies/graknlabs/repositories.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,10 @@ def graknlabs_common():
3232
remote = "https://github.com/graknlabs/common",
3333
commit = "cfd261fd5412a0b45cb5494555e4491dd3ce5f64" # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_common
3434
)
35+
36+
def graknlabs_behaviour():
37+
git_repository(
38+
name = "graknlabs_behaviour",
39+
remote = "https://github.com/graknlabs/behaviour",
40+
commit = "7088622b9f4a3bd99cf4fb278130ad76ff35af6b" # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_behaviour
41+
)

grakn/client.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
from grakn.rpc.database_manager import DatabaseManager as _DatabaseManager
2424
from grakn.rpc.session import Session as _Session, SessionType
2525

26-
# Repackaging these enums allows users to import everything they (most likely) need from "grakn.client"
27-
from grakn.rpc.transaction import TransactionType # noqa # pylint: disable=unused-import
26+
# Repackaging these symbols allows users to import everything they (most likely) need from "grakn.client"
27+
from grakn.common.exception import GraknClientException # noqa # pylint: disable=unused-import
2828
from grakn.concept.type.attribute_type import ValueType # noqa # pylint: disable=unused-import
29+
from grakn.rpc.transaction import TransactionType # noqa # pylint: disable=unused-import
2930

3031

3132
class GraknClient(object):
@@ -34,6 +35,7 @@ class GraknClient(object):
3435
def __init__(self, address=DEFAULT_URI):
3536
self._channel = grpc.insecure_channel(address)
3637
self._databases = _DatabaseManager(self._channel)
38+
self._is_open = True
3739

3840
def session(self, database: str, session_type: SessionType, options=GraknOptions()):
3941
return _Session(self, database, session_type, options)
@@ -43,6 +45,10 @@ def databases(self):
4345

4446
def close(self):
4547
self._channel.close()
48+
self._is_open = False
49+
50+
def is_open(self):
51+
return self._is_open
4652

4753
def __enter__(self):
4854
return self

grakn/query/query_manager.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,28 @@ def delete(self, query: str, options=GraknOptions()):
5151
delete_req = query_proto.Graql.Delete.Req()
5252
delete_req.query = query
5353
request.delete_req.CopyFrom(delete_req)
54-
self._run_query(request, options)
54+
return self._run_query(request, options)
5555

5656
def define(self, query: str, options=GraknOptions()):
5757
request = query_proto.Query.Req()
5858
define_req = query_proto.Graql.Define.Req()
5959
define_req.query = query
6060
request.define_req.CopyFrom(define_req)
61-
self._run_query(request, options)
61+
return self._run_query(request, options)
6262

6363
def undefine(self, query: str, options=GraknOptions()):
6464
request = query_proto.Query.Req()
6565
undefine_req = query_proto.Graql.Undefine.Req()
6666
undefine_req.query = query
6767
request.undefine_req.CopyFrom(undefine_req)
68-
self._run_query(request, options)
68+
return self._run_query(request, options)
6969

7070
def _run_query(self, query_req: query_proto.Query.Req, options: GraknOptions):
7171
req = transaction_proto.Transaction.Req()
7272
query_req.options.CopyFrom(grakn_proto_builder.options(options))
7373
req.query_req.CopyFrom(query_req)
7474
# Using stream makes this request asynchronous.
75-
self._transaction._stream(req)
75+
return self._transaction._stream(req)
7676

7777
def _iterate_query(self, query_req: query_proto.Query.Req, response_reader: Callable[[transaction_proto.Transaction.Res], List], options: GraknOptions):
7878
req = transaction_proto.Transaction.Req()

grakn/rpc/session.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ def __init__(self, client, database: str, session_type: SessionType, options=Gra
5252

5353
self._session_id = self._grpc_stub.session_open(open_req).session_id
5454
self._is_open = True
55-
self._scheduler.enter(delay=self._PULSE_FREQUENCY_SECONDS, priority=1, action=self._transmit_pulse, argument=())
55+
self._pulse = self._scheduler.enter(delay=self._PULSE_FREQUENCY_SECONDS, priority=1, action=self._transmit_pulse, argument=())
5656
# TODO: This thread blocks the process from closing. We should try cancelling the scheduled task when the
5757
# session closes. If that doesn't work, we need some other way of getting the thread to exit.
58-
Thread(target=self._scheduler.run).start()
58+
Thread(target=self._scheduler.run, daemon=True).start()
5959

6060
def transaction(self, transaction_type: TransactionType, options=GraknOptions()):
6161
return Transaction(self._channel, self._session_id, transaction_type, options)
@@ -67,6 +67,8 @@ def is_open(self): return self._is_open
6767
def close(self):
6868
if self._is_open:
6969
self._is_open = False
70+
self._scheduler.cancel(self._pulse)
71+
self._scheduler.empty()
7072
req = session_proto.Session.Close.Req()
7173
req.session_id = self._session_id
7274
self._grpc_stub.session_close(req)
@@ -80,8 +82,8 @@ def _transmit_pulse(self):
8082
pulse_req.session_id = self._session_id
8183
res = self._grpc_stub.session_pulse(pulse_req)
8284
if res.alive:
83-
self._scheduler.enter(delay=self._PULSE_FREQUENCY_SECONDS, priority=1, action=self._transmit_pulse, argument=())
84-
Thread(target=self._scheduler.run).start()
85+
self._pulse = self._scheduler.enter(delay=self._PULSE_FREQUENCY_SECONDS, priority=1, action=self._transmit_pulse, argument=())
86+
Thread(target=self._scheduler.run, daemon=True).start()
8587

8688
def __enter__(self):
8789
return self

grakn/rpc/stream.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Stream(six.Iterator):
3131
_CONTINUE = "continue"
3232
_DONE = "done"
3333

34-
def __init__(self, transaction, request_id: str, transform_response: Callable[[transaction_proto.Transaction.Res], List] = None):
34+
def __init__(self, transaction, request_id: str, transform_response: Callable[[transaction_proto.Transaction.Res], List] = lambda res: None):
3535
self._transaction = transaction
3636
self._request_id = request_id
3737
self._transform_response = transform_response

requirements.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,21 @@
1717
# under the License.
1818
#
1919

20-
# --extra-index-url https://repo.grakn.ai/repository/pypi-snapshot/simple
20+
21+
## Configuration options
22+
23+
# --extra-index-url https://repo.grakn.ai/repository/pypi-snapshot/simple # Allow importing of snapshots
24+
25+
26+
## Dependencies
27+
2128
graknprotocol==2.0.0a4
2229
grpcio==1.33.2
2330
protobuf==3.6.1
2431
six>=1.11.0
32+
33+
34+
# Dev dependencies (not required to use the grakn-client package, but necessary for its development)
35+
36+
behave==1.2.6
37+
PyHamcrest==2.0.2

test/BUILD renamed to tests/BUILD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# under the License.
1818
#
1919

20+
# Note: Do NOT rename this folder to 'test' as it conflicts with a built-in Python package!
2021
load("@graknlabs_common//test/server:rules.bzl", "native_grakn_artifact")
2122
load("@graknlabs_dependencies//tool/checkstyle:rules.bzl", "checkstyle_test")
2223
load("@rules_python//python:defs.bzl", "py_library", "py_test")
@@ -26,7 +27,7 @@ native_grakn_artifact(
2627
mac_artifact = "@graknlabs_grakn_core_artifact_mac//file",
2728
linux_artifact = "@graknlabs_grakn_core_artifact_linux//file",
2829
windows_artifact = "@graknlabs_grakn_core_artifact_windows//file",
29-
visibility = ["//test:__subpackages__"],
30+
visibility = ["//tests:__subpackages__"],
3031
)
3132

3233
py_test(

0 commit comments

Comments
 (0)