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

146 lines
5.5 KiB
JavaScript

import util from 'node:util'
import { describe, it } from 'node:test'
import assert from '../assert.js'
import { node, edge, matchClause } from '../../src/query/match.js'
import { makeDirectedEdge, makeEdge, makeNode, makeRelationship } from '../utils.js'
describe('node', () => {
it('should match a node with a name, label, and properties', () => {
assert.parseOk(node, '(:Node)', ([actual]) => {
const expected = makeNode(undefined, 'Node')
assert.deepEqual(actual, expected)
assert.parseOk(node, '(node:Node)', ([actual]) => {
const expected = makeNode('node', 'Node')
assert.deepEqual(actual, expected)
})
assert.parseOk(node, '(:Label { name: "Rowan", gay: true })', ([actual]) => {
const expected = makeNode(undefined, 'Label', [['name', 'Rowan'], ['gay', true]])
assert.deepEqual(actual, expected)
})
assert.parseOk(node, '(name:Label { name: "Rowan", gay: true })', ([actual]) => {
const expected = makeNode('name', 'Label', [['name', 'Rowan'], ['gay', true]])
assert.deepEqual(actual, expected)
})
})
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)
})
})
})
})
describe('edge', () => {
it('should match a relationship with a name, label, properties', () => {
assert.parseOk(edge, '[name:Label]', ([actual]) => {
const expected = makeEdge('name', 'Label')
assert.deepEqual(actual, expected)
})
assert.parseOk(edge, '[:Label]', ([actual]) => {
const expected = makeEdge(undefined, 'Label')
assert.deepEqual(actual, expected)
})
assert.parseOk(edge, '[gay:Queer { binary: "-∞" }]', ([actual]) => {
const expected = makeEdge('gay', 'Queer', [['binary', '-∞']])
assert.deepEqual(actual, expected)
})
assert.parseOk(edge, '[:Queer { binary: "-∞" }]', ([actual]) => {
const expected = makeEdge(undefined, 'Queer', [['binary', '-∞']])
assert.deepEqual(actual, expected)
})
})
it('should handle whitespace', () => {
assert.parseOk(edge, '[ : Queer { binary : "-∞" } ]', ([actual]) => {
const expected = makeEdge(undefined, 'Queer', [['binary', '-∞']])
assert.deepEqual(actual, expected)
})
})
})
describe('MATCH keyword', () => {
it('should match a single node with no relationships', () => {
assert.parseOk(matchClause, 'MATCH (:Label)', ([actual]) => {
const expected = makeNode(undefined, 'Label')
assert.deepEqual(actual, expected)
})
assert.parseOk(matchClause, 'MATCH (node:Label)', ([actual]) => {
const expected = makeNode('node', 'Label')
assert.deepEqual(actual, expected)
})
assert.parseOk(matchClause, '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', () => {
assert.parseOk(matchClause, 'MATCH (:Node)-[:Edge]->(:Node)', ([actual]) => {
const expected = makeRelationship(
makeNode(undefined, 'Node'),
makeDirectedEdge(undefined, 'Edge', 1),
makeNode(undefined, 'Node'),
)
assert.deepEqual(actual, expected)
})
assert.parseOk(matchClause, 'MATCH (a:Node)-[e:Edge]->(b:Node)', ([actual]) => {
const expected = makeRelationship(
makeNode('a', 'Node'),
makeDirectedEdge('e', 'Edge', 1),
makeNode('b', 'Node'),
)
assert.deepEqual(actual, expected)
})
assert.parseOk(matchClause, 'MATCH (a:Node { db: 0.7 })-[e:Edge]->(b:Node { db: 0.95 })', ([actual]) => {
const expected = makeRelationship(
makeNode('a', 'Node', [['db', 0.7]]),
makeDirectedEdge('e', 'Edge', 1),
makeNode('b', 'Node', [['db', 0.95]]),
)
assert.deepEqual(actual, expected)
})
assert.parseOk(matchClause, 'MATCH (:Node { db: 0.7 })-[:Edge]->(:Node { db: 0.95 })', ([actual]) => {
const expected = makeRelationship(
makeNode(undefined, 'Node', [['db', 0.7]]),
makeDirectedEdge(undefined, 'Edge', 1),
makeNode(undefined, 'Node', [['db', 0.95]]),
)
assert.deepEqual(actual, expected)
})
})
it('should handle multiple relationships', () => {
assert.parseOk(matchClause, 'MATCH (player:Player)-[:Knows]->(a:NPC)-[:Knows]->(b:NPC)', ([actual]) => {
const expected = makeRelationship(
makeNode('player', 'Player'),
makeDirectedEdge(undefined, 'Knows', 1),
makeRelationship(
makeNode('a', 'NPC'),
makeDirectedEdge(undefined, 'Knows', 1),
makeNode('b', 'NPC'),
)
)
assert.deepEqual(actual, expected)
})
})
})