|
| 1 | +import type { Record } from 'neo4j-driver'; |
| 2 | +import { Node, Path, PathSegment, Relationship } from 'neo4j-driver'; |
| 3 | + |
| 4 | +import { describe, expect, test } from 'vitest'; |
| 5 | +import { extractUniqueNodesAndRels } from './extract-unique-nodes-and-relationships'; |
| 6 | + |
| 7 | +describe('extractNodesAndRels', () => { |
| 8 | + test('should map bolt records with a path to nodes and relationships', () => { |
| 9 | + const startNode = new Node( |
| 10 | + 1, |
| 11 | + ['Person'], |
| 12 | + { |
| 13 | + prop1: 'prop1', |
| 14 | + }, |
| 15 | + 'node1', |
| 16 | + ); |
| 17 | + const endNode = new Node( |
| 18 | + 2, |
| 19 | + ['Movie'], |
| 20 | + { |
| 21 | + prop2: 'prop2', |
| 22 | + }, |
| 23 | + 'node2', |
| 24 | + ); |
| 25 | + |
| 26 | + const relationship = new Relationship( |
| 27 | + 3, |
| 28 | + 1, |
| 29 | + 2, |
| 30 | + 'ACTED_IN', |
| 31 | + {}, |
| 32 | + 'rel1', |
| 33 | + 'node1', |
| 34 | + 'node2', |
| 35 | + ); |
| 36 | + const pathSegment = new PathSegment(startNode, relationship, endNode); |
| 37 | + const path = new Path(startNode, endNode, [pathSegment]); |
| 38 | + |
| 39 | + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions |
| 40 | + const boltRecord = { |
| 41 | + keys: ['p'], |
| 42 | + get: () => path, |
| 43 | + } as unknown as Record; |
| 44 | + |
| 45 | + const { nodes, relationships } = extractUniqueNodesAndRels([boltRecord]); |
| 46 | + |
| 47 | + const [graphNodeStart] = nodes.filter( |
| 48 | + (node) => node.elementId.toString() === 'node1', |
| 49 | + ); |
| 50 | + const [graphNodeEnd] = nodes.filter( |
| 51 | + (node) => node.elementId.toString() === 'node2', |
| 52 | + ); |
| 53 | + const [firstRel] = relationships; |
| 54 | + |
| 55 | + if ( |
| 56 | + graphNodeStart === undefined || |
| 57 | + graphNodeEnd === undefined || |
| 58 | + firstRel === undefined |
| 59 | + ) { |
| 60 | + throw new Error('Error in test data, got undefined'); |
| 61 | + } |
| 62 | + |
| 63 | + expect(nodes.length).toBe(2); |
| 64 | + |
| 65 | + expect(graphNodeStart.labels).toEqual(['Person']); |
| 66 | + expect(graphNodeStart.properties).toEqual({ prop1: 'prop1' }); |
| 67 | + |
| 68 | + expect(graphNodeEnd.labels).toEqual(['Movie']); |
| 69 | + expect(graphNodeEnd.properties).toEqual({ prop2: 'prop2' }); |
| 70 | + expect(relationships.length).toBe(1); |
| 71 | + |
| 72 | + expect(firstRel.elementId.toString()).toEqual('rel1'); |
| 73 | + expect(firstRel.startNodeElementId.toString()).toEqual('node1'); |
| 74 | + expect(firstRel.endNodeElementId.toString()).toEqual('node2'); |
| 75 | + expect(firstRel.type).toEqual('ACTED_IN'); |
| 76 | + expect(firstRel.properties).toEqual({}); |
| 77 | + }); |
| 78 | + |
| 79 | + test('should deduplicate bolt records based on node id and filter out dangling relationships', () => { |
| 80 | + const node1 = new Node( |
| 81 | + 1, |
| 82 | + ['Person'], |
| 83 | + { |
| 84 | + prop1: 'prop1', |
| 85 | + }, |
| 86 | + 'node1', |
| 87 | + ); |
| 88 | + const node2 = new Node( |
| 89 | + 1, |
| 90 | + ['Person'], |
| 91 | + { |
| 92 | + prop1: 'prop1', |
| 93 | + }, |
| 94 | + 'node1', |
| 95 | + ); |
| 96 | + const relationship = new Relationship( |
| 97 | + 2, |
| 98 | + 1, |
| 99 | + 34, |
| 100 | + 'ACTED_IN', |
| 101 | + {}, |
| 102 | + 'rel1', |
| 103 | + 'node1', |
| 104 | + 'node34', |
| 105 | + ); |
| 106 | + |
| 107 | + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions |
| 108 | + const boltRecord = { |
| 109 | + keys: ['n'], |
| 110 | + get: () => [node1, node2, relationship], |
| 111 | + } as unknown as Record; |
| 112 | + |
| 113 | + const { nodes, relationships, limitHit } = extractUniqueNodesAndRels([ |
| 114 | + boltRecord, |
| 115 | + ]); |
| 116 | + expect(limitHit).toBe(false); |
| 117 | + expect(nodes.length).toBe(1); |
| 118 | + expect(relationships.length).toBe(0); |
| 119 | + }); |
| 120 | + |
| 121 | + test('should respect the max nodes limit and filter out dangling relations', () => { |
| 122 | + const startNode = new Node( |
| 123 | + 1, |
| 124 | + ['Person'], |
| 125 | + { |
| 126 | + prop1: 'prop1', |
| 127 | + }, |
| 128 | + 'node1', |
| 129 | + ); |
| 130 | + const endNode = new Node( |
| 131 | + 2, |
| 132 | + ['Movie'], |
| 133 | + { |
| 134 | + prop2: 'prop2', |
| 135 | + }, |
| 136 | + 'node2', |
| 137 | + ); |
| 138 | + const relationship = new Relationship( |
| 139 | + 3, |
| 140 | + 1, |
| 141 | + 2, |
| 142 | + 'ACTED_IN', |
| 143 | + {}, |
| 144 | + 'rel1', |
| 145 | + 'node1', |
| 146 | + 'node2', |
| 147 | + ); |
| 148 | + const pathSegment = new PathSegment(startNode, relationship, endNode); |
| 149 | + const path = new Path(startNode, endNode, [pathSegment]); |
| 150 | + |
| 151 | + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions |
| 152 | + const boltRecord = { |
| 153 | + keys: ['p'], |
| 154 | + get: () => path, |
| 155 | + } as unknown as Record; |
| 156 | + |
| 157 | + const { nodes, relationships, limitHit } = extractUniqueNodesAndRels( |
| 158 | + [boltRecord], |
| 159 | + { nodeLimit: 1 }, |
| 160 | + ); |
| 161 | + expect(limitHit).toBe(true); |
| 162 | + expect(nodes.length).toBe(1); |
| 163 | + const [graphNodeStart] = nodes; |
| 164 | + expect(graphNodeStart).toBeDefined(); |
| 165 | + if (graphNodeStart === undefined) { |
| 166 | + throw new Error('Error in test data, got undefined'); |
| 167 | + } |
| 168 | + expect(graphNodeStart.labels).toEqual(['Person']); |
| 169 | + expect(graphNodeStart.properties).toEqual({ prop1: 'prop1' }); |
| 170 | + expect(relationships.length).toBe(0); |
| 171 | + }); |
| 172 | + |
| 173 | + test('should respect the max nodes limit and not filter out dangling relations when asked to keep them', () => { |
| 174 | + const startNode = new Node( |
| 175 | + 1, |
| 176 | + ['Person'], |
| 177 | + { |
| 178 | + prop1: 'prop1', |
| 179 | + }, |
| 180 | + 'node1', |
| 181 | + ); |
| 182 | + const endNode = new Node( |
| 183 | + 2, |
| 184 | + ['Movie'], |
| 185 | + { |
| 186 | + prop2: 'prop2', |
| 187 | + }, |
| 188 | + 'node2', |
| 189 | + ); |
| 190 | + const relationship = new Relationship( |
| 191 | + 3, |
| 192 | + 1, |
| 193 | + 2, |
| 194 | + 'ACTED_IN', |
| 195 | + {}, |
| 196 | + 'rel1', |
| 197 | + 'node1', |
| 198 | + 'node2', |
| 199 | + ); |
| 200 | + const pathSegment = new PathSegment(startNode, relationship, endNode); |
| 201 | + const path = new Path(startNode, endNode, [pathSegment]); |
| 202 | + |
| 203 | + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions |
| 204 | + const boltRecord = { |
| 205 | + keys: ['p'], |
| 206 | + get: () => path, |
| 207 | + } as unknown as Record; |
| 208 | + |
| 209 | + const { nodes, relationships, limitHit } = extractUniqueNodesAndRels( |
| 210 | + [boltRecord], |
| 211 | + { |
| 212 | + nodeLimit: 1, |
| 213 | + keepDanglingRels: true, |
| 214 | + }, |
| 215 | + ); |
| 216 | + expect(limitHit).toBe(true); |
| 217 | + expect(nodes.length).toBe(1); |
| 218 | + const [graphNodeStart] = nodes; |
| 219 | + expect(graphNodeStart).toBeDefined(); |
| 220 | + if (graphNodeStart === undefined) { |
| 221 | + throw new Error('Error in test data, got undefined'); |
| 222 | + } |
| 223 | + |
| 224 | + expect(graphNodeStart.labels).toEqual(['Person']); |
| 225 | + expect(graphNodeStart.properties).toEqual({ prop1: 'prop1' }); |
| 226 | + expect(relationships.length).toBe(1); |
| 227 | + }); |
| 228 | + |
| 229 | + test('should handle empty results', () => { |
| 230 | + const { nodes, relationships, limitHit } = extractUniqueNodesAndRels([]); |
| 231 | + expect(limitHit).toBe(false); |
| 232 | + expect(nodes.length).toBe(0); |
| 233 | + expect(relationships.length).toBe(0); |
| 234 | + }); |
| 235 | +}); |
0 commit comments