2023-01-04 17:22:49 +03:00
const ObjectId = require ( 'bson-objectid' ) . default ;
2023-01-04 18:06:40 +03:00
const sinon = require ( 'sinon' ) ;
2022-11-21 12:29:53 +03:00
2023-01-04 17:22:49 +03:00
const createModel = ( propertiesAndRelations ) => {
2023-01-10 16:58:50 +03:00
const id = propertiesAndRelations . id ? ? ObjectId ( ) . toHexString ( ) ;
2023-01-04 17:22:49 +03:00
return {
2023-01-10 16:58:50 +03:00
id ,
2023-01-04 17:22:49 +03:00
getLazyRelation : ( relation ) => {
2023-01-11 14:13:13 +03:00
propertiesAndRelations . loaded = propertiesAndRelations . loaded ? ? [ ] ;
if ( ! propertiesAndRelations . loaded . includes ( relation ) ) {
propertiesAndRelations . loaded . push ( relation ) ;
}
if ( Array . isArray ( propertiesAndRelations [ relation ] ) ) {
return Promise . resolve ( {
models : propertiesAndRelations [ relation ]
} ) ;
}
2023-01-04 17:22:49 +03:00
return Promise . resolve ( propertiesAndRelations [ relation ] ) ;
} ,
2023-01-11 14:13:13 +03:00
related : ( relation ) => {
if ( ! Object . keys ( propertiesAndRelations ) . includes ( 'loaded' ) ) {
throw new Error ( ` Model.related(' ${ relation } '): When creating a test model via createModel you must include 'loaded' to specify which relations are already loaded and useable via Model.related. ` ) ;
}
if ( ! propertiesAndRelations . loaded . includes ( relation ) ) {
throw new Error ( ` Model.related(' ${ relation } ') was used on a test model that didn't explicitly loaded that relation. ` ) ;
}
return propertiesAndRelations [ relation ] ;
} ,
2023-01-04 17:22:49 +03:00
get : ( property ) => {
return propertiesAndRelations [ property ] ;
} ,
save : ( properties ) => {
Object . assign ( propertiesAndRelations , properties ) ;
return Promise . resolve ( ) ;
2023-01-10 16:58:50 +03:00
} ,
toJSON : ( ) => {
return {
id ,
... propertiesAndRelations
} ;
2023-01-04 17:22:49 +03:00
}
} ;
} ;
2022-11-21 12:29:53 +03:00
2023-01-04 17:22:49 +03:00
const createModelClass = ( options = { } ) => {
return {
... options ,
add : async ( properties ) => {
return Promise . resolve ( createModel ( properties ) ) ;
} ,
findOne : async ( data , o ) => {
if ( options . findOne === null && o . require ) {
return Promise . reject ( new Error ( 'NotFound' ) ) ;
}
if ( options . findOne === null ) {
return Promise . resolve ( null ) ;
}
return Promise . resolve (
createModel ( { ... options . findOne , ... data } )
) ;
2023-01-10 16:58:50 +03:00
} ,
findAll : async ( data ) => {
return Promise . resolve (
( options . findAll ? ? [ ] ) . map ( f => createModel ( { ... f , ... data } ) )
) ;
} ,
transaction : async ( callback ) => {
const transacting = { transacting : 'transacting' } ;
return await callback ( transacting ) ;
} ,
where : function ( ) {
return this ;
} ,
save : async function ( ) {
return Promise . resolve ( ) ;
2023-01-04 17:22:49 +03:00
}
} ;
} ;
2023-01-10 18:36:41 +03:00
const createDb = ( { first , all } = { } ) => {
2023-01-10 16:58:50 +03:00
let a = all ;
2023-01-04 18:06:40 +03:00
const db = {
knex : function ( ) {
return this ;
} ,
where : function ( ) {
return this ;
} ,
whereNull : function ( ) {
return this ;
} ,
select : function ( ) {
return this ;
} ,
2023-01-10 16:58:50 +03:00
limit : function ( n ) {
a = all . slice ( 0 , n ) ;
return this ;
} ,
2023-01-04 18:06:40 +03:00
update : sinon . stub ( ) . resolves ( ) ,
2023-01-10 16:58:50 +03:00
orderByRaw : function ( ) {
return this ;
} ,
insert : function ( ) {
return this ;
} ,
2023-01-04 18:06:40 +03:00
first : ( ) => {
return Promise . resolve ( first ) ;
2023-01-10 16:58:50 +03:00
} ,
then : function ( resolve ) {
resolve ( a ) ;
} ,
transacting : function ( ) {
return this ;
2023-01-04 18:06:40 +03:00
}
} ;
db . knex . raw = function ( ) {
return this ;
} ;
return db ;
} ;
2023-01-10 16:58:50 +03:00
const sleep = ( ms ) => {
return new Promise ( ( resolve ) => {
setTimeout ( resolve , ms ) ;
} ) ;
} ;
2023-01-04 17:22:49 +03:00
module . exports = {
createModel ,
2023-01-04 18:06:40 +03:00
createModelClass ,
2023-01-10 16:58:50 +03:00
createDb ,
sleep
2023-01-04 17:22:49 +03:00
} ;