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

74 lines
2.8 KiB
JavaScript
Raw Normal View History

2024-11-18 23:06:58 +01:00
import { describe, it } from 'node:test'
import assert from '../assert.js'
2024-11-20 20:21:41 +01:00
import { query } from '../../src/query-parser/index.js'
import { Alias, Identifier, ObjectPath, Query, ReturnValues, SelectedGraph } from '../../src/query-parser/types.js'
2024-11-19 02:10:29 +01:00
import { makeNode, makeRelationship, makeRightEdge } from '../utils.js'
2024-11-18 23:06:58 +01:00
2024-11-19 02:10:29 +01:00
const path = (...v) => new ObjectPath(...v)
const alias = (...v) => new Alias(...v)
const identifier = n => new Identifier(n)
const graph = n => new SelectedGraph(identifier(n))
const returnVals = (...v) => new ReturnValues(...v)
2024-11-18 23:06:58 +01:00
const q = (u, m, rv) => new Query(u, m, rv)
describe('query', () => {
2024-11-19 02:10:29 +01:00
it('should match a node', () => {
assert.parseOk(query, 'MATCH (node:Label) RETURN node', ([actual]) => {
const expected = q(
undefined, // no use clause
makeNode('node', 'Label'),
returnVals(identifier('node'))
)
assert.deepEqual(actual, expected)
})
})
2024-11-18 23:06:58 +01:00
it('should match a relationship', () => {
2024-11-19 02:10:29 +01:00
assert.parseOk(query, 'MATCH (rown:Creature)-[:Petpats]->(kbity:NetCat) RETURN rown, kbity', ([actual]) => {
const expected = q(
undefined, // no use clause
makeRelationship(
makeNode('rown', 'Creature'),
makeRightEdge(undefined, 'Petpats'),
makeNode('kbity', 'NetCat'),
),
returnVals(identifier('rown'), identifier('kbity'))
)
assert.deepEqual(actual, expected)
})
})
it('should support the USE clause', () => {
assert.parseOk(query, 'USE aminals MATCH (kbity:Cat) RETURN kbity', ([actual]) => {
const expected = q(
graph('aminals'),
makeNode('kbity', 'Cat'),
returnVals(identifier('kbity'))
)
assert.deepEqual(actual, expected)
})
})
it('should support complex queries', () => {
assert.parseOk(query, 'USE aminals MATCH (kbity:Cat { type: "cute" })-[:Eats { schedule: "daily" }]->(snacc:Feesh { type: "vegan", weight: 0.7 }) RETURN kbity.name AS name, snacc AS food', ([actual]) => {
const expected = q(
graph('aminals'),
makeRelationship(
makeNode('kbity', 'Cat', [['type', 'cute']]),
makeRightEdge(undefined, 'Eats', [['schedule', 'daily']]),
makeNode('snacc', 'Feesh', [['type', 'vegan'], ['weight', 0.7]])
),
returnVals(
alias(path(identifier('kbity'), identifier('name')), identifier('name')),
alias(identifier('snacc'), identifier('food'))
)
)
assert.deepEqual(actual, expected)
2024-11-18 23:06:58 +01:00
})
})
})