11import { describe , expect , mock , test } from "bun:test"
22import { createRequire } from "node:module"
3- import { extractNestedPaths , normalizeMqlCommand } from "../src/mongodb"
3+ import { normalizeMqlCommand } from "../src/mongodb"
44import { restoreModuleMocksAfterTests } from "./module-mock"
55
66await restoreModuleMocksAfterTests ( "mongodb" )
@@ -13,6 +13,7 @@ async function fakeMongo(
1313 metadata : { collections ?: Record < string , string [ ] > ; error ?: Error } = { } ,
1414) {
1515 const finds : unknown [ ] = [ ]
16+ const findOptions : unknown [ ] = [ ]
1617 const pipelines : Record < string , unknown > [ ] [ ] = [ ]
1718 const aggregateOptions : unknown [ ] = [ ]
1819 const counts : { filter : unknown ; options : unknown } [ ] = [ ]
@@ -50,8 +51,9 @@ async function fakeMongo(
5051 collections [ database ] . push ( name )
5152 } ,
5253 collection : ( ) => ( {
53- find : ( filter : unknown ) => {
54+ find : ( filter : unknown , options : unknown ) => {
5455 finds . push ( filter )
56+ findOptions . push ( options )
5557 return { limit : ( ) => ( { toArray : async ( ) => docs , async * [ Symbol . asyncIterator ] ( ) { yield * docs } } ) }
5658 } ,
5759 aggregate : ( pipeline : Record < string , unknown > [ ] , options : unknown ) => {
@@ -81,7 +83,7 @@ async function fakeMongo(
8183 const connector = await connect ( { type : "mongodb" , database : "test" } )
8284 await connector . connect ( )
8385 const query = ( command : string , extra : object = { } ) => JSON . stringify ( { collection : "articles" , command, ...extra } )
84- return { connector, query, collections, finds, pipelines, aggregateOptions, counts, distincts, collectionLists, databaseLists, estimates }
86+ return { connector, query, collections, finds, findOptions , pipelines, aggregateOptions, counts, distincts, collectionLists, databaseLists, estimates }
8587}
8688
8789describe ( "MongoDB read command shapes" , ( ) => {
@@ -461,46 +463,30 @@ describe("MongoDB native command filters", () => {
461463 } )
462464} )
463465
464- describe ( "MongoDB nested field paths " , ( ) => {
466+ describe ( "MongoDB nested documents " , ( ) => {
465467 const docs = [
466- { _id : 1 , user : { name : "a" , geo : { lat : 1.5 , deep : { too : "far" } } } , items : [ { sku : "x" , qty : 1 } , { sku : "y" } ] , tags : [ "t" ] } ,
467- { _id : 2 , user : { name : "b" } , items : [ { sku : "z" , qty : null } ] , tags : [ ] } ,
468- { _id : 3 , user : null , items : [ ] } ,
468+ { _id : 1 , user : { name : "a" , geo : { lat : 1.5 } } , items : [ { sku : "x" } ] , "a.b" : 1 } ,
469+ { _id : 2 , user : null , items : [ ] } ,
469470 ]
470471
471- test ( "extracts dotted and array paths to depth 3 with types and per-document presence" , ( ) => {
472- const paths = extractNestedPaths ( docs )
473- expect ( [ ...paths . keys ( ) ] . sort ( ) ) . toEqual ( [
474- "items[].qty" ,
475- "items[].sku" ,
476- "user.geo" ,
477- "user.geo.deep" ,
478- "user.geo.lat" ,
479- "user.name" ,
472+ test ( "describeTable stays top-level: nested objects are 'object', arrays 'array', no nested paths" , async ( ) => {
473+ const { connector } = await fakeMongo ( docs )
474+ expect ( await connector . describeTable ( "test" , "articles" ) ) . toEqual ( [
475+ { name : "_id" , data_type : "int32" , nullable : false } ,
476+ { name : "a.b" , data_type : "int32" , nullable : true } ,
477+ { name : "items" , data_type : "array" , nullable : false } ,
478+ { name : "user" , data_type : "object" , nullable : true } ,
480479 ] )
481- expect ( paths . get ( "user.geo.deep" ) ! . types ) . toEqual ( new Set ( [ "object" ] ) )
482- // sku appears twice in document 1 but the document is counted once
483- expect ( paths . get ( "items[].sku" ) ! . docs ) . toBe ( 2 )
484- expect ( [ ...paths . get ( "items[].qty" ) ! . types ] . sort ( ) ) . toEqual ( [ "int32" , "null" ] )
485- expect ( paths . get ( "user.name" ) ! . docs ) . toBe ( 2 )
486480 } )
487481
488- test ( "describeTable keeps top-level columns and adds nested paths with a presence share" , async ( ) => {
489- const { connector } = await fakeMongo ( docs )
490- const columns = await connector . describeTable ( "test" , "articles" )
491- const byName = Object . fromEntries ( columns . map ( ( c ) => [ c . name , c ] ) )
492- expect ( byName . user ) . toEqual ( { name : "user" , data_type : "object" , nullable : true } )
493- expect ( byName . items ) . toEqual ( { name : "items" , data_type : "array" , nullable : false } )
494- expect ( byName [ "user.name" ] ) . toEqual ( { name : "user.name" , data_type : "string" , nullable : true , presence : 2 / 3 } )
495- expect ( byName [ "items[].sku" ] ) . toMatchObject ( { data_type : "string" , presence : 2 / 3 } )
496- expect ( byName [ "items[].qty" ] ) . toMatchObject ( { data_type : "int32" , nullable : true } )
497- expect ( columns [ 0 ] ! . name ) . toBe ( "_id" )
498- const names = columns . map ( ( c ) => c . name )
499- expect ( names . slice ( names . indexOf ( "user" ) ) ) . toEqual ( [ "user" , "user.geo" , "user.geo.deep" , "user.geo.lat" , "user.name" ] )
500- } )
501-
502- test ( "sampleDocuments returns the sampled documents" , async ( ) => {
482+ test ( "sampleDocuments returns raw documents under the smaller of the caller and connection deadlines" , async ( ) => {
483+ const { connector, findOptions } = await fakeMongo ( docs )
484+ expect ( await connector . sampleDocuments ! ( "test" , "articles" , 50 , { maxTimeMS : 15_000 } ) ) . toEqual ( docs )
485+ expect ( findOptions ) . toEqual ( [ { maxTimeMS : 15_000 } ] )
486+ } )
487+
488+ test ( "sampleDocuments honours an aborted signal" , async ( ) => {
503489 const { connector } = await fakeMongo ( docs )
504- expect ( await connector . sampleDocuments ! ( "test" , "articles" , 50 ) ) . toEqual ( docs )
490+ await expect ( connector . sampleDocuments ! ( "test" , "articles" , 50 , { signal : AbortSignal . abort ( new Error ( "stop" ) ) } ) ) . rejects . toThrow ( "stop" )
505491 } )
506492} )
0 commit comments