Skip to content

Commit c87aa18

Browse files
committed
add test to the procedure runners
1 parent f4172b1 commit c87aa18

File tree

3 files changed

+230
-0
lines changed

3 files changed

+230
-0
lines changed

test/internal/fake-session.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Copyright (c) 2002-2020 "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+
export default class FakeSession {
21+
constructor (runResponse, fakeConnection) {
22+
this._runResponse = runResponse
23+
this._fakeConnection = fakeConnection
24+
this._closed = false
25+
}
26+
27+
static successful (result) {
28+
return new FakeSession(Promise.resolve(result), null)
29+
}
30+
31+
static failed (error) {
32+
return new FakeSession(Promise.reject(error), null)
33+
}
34+
35+
static withFakeConnection (connection) {
36+
return new FakeSession(null, connection)
37+
}
38+
39+
_run (ignoreQuery, ignoreParameters, queryRunner) {
40+
if (this._runResponse) {
41+
return this._runResponse
42+
}
43+
queryRunner(this._fakeConnection)
44+
return Promise.resolve()
45+
}
46+
47+
withBookmark (bookmark) {
48+
this._lastBookmark = bookmark
49+
return this
50+
}
51+
52+
withDatabase (database) {
53+
this._database = database || ''
54+
return this
55+
}
56+
57+
withMode (mode) {
58+
this._mode = mode
59+
return this
60+
}
61+
62+
withOnComplete (onComplete) {
63+
this._onComplete = onComplete
64+
return this
65+
}
66+
67+
close () {
68+
this._closed = true
69+
return Promise.resolve()
70+
}
71+
72+
isClosed () {
73+
return this._closed
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Copyright (c) 2002-2020 "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 MultiDatabaseRountingProcuderRunner from '../../../src/internal/routing-table-getter/routing-procedure-runner-multi-database'
21+
import ServerAddress from '../../../src/internal/server-address'
22+
import TxConfig from '../../../src/internal/tx-config'
23+
import Result from '../../../src/result'
24+
import FakeConnection from '../fake-connection'
25+
import FakeSession from '../fake-session'
26+
27+
describe('#unit MultiDatabaseRountingProcuderRunner', () => {
28+
it('should run query over protocol with the correct params', () => {
29+
const initialAddress = ServerAddress.fromUrl('bolt://127.0.0.1')
30+
const bookmark = 'bookmark'
31+
const mode = 'READ'
32+
const database = 'adb'
33+
const sessionDatabase = 'session.database'
34+
const onComplete = () => 'nothing'
35+
const connection = new FakeConnection().withProtocolVersion(3)
36+
const context = { someContext: '1234' }
37+
const session = new FakeSession(null, connection)
38+
.withBookmark(bookmark)
39+
.withMode(mode)
40+
.withDatabase(sessionDatabase)
41+
.withOnComplete(onComplete)
42+
43+
run({ connection, context, session, database, initialAddress })
44+
45+
expect(connection.seenQueries).toEqual([
46+
'CALL dbms.routing.getRoutingTable($context, $database)'
47+
])
48+
expect(connection.seenParameters).toEqual([
49+
{ context: { ...context, address: initialAddress }, database }
50+
])
51+
expect(connection.seenProtocolOptions).toEqual([
52+
{
53+
bookmark,
54+
txConfig: TxConfig.empty(),
55+
mode,
56+
database: sessionDatabase,
57+
afterComplete: onComplete
58+
}
59+
])
60+
})
61+
62+
it('should return a result', () => {
63+
const connection = new FakeConnection().withProtocolVersion(3)
64+
const context = { someContext: '1234' }
65+
const session = new FakeSession(null, connection)
66+
67+
const result = run({ connection, context, session })
68+
69+
expect(result).toEqual(jasmine.any(Result))
70+
expect(result._streamObserverPromise).toEqual(jasmine.any(Promise))
71+
})
72+
})
73+
74+
function run ({
75+
connection,
76+
database = 'adb',
77+
context,
78+
session,
79+
initialAddress
80+
}) {
81+
const runner = new MultiDatabaseRountingProcuderRunner(initialAddress)
82+
return runner.run(connection, database, context, session)
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Copyright (c) 2002-2020 "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 SingleDatabaseRountingProcuderRunner from '../../../src/internal/routing-table-getter/routing-procedure-runner-single-database'
21+
import TxConfig from '../../../src/internal/tx-config'
22+
import Result from '../../../src/result'
23+
import FakeConnection from '../fake-connection'
24+
import FakeSession from '../fake-session'
25+
26+
describe('#unit SingleDatabaseRountingProcuderRunner', () => {
27+
it('should run query over protocol with the correct params', () => {
28+
const bookmark = 'bookmark'
29+
const mode = 'READ'
30+
const sessionDatabase = 'session.database'
31+
const onComplete = () => 'nothing'
32+
const connection = new FakeConnection().withProtocolVersion(3)
33+
const context = { someContext: '1234' }
34+
const session = new FakeSession(null, connection)
35+
.withBookmark(bookmark)
36+
.withMode(mode)
37+
.withDatabase(sessionDatabase)
38+
.withOnComplete(onComplete)
39+
40+
run({ connection, context, session })
41+
42+
expect(connection.seenQueries).toEqual([
43+
'CALL dbms.cluster.routing.getRoutingTable($context)'
44+
])
45+
expect(connection.seenParameters).toEqual([{ context }])
46+
expect(connection.seenProtocolOptions).toEqual([
47+
{
48+
bookmark,
49+
txConfig: TxConfig.empty(),
50+
mode,
51+
database: sessionDatabase,
52+
afterComplete: onComplete
53+
}
54+
])
55+
})
56+
57+
it('should return a result', () => {
58+
const connection = new FakeConnection().withProtocolVersion(3)
59+
const context = { someContext: '1234' }
60+
const session = new FakeSession(null, connection)
61+
62+
const result = run({ connection, context, session })
63+
64+
expect(result).toEqual(jasmine.any(Result))
65+
expect(result._streamObserverPromise).toEqual(jasmine.any(Promise))
66+
})
67+
})
68+
69+
function run ({ connection, database = 'adb', context, session }) {
70+
const runner = new SingleDatabaseRountingProcuderRunner()
71+
return runner.run(connection, database, context, session)
72+
}

0 commit comments

Comments
 (0)