-
-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathinsertTest.ts
More file actions
114 lines (94 loc) · 5.76 KB
/
Copy pathinsertTest.ts
File metadata and controls
114 lines (94 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import assert from 'node:assert'
import { randomUUID } from 'node:crypto'
import * as helper from './testHelper.ts'
import { type PgBoss } from '../src/index.ts'
describe('insert', function () {
it('should create jobs from an array', async function () {
this.boss = await helper.start(this.bossConfig) as PgBoss
const input = [{}, {}, {}]
await this.boss.insert(this.schema, input)
const { queuedCount } = await this.boss.getQueueStats(this.schema)
assert.strictEqual(queuedCount, 3)
})
it('should create jobs from an array with all properties', async function () {
this.boss = await helper.start(this.bossConfig) as PgBoss
const deadLetter = `${this.schema}_dlq`
await this.boss.createQueue(deadLetter)
await this.boss.updateQueue(this.schema, { deadLetter })
const input = {
id: randomUUID(),
priority: 1,
data: { some: 'data' },
retryLimit: 1,
retryDelay: 2,
retryBackoff: true,
retryDelayMax: 3,
startAfter: new Date().toISOString(),
expireInSeconds: 5,
deleteAfterSeconds: 60,
singletonKey: '123',
retentionSeconds: 60
}
const keepUntil = new Date(new Date(input.startAfter).getTime() + (input.retentionSeconds * 1000)).toISOString()
await this.boss.insert(this.schema, [input])
const job = await this.boss.getJobById(this.schema, input.id)
assert.strictEqual(job.id, input.id, `id input ${input.id} didn't match job ${job.id}`)
assert.strictEqual(job.priority, input.priority, `priority input ${input.priority} didn't match job ${job.priority}`)
assert.strictEqual(JSON.stringify(job.data), JSON.stringify(input.data), `data input ${input.data} didn't match job ${job.data}`)
assert.strictEqual(job.retryLimit, input.retryLimit, `retryLimit input ${input.retryLimit} didn't match job ${job.retryLimit}`)
assert.strictEqual(job.retryDelay, input.retryDelay, `retryDelay input ${input.retryDelay} didn't match job ${job.retryDelay}`)
assert.strictEqual(job.retryBackoff, input.retryBackoff, `retryBackoff input ${input.retryBackoff} didn't match job ${job.retryBackoff}`)
assert.strictEqual(job.retryDelayMax, input.retryDelayMax, `retryDelayMax input ${input.retryDelayMax} didn't match job ${job.retryDelayMax}`)
assert.strictEqual(new Date(job.startAfter).toISOString(), input.startAfter, `startAfter input ${input.startAfter} didn't match job ${job.startAfter}`)
assert.strictEqual(job.expireInSeconds, input.expireInSeconds, `expireInSeconds input ${input.expireInSeconds} didn't match job ${job.expireInSeconds}`)
assert.strictEqual(job.deleteAfterSeconds, input.deleteAfterSeconds, `deleteAfterSeconds input ${input.deleteAfterSeconds} didn't match job ${job.deleteAfterSeconds}`)
assert.strictEqual(job.singletonKey, input.singletonKey, `name input ${input.singletonKey} didn't match job ${job.singletonKey}`)
assert.strictEqual(new Date(job.keepUntil).toISOString(), keepUntil, `keepUntil input ${keepUntil} didn't match job ${job.keepUntil}`)
})
it('should create jobs from an array with all properties and custom connection', async function () {
this.boss = await helper.start(this.bossConfig) as PgBoss
const deadLetter = `${this.schema}_dlq`
await this.boss.createQueue(deadLetter)
await this.boss.updateQueue(this.schema, { deadLetter })
const input = {
id: randomUUID(),
priority: 1,
data: { some: 'data' },
retryLimit: 1,
retryDelay: 2,
retryBackoff: true,
retryDelayMax: 3,
startAfter: new Date().toISOString(),
expireInSeconds: 5,
deleteAfterSeconds: 45,
singletonKey: '123',
retentionSeconds: 60
}
const keepUntil = new Date(new Date(input.startAfter).getTime() + (input.retentionSeconds * 1000)).toISOString()
let called = false
const db = await helper.getDb()
const options = {
db: {
async executeSql (sql, values) {
called = true
return db.pool.query(sql, values)
}
}
}
await this.boss.insert(this.schema, [input], options)
const job = await this.boss.getJobById(this.schema, input.id)
assert.strictEqual(job.id, input.id, `id input ${input.id} didn't match job ${job.id}`)
assert.strictEqual(job.priority, input.priority, `priority input ${input.priority} didn't match job ${job.priority}`)
assert.strictEqual(JSON.stringify(job.data), JSON.stringify(input.data), `data input ${input.data} didn't match job ${job.data}`)
assert.strictEqual(job.retryLimit, input.retryLimit, `retryLimit input ${input.retryLimit} didn't match job ${job.retryLimit}`)
assert.strictEqual(job.retryDelay, input.retryDelay, `retryDelay input ${input.retryDelay} didn't match job ${job.retryDelay}`)
assert.strictEqual(job.retryBackoff, input.retryBackoff, `retryBackoff input ${input.retryBackoff} didn't match job ${job.retryBackoff}`)
assert.strictEqual(job.retryDelayMax, input.retryDelayMax, `retryDelayMax input ${input.retryDelayMax} didn't match job ${job.retryDelayMax}`)
assert.strictEqual(new Date(job.startAfter).toISOString(), input.startAfter, `startAfter input ${input.startAfter} didn't match job ${job.startAfter}`)
assert.strictEqual(job.expireInSeconds, input.expireInSeconds, `expireInSeconds input ${input.expireInSeconds} didn't match job ${job.expireInSeconds}`)
assert.strictEqual(job.deleteAfterSeconds, input.deleteAfterSeconds, `deleteAfterSeconds input ${input.deleteAfterSeconds} didn't match job ${job.deleteAfterSeconds}`)
assert.strictEqual(job.singletonKey, input.singletonKey, `name input ${input.singletonKey} didn't match job ${job.singletonKey}`)
assert.strictEqual(new Date(job.keepUntil).toISOString(), keepUntil, `keepUntil input ${keepUntil} didn't match job ${job.keepUntil}`)
assert.strictEqual(called, true)
})
})