|
| 1 | +# Copyright (c) "Neo4j" |
| 2 | +# Neo4j Sweden AB [http://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 | +# http://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 logging |
| 20 | + |
1 | 21 | import pytest |
2 | 22 |
|
3 | | -from neo4j.io._common import Outbox |
| 23 | +from neo4j.io._common import ( |
| 24 | + Outbox, |
| 25 | + ResetResponse, |
| 26 | +) |
| 27 | + |
| 28 | +from ..work import fake_connection |
4 | 29 |
|
5 | 30 |
|
6 | 31 | @pytest.mark.parametrize(("chunk_size", "data", "result"), ( |
@@ -30,3 +55,100 @@ def test_outbox_chunking(chunk_size, data, result): |
30 | 55 | assert bytes(outbox.view()) == result |
31 | 56 | outbox.clear() |
32 | 57 | assert bytes(outbox.view()) == b"" |
| 58 | + |
| 59 | + |
| 60 | +def get_handler_arg(response): |
| 61 | + if response == "RECORD": |
| 62 | + return [] |
| 63 | + elif response == "IGNORED": |
| 64 | + return {} |
| 65 | + elif response == "FAILURE": |
| 66 | + return {} |
| 67 | + elif response == "SUCCESS": |
| 68 | + return {} |
| 69 | + else: |
| 70 | + raise ValueError(f"Unexpected response: {response}") |
| 71 | + |
| 72 | + |
| 73 | +def call_handler(handler, response, arg=None): |
| 74 | + if arg is None: |
| 75 | + arg = get_handler_arg(response) |
| 76 | + |
| 77 | + if response == "RECORD": |
| 78 | + return handler.on_records(arg) |
| 79 | + elif response == "IGNORED": |
| 80 | + return handler.on_ignored(arg) |
| 81 | + elif response == "FAILURE": |
| 82 | + return handler.on_failure(arg) |
| 83 | + elif response == "SUCCESS": |
| 84 | + return handler.on_success(arg) |
| 85 | + else: |
| 86 | + raise ValueError(f"Unexpected response: {response}") |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.parametrize( |
| 90 | + ("response", "unexpected"), |
| 91 | + ( |
| 92 | + ("RECORD", True), |
| 93 | + ("IGNORED", True), |
| 94 | + ("FAILURE", True), |
| 95 | + ("SUCCESS", False), |
| 96 | + ) |
| 97 | +) |
| 98 | +def test_reset_response_closes_connection_on_unexpected_responses( |
| 99 | + response, unexpected, fake_connection |
| 100 | +): |
| 101 | + handler = ResetResponse(fake_connection, "reset") |
| 102 | + fake_connection.close.assert_not_called() |
| 103 | + |
| 104 | + call_handler(handler, response) |
| 105 | + |
| 106 | + if unexpected: |
| 107 | + fake_connection.close.assert_called_once() |
| 108 | + else: |
| 109 | + fake_connection.close.assert_not_called() |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.parametrize( |
| 113 | + ("response", "unexpected"), |
| 114 | + ( |
| 115 | + ("RECORD", True), |
| 116 | + ("IGNORED", True), |
| 117 | + ("FAILURE", True), |
| 118 | + ("SUCCESS", False), |
| 119 | + ) |
| 120 | +) |
| 121 | +def test_reset_response_logs_warning_on_unexpected_responses( |
| 122 | + response, unexpected, fake_connection, caplog |
| 123 | +): |
| 124 | + handler = ResetResponse(fake_connection, "reset") |
| 125 | + |
| 126 | + with caplog.at_level(logging.WARNING): |
| 127 | + call_handler(handler, response) |
| 128 | + |
| 129 | + log_message_found = any("RESET" in msg and "unexpected response" in msg |
| 130 | + for msg in caplog.messages) |
| 131 | + if unexpected: |
| 132 | + assert log_message_found |
| 133 | + else: |
| 134 | + assert not log_message_found |
| 135 | + |
| 136 | + |
| 137 | +@pytest.mark.parametrize("response", |
| 138 | + ("RECORD", "IGNORED", "FAILURE", "SUCCESS")) |
| 139 | +def test_reset_response_never_calls_handlers( |
| 140 | + response, fake_connection, mocker |
| 141 | +): |
| 142 | + handlers = { |
| 143 | + key: mocker.MagicMock(name=key) |
| 144 | + for key in |
| 145 | + ("on_records", "on_ignored", "on_failure", "on_success", "on_summary") |
| 146 | + } |
| 147 | + |
| 148 | + handler = ResetResponse(fake_connection, "reset", **handlers) |
| 149 | + |
| 150 | + arg = get_handler_arg(response) |
| 151 | + call_handler(handler, response, arg) |
| 152 | + |
| 153 | + for handler in handlers.values(): |
| 154 | + handler.assert_not_called() |
0 commit comments