|
| 1 | +/** |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import Connection from '../src/connection/connection' |
| 21 | + |
| 22 | +/** |
| 23 | + * This class is like a mock of {@link Connection} that tracks invocations count. |
| 24 | + * It tries to maintain same "interface" as {@link Connection}. |
| 25 | + * It could be replaced with a proper mock by a library like testdouble. |
| 26 | + * At the time of writing such libraries require {@link Proxy} support but browser tests execute in |
| 27 | + * PhantomJS which does not support proxies. |
| 28 | + */ |
| 29 | +export default class FakeConnection extends Connection { |
| 30 | + constructor () { |
| 31 | + super(null) |
| 32 | + |
| 33 | + this._open = true |
| 34 | + this._id = 0 |
| 35 | + this._databaseId = null |
| 36 | + this._requestRoutingInformationMock = null |
| 37 | + this.creationTimestamp = Date.now() |
| 38 | + |
| 39 | + this.resetInvoked = 0 |
| 40 | + this.releaseInvoked = 0 |
| 41 | + this.seenQueries = [] |
| 42 | + this.seenParameters = [] |
| 43 | + this.seenProtocolOptions = [] |
| 44 | + this._server = {} |
| 45 | + this.protocolVersion = undefined |
| 46 | + this.protocolErrorsHandled = 0 |
| 47 | + this.seenProtocolErrors = [] |
| 48 | + this.seenRequestRoutingInformation = [] |
| 49 | + } |
| 50 | + |
| 51 | + get id () { |
| 52 | + return this._id |
| 53 | + } |
| 54 | + |
| 55 | + get databaseId () { |
| 56 | + return this._databaseId |
| 57 | + } |
| 58 | + |
| 59 | + set databaseId (value) { |
| 60 | + this._databaseId = value |
| 61 | + } |
| 62 | + |
| 63 | + get server () { |
| 64 | + return this._server |
| 65 | + } |
| 66 | + |
| 67 | + get version () { |
| 68 | + return this._server.version |
| 69 | + } |
| 70 | + |
| 71 | + set version (value) { |
| 72 | + this._server.version = value |
| 73 | + } |
| 74 | + |
| 75 | + protocol () { |
| 76 | + // return fake protocol object that simply records seen queries and parameters |
| 77 | + return { |
| 78 | + run: (query, parameters, protocolOptions) => { |
| 79 | + this.seenQueries.push(query) |
| 80 | + this.seenParameters.push(parameters) |
| 81 | + this.seenProtocolOptions.push(protocolOptions) |
| 82 | + }, |
| 83 | + requestRoutingInformation: params => { |
| 84 | + this.seenRequestRoutingInformation.push(params) |
| 85 | + if (this._requestRoutingInformationMock) { |
| 86 | + this._requestRoutingInformationMock(params) |
| 87 | + } |
| 88 | + }, |
| 89 | + version: this.protocolVersion |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + resetAndFlush () { |
| 94 | + this.resetInvoked++ |
| 95 | + return Promise.resolve() |
| 96 | + } |
| 97 | + |
| 98 | + _release () { |
| 99 | + this.releaseInvoked++ |
| 100 | + return Promise.resolve() |
| 101 | + } |
| 102 | + |
| 103 | + isOpen () { |
| 104 | + return this._open |
| 105 | + } |
| 106 | + |
| 107 | + isNeverReleased () { |
| 108 | + return this.isReleasedTimes(0) |
| 109 | + } |
| 110 | + |
| 111 | + isReleasedOnce () { |
| 112 | + return this.isReleasedTimes(1) |
| 113 | + } |
| 114 | + |
| 115 | + isReleasedTimes (times) { |
| 116 | + return this.resetInvoked === times && this.releaseInvoked === times |
| 117 | + } |
| 118 | + |
| 119 | + _handleProtocolError (message) { |
| 120 | + this.protocolErrorsHandled++ |
| 121 | + this.seenProtocolErrors.push(message) |
| 122 | + } |
| 123 | + |
| 124 | + withProtocolVersion (version) { |
| 125 | + this.protocolVersion = version |
| 126 | + return this |
| 127 | + } |
| 128 | + |
| 129 | + withCreationTimestamp (value) { |
| 130 | + this.creationTimestamp = value |
| 131 | + return this |
| 132 | + } |
| 133 | + |
| 134 | + withRequestRoutingInformationMock (requestRoutingInformationMock) { |
| 135 | + this._requestRoutingInformationMock = requestRoutingInformationMock |
| 136 | + return this |
| 137 | + } |
| 138 | + |
| 139 | + closed () { |
| 140 | + this._open = false |
| 141 | + return this |
| 142 | + } |
| 143 | +} |
0 commit comments