@@ -62,7 +62,7 @@ test('createSchemaModel() creates a new schema model with DB driver methods atta
6262} )
6363
6464test ( 'Model.driver.insertOne() inserts a new record in memory' , async ( ) => {
65- const UserModel = createSchemaModel ( UserSchema , 'User1 ' )
65+ const UserModel = createSchemaModel ( UserSchema , 'UserInsertOne1 ' )
6666 const newUser = await UserModel . driver . insertOne ( {
6767 name : 'Thorr' ,
6868 email : 'thorr@codinsonn.dev' ,
@@ -74,7 +74,7 @@ test('Model.driver.insertOne() inserts a new record in memory', async () => {
7474} )
7575
7676test ( 'Model.driver.insertOne() throws if inserted record does not match schema' , async ( ) => {
77- const UserModel = createSchemaModel ( UserSchema , 'User2 ' )
77+ const UserModel = createSchemaModel ( UserSchema , 'UserInsertOne2 ' )
7878 // Check that we throw an error when inserting a record with invalid data
7979 await expect ( UserModel . driver . insertOne ( { } ) ) . rejects . toThrow ( ) // Missing required fields
8080 await expect ( UserModel . driver . insertOne ( { name : 'Thorr' } ) ) . rejects . toThrow ( ) // Missing required field email
@@ -83,7 +83,7 @@ test('Model.driver.insertOne() throws if inserted record does not match schema',
8383} )
8484
8585test ( 'Model.driver.insertMany() inserts multiple records in memory' , async ( ) => {
86- const UserModel = createSchemaModel ( UserSchema , 'User3 ' )
86+ const UserModel = createSchemaModel ( UserSchema , 'UserInsertMany1 ' )
8787 const newUsers = await UserModel . driver . insertMany ( [
8888 {
8989 name : 'Thorr' ,
@@ -107,7 +107,7 @@ test('Model.driver.insertMany() inserts multiple records in memory', async () =>
107107} )
108108
109109test ( 'Model.driver.insertMany() throws if inserted records do not match schema' , async ( ) => {
110- const UserModel = createSchemaModel ( UserSchema , 'User4 ' )
110+ const UserModel = createSchemaModel ( UserSchema , 'UserInsertMany2 ' )
111111 // Check that we throw an error when inserting records with invalid data
112112 await expect ( UserModel . driver . insertMany ( [ { } ] ) ) . rejects . toThrow ( ) // Missing required fields
113113 await expect ( UserModel . driver . insertMany ( [ { name : 'Thorr' } ] ) ) . rejects . toThrow ( ) // Missing required field email
@@ -116,7 +116,7 @@ test('Model.driver.insertMany() throws if inserted records do not match schema',
116116} )
117117
118118test ( 'Model.driver.findOne() finds a record stored in memory' , async ( ) => {
119- const UserModel = createSchemaModel ( UserSchema , 'User5 ' )
119+ const UserModel = createSchemaModel ( UserSchema , 'UserFindOne1 ' )
120120 const newUser = await UserModel . driver . insertOne ( {
121121 name : 'Thorr' ,
122122 email : 'thorr@codinsonn.dev' ,
@@ -145,7 +145,7 @@ test('Model.driver.findOne() finds a record stored in memory', async () => {
145145} )
146146
147147test ( 'Model.driver.findMany() finds multiple records stored in memory' , async ( ) => {
148- const UserModel = createSchemaModel ( UserSchema , 'User6 ' )
148+ const UserModel = createSchemaModel ( UserSchema , 'UserFindMany1 ' )
149149 const newUsers = await UserModel . driver . insertMany ( [
150150 {
151151 name : 'Thorr' ,
@@ -168,7 +168,7 @@ test('Model.driver.findMany() finds multiple records stored in memory', async ()
168168} )
169169
170170test ( 'Model.driver.updateOne() updates a record stored in memory' , async ( ) => {
171- const UserModel = createSchemaModel ( UserSchema , 'User7 ' )
171+ const UserModel = createSchemaModel ( UserSchema , 'UserUpdateOne1 ' )
172172 const newUser = await UserModel . driver . insertOne ( {
173173 name : 'Thorr' ,
174174 email : 'thorr@codinsonn.dev' ,
@@ -183,7 +183,7 @@ test('Model.driver.updateOne() updates a record stored in memory', async () => {
183183} )
184184
185185test ( 'Model.driver.updateOne() with errorOnUnmatched: true throws if updated record does not match schema' , async ( ) => {
186- const UserModel = createSchemaModel ( UserSchema , 'User8 ' )
186+ const UserModel = createSchemaModel ( UserSchema , 'UserUpdateOne2 ' )
187187 const newUser = await UserModel . driver . insertOne ( {
188188 name : 'Thorr' ,
189189 email : 'thorr@codinsonn.dev' ,
@@ -202,7 +202,7 @@ test('Model.driver.updateOne() with errorOnUnmatched: true throws if updated rec
202202} )
203203
204204test ( 'Model.driver.updateMany() updates multiple records stored in memory' , async ( ) => {
205- const UserModel = createSchemaModel ( UserSchema , 'User9 ' )
205+ const UserModel = createSchemaModel ( UserSchema , 'UserUpdateMany1 ' )
206206 const newUsers = await UserModel . driver . insertMany ( [
207207 {
208208 name : 'Thorr' ,
@@ -223,7 +223,7 @@ test('Model.driver.updateMany() updates multiple records stored in memory', asyn
223223} )
224224
225225test ( 'Model.driver.updateMany() with errorOnUnmatched: true throws if updated records do not match schema' , async ( ) => {
226- const UserModel = createSchemaModel ( UserSchema , 'User10 ' )
226+ const UserModel = createSchemaModel ( UserSchema , 'UserUpdateMany2 ' )
227227 const newUsers = await UserModel . driver . insertMany ( [
228228 {
229229 name : 'Thorr' ,
@@ -246,8 +246,59 @@ test('Model.driver.updateMany() with errorOnUnmatched: true throws if updated re
246246 await expect ( ( ) => UserModel . driver . updateMany ( { email : '' } , { name : 'Thorr' } ) ) . not . toThrow ( )
247247} )
248248
249+ test ( 'Model.driver.upsertOne() inserts a new record if it does not exist' , async ( ) => {
250+ const UserModel = createSchemaModel ( UserSchema , 'UserUpsertOne1' )
251+ // Check that we can insert a new record
252+ const newUser = await UserModel . driver . upsertOne ( {
253+ name : 'Thorr' ,
254+ } , {
255+ name : 'Thorr' ,
256+ email : 'thorr@codinsonn.dev' ,
257+ } )
258+ // Check that the record was inserted
259+ expect ( newUser ) . toBeDefined ( )
260+ expect ( newUser . id ) . toBeDefined ( )
261+ expect ( newUser . name ) . toBe ( 'Thorr' )
262+ expect ( newUser . email ) . toBe ( 'thorr@codinsonn.dev' )
263+ // Check that we can find the record by id
264+ const foundById = await UserModel . driver . findOne ( { id : newUser . id } )
265+ expect ( foundById ) . toBeDefined ( )
266+ expect ( foundById . id ) . toBe ( newUser . id )
267+ expect ( foundById . name ) . toBe ( 'Thorr' )
268+ expect ( foundById . email ) . toBe ( 'thorr@codinsonn.dev' )
269+ } )
270+
271+ test ( 'Model.driver.upsertOne() updates an existing record if it exists' , async ( ) => {
272+ const UserModel = createSchemaModel ( UserSchema , 'UserUpsertOne2' )
273+ const onlyUser = await UserModel . driver . insertOne ( {
274+ name : 'Thorr' ,
275+ email : 'thorr@codinsonn.dev' ,
276+ } )
277+ // Check that we can update an existing record
278+ const updatedUser = await UserModel . driver . upsertOne ( {
279+ name : 'Thorr' ,
280+ } , {
281+ name : 'Thorr' ,
282+ email : 'thorr@fullproduct.dev' ,
283+ } )
284+ // Check that the record was updated
285+ expect ( updatedUser ) . toBeDefined ( )
286+ expect ( updatedUser . id ) . toBe ( onlyUser . id )
287+ expect ( updatedUser . name ) . toBe ( 'Thorr' )
288+ expect ( updatedUser . email ) . toBe ( 'thorr@fullproduct.dev' )
289+ // Check that we can find the record by id
290+ const foundById = await UserModel . driver . findOne ( { id : onlyUser . id } )
291+ expect ( foundById ) . toBeDefined ( )
292+ expect ( foundById . id ) . toBe ( onlyUser . id )
293+ expect ( foundById . name ) . toBe ( 'Thorr' )
294+ expect ( foundById . email ) . toBe ( 'thorr@fullproduct.dev' )
295+ // Check that we can no longer find the record by the old email
296+ const notFoundByEmail = await UserModel . driver . findOne ( { email : 'thorr@codinsonn.dev' } )
297+ expect ( notFoundByEmail ) . toBeUndefined ( )
298+ } )
299+
249300test ( 'Model.driver.deleteOne() deletes a record stored in memory' , async ( ) => {
250- const UserModel = createSchemaModel ( UserSchema , 'User11 ' )
301+ const UserModel = createSchemaModel ( UserSchema , 'UserDeleteOne1 ' )
251302 const newUsers = await UserModel . driver . insertMany ( [
252303 {
253304 name : 'Thorr' ,
@@ -275,7 +326,7 @@ test('Model.driver.deleteOne() deletes a record stored in memory', async () => {
275326} )
276327
277328test ( 'Model.driver.deleteMany() deletes multiple records stored in memory' , async ( ) => {
278- const UserModel = createSchemaModel ( UserSchema , 'User12 ' )
329+ const UserModel = createSchemaModel ( UserSchema , 'UserDeleteMany1 ' )
279330 const newUsers = await UserModel . driver . insertMany ( [
280331 {
281332 name : 'Thorr' ,
@@ -301,7 +352,7 @@ test('Model.driver.deleteMany() deletes multiple records stored in memory', asyn
301352} )
302353
303354test ( 'Model.driver.deleteMany() returns an empty array by default if no records are found' , async ( ) => {
304- const UserModel = createSchemaModel ( UserSchema , 'User13 ' )
355+ const UserModel = createSchemaModel ( UserSchema , 'UserDeleteMany2 ' )
305356 const newUsers = await UserModel . driver . insertMany ( [
306357 {
307358 name : 'Thorr' ,
@@ -319,7 +370,7 @@ test('Model.driver.deleteMany() returns an empty array by default if no records
319370} )
320371
321372test ( 'Model query filters support logical $and, $or, $nor & $not operators' , async ( ) => {
322- const UserModel = createSchemaModel ( UserSchema , 'User14 ' )
373+ const UserModel = createSchemaModel ( UserSchema , 'UserQueryFilters1 ' )
323374 const newUsers = await UserModel . driver . insertMany ( [
324375 {
325376 name : 'Thorr' ,
@@ -364,7 +415,7 @@ test('Model query filters support logical $and, $or, $nor & $not operators', asy
364415} )
365416
366417test ( 'Model query filters support conditional field operators $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin' , async ( ) => {
367- const UserModel = createSchemaModel ( UserSchema , 'User15 ' )
418+ const UserModel = createSchemaModel ( UserSchema , 'UserQueryFilters2 ' )
368419 const newUsers = await UserModel . driver . insertMany ( [
369420 {
370421 name : 'Thorr' ,
@@ -446,7 +497,7 @@ test('Model query filters support conditional field operators $eq, $ne, $gt, $gt
446497} )
447498
448499test ( "Model query filters support nested fields" , async ( ) => {
449- const UserModel = createSchemaModel ( UserSchema , 'User16 ' )
500+ const UserModel = createSchemaModel ( UserSchema , 'UserQueryFilters3 ' )
450501 const newUsers = await UserModel . driver . insertMany ( [
451502 {
452503 name : 'Thorr' ,
0 commit comments