Skip to content

Commit

Permalink
TestKit backend: except txMeta as Cypher types (neo4j#1039)
Browse files Browse the repository at this point in the history
Signed-off-by: Antonio Barcelos <antonio.barcelos@neo4j.com>
  • Loading branch information
robsdedude authored and bigmontz committed Jan 9, 2023
1 parent 6f5ccf4 commit 72c8474
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 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-rx.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,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 rxResult
try {
Expand Down Expand Up @@ -112,8 +109,9 @@ export function ResultConsume (_, 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 @@ -188,8 +186,9 @@ export function TransactionClose (_, 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)

try {
return session.executeRead(tx => {
Expand All @@ -207,8 +206,9 @@ export function SessionReadTransaction (_, 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)

try {
return session.executeWrite(tx => {
Expand Down
20 changes: 11 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,10 @@ 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 +273,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 +327,10 @@ 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 72c8474

Please sign in to comment.