Skip to content

Python. Fix driver.close(). Add integration tests for connection closing for both core and cloud #670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .factory/automation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,13 @@ build:

tool/test/start-core-server.sh &&
bazel test //python/tests/integration:test_stream --test_output=streamed --jobs=1 &&
bazel test //python/tests/integration:test_core_connection --test_output=streamed --jobs=1 &&
export CORE_FAILED= || export CORE_FAILED=1
tool/test/stop-core-server.sh
if [[ -n "$CORE_FAILED" ]]; then exit 1; fi

source tool/test/start-cloud-servers.sh 3 && # use source to receive export vars
bazel test //python/tests/integration:test_connection --test_env=ROOT_CA=$ROOT_CA --test_output=streamed --jobs=1 &&
bazel test //python/tests/integration:test_cloud_connection --test_env=ROOT_CA=$ROOT_CA --test_output=streamed --jobs=1 &&
# TODO #635: currently broken test
# bazel test //python/tests/integration:test_cloud_failover --test_env=ROOT_CA=$ROOT_CA --test_output=streamed --jobs=1 &&
export CLOUD_FAILED= || export CLOUD_FAILED=1
Expand Down
15 changes: 13 additions & 2 deletions python/tests/integration/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,19 @@ typedb_cloud_py_test(
)

py_test(
name = "test_connection",
srcs = ["test_connection.py"],
name = "test_core_connection",
srcs = ["test_core_connection.py"],
deps = [
"//python:driver_python",
requirement("PyHamcrest"),
],
data = ["//python:native-driver-binary-link", "//python:native-driver-wrapper-link"],
python_version = "PY3"
)

py_test(
name = "test_cloud_connection",
srcs = ["test_cloud_connection.py"],
deps = [
"//python:driver_python",
requirement("PyHamcrest"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,32 @@
TYPEDB = "typedb"
DATA = SessionType.DATA
WRITE = TransactionType.WRITE
CREDENTIAL = TypeDBCredential("admin", "password", tls_enabled=True, tls_root_ca_path=os.environ["ROOT_CA"])


class TestDebug(TestCase):

def test_missing_port(self):
assert_that(calling(lambda: TypeDB.core_driver("localhost")), raises(TypeDBDriverException))
def test_core_connection_while_running_cloud(self):
assert_that(calling(lambda: TypeDB.core_driver("localhost:11729")), raises(TypeDBDriverException))

def test_open_close_transaction(self):
addresses = [
"localhost:11729",
"localhost:21729",
"localhost:31729"
]
driver = TypeDB.cloud_driver(addresses, CREDENTIAL)
assert_that(driver.is_open(), is_(True))
driver.close()
assert_that(driver.is_open(), is_(False))

def test_address_translation(self):
address_translation = {
"localhost:11729": "localhost:11729",
"localhost:21729": "localhost:21729",
"localhost:31729": "localhost:31729"
}
credential = TypeDBCredential("admin", "password", tls_enabled=True, tls_root_ca_path=os.environ["ROOT_CA"])
with TypeDB.cloud_driver(address_translation, credential) as driver:
with TypeDB.cloud_driver(address_translation, CREDENTIAL) as driver:
if TYPEDB not in [db.name for db in driver.databases.all()]:
driver.databases.create(TYPEDB)
with driver.session(TYPEDB, DATA) as session, session.transaction(WRITE) as tx:
Expand Down
45 changes: 45 additions & 0 deletions python/tests/integration/test_core_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import os
import unittest
from unittest import TestCase

from hamcrest import *

from typedb.driver import *

TYPEDB = "typedb"
DATA = SessionType.DATA
WRITE = TransactionType.WRITE


class TestDebug(TestCase):

def test_missing_port(self):
assert_that(calling(lambda: TypeDB.core_driver("localhost")), raises(TypeDBDriverException))

def test_open_close_transaction(self):
driver = TypeDB.core_driver(TypeDB.DEFAULT_ADDRESS)
assert_that(driver.is_open(), is_(True))
driver.close()
assert_that(driver.is_open(), is_(False))


if __name__ == "__main__":
unittest.main(verbosity=2)

2 changes: 1 addition & 1 deletion python/typedb/connection/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,5 @@ def __exit__(self, exc_type, exc_val, exc_tb):
return False

def close(self) -> None:
if not self.is_open():
if self.is_open():
connection_force_close(self._native_connection)