fix tests

This commit is contained in:
Rowan 2024-11-13 17:32:46 -06:00
parent 3aaa48aa59
commit 5411742372
5 changed files with 67 additions and 51 deletions

View file

@ -1,11 +1,10 @@
import assert from 'node:assert'
import { curry } from '../src/fn.js'
import { parse } from 'node:path'
import { parse } from '../src/parser.js'
const isOk = curry((fn, result) => {
assert.doesNotThrow(() => {
const [[actual]] = result.unwrap()
fn(actual)
fn(result.unwrap()[0])
})
})

View file

@ -1,89 +1,107 @@
import { describe, it } from 'node:test'
import assert from '../assert.js'
import { node, edge, properties, statement } from '../../src/query/match.js'
import { node, edge, statement } from '../../src/query/match.js'
import { makeEdge, makeNode } from '../utils.js'
describe('node', () => {
it('should match a label with an empty name', () => {
assert.parseOk(node, '(:Node)', actual => {
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)
})
})
it('should match a name and label', () => {
assert.parseOk(node, '(node:Node)', actual => {
assert.parseOk(node, '(node:Node)', ([actual]) => {
const expected = makeNode('node', 'Node')
assert.deepEqual(actual, expected)
})
})
it('should match a node with properties and no name', () => {
assert.parseOk(node, '(:Label { name: "Rowan", gay: true })', actual => {
assert.parseOk(node, '(:Label { name: "Rowan", gay: true })', ([actual]) => {
const expected = makeNode(undefined, 'Label', [['name', 'Rowan'], ['gay', true]])
assert.deepEqual(actual, expected)
})
})
it('should match a node with properties and a name', () => {
assert.parseOk(node, '(name:Label { name: "Rowan", gay: true })', actual => {
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 => {
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 and label', () => {
assert.parseOk(edge, '[name:Label]', actual => {
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)
})
})
it('should match a relationship with no name', () => {
assert.parseOk(edge, '[:Label]', actual => {
assert.parseOk(edge, '[:Label]', ([actual]) => {
const expected = makeEdge(undefined, 'Label', [])
assert.deepEqual(actual, expected)
})
})
it('should match a relationship with a name and properties', () => {
assert.parseOk(edge, '[gay:Queer { binary: "-∞" }]', actual => {
assert.parseOk(edge, '[gay:Queer { binary: "-∞" }]', ([actual]) => {
const expected = makeEdge('gay', 'Queer', [['binary', '-∞']])
assert.deepEqual(actual, expected)
})
})
it('should match a relationship with properties and no name', () => {
assert.parseOk(edge, '[:Queer { binary: "-∞" }]', actual => {
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 => {
assert.parseOk(edge, '[ : Queer { binary : "-∞" } ]', ([actual]) => {
const expected = makeEdge(undefined, 'Queer', [['binary', '-∞']])
assert.deepEqual(actual, expected)
})
})
})
describe('match', () => {
it('should match nodes', () => {
assert.parseOk(statement, '(node:Label)', actual => {
describe('MATCH keyword', () => {
it('should match nodes with a relationship to another another node', () => {
assert.parseOk(statement, 'MATCH (node:Label)', ([actual]) => {
const expected = makeNode('node', 'Label')
assert.deepEqual(actual, expected)
})
assert.parseOk(statement, 'MATCH (:Node)-[:Edge]->(:Node)', actual => {
const expected = [
makeNode(undefined, 'Node'),
makeEdge(undefined, 'Edge'),
makeNode(undefined, 'Node'),
]
assert.deepEqual(actual, expected)
})
assert.parseOk(statement, 'MATCH (a:Node)-[e:Edge]->(b:Node)', actual => {
const expected = [
makeNode('a', 'Node'),
makeEdge('e', 'Edge'),
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]]),
makeEdge('e', 'Edge'),
makeNode('b', 'Node', [['db', 0.95]]),
]
assert.deepEqual(actual, expected)
})
})
})

View file

@ -3,9 +3,9 @@ import assert from '../assert.js'
import { statement } from '../../src/query/return.js'
import { Alias, Identifier, Literal } from '../../src/query/types.js'
describe('return parser', () => {
describe('RETURN keyword', () => {
it('should collect a single value for a query to return', () => {
assert.parseOk(statement, 'RETURN folklore AS f', actual => {
assert.parseOk(statement, 'RETURN folklore AS f', ([actual]) => {
assert.deepEqual(actual, new Alias(new Identifier('folklore'), new Identifier('f')))
})
})

View file

@ -1,12 +1,11 @@
import { describe, it } from 'node:test'
import assert from '../assert.js'
import { parse } from '../../src/parser.js'
import { statement } from '../../src/query/use.js'
import { Identifier } from '../../src/query/types.js'
describe('use parser', () => {
describe('USE keyword', () => {
it('should select a graph to query', () => {
assert.parseOk(statement, 'USE default', actual => {
assert.parseOk(statement, 'USE default', ([actual]) => {
assert.deepEqual(actual.identifier, new Identifier('default'))
})
})

View file

@ -1,11 +1,11 @@
import { KeyValuePair, Name, Label, Identifier, Literal, Edge } from '../src/query/types.js'
import { KeyValuePair, Name, Label, Identifier, Literal, Edge, Node } from '../src/query/types.js'
export const keyValuePair = ([k, v]) => new KeyValuePair(new Name(new Identifier(k)), new Literal(v))
export const graphObject = (name, label, props = [], Type = Node) => new Type(
name && new Name(new Identifier(name)),
new Label(new Identifier(label)),
props.map(makeKvp)
props.map(keyValuePair)
)
export const makeNode = graphObject