|
| 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, ResultObserver, Record, ResultSummary } from '../../src' |
| 21 | +import { ResultStreamObserver } from '../../src/internal/observers' |
| 22 | + |
| 23 | + |
| 24 | +/** |
| 25 | + * This class is like a mock of {@link Connection} that tracks invocations count. |
| 26 | + * It tries to maintain same "interface" as {@link Connection}. |
| 27 | + * It could be replaced with a proper mock by a library like testdouble. |
| 28 | + * At the time of writing such libraries require {@link Proxy} support but browser tests execute in |
| 29 | + * PhantomJS which does not support proxies. |
| 30 | + */ |
| 31 | +export default class FakeConnection extends Connection { |
| 32 | + private _open: boolean |
| 33 | + private _requestRoutingInformationMock: ((params: any) => void) | null |
| 34 | + public creationTimestamp: number |
| 35 | + public resetInvoked: number |
| 36 | + public releaseInvoked: number |
| 37 | + public seenQueries: string[] |
| 38 | + public seenParameters: any[] |
| 39 | + public seenProtocolOptions: any[] |
| 40 | + private _server: any |
| 41 | + public protocolVersion: number | undefined |
| 42 | + public protocolErrorsHandled: number |
| 43 | + public seenProtocolErrors: string[] |
| 44 | + public seenRequestRoutingInformation: any[] |
| 45 | + public rollbackInvoked: number |
| 46 | + public _rollbackError: Error | null |
| 47 | + |
| 48 | + constructor() { |
| 49 | + super() |
| 50 | + |
| 51 | + this._open = true |
| 52 | + this._requestRoutingInformationMock = null |
| 53 | + this.creationTimestamp = Date.now() |
| 54 | + |
| 55 | + this.resetInvoked = 0 |
| 56 | + this.releaseInvoked = 0 |
| 57 | + this.seenQueries = [] |
| 58 | + this.seenParameters = [] |
| 59 | + this.seenProtocolOptions = [] |
| 60 | + this._server = {} |
| 61 | + this.protocolVersion = undefined |
| 62 | + this.protocolErrorsHandled = 0 |
| 63 | + this.seenProtocolErrors = [] |
| 64 | + this.seenRequestRoutingInformation = [] |
| 65 | + this.rollbackInvoked = 0 |
| 66 | + this._rollbackError = null |
| 67 | + } |
| 68 | + |
| 69 | + protocol() { |
| 70 | + // return fake protocol object that simply records seen queries and parameters |
| 71 | + return { |
| 72 | + run: (query: string, parameters: any | undefined, protocolOptions: any | undefined): ResultStreamObserver => { |
| 73 | + this.seenQueries.push(query) |
| 74 | + this.seenParameters.push(parameters) |
| 75 | + this.seenProtocolOptions.push(protocolOptions) |
| 76 | + return mockResultStreamObserver(query, parameters) |
| 77 | + }, |
| 78 | + commitTransaction: () => { |
| 79 | + return mockResultStreamObserver('COMMIT', {}) |
| 80 | + }, |
| 81 | + beginTransaction: () => { |
| 82 | + return Promise.resolve() |
| 83 | + }, |
| 84 | + rollbackTransaction: () => { |
| 85 | + this.rollbackInvoked ++ |
| 86 | + if (this._rollbackError !== null) { |
| 87 | + return mockResultStreamObserverWithError('ROLLBACK', {}, this._rollbackError) |
| 88 | + } |
| 89 | + return mockResultStreamObserver('ROLLBACK', {}) |
| 90 | + }, |
| 91 | + requestRoutingInformation: (params: any | undefined) => { |
| 92 | + this.seenRequestRoutingInformation.push(params) |
| 93 | + if (this._requestRoutingInformationMock) { |
| 94 | + this._requestRoutingInformationMock(params) |
| 95 | + } |
| 96 | + }, |
| 97 | + version: this.protocolVersion |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + resetAndFlush() { |
| 102 | + this.resetInvoked++ |
| 103 | + return Promise.resolve() |
| 104 | + } |
| 105 | + |
| 106 | + _release() { |
| 107 | + this.releaseInvoked++ |
| 108 | + return Promise.resolve() |
| 109 | + } |
| 110 | + |
| 111 | + isOpen() { |
| 112 | + return this._open |
| 113 | + } |
| 114 | + |
| 115 | + isNeverReleased() { |
| 116 | + return this.isReleasedTimes(0) |
| 117 | + } |
| 118 | + |
| 119 | + isReleasedOnce() { |
| 120 | + return this.isReleasedTimes(1) |
| 121 | + } |
| 122 | + |
| 123 | + isReleasedTimes(times: number) { |
| 124 | + return this.resetInvoked === times && this.releaseInvoked === times |
| 125 | + } |
| 126 | + |
| 127 | + _handleProtocolError(message: string) { |
| 128 | + this.protocolErrorsHandled++ |
| 129 | + this.seenProtocolErrors.push(message) |
| 130 | + } |
| 131 | + |
| 132 | + withProtocolVersion(version: number) { |
| 133 | + this.protocolVersion = version |
| 134 | + return this |
| 135 | + } |
| 136 | + |
| 137 | + withCreationTimestamp(value: number) { |
| 138 | + this.creationTimestamp = value |
| 139 | + return this |
| 140 | + } |
| 141 | + |
| 142 | + withRequestRoutingInformationMock(requestRoutingInformationMock: (params: any) => void) { |
| 143 | + this._requestRoutingInformationMock = requestRoutingInformationMock |
| 144 | + return this |
| 145 | + } |
| 146 | + |
| 147 | + withRollbackError(error: Error) { |
| 148 | + this._rollbackError = error |
| 149 | + return this |
| 150 | + } |
| 151 | + |
| 152 | + closed() { |
| 153 | + this._open = false |
| 154 | + return this |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +function mockResultStreamObserverWithError (query: string, parameters: any | undefined, error: Error) { |
| 159 | + const observer = mockResultStreamObserver(query, parameters) |
| 160 | + observer.subscribe = (observer: ResultObserver) => { |
| 161 | + if (observer && observer.onError) { |
| 162 | + observer.onError(error) |
| 163 | + } |
| 164 | + } |
| 165 | + return observer |
| 166 | +} |
| 167 | + |
| 168 | +function mockResultStreamObserver(query: string, parameters: any | undefined): ResultStreamObserver { |
| 169 | + return { |
| 170 | + onError: (error: any) => { }, |
| 171 | + onCompleted: () => { }, |
| 172 | + onNext: (result: any) => { }, |
| 173 | + cancel: () => { }, |
| 174 | + prepareToHandleSingleResponse: () => { }, |
| 175 | + markCompleted: () => { }, |
| 176 | + subscribe: (observer: ResultObserver) => { |
| 177 | + if (observer && observer.onCompleted) { |
| 178 | + observer.onCompleted(new ResultSummary(query, parameters, {})) |
| 179 | + } |
| 180 | + |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments