Skip to content

Commit 8604cb7

Browse files
committed
use contextmanager for layer and connection
1 parent af1987f commit 8604cb7

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

pytest_crate/plugin.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@ def __init__(self, name: str, version: str) -> None:
2727
def __repr__(self) -> str:
2828
return self.name
2929

30-
def start(self) -> None:
30+
def __enter__(self):
31+
self._start()
32+
return self
33+
34+
def __exit__(self, exception_type, exception_value, traceback):
35+
self._stop()
36+
37+
def _start(self) -> None:
3138
print(f"Starting CrateDB {self} ...")
3239
self.tmp = tempfile.mkdtemp()
3340
settings = {
@@ -41,7 +48,7 @@ def start(self) -> None:
4148
self.node.start()
4249
print(f"CrateDB {self} started")
4350

44-
def stop(self) -> None:
51+
def _stop(self) -> None:
4552
print(f"Stopping CrateDB {self} ...")
4653
self.node.stop()
4754
shutil.rmtree(self.tmp, ignore_errors=True)
@@ -83,10 +90,8 @@ def crate_version(self, pytestconfig) -> Generator[str, None, None]:
8390
@pytest.fixture(scope="session")
8491
def crate_layer(self) -> CrateLayerFactoryGenerator:
8592
def layer_factory(name: str, version: str):
86-
layer = CrateLayer(name, version)
87-
layer.start()
88-
yield layer
89-
layer.stop()
93+
with CrateLayer(name, version) as layer:
94+
yield layer
9095
yield layer_factory
9196

9297
@pytest.fixture(scope="session")
@@ -97,9 +102,8 @@ def crate(self, crate_layer, crate_version) -> CrateLayerGenerator:
97102

98103
@pytest.fixture
99104
def crate_cursor(self, crate) -> Generator[Cursor, None, None]:
100-
connection = connect(crate.dsn())
101-
yield connection.cursor()
102-
connection.close()
105+
with connect(crate.dsn()) as connection:
106+
yield connection.cursor()
103107

104108
@pytest.fixture
105109
def crate_execute(self, crate_cursor) -> Generator[Callable, None, None]:

0 commit comments

Comments
 (0)