Skip to content

Commit 86b70cc

Browse files
committed
TestKit backend: fix warning assertions
1 parent 070a3be commit 86b70cc

File tree

2 files changed

+99
-10
lines changed

2 files changed

+99
-10
lines changed

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/requests.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,21 @@
1414
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
17+
18+
1719
import json
1820
from os import path
19-
import warnings
2021

2122
import neo4j
2223
import testkitbackend.fromtestkit as fromtestkit
2324
import testkitbackend.totestkit as totestkit
2425
from testkitbackend.fromtestkit import to_meta_and_timeout
2526

27+
from ._warning_check import (
28+
warning_check,
29+
warnings_check,
30+
)
31+
2632

2733
class FrontendError(Exception):
2834
pass
@@ -98,9 +104,21 @@ def NewDriver(backend, data):
98104
data.mark_item_as_read_if_equals("livenessCheckTimeoutMs", None)
99105

100106
data.mark_item_as_read("domainNameResolverRegistered")
101-
driver = neo4j.GraphDatabase.driver(
102-
data["uri"], auth=auth, user_agent=data["userAgent"], **kwargs
103-
)
107+
expected_warnings = []
108+
if "update_routing_table_timeout" in kwargs:
109+
expected_warnings.append((
110+
DeprecationWarning,
111+
"The 'update_routing_table_timeout' config key is deprecated"
112+
))
113+
if "session_connection_timeout" in kwargs:
114+
expected_warnings.append((
115+
DeprecationWarning,
116+
"The 'session_connection_timeout' config key is deprecated"
117+
))
118+
with warnings_check(expected_warnings):
119+
driver = neo4j.GraphDatabase.driver(
120+
data["uri"], auth=auth, user_agent=data["userAgent"], **kwargs
121+
)
104122
key = backend.next_key()
105123
backend.drivers[key] = driver
106124
backend.send_response("Driver", {"id": key})
@@ -109,19 +127,26 @@ def NewDriver(backend, data):
109127
def VerifyConnectivity(backend, data):
110128
driver_id = data["driverId"]
111129
driver = backend.drivers[driver_id]
112-
with warnings.catch_warnings():
113-
warnings.simplefilter("ignore", category=neo4j.ExperimentalWarning)
130+
with warning_check(
131+
neo4j.ExperimentalWarning,
132+
"The configuration may change in the future."
133+
):
114134
driver.verify_connectivity()
115135
backend.send_response("Driver", {"id": driver_id})
116136

117137

118138
def CheckMultiDBSupport(backend, data):
119139
driver_id = data["driverId"]
120140
driver = backend.drivers[driver_id]
121-
backend.send_response(
122-
"MultiDBSupport",
123-
{"id": backend.next_key(), "available": driver.supports_multi_db()}
124-
)
141+
with warning_check(
142+
neo4j.ExperimentalWarning,
143+
"Feature support query, based on Bolt protocol version and Neo4j "
144+
"server version will change in the future."
145+
):
146+
available = driver.supports_multi_db()
147+
backend.send_response("MultiDBSupport", {
148+
"id": backend.next_key(), "available": available
149+
})
125150

126151

127152
def resolution_func(backend, custom_resolver=False, custom_dns_resolver=False):

0 commit comments

Comments
 (0)