@@ -28,8 +28,14 @@ import { MongoDBDriver } from './mongodb-driver.js';
2828
2929type Doc = Record < string , unknown > ;
3030
31- /** A fake `Db`: one collection, stored documents kept as inserted. */
32- function fakeDb ( ) {
31+ /**
32+ * A fake `Db`: one store of documents kept as inserted, served through a
33+ * collection that declares ONLY the native `Collection` members the path under
34+ * test calls. Two shapes, one per path: the single-row doors (`create`,
35+ * `update`, `findOne`) reach `insertOne` / `updateOne` / `findOne`; the batch
36+ * door (`bulkCreate`) reaches `insertMany`, and its next read is a `find`.
37+ */
38+ function fakeStore ( ) {
3339 const docs : Doc [ ] = [ ] ;
3440 const project = ( doc : Doc , projection : Record < string , number > | undefined ) : Doc => {
3541 const keep = Object . keys ( projection ?? { } ) . filter ( ( k ) => projection ! [ k ] === 1 ) ;
@@ -54,25 +60,43 @@ function fakeDb() {
5460 }
5561 return doc [ k ] === v ;
5662 } ) ;
63+ const push = ( doc : Doc ) => { docs . push ( { _id : `oid_${ docs . length } ` , ...doc } ) ; } ;
64+ const pick = ( filter : Doc ) => docs . find ( ( x ) => matches ( x , filter ) ) ;
65+ const pickAll = ( filter : Doc , options ?: { projection ?: Record < string , number > } ) =>
66+ docs . filter ( ( x ) => matches ( x , filter ) ) . map ( ( d ) => project ( d , options ?. projection ) ) ;
67+ return { docs, push, pick, pickAll, project } ;
68+ }
69+
70+ /** The single-row doors: `create`, `update` and `findOne`. */
71+ function singleRowDb ( ) {
72+ const store = fakeStore ( ) ;
5773 const collection = {
58- insertOne : async ( doc : Doc ) => { docs . push ( { _id : `oid_${ docs . length } ` , ...doc } ) ; return { } ; } ,
59- insertMany : async ( many : Doc [ ] ) => { for ( const doc of many ) docs . push ( { _id : `oid_${ docs . length } ` , ...doc } ) ; return { } ; } ,
74+ insertOne : async ( doc : Doc ) => { store . push ( doc ) ; return { } ; } ,
6075 updateOne : async ( filter : Doc , update : { $set ?: Doc } ) => {
6176 const updateOps = Object . keys ( update ) ;
6277 if ( updateOps . length !== 1 || updateOps [ 0 ] !== '$set' ) throw new Error ( `fake Db: update ${ updateOps . join ( ',' ) } is not implemented` ) ;
63- const d = docs . find ( ( x ) => matches ( x , filter ) ) ;
78+ const d = store . pick ( filter ) ;
6479 if ( d ) Object . assign ( d , update . $set ) ;
6580 return { matchedCount : d ? 1 : 0 } ;
6681 } ,
6782 findOne : async ( filter : Doc , options : { projection ?: Record < string , number > } ) => {
68- const d = docs . find ( ( x ) => matches ( x , filter ) ) ;
69- return d ? project ( d , options ?. projection ) : null ;
83+ const d = store . pick ( filter ) ;
84+ return d ? store . project ( d , options ?. projection ) : null ;
7085 } ,
86+ } ;
87+ return { db : { collection : ( ) => collection } , docs : store . docs } ;
88+ }
89+
90+ /** The batch door: `bulkCreate`, and `find` for the next read. */
91+ function batchDb ( ) {
92+ const store = fakeStore ( ) ;
93+ const collection = {
94+ insertMany : async ( many : Doc [ ] ) => { for ( const doc of many ) store . push ( doc ) ; return { } ; } ,
7195 find : ( filter : Doc , options : { projection ?: Record < string , number > } ) => ( {
72- toArray : async ( ) => docs . filter ( ( x ) => matches ( x , filter ) ) . map ( ( d ) => project ( d , options ?. projection ) ) ,
96+ toArray : async ( ) => store . pickAll ( filter , options ) ,
7397 } ) ,
7498 } ;
75- return { db : { collection : ( ) => collection } , docs } ;
99+ return { db : { collection : ( ) => collection } , docs : store . docs } ;
76100}
77101
78102const OBJECT = 'nk_mongo_write_item' ;
@@ -84,9 +108,8 @@ describe('[#29538] driver-mongodb: a write answers the record the next read answ
84108 let engine : ObjectQL ;
85109 let docs : Doc [ ] ;
86110
87- beforeEach ( async ( ) => {
111+ const boot = async ( fake : { db : unknown ; docs : Doc [ ] } ) => {
88112 const driver = new MongoDBDriver ( { url : 'mongodb://127.0.0.1:1/unused' } ) ;
89- const fake = fakeDb ( ) ;
90113 docs = fake . docs ;
91114 ( driver as any ) . db = fake . db ;
92115 ( driver as any ) . connect = async ( ) => { } ;
@@ -104,10 +127,12 @@ describe('[#29538] driver-mongodb: a write answers the record the next read answ
104127 doubled : { type : 'formula' , expression : { dialect : 'cel' , source : 'record.qty != null ? record.qty * 2 : -1' } } ,
105128 } ,
106129 } as never ) ;
107- } ) ;
130+ } ;
108131
109- const expectAnswerIsNextRead = async ( answer : Doc ) => {
110- expect ( answer ) . toStrictEqual ( await engine . findOne ( OBJECT , { where : { id : answer . id } } ) ) ;
132+ beforeEach ( async ( ) => { await boot ( singleRowDb ( ) ) ; } ) ;
133+
134+ const expectAnswerIsNextRead = async ( answer : Doc , next ?: Doc ) => {
135+ expect ( answer ) . toStrictEqual ( next ?? await engine . findOne ( OBJECT , { where : { id : answer . id } } ) ) ;
111136 expect ( own ( answer , 'note' ) ) . toBe ( true ) ;
112137 expect ( answer . note ) . toBeNull ( ) ;
113138 expect ( own ( answer , 'qty' ) ) . toBe ( true ) ;
@@ -122,10 +147,15 @@ describe('[#29538] driver-mongodb: a write answers the record the next read answ
122147 await expectAnswerIsNextRead ( answer ) ;
123148 } ) ;
124149
125- it ( 'batch insert (bulkCreate): every answered row deep-equals its next findOne' , async ( ) => {
150+ it ( 'batch insert (bulkCreate): every answered row deep-equals its next read' , async ( ) => {
151+ await boot ( batchDb ( ) ) ;
126152 const answers = await engine . insert ( OBJECT , [ { name : 'Ada' } , { name : 'Bo' } ] ) as Doc [ ] ;
127153 expect ( answers ) . toHaveLength ( 2 ) ;
128- for ( const answer of answers ) await expectAnswerIsNextRead ( answer ) ;
154+ expect ( own ( docs [ 0 ] , 'note' ) ) . toBe ( false ) ;
155+ for ( const answer of answers ) {
156+ const [ next ] = await engine . find ( OBJECT , { where : { id : answer . id } } ) as Doc [ ] ;
157+ await expectAnswerIsNextRead ( answer , next ) ;
158+ }
129159 } ) ;
130160
131161 it ( 'update by id: the answer deep-equals the next findOne' , async ( ) => {
0 commit comments