graph-ecs/tests/query/match.test.js

144 lines
5.3 KiB
JavaScript
Raw Normal View History

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-14 00:32:46 +01:00
import { node, edge, statement } from '../../src/query/match.js'
2024-11-15 07:36:19 +01:00
import { makeDirectedEdge, makeEdge, makeNode } 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', () => {
assert.parseOk(statement, 'MATCH (:Label)', ([actual]) => {
const expected = makeNode(undefined, 'Label')
assert.deepEqual(actual, expected)
})
2024-11-14 00:32:46 +01:00
assert.parseOk(statement, '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-14 02:21:51 +01:00
assert.parseOk(statement, 'MATCH (node:Label { prop: true, value: "test" })', ([actual]) => {
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-14 00:32:46 +01:00
assert.parseOk(statement, 'MATCH (:Node)-[:Edge]->(:Node)', actual => {
const expected = [
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'),
]
assert.deepEqual(actual, expected)
})
assert.parseOk(statement, 'MATCH (a:Node)-[e:Edge]->(b:Node)', actual => {
const expected = [
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'),
]
assert.deepEqual(actual, expected)
})
assert.parseOk(statement, 'MATCH (a:Node { db: 0.7 })-[e:Edge]->(b:Node { db: 0.95 })', actual => {
const expected = [
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]]),
]
assert.deepEqual(actual, expected)
})
2024-11-14 02:21:51 +01:00
assert.parseOk(statement, 'MATCH (:Node { db: 0.7 })-[:Edge]->(:Node { db: 0.95 })', actual => {
const expected = [
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]]),
]
assert.deepEqual(actual, expected)
})
})
it('should handle multiple relationships', () => {
assert.parseOk(statement, 'MATCH (player:Player)-[:Knows]->(a:NPC)-[:Knows]->(b:NPC)', actual => {
const expected = [
makeNode('player', 'Player'),
2024-11-15 07:36:19 +01:00
makeDirectedEdge(undefined, 'Knows', 1),
2024-11-14 02:21:51 +01:00
makeNode('a', 'NPC'),
2024-11-15 07:36:19 +01:00
makeDirectedEdge(undefined, 'Knows', 1),
2024-11-14 02:21:51 +01:00
makeNode('b', 'NPC'),
]
assert.deepEqual(actual, expected)
})
2024-11-13 20:24:29 +01:00
})
})
2024-11-10 10:15:43 +01:00