2024-11-10 10:15:43 +01:00
|
|
|
import { describe, it } from 'node:test'
|
2024-11-13 20:24:29 +01:00
|
|
|
import assert from '../assert.js'
|
2024-11-18 23:06:58 +01:00
|
|
|
import { node, edge, matchClause } from '../../src/query/match.js'
|
|
|
|
import { makeDirectedEdge, makeEdge, makeNode, makeRelationship } from '../utils.js'
|
2024-11-10 10:15:43 +01:00
|
|
|
|
2024-11-13 20:24:29 +01:00
|
|
|
describe('node', () => {
|
2024-11-14 00:32:46 +01:00
|
|
|
it('should match a node with a name, label, and properties', () => {
|
|
|
|
assert.parseOk(node, '(:Node)', ([actual]) => {
|
2024-11-13 20:24:29 +01:00
|
|
|
const expected = makeNode(undefined, 'Node')
|
|
|
|
assert.deepEqual(actual, expected)
|
|
|
|
|
2024-11-14 00:32:46 +01:00
|
|
|
assert.parseOk(node, '(node:Node)', ([actual]) => {
|
|
|
|
const expected = makeNode('node', 'Node')
|
|
|
|
assert.deepEqual(actual, expected)
|
|
|
|
})
|
2024-11-13 20:24:29 +01:00
|
|
|
|
2024-11-14 00:32:46 +01:00
|
|
|
assert.parseOk(node, '(:Label { name: "Rowan", gay: true })', ([actual]) => {
|
|
|
|
const expected = makeNode(undefined, 'Label', [['name', 'Rowan'], ['gay', true]])
|
|
|
|
assert.deepEqual(actual, expected)
|
|
|
|
})
|
2024-11-13 20:24:29 +01:00
|
|
|
|
2024-11-14 00:32:46 +01:00
|
|
|
assert.parseOk(node, '(name:Label { name: "Rowan", gay: true })', ([actual]) => {
|
|
|
|
const expected = makeNode('name', 'Label', [['name', 'Rowan'], ['gay', true]])
|
|
|
|
assert.deepEqual(actual, expected)
|
|
|
|
})
|
2024-11-13 20:24:29 +01:00
|
|
|
})
|
|
|
|
|
2024-11-14 00:32:46 +01:00
|
|
|
it('should handle whitespace', () => {
|
|
|
|
assert.parseOk(node, '(name:Label { name: "Rowan", gay: true })', ([actual]) => {
|
|
|
|
const expected = makeNode('name', 'Label', [['name', 'Rowan'], ['gay', true]])
|
|
|
|
assert.deepEqual(actual, expected)
|
|
|
|
})
|
2024-11-13 20:24:29 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
describe('edge', () => {
|
2024-11-14 00:32:46 +01:00
|
|
|
it('should match a relationship with a name, label, properties', () => {
|
|
|
|
assert.parseOk(edge, '[name:Label]', ([actual]) => {
|
2024-11-15 07:36:19 +01:00
|
|
|
const expected = makeEdge('name', 'Label')
|
2024-11-13 20:24:29 +01:00
|
|
|
assert.deepEqual(actual, expected)
|
|
|
|
})
|
|
|
|
|
2024-11-14 00:32:46 +01:00
|
|
|
assert.parseOk(edge, '[:Label]', ([actual]) => {
|
2024-11-15 07:36:19 +01:00
|
|
|
const expected = makeEdge(undefined, 'Label')
|
2024-11-13 20:24:29 +01:00
|
|
|
assert.deepEqual(actual, expected)
|
|
|
|
})
|
|
|
|
|
2024-11-14 00:32:46 +01:00
|
|
|
assert.parseOk(edge, '[gay:Queer { binary: "-∞" }]', ([actual]) => {
|
2024-11-13 20:24:29 +01:00
|
|
|
const expected = makeEdge('gay', 'Queer', [['binary', '-∞']])
|
|
|
|
assert.deepEqual(actual, expected)
|
|
|
|
})
|
|
|
|
|
2024-11-14 00:32:46 +01:00
|
|
|
assert.parseOk(edge, '[:Queer { binary: "-∞" }]', ([actual]) => {
|
2024-11-13 20:24:29 +01:00
|
|
|
const expected = makeEdge(undefined, 'Queer', [['binary', '-∞']])
|
|
|
|
assert.deepEqual(actual, expected)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should handle whitespace', () => {
|
2024-11-14 00:32:46 +01:00
|
|
|
assert.parseOk(edge, '[ : Queer { binary : "-∞" } ]', ([actual]) => {
|
2024-11-13 20:24:29 +01:00
|
|
|
const expected = makeEdge(undefined, 'Queer', [['binary', '-∞']])
|
|
|
|
assert.deepEqual(actual, expected)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-11-14 00:32:46 +01:00
|
|
|
describe('MATCH keyword', () => {
|
2024-11-14 02:21:51 +01:00
|
|
|
it('should match a single node with no relationships', () => {
|
2024-11-18 23:06:58 +01:00
|
|
|
assert.parseOk(matchClause, 'MATCH (:Label)', ([actual]) => {
|
2024-11-14 02:21:51 +01:00
|
|
|
const expected = makeNode(undefined, 'Label')
|
|
|
|
assert.deepEqual(actual, expected)
|
|
|
|
})
|
|
|
|
|
2024-11-18 23:06:58 +01:00
|
|
|
assert.parseOk(matchClause, 'MATCH (node:Label)', ([actual]) => {
|
2024-11-13 20:24:29 +01:00
|
|
|
const expected = makeNode('node', 'Label')
|
|
|
|
assert.deepEqual(actual, expected)
|
|
|
|
})
|
2024-11-14 00:32:46 +01:00
|
|
|
|
2024-11-18 23:06:58 +01:00
|
|
|
assert.parseOk(matchClause, 'MATCH (node:Label { prop: true, value: "test" })', ([actual]) => {
|
2024-11-14 02:21:51 +01:00
|
|
|
const expected = makeNode('node', 'Label', [['prop', true], ['value', 'test']])
|
|
|
|
assert.deepEqual(actual, expected)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should match nodes with a relationship to another another node', () => {
|
2024-11-18 23:06:58 +01:00
|
|
|
assert.parseOk(matchClause, 'MATCH (:Node)-[:Edge]->(:Node)', ([actual]) => {
|
|
|
|
const expected = makeRelationship(
|
2024-11-14 00:32:46 +01:00
|
|
|
makeNode(undefined, 'Node'),
|
2024-11-15 07:36:19 +01:00
|
|
|
makeDirectedEdge(undefined, 'Edge', 1),
|
2024-11-14 00:32:46 +01:00
|
|
|
makeNode(undefined, 'Node'),
|
2024-11-18 23:06:58 +01:00
|
|
|
)
|
2024-11-14 00:32:46 +01:00
|
|
|
|
|
|
|
assert.deepEqual(actual, expected)
|
|
|
|
})
|
|
|
|
|
2024-11-18 23:06:58 +01:00
|
|
|
assert.parseOk(matchClause, 'MATCH (a:Node)-[e:Edge]->(b:Node)', ([actual]) => {
|
|
|
|
const expected = makeRelationship(
|
2024-11-14 00:32:46 +01:00
|
|
|
makeNode('a', 'Node'),
|
2024-11-15 07:36:19 +01:00
|
|
|
makeDirectedEdge('e', 'Edge', 1),
|
2024-11-14 00:32:46 +01:00
|
|
|
makeNode('b', 'Node'),
|
2024-11-18 23:06:58 +01:00
|
|
|
)
|
2024-11-14 00:32:46 +01:00
|
|
|
|
|
|
|
assert.deepEqual(actual, expected)
|
|
|
|
})
|
|
|
|
|
2024-11-18 23:06:58 +01:00
|
|
|
assert.parseOk(matchClause, 'MATCH (a:Node { db: 0.7 })-[e:Edge]->(b:Node { db: 0.95 })', ([actual]) => {
|
|
|
|
const expected = makeRelationship(
|
2024-11-14 00:32:46 +01:00
|
|
|
makeNode('a', 'Node', [['db', 0.7]]),
|
2024-11-15 07:36:19 +01:00
|
|
|
makeDirectedEdge('e', 'Edge', 1),
|
2024-11-14 00:32:46 +01:00
|
|
|
makeNode('b', 'Node', [['db', 0.95]]),
|
2024-11-18 23:06:58 +01:00
|
|
|
)
|
2024-11-14 00:32:46 +01:00
|
|
|
|
|
|
|
assert.deepEqual(actual, expected)
|
|
|
|
})
|
2024-11-14 02:21:51 +01:00
|
|
|
|
2024-11-18 23:06:58 +01:00
|
|
|
assert.parseOk(matchClause, 'MATCH (:Node { db: 0.7 })-[:Edge]->(:Node { db: 0.95 })', ([actual]) => {
|
|
|
|
const expected = makeRelationship(
|
2024-11-14 02:21:51 +01:00
|
|
|
makeNode(undefined, 'Node', [['db', 0.7]]),
|
2024-11-15 07:36:19 +01:00
|
|
|
makeDirectedEdge(undefined, 'Edge', 1),
|
2024-11-14 02:21:51 +01:00
|
|
|
makeNode(undefined, 'Node', [['db', 0.95]]),
|
2024-11-18 23:06:58 +01:00
|
|
|
)
|
2024-11-14 02:21:51 +01:00
|
|
|
|
|
|
|
assert.deepEqual(actual, expected)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should handle multiple relationships', () => {
|
2024-11-18 23:06:58 +01:00
|
|
|
assert.parseOk(matchClause, 'MATCH (player:Player)-[:Knows]->(a:NPC)-[:Knows]->(b:NPC)', ([actual]) => {
|
|
|
|
const expected = makeRelationship(
|
2024-11-14 02:21:51 +01:00
|
|
|
makeNode('player', 'Player'),
|
2024-11-15 07:36:19 +01:00
|
|
|
makeDirectedEdge(undefined, 'Knows', 1),
|
2024-11-18 23:06:58 +01:00
|
|
|
makeRelationship(
|
|
|
|
makeNode('a', 'NPC'),
|
|
|
|
makeDirectedEdge(undefined, 'Knows', 1),
|
|
|
|
makeNode('b', 'NPC'),
|
|
|
|
)
|
|
|
|
)
|
2024-11-14 02:21:51 +01:00
|
|
|
|
|
|
|
assert.deepEqual(actual, expected)
|
|
|
|
})
|
2024-11-13 20:24:29 +01:00
|
|
|
})
|
|
|
|
})
|
2024-11-10 10:15:43 +01:00
|
|
|
|