|
19 | 19 |
|
20 | 20 | import neo4j from '../../src/v1'; |
21 | 21 | import sharedNeo4j from '../internal/shared-neo4j'; |
| 22 | +import _ from 'lodash'; |
| 23 | +import {ServerVersion, VERSION_3_2_0} from '../../src/v1/internal/server-version'; |
22 | 24 |
|
23 | 25 | describe('floating point values', () => { |
24 | 26 | it('should support float 1.0 ', testValue(1)); |
@@ -136,18 +138,117 @@ describe('path values', () => { |
136 | 138 | }); |
137 | 139 | }); |
138 | 140 |
|
139 | | -function testValue(actual, expected) { |
140 | | - return done => { |
| 141 | +describe('byte arrays', () => { |
| 142 | + |
| 143 | + let originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL; |
| 144 | + let serverSupportsByteArrays = false; |
| 145 | + |
| 146 | + beforeEach(done => { |
| 147 | + jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000; |
| 148 | + |
141 | 149 | const driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken); |
142 | 150 | const session = driver.session(); |
| 151 | + session.run('RETURN 1').then(result => { |
| 152 | + driver.close(); |
| 153 | + const serverVersion = ServerVersion.fromString(result.summary.server.version); |
| 154 | + serverSupportsByteArrays = serverVersion.compareTo(VERSION_3_2_0) >= 0; |
| 155 | + done(); |
| 156 | + }); |
| 157 | + }); |
143 | 158 |
|
144 | | - session.run('RETURN {val} as v', {val: actual}) |
145 | | - .then(result => { |
146 | | - expect(result.records[0].get('v')).toEqual(expected || actual); |
147 | | - driver.close(); |
148 | | - done(); |
149 | | - }).catch(err => { |
150 | | - console.log(err); |
| 159 | + afterEach(() => { |
| 160 | + jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout; |
| 161 | + }); |
| 162 | + |
| 163 | + it('should support returning empty byte array', done => { |
| 164 | + if(!serverSupportsByteArrays) { |
| 165 | + done(); |
| 166 | + return; |
| 167 | + } |
| 168 | + |
| 169 | + testValue(new Int8Array(0))(done); |
| 170 | + }); |
| 171 | + |
| 172 | + it('should support returning empty byte array', conditionalTestValues(serverSupportsByteArrays, new Int8Array(0))); |
| 173 | + |
| 174 | + it('should support returning short byte arrays', conditionalTestValues(serverSupportsByteArrays, randomByteArrays(100, 1, 255))); |
| 175 | + |
| 176 | + it('should support returning medium byte arrays', conditionalTestValues(serverSupportsByteArrays, randomByteArrays(50, 256, 65535))); |
| 177 | + |
| 178 | + it('should support returning long byte arrays', conditionalTestValues(serverSupportsByteArrays, randomByteArrays(10, 65536, 2 * 65536))); |
| 179 | + |
| 180 | + it('should fail to return byte array', done => { |
| 181 | + if (serverSupportsByteArrays) { |
| 182 | + done(); |
| 183 | + return; |
| 184 | + } |
| 185 | + |
| 186 | + const driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken); |
| 187 | + const session = driver.session(); |
| 188 | + session.run('RETURN {array}', {array: randomByteArray(42)}).catch(error => { |
| 189 | + driver.close(); |
| 190 | + expect(error.message).toEqual('Byte arrays are not supported by the database this driver is connected to'); |
| 191 | + done(); |
| 192 | + }); |
| 193 | + }); |
| 194 | +}); |
| 195 | + |
| 196 | +function conditionalTestValues(condition, values) { |
| 197 | + if (!condition) { |
| 198 | + return done => done(); |
| 199 | + } |
| 200 | + |
| 201 | + const driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken); |
| 202 | + const queriesPromise = values.reduce((acc, value) => |
| 203 | + acc.then(() => runReturnQuery(driver, value)), Promise.resolve()); |
| 204 | + return asTestFunction(queriesPromise, driver); |
| 205 | +} |
| 206 | + |
| 207 | +function testValue(actual, expected) { |
| 208 | + const driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken); |
| 209 | + const queryPromise = runReturnQuery(driver, actual, expected); |
| 210 | + return asTestFunction(queryPromise, driver); |
| 211 | +} |
| 212 | + |
| 213 | +function testValues(values) { |
| 214 | + const driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken); |
| 215 | + const queriesPromise = values.reduce((acc, value) => |
| 216 | + acc.then(() => runReturnQuery(driver, value)), Promise.resolve()); |
| 217 | + return asTestFunction(queriesPromise, driver); |
| 218 | +} |
| 219 | + |
| 220 | +function runReturnQuery(driver, actual, expected) { |
| 221 | + const session = driver.session(); |
| 222 | + return new Promise((resolve, reject) => { |
| 223 | + session.run('RETURN {val} as v', {val: actual}).then(result => { |
| 224 | + expect(result.records[0].get('v')).toEqual(expected || actual); |
| 225 | + session.close(); |
| 226 | + resolve(); |
| 227 | + }).catch(error => { |
| 228 | + reject(error); |
151 | 229 | }); |
152 | | - }; |
| 230 | + }); |
| 231 | +} |
| 232 | + |
| 233 | +function asTestFunction(promise, driver) { |
| 234 | + return done => |
| 235 | + promise.then(() => { |
| 236 | + driver.close(); |
| 237 | + done(); |
| 238 | + }).catch(error => { |
| 239 | + driver.close(); |
| 240 | + console.log(error); |
| 241 | + }); |
| 242 | +} |
| 243 | + |
| 244 | +function randomByteArrays(count, minLength, maxLength) { |
| 245 | + return _.range(count).map(() => { |
| 246 | + const length = _.random(minLength, maxLength); |
| 247 | + return randomByteArray(length); |
| 248 | + }); |
| 249 | +} |
| 250 | + |
| 251 | +function randomByteArray(length) { |
| 252 | + const array = _.range(length).map(() => _.random(-128, 127)); |
| 253 | + return new Int8Array(array); |
153 | 254 | } |
0 commit comments