Skip to content

Commit

Permalink
TestKit backend: except txMeta as Cypher types
Browse files Browse the repository at this point in the history
  • Loading branch information
robsdedude committed Dec 7, 2022
1 parent ee72ed0 commit 3ed87bf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
24 changes: 14 additions & 10 deletions packages/testkit-backend/src/cypher-native-binders.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ export default function CypherNativeBinders (neo4j) {
function objectToCypher (obj) {
return objectMapper(obj, nativeToCypher)
}

function objectToNative (obj) {
return objectMapper(obj, cypherToNative)
}

function objectMemberBitIntToNumber (obj, recursive = false) {
return objectMapper(obj, val => {
if (typeof val === 'bigint') {
Expand All @@ -19,7 +22,7 @@ export default function CypherNativeBinders (neo4j) {
return val
})
}

function objectMapper (obj, mapper) {
if (obj === null || obj === undefined) {
return obj
Expand All @@ -28,7 +31,7 @@ export default function CypherNativeBinders (neo4j) {
return { ...acc, [key]: mapper(obj[key]) }
}, {})
}

function nativeToCypher (x) {
if (x == null) {
return valueResponse('CypherNull', null)
Expand All @@ -53,7 +56,7 @@ export default function CypherNativeBinders (neo4j) {
console.log(err)
throw Error(err)
}

function valueResponseOfObject (x) {
if (neo4j.isInt(x)) {
// TODO: Broken!!!
Expand Down Expand Up @@ -105,7 +108,7 @@ export default function CypherNativeBinders (neo4j) {
},
{ nodes: [x.start], relationships: [] }
)

return {
name: 'CypherPath',
data: {
Expand All @@ -114,7 +117,7 @@ export default function CypherNativeBinders (neo4j) {
}
}
}

if (neo4j.isDate(x)) {
return structResponse('CypherDate', {
year: x.year,
Expand Down Expand Up @@ -149,15 +152,15 @@ export default function CypherNativeBinders (neo4j) {
nanoseconds: x.nanoseconds
})
}

// If all failed, interpret as a map
const map = {}
for (const [key, value] of Object.entries(x)) {
map[key] = nativeToCypher(value)
}
return valueResponse('CypherMap', map)
}

function structResponse (name, data) {
const map = {}
for (const [key, value] of Object.entries(data)) {
Expand All @@ -167,7 +170,7 @@ export default function CypherNativeBinders (neo4j) {
}
return { name, data: map }
}

function cypherToNative (c) {
const {
name,
Expand Down Expand Up @@ -249,9 +252,10 @@ export default function CypherNativeBinders (neo4j) {
console.log(err)
throw Error(err)
}

this.valueResponse = valueResponse
this.objectToCypher = objectToCypher
this.objectToNative = objectToNative
this.objectMemberBitIntToNumber = objectMemberBitIntToNumber
this.nativeToCypher = nativeToCypher
this.cypherToNative = cypherToNative
Expand Down
18 changes: 9 additions & 9 deletions packages/testkit-backend/src/request-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,10 @@ export function SessionClose (_, context, data, wire) {
}

export function SessionRun (_, context, data, wire) {
const { sessionId, cypher, params, txMeta: metadata, timeout } = data
const { sessionId, cypher, timeout } = data
const session = context.getSession(sessionId)
if (params) {
for (const [key, value] of Object.entries(params)) {
params[key] = context.binder.cypherToNative(value)
}
}
const params = context.binder.objectToNative(data.params)
const metadata = context.binder.objectToNative(data.txMeta)

let result
try {
Expand Down Expand Up @@ -230,8 +227,9 @@ export function ResultList (_, context, data, wire) {
}

export function SessionReadTransaction (_, context, data, wire) {
const { sessionId, txMeta: metadata } = data
const { sessionId } = data
const session = context.getSession(sessionId)
const metadata = context.binder.objectToNative(data.txMeta)
return session
.executeRead(
tx =>
Expand Down Expand Up @@ -274,8 +272,9 @@ export function RetryableNegative (_, context, data, wire) {
}

export function SessionBeginTransaction (_, context, data, wire) {
const { sessionId, txMeta: metadata, timeout } = data
const { sessionId, timeout } = data
const session = context.getSession(sessionId)
const metadata = context.binder.objectToNative(data.txMeta)

try {
return session.beginTransaction({ metadata, timeout })
Expand Down Expand Up @@ -327,8 +326,9 @@ export function SessionLastBookmarks (_, context, data, wire) {
}

export function SessionWriteTransaction (_, context, data, wire) {
const { sessionId, txMeta: metadata } = data
const { sessionId } = data
const session = context.getSession(sessionId)
const metadata = context.binder.objectToNative(data.txMeta)
return session
.executeWrite(
tx =>
Expand Down

0 comments on commit 3ed87bf

Please sign in to comment.