|
| 1 | +/** |
| 2 | + * Copyright (c) 2002-2019 "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 neo4j from '../src' |
| 21 | +import { statementType } from '../src/result-summary' |
| 22 | +import Session from '../src/session' |
| 23 | +import { READ } from '../src/driver' |
| 24 | +import SingleConnectionProvider from '../src/internal/connection-provider-single' |
| 25 | +import FakeConnection from './internal/fake-connection' |
| 26 | +import sharedNeo4j from './internal/shared-neo4j' |
| 27 | +import _ from 'lodash' |
| 28 | +import { ServerVersion, VERSION_4_0_0 } from '../src/internal/server-version' |
| 29 | +import { isString } from '../src/internal/util' |
| 30 | +import testUtils from './internal/test-utils' |
| 31 | +import { newError, PROTOCOL_ERROR, SESSION_EXPIRED } from '../src/error' |
| 32 | +import ServerAddress from '../src/internal/server-address' |
| 33 | +import { |
| 34 | + bufferCount, |
| 35 | + catchError, |
| 36 | + concat, |
| 37 | + flatMap, |
| 38 | + map, |
| 39 | + materialize, |
| 40 | + toArray |
| 41 | +} from 'rxjs/operators' |
| 42 | +import { Notification, throwError } from 'rxjs' |
| 43 | + |
| 44 | +describe('#integration session', () => { |
| 45 | + let driver |
| 46 | + let session |
| 47 | + // eslint-disable-next-line no-unused-vars |
| 48 | + let serverVersion |
| 49 | + let originalTimeout |
| 50 | + |
| 51 | + beforeEach(async () => { |
| 52 | + driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken) |
| 53 | + session = driver.session({ fetchSize: 2 }) |
| 54 | + originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL |
| 55 | + jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000 |
| 56 | + |
| 57 | + serverVersion = await sharedNeo4j.cleanupAndGetVersion(driver) |
| 58 | + }) |
| 59 | + |
| 60 | + afterEach(async () => { |
| 61 | + jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout |
| 62 | + await driver.close() |
| 63 | + }) |
| 64 | + |
| 65 | + it('should handle nested queries within one transaction', done => { |
| 66 | + const size = 20 |
| 67 | + let count = 0 |
| 68 | + const tx = session.beginTransaction() |
| 69 | + const results = [] |
| 70 | + tx.run('UNWIND range(1, $size) AS x RETURN x', { size: size }).subscribe({ |
| 71 | + onNext: record => { |
| 72 | + const x = record.get('x').toInt() |
| 73 | + let index = 0 |
| 74 | + const result = tx.run( |
| 75 | + 'UNWIND range (1, $x) AS x CREATE (n:Node {id: x}) RETURN n.id', |
| 76 | + { x: x } |
| 77 | + ) |
| 78 | + results.push(result) |
| 79 | + result.subscribe({ |
| 80 | + onNext (record) { |
| 81 | + const value = record.get('n.id') |
| 82 | + index++ |
| 83 | + expect(value.toInt()).toEqual(index) |
| 84 | + }, |
| 85 | + onCompleted (summary) { |
| 86 | + expect(index).toEqual(x) |
| 87 | + count += x |
| 88 | + } |
| 89 | + }) |
| 90 | + }, |
| 91 | + onCompleted: () => { |
| 92 | + Promise.all(results).then(() => { |
| 93 | + tx.commit().then(() => { |
| 94 | + expect(count).toBe(((1 + size) * size) / 2) |
| 95 | + session.close().then(() => done()) |
| 96 | + }) |
| 97 | + }) |
| 98 | + }, |
| 99 | + onError: error => { |
| 100 | + console.log(error) |
| 101 | + } |
| 102 | + }) |
| 103 | + }) |
| 104 | + |
| 105 | + it('should give proper error when nesting queries within one session', done => { |
| 106 | + const size = 20 |
| 107 | + const count = 0 |
| 108 | + const result = session.run('UNWIND range(1, $size) AS x RETURN x', { |
| 109 | + size: size |
| 110 | + }) |
| 111 | + result.subscribe({ |
| 112 | + onNext: async record => { |
| 113 | + const x = record.get('x').toInt() |
| 114 | + await expectAsync( |
| 115 | + session.run('CREATE (n:Node {id: $x}) RETURN n.id', { x: x }) |
| 116 | + ).toBeRejectedWith( |
| 117 | + jasmine.objectContaining({ |
| 118 | + message: |
| 119 | + 'Statements cannot be run directly on a session with an open transaction; ' + |
| 120 | + 'either run from within the transaction or use a different session.' |
| 121 | + }) |
| 122 | + ) |
| 123 | + }, |
| 124 | + onCompleted: () => { |
| 125 | + session.close().then(() => done()) |
| 126 | + }, |
| 127 | + onError: error => { |
| 128 | + console.log(error) |
| 129 | + } |
| 130 | + }) |
| 131 | + }) |
| 132 | + |
| 133 | + it('should handle sequential query runs within one session', done => { |
| 134 | + const size = 20 |
| 135 | + let count = 0 |
| 136 | + session |
| 137 | + .run('UNWIND range(1, $size) AS x RETURN x', { size: size }) |
| 138 | + .then(async result => { |
| 139 | + for (const record of result.records) { |
| 140 | + const x = record.get('x') |
| 141 | + const innerResult = await session.run( |
| 142 | + 'CREATE (n:Node {id: $x}) RETURN n.id', |
| 143 | + { x: x } |
| 144 | + ) |
| 145 | + expect(innerResult.records.length).toEqual(1) |
| 146 | + expect(innerResult.records[0].get('n.id')).toEqual(x) |
| 147 | + count++ |
| 148 | + } |
| 149 | + expect(count).toEqual(size) |
| 150 | + session.close().then(() => done()) |
| 151 | + }) |
| 152 | + }) |
| 153 | +}) |
0 commit comments