@@ -28,7 +28,10 @@ import {
2828 resultHasPlan ,
2929 resultIsError ,
3030 getRecordsToDisplayInTable ,
31- initialView
31+ initialView ,
32+ extractRecordsToResultArray ,
33+ flattenGraphItemsInResultArray ,
34+ stringifyResultArray
3235} from './helpers'
3336
3437describe ( 'helpers' , ( ) => {
@@ -474,4 +477,129 @@ describe('helpers', () => {
474477 expect ( view ) . toEqual ( viewTypes . TABLE )
475478 } )
476479 } )
480+ describe ( 'record transformations' , ( ) => {
481+ test ( 'extractRecordsToResultArray handles empty records' , ( ) => {
482+ // Given
483+ const records = null
484+
485+ // When
486+ const res = extractRecordsToResultArray ( records )
487+
488+ // Then
489+ expect ( res ) . toEqual ( [ ] )
490+ } )
491+ test ( 'extractRecordsToResultArray handles regular records' , ( ) => {
492+ // Given
493+ const start = new neo4j . types . Node ( 1 , [ 'X' ] , { x : 1 } )
494+ const end = new neo4j . types . Node ( 2 , [ 'Y' ] , { y : new neo4j . Int ( 1 ) } )
495+ const rel = new neo4j . types . Relationship ( 3 , 1 , 2 , 'REL' , { rel : 1 } )
496+ const segments = [ new neo4j . types . PathSegment ( start , rel , end ) ]
497+ const path = new neo4j . types . Path ( start , end , segments )
498+
499+ const records = [
500+ {
501+ keys : [ '"x"' , '"y"' , '"n"' ] ,
502+ _fields : [ 'x' , 'y' , new neo4j . types . Node ( '1' , [ 'Person' ] , { prop1 : 'prop1' } ) ]
503+ } ,
504+ {
505+ keys : [ '"x"' , '"y"' , '"n"' ] ,
506+ _fields : [ 'xx' , 'yy' , path ]
507+ }
508+ ]
509+
510+ // When
511+ const res = extractRecordsToResultArray ( records )
512+
513+ // Then
514+ expect ( res ) . toEqual ( [
515+ [ '"x"' , '"y"' , '"n"' ] ,
516+ [ 'x' , 'y' , new neo4j . types . Node ( '1' , [ 'Person' ] , { prop1 : 'prop1' } ) ] ,
517+ [ 'xx' , 'yy' , path ]
518+ ] )
519+ } )
520+ test ( 'flattenGraphItemsInResultArray extracts props from graph items' , ( ) => {
521+ // Given
522+ const start = new neo4j . types . Node ( 1 , [ 'X' ] , { x : 1 } )
523+ const end = new neo4j . types . Node ( 2 , [ 'Y' ] , { y : 1 } )
524+ const rel = new neo4j . types . Relationship ( 3 , 1 , 2 , 'REL' , { rel : 1 } )
525+ const segments = [ new neo4j . types . PathSegment ( start , rel , end ) ]
526+ const path = new neo4j . types . Path ( start , end , segments )
527+
528+ const records = [
529+ {
530+ keys : [ '"x"' , '"y"' , '"n"' ] ,
531+ _fields : [ 'x' , 'y' , new neo4j . types . Node ( '1' , [ 'Person' ] , { prop1 : 'prop1' } ) ]
532+ } ,
533+ {
534+ keys : [ '"x"' , '"y"' , '"n"' ] ,
535+ _fields : [ 'xx' , 'yy' , { prop : path } ]
536+ }
537+ ]
538+
539+ // When
540+ const step1 = extractRecordsToResultArray ( records )
541+ const res = flattenGraphItemsInResultArray ( neo4j . types , neo4j . isInt , step1 )
542+
543+ // Then
544+ expect ( res ) . toEqual ( [
545+ [ '"x"' , '"y"' , '"n"' ] ,
546+ [ 'x' , 'y' , { prop1 : 'prop1' } ] ,
547+ [ 'xx' , 'yy' , { prop : [ { x : 1 } , { rel : 1 } , { y : 1 } ] } ]
548+ ] )
549+ } )
550+ test ( 'stringifyResultArray uses stringifyMod to serialize' , ( ) => {
551+ // Given
552+ const records = [
553+ {
554+ keys : [ '"neoInt"' , '"int"' , '"any"' ] ,
555+ _fields : [ new neo4j . Int ( '882573709873217509' ) , 100 , 0.5 ]
556+ } ,
557+ {
558+ keys : [ '"neoInt"' , '"int"' , '"any"' ] ,
559+ _fields : [ new neo4j . Int ( 300 ) , 100 , 'string' ]
560+ }
561+ ]
562+
563+ // When
564+ const step1 = extractRecordsToResultArray ( records )
565+ const step2 = flattenGraphItemsInResultArray ( neo4j . types , neo4j . isInt , step1 )
566+ const res = stringifyResultArray ( neo4j . isInt , step2 )
567+ // Then
568+ expect ( res ) . toEqual ( [
569+ [ JSON . stringify ( '"neoInt"' ) , JSON . stringify ( '"int"' ) , JSON . stringify ( '"any"' ) ] ,
570+ [ '882573709873217509' , '100' , '0.5' ] ,
571+ [ '300' , '100' , '"string"' ]
572+ ] )
573+ } )
574+ test ( 'stringifyResultArray handles neo4j integers nested within graph items' , ( ) => {
575+ // Given
576+ const start = new neo4j . types . Node ( 1 , [ 'X' ] , { x : 1 } )
577+ const end = new neo4j . types . Node ( 2 , [ 'Y' ] , { y : new neo4j . Int ( 2 ) } ) // <-- Neo4j integer
578+ const rel = new neo4j . types . Relationship ( 3 , 1 , 2 , 'REL' , { rel : 1 } )
579+ const segments = [ new neo4j . types . PathSegment ( start , rel , end ) ]
580+ const path = new neo4j . types . Path ( start , end , segments )
581+
582+ const records = [
583+ {
584+ keys : [ '"x"' , '"y"' , '"n"' ] ,
585+ _fields : [ 'x' , 'y' , new neo4j . types . Node ( '1' , [ 'Person' ] , { prop1 : 'prop1' } ) ]
586+ } ,
587+ {
588+ keys : [ '"x"' , '"y"' , '"n"' ] ,
589+ _fields : [ 'xx' , 'yy' , { prop : path } ]
590+ }
591+ ]
592+
593+ // When
594+ const step1 = extractRecordsToResultArray ( records )
595+ const step2 = flattenGraphItemsInResultArray ( neo4j . types , neo4j . isInt , step1 )
596+ const res = stringifyResultArray ( neo4j . isInt , step2 )
597+ // Then
598+ expect ( res ) . toEqual ( [
599+ [ JSON . stringify ( '"x"' ) , JSON . stringify ( '"y"' ) , JSON . stringify ( '"n"' ) ] ,
600+ [ '"x"' , '"y"' , JSON . stringify ( { prop1 : 'prop1' } ) ] ,
601+ [ '"xx"' , '"yy"' , JSON . stringify ( { prop : [ { x : 1 } , { rel : 1 } , { y : 2 } ] } ) ] // <--
602+ ] )
603+ } )
604+ } )
477605} )
0 commit comments