Skip to content

Commit fd8d04e

Browse files
authored
Python. Fix driver.close(). Add integration tests for connection closing for both core and cloud (#670)
## Usage and product changes We fix the issue #669, where the Python Driver didn't close the connection when calling `TypeDBDriver.close()`. ## Implementation We needed to reverse the condition of `is_open()`. The main issue is the absence of tests for our drivers' connections, so we: * add new integration tests for both core and cloud; * create an issue for behaviour tests to cover this hole for a brighter future: typedb/typedb-behaviour#289.
1 parent b45116c commit fd8d04e

File tree

5 files changed

+75
-8
lines changed

5 files changed

+75
-8
lines changed

Diff for: .factory/automation.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,13 @@ build:
287287
288288
tool/test/start-core-server.sh &&
289289
bazel test //python/tests/integration:test_stream --test_output=streamed --jobs=1 &&
290+
bazel test //python/tests/integration:test_core_connection --test_output=streamed --jobs=1 &&
290291
export CORE_FAILED= || export CORE_FAILED=1
291292
tool/test/stop-core-server.sh
292293
if [[ -n "$CORE_FAILED" ]]; then exit 1; fi
293294
294295
source tool/test/start-cloud-servers.sh 3 && # use source to receive export vars
295-
bazel test //python/tests/integration:test_connection --test_env=ROOT_CA=$ROOT_CA --test_output=streamed --jobs=1 &&
296+
bazel test //python/tests/integration:test_cloud_connection --test_env=ROOT_CA=$ROOT_CA --test_output=streamed --jobs=1 &&
296297
# TODO #635: currently broken test
297298
# bazel test //python/tests/integration:test_cloud_failover --test_env=ROOT_CA=$ROOT_CA --test_output=streamed --jobs=1 &&
298299
export CLOUD_FAILED= || export CLOUD_FAILED=1

Diff for: python/tests/integration/BUILD

+13-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,19 @@ typedb_cloud_py_test(
3232
)
3333

3434
py_test(
35-
name = "test_connection",
36-
srcs = ["test_connection.py"],
35+
name = "test_core_connection",
36+
srcs = ["test_core_connection.py"],
37+
deps = [
38+
"//python:driver_python",
39+
requirement("PyHamcrest"),
40+
],
41+
data = ["//python:native-driver-binary-link", "//python:native-driver-wrapper-link"],
42+
python_version = "PY3"
43+
)
44+
45+
py_test(
46+
name = "test_cloud_connection",
47+
srcs = ["test_cloud_connection.py"],
3748
deps = [
3849
"//python:driver_python",
3950
requirement("PyHamcrest"),

Diff for: python/tests/integration/test_connection.py renamed to python/tests/integration/test_cloud_connection.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,32 @@
2626
TYPEDB = "typedb"
2727
DATA = SessionType.DATA
2828
WRITE = TransactionType.WRITE
29+
CREDENTIAL = TypeDBCredential("admin", "password", tls_enabled=True, tls_root_ca_path=os.environ["ROOT_CA"])
2930

3031

3132
class TestDebug(TestCase):
3233

33-
def test_missing_port(self):
34-
assert_that(calling(lambda: TypeDB.core_driver("localhost")), raises(TypeDBDriverException))
34+
def test_core_connection_while_running_cloud(self):
35+
assert_that(calling(lambda: TypeDB.core_driver("localhost:11729")), raises(TypeDBDriverException))
3536

37+
def test_open_close_transaction(self):
38+
addresses = [
39+
"localhost:11729",
40+
"localhost:21729",
41+
"localhost:31729"
42+
]
43+
driver = TypeDB.cloud_driver(addresses, CREDENTIAL)
44+
assert_that(driver.is_open(), is_(True))
45+
driver.close()
46+
assert_that(driver.is_open(), is_(False))
3647

3748
def test_address_translation(self):
3849
address_translation = {
3950
"localhost:11729": "localhost:11729",
4051
"localhost:21729": "localhost:21729",
4152
"localhost:31729": "localhost:31729"
4253
}
43-
credential = TypeDBCredential("admin", "password", tls_enabled=True, tls_root_ca_path=os.environ["ROOT_CA"])
44-
with TypeDB.cloud_driver(address_translation, credential) as driver:
54+
with TypeDB.cloud_driver(address_translation, CREDENTIAL) as driver:
4555
if TYPEDB not in [db.name for db in driver.databases.all()]:
4656
driver.databases.create(TYPEDB)
4757
with driver.session(TYPEDB, DATA) as session, session.transaction(WRITE) as tx:

Diff for: python/tests/integration/test_core_connection.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
import os
19+
import unittest
20+
from unittest import TestCase
21+
22+
from hamcrest import *
23+
24+
from typedb.driver import *
25+
26+
TYPEDB = "typedb"
27+
DATA = SessionType.DATA
28+
WRITE = TransactionType.WRITE
29+
30+
31+
class TestDebug(TestCase):
32+
33+
def test_missing_port(self):
34+
assert_that(calling(lambda: TypeDB.core_driver("localhost")), raises(TypeDBDriverException))
35+
36+
def test_open_close_transaction(self):
37+
driver = TypeDB.core_driver(TypeDB.DEFAULT_ADDRESS)
38+
assert_that(driver.is_open(), is_(True))
39+
driver.close()
40+
assert_that(driver.is_open(), is_(False))
41+
42+
43+
if __name__ == "__main__":
44+
unittest.main(verbosity=2)
45+

Diff for: python/typedb/connection/driver.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,5 @@ def __exit__(self, exc_type, exc_val, exc_tb):
9393
return False
9494

9595
def close(self) -> None:
96-
if not self.is_open():
96+
if self.is_open():
9797
connection_force_close(self._native_connection)

0 commit comments

Comments
 (0)