Skip to content

Commit

Permalink
fix: log error on missing required fields when finding with fields se…
Browse files Browse the repository at this point in the history
…lection
  • Loading branch information
onhate committed Jul 3, 2024
1 parent 8fff64f commit c72ae56
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
19 changes: 18 additions & 1 deletion test/find.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
/*
find-and-scan.ts - Basic find and scan options
*/
import {AWS, Client, Match, Table, print, dump, delay} from './utils/init'
import {DefaultSchema} from './schemas'
import {Client, Table} from './utils/init'

// jest.setTimeout(7200 * 1000)

const onErrorLog = jest.fn();
const table = new Table({
name: 'FindTable',
client: Client,
partial: false,
schema: DefaultSchema,
logger: (level, message, ctx) => {
if (level === 'info') return console.log({message, ctx})
if (level === 'warn') return console.warn({message, ctx})
if (level === 'error') {
onErrorLog({message, ctx})
return console.error({message, ctx})
}
},
})

test('Create Table', async () => {
Expand All @@ -21,6 +30,7 @@ test('Create Table', async () => {
})

let User = table.getModel('User')
let Pet = table.getModel('Pet')
let user: any
let users: any

Expand Down Expand Up @@ -99,6 +109,13 @@ test('List with begins_with', async () => {
expect(items.length).toBe(1)
})

test('Find Pets names only (should not log error on missing required fields)', async () => {
await Pet.create({name: 'Guinness', race: 'dog', breed:'shih-tzu'})
let items = await Pet.find({}, {fields: ['name']})
expect(items).toHaveLength(1)
expect(onErrorLog).not.toHaveBeenCalled();
})

test('Destroy Table', async () => {
await table.deleteTable('DeleteTableForever')
expect(await table.exists()).toBe(false)
Expand Down
8 changes: 8 additions & 0 deletions test/schemas/defaultSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export default {
gs3pk: {type: String, value: '${_type}#${status}'},
gs3sk: {type: String, value: '${_type}#${name}'},
},
Pet: {
pk: {type: String, value: '${_type}', hidden: true},
sk: {type: String, value: '${_type}#${id}', hidden: true},
id: {type: String, generate: 'ulid'},
name: {type: String},
race: {type: String, enum: ['dog', 'cat', 'fish'], required: true},
breed: {type: String, required: true},
}
},
params: {
isoDates: true,
Expand Down

0 comments on commit c72ae56

Please sign in to comment.