Skip to content

Commit c330370

Browse files
committed
TestKit backend: fix warning assertions
1 parent 45283f7 commit c330370

File tree

7 files changed

+121
-36
lines changed

7 files changed

+121
-36
lines changed

neo4j/_async/driver.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,5 @@ def __init__(self, pool, default_workspace_config):
675675

676676
def session(self, **config) -> AsyncSession:
677677
session_config = SessionConfig(self._default_workspace_config,
678-
config)
679-
SessionConfig.consume(config) # Consume the config
678+
SessionConfig.consume(config))
680679
return AsyncSession(self._pool, session_config)

neo4j/_meta.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818

1919
import asyncio
20+
import tracemalloc
2021
import typing as t
2122
from functools import wraps
2223
from warnings import warn
@@ -130,8 +131,6 @@ def inner(*args, **kwargs):
130131

131132

132133
def unclosed_resource_warn(obj):
133-
import tracemalloc
134-
from warnings import warn
135134
msg = f"Unclosed {obj!r}."
136135
trace = tracemalloc.get_object_traceback(obj)
137136
if trace:

neo4j/_sync/driver.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,5 @@ def __init__(self, pool, default_workspace_config):
674674

675675
def session(self, **config) -> Session:
676676
session_config = SessionConfig(self._default_workspace_config,
677-
config)
678-
SessionConfig.consume(config) # Consume the config
677+
SessionConfig.consume(config))
679678
return Session(self._pool, session_config)

testkitbackend/_async/requests.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import warnings
2222
from os import path
2323

24-
import pytest
25-
2624
import neo4j
2725
import neo4j.api
2826
from neo4j._async_compat.util import AsyncUtil
@@ -32,6 +30,7 @@
3230
test_subtest_skips,
3331
totestkit,
3432
)
33+
from .._warning_check import warning_check
3534
from ..exceptions import MarkdAsDriverException
3635

3736

@@ -178,10 +177,10 @@ async def GetServerInfo(backend, data):
178177
async def CheckMultiDBSupport(backend, data):
179178
driver_id = data["driverId"]
180179
driver = backend.drivers[driver_id]
181-
with pytest.warns(
182-
DeprecationWarning,
183-
match="Feature support query, based on Bolt protocol version and "
184-
"Neo4j server version will change in the future."
180+
with warning_check(
181+
neo4j.ExperimentalWarning,
182+
"Feature support query, based on Bolt protocol version and Neo4j "
183+
"server version will change in the future."
185184
):
186185
available = await driver.supports_multi_db()
187186
await backend.send_response("MultiDBSupport", {
@@ -266,7 +265,14 @@ async def NewBookmarkManager(backend, data):
266265
backend, bookmark_manager_id
267266
)
268267

269-
bookmark_manager = neo4j.AsyncGraphDatabase.bookmark_manager(**bmm_kwargs)
268+
with warning_check(
269+
neo4j.ExperimentalWarning,
270+
"The bookmark manager feature is experimental. It might be changed or "
271+
"removed any time even without prior notice."
272+
):
273+
bookmark_manager = neo4j.AsyncGraphDatabase.bookmark_manager(
274+
**bmm_kwargs
275+
)
270276
backend.bookmark_managers[bookmark_manager_id] = bookmark_manager
271277
await backend.send_response("BookmarkManager", {"id": bookmark_manager_id})
272278

@@ -382,7 +388,15 @@ async def NewSession(backend, data):
382388
):
383389
if data_name in data:
384390
config[conf_name] = data[data_name]
385-
session = driver.session(**config)
391+
if "bookmark_manager" in config:
392+
with warning_check(
393+
neo4j.ExperimentalWarning,
394+
"The 'bookmark_manager' config key is experimental. It might be "
395+
"changed or removed any time even without prior notice."
396+
):
397+
session = driver.session(**config)
398+
else:
399+
session = driver.session(**config)
386400
key = backend.next_key()
387401
backend.sessions[key] = SessionTracker(session)
388402
await backend.send_response("Session", {"id": key})

testkitbackend/_sync/requests.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import warnings
2222
from os import path
2323

24-
import pytest
25-
2624
import neo4j
2725
import neo4j.api
2826
from neo4j._async_compat.util import Util
@@ -32,6 +30,7 @@
3230
test_subtest_skips,
3331
totestkit,
3432
)
33+
from .._warning_check import warning_check
3534
from ..exceptions import MarkdAsDriverException
3635

3736

@@ -178,10 +177,10 @@ def GetServerInfo(backend, data):
178177
def CheckMultiDBSupport(backend, data):
179178
driver_id = data["driverId"]
180179
driver = backend.drivers[driver_id]
181-
with pytest.warns(
182-
DeprecationWarning,
183-
match="Feature support query, based on Bolt protocol version and "
184-
"Neo4j server version will change in the future."
180+
with warning_check(
181+
neo4j.ExperimentalWarning,
182+
"Feature support query, based on Bolt protocol version and Neo4j "
183+
"server version will change in the future."
185184
):
186185
available = driver.supports_multi_db()
187186
backend.send_response("MultiDBSupport", {
@@ -266,7 +265,14 @@ def NewBookmarkManager(backend, data):
266265
backend, bookmark_manager_id
267266
)
268267

269-
bookmark_manager = neo4j.GraphDatabase.bookmark_manager(**bmm_kwargs)
268+
with warning_check(
269+
neo4j.ExperimentalWarning,
270+
"The bookmark manager feature is experimental. It might be changed or "
271+
"removed any time even without prior notice."
272+
):
273+
bookmark_manager = neo4j.GraphDatabase.bookmark_manager(
274+
**bmm_kwargs
275+
)
270276
backend.bookmark_managers[bookmark_manager_id] = bookmark_manager
271277
backend.send_response("BookmarkManager", {"id": bookmark_manager_id})
272278

@@ -382,7 +388,15 @@ def NewSession(backend, data):
382388
):
383389
if data_name in data:
384390
config[conf_name] = data[data_name]
385-
session = driver.session(**config)
391+
if "bookmark_manager" in config:
392+
with warning_check(
393+
neo4j.ExperimentalWarning,
394+
"The 'bookmark_manager' config key is experimental. It might be "
395+
"changed or removed any time even without prior notice."
396+
):
397+
session = driver.session(**config)
398+
else:
399+
session = driver.session(**config)
386400
key = backend.next_key()
387401
backend.sessions[key] = SessionTracker(session)
388402
backend.send_response("Session", {"id": key})

testkitbackend/_warning_check.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright (c) "Neo4j"
2+
# Neo4j Sweden AB [https://neo4j.com]
3+
#
4+
# This file is part of Neo4j.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# https://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
19+
import re
20+
import warnings
21+
from contextlib import contextmanager
22+
23+
24+
@contextmanager
25+
def warning_check(category, message):
26+
with warnings.catch_warnings(record=True) as warn_log:
27+
warnings.filterwarnings("always", category=category, message=message)
28+
yield
29+
if len(warn_log) != 1:
30+
raise AssertionError("Expected 1 warning, found %d: %s"
31+
% (len(warn_log), warn_log))
32+
33+
34+
@contextmanager
35+
def warnings_check(category_message_pairs):
36+
with warnings.catch_warnings(record=True) as warn_log:
37+
for category, message in category_message_pairs:
38+
warnings.filterwarnings("always", category=category,
39+
message=message)
40+
yield
41+
if len(warn_log) != len(category_message_pairs):
42+
raise AssertionError(
43+
"Expected %d warnings, found %d: %s"
44+
% (len(category_message_pairs), len(warn_log), warn_log)
45+
)
46+
category_message_pairs = [
47+
(category, re.compile(message, re.I))
48+
for category, message in category_message_pairs
49+
]
50+
for category, matcher in category_message_pairs:
51+
match = None
52+
for i, warning in enumerate(warn_log):
53+
if (
54+
warning.category == category
55+
and matcher.match(warning.message.args[0])
56+
):
57+
match = i
58+
break
59+
if match is None:
60+
raise AssertionError(
61+
"Expected warning not found: %r %r"
62+
% (category, matcher.pattern)
63+
)
64+
warn_log.pop(match)

testkitbackend/totestkit.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import math
2020

21-
import pytest
22-
2321
from neo4j.graph import (
2422
Node,
2523
Path,
@@ -36,6 +34,8 @@
3634
Time,
3735
)
3836

37+
from ._warning_check import warning_check
38+
3939

4040
def record(rec):
4141
fields = []
@@ -75,9 +75,8 @@ def to(name, val):
7575
if isinstance(v, (bytes, bytearray)):
7676
return to("CypherBytes", " ".join("{:02x}".format(byte) for byte in v))
7777
if isinstance(v, Node):
78-
with pytest.warns(
79-
DeprecationWarning,
80-
match="`id` is deprecated, use `element_id` instead"
78+
with warning_check(
79+
DeprecationWarning, "`id` is deprecated, use `element_id` instead"
8180
):
8281
id_ = v.id
8382
node = {
@@ -88,19 +87,16 @@ def to(name, val):
8887
}
8988
return {"name": "Node", "data": node}
9089
if isinstance(v, Relationship):
91-
with pytest.warns(
92-
DeprecationWarning,
93-
match="`id` is deprecated, use `element_id` instead"
90+
with warning_check(
91+
DeprecationWarning, "`id` is deprecated, use `element_id` instead"
9492
):
9593
id_ = v.id
96-
with pytest.warns(
97-
DeprecationWarning,
98-
match="`id` is deprecated, use `element_id` instead"
94+
with warning_check(
95+
DeprecationWarning, "`id` is deprecated, use `element_id` instead"
9996
):
10097
start_id = v.start_node.id
101-
with pytest.warns(
102-
DeprecationWarning,
103-
match="`id` is deprecated, use `element_id` instead"
98+
with warning_check(
99+
DeprecationWarning, "`id` is deprecated, use `element_id` instead"
104100
):
105101
end_id = v.end_node.id
106102
rel = {

0 commit comments

Comments
 (0)