Skip to content

Commit d91df6d

Browse files
authored
teskit-backend: Enabling tests for Result.then (#813)
These changes enable the test relate to consume all records as list in one function call. The Result implementation in javascript doesn't allow resolve and subscribe at the same result without having strange behaviours. Then, the moment `ResultObserver` subscribes to the `Result` events was moved to the moment someone interacts with some `ResultObserver` method. This also implies in text which call next before list being skipped.
1 parent 1e1e517 commit d91df6d

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

packages/testkit-backend/src/request-handlers.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ export function SessionRun (context, data, wire) {
125125
.then(_ => {
126126
const result = session.run(cypher, params, { metadata, timeout })
127127
const resultObserver = new ResultObserver({ sessionId, result })
128-
result.subscribe(resultObserver)
129128
const id = context.addResultObserver(resultObserver)
130129
wire.writeResponse('Result', { id })
131130
})
@@ -169,6 +168,22 @@ export function ResultConsume (context, data, wire) {
169168
.catch(e => wire.writeError(e))
170169
}
171170

171+
export function ResultList (context, data, wire) {
172+
const { resultId } = data
173+
174+
const resultObserver = context.getResultObserver(resultId)
175+
const result = resultObserver.result
176+
177+
result
178+
.then(({ records }) => {
179+
const cypherRecords = records.map(rec => {
180+
return { values: Array.from(rec.values()).map(nativeToCypher) }
181+
})
182+
wire.writeResponse('RecordList', { records: cypherRecords})
183+
})
184+
.catch(error => wire.writeError(error))
185+
}
186+
172187
export function SessionReadTransaction (context, data, wire) {
173188
const { sessionId, txMeta: metadata } = data
174189
const session = context.getSession(sessionId)
@@ -194,7 +209,6 @@ export function TransactionRun (context, data, wire) {
194209
}
195210
const result = tx.tx.run(cypher, params)
196211
const resultObserver = new ResultObserver({ result })
197-
result.subscribe(resultObserver)
198212
const id = context.addResultObserver(resultObserver)
199213
wire.writeResponse('Result', { id })
200214
}
@@ -285,6 +299,7 @@ export function GetFeatures (_context, _params, wire) {
285299
'Feature:Bolt:4.2',
286300
'Feature:Bolt:4.3',
287301
'Feature:Bolt:4.4',
302+
'Feature:API:Result.List',
288303
...SUPPORTED_TLS
289304
]
290305
})

packages/testkit-backend/src/result-observer.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default class ResultObserver {
1414
this.onCompleted = this.onCompleted.bind(this)
1515
this.onError = this.onError.bind(this)
1616
this._completitionPromise = null
17+
this._subscribed = false
1718
}
1819

1920
onKeys (keys) {
@@ -39,8 +40,13 @@ export default class ResultObserver {
3940
this._completitionPromise = null
4041
}
4142

43+
get result () {
44+
return this._result
45+
}
46+
4247
// Returns a promise, only one outstanding next!
4348
next () {
49+
this._subscribe()
4450
return new Promise((resolve, reject) => {
4551
this._promise = {
4652
resolve,
@@ -51,6 +57,7 @@ export default class ResultObserver {
5157
}
5258

5359
completitionPromise () {
60+
this._subscribe()
5461
return new Promise((resolve, reject) => {
5562
if (this._summary) {
5663
resolve(this._summary)
@@ -110,4 +117,11 @@ export default class ResultObserver {
110117
promise.reject(err)
111118
}
112119
}
120+
121+
_subscribe () {
122+
if (!this._subscribed) {
123+
this._result.subscribe(this)
124+
this._subscribed = true
125+
}
126+
}
113127
}

packages/testkit-backend/src/skipped-tests.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ const skippedTests = [
9494
ifEquals(
9595
'neo4j.sessionrun.TestSessionRun.test_partial_iteration'
9696
)
97+
),
98+
skip(
99+
'Driver does not support mixing Result.subscribe with Result.then',
100+
ifEquals(
101+
'stub.iteration.test_result_list.TestResultList.test_tx_run_result_list_pulls_all_records_at_once_next_before_list'
102+
),
103+
ifEquals(
104+
'stub.iteration.test_result_list.TestResultList.test_tx_func_result_list_pulls_all_records_at_once_next_before_list'
105+
),
106+
ifEquals(
107+
'stub.iteration.test_result_list.TestResultList.test_session_run_result_list_pulls_all_records_at_once_next_before_list'
108+
)
97109
)
98110
]
99111

0 commit comments

Comments
 (0)