-
-
Notifications
You must be signed in to change notification settings - Fork 676
/
Copy pathgenerateCollectionInsert.test.ts
106 lines (95 loc) · 3.54 KB
/
generateCollectionInsert.test.ts
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
import { describe, expect, test } from 'vitest'
import { z } from 'zod'
import { generateCollectionInsert, defineCollection, resolveCollection, getTableName, SLICE_SIZE, MAX_SQL_QUERY_SIZE } from '../../src/utils/collection'
describe('generateCollectionInsert', () => {
test('Respect Schema\'s default values', () => {
const collection = resolveCollection('content', defineCollection({
type: 'data',
source: '**',
schema: z.object({
customField: z.number().default(13),
otherField: z.string().default('untitled'),
otherField2: z.boolean().default(true),
date: z.date().default(new Date('2022-01-01')),
}),
}))!
const { queries: sql } = generateCollectionInsert(collection, {
id: 'foo.md',
stem: 'foo',
extension: 'md',
meta: {},
})
expect(sql[0]).toBe([
`INSERT INTO ${getTableName('content', collection.hash)}`,
' VALUES',
' (\'foo.md\', 13, \'2022-01-01T00:00:00.000Z\', \'md\', \'{}\', \'untitled\', true, \'foo\', \'vPdICyZ7sjhw1YY4ISEATbCTIs_HqNpMVWHnBWhOOYY\');',
].join(''))
})
test('Overwrite default fields', () => {
const collection = resolveCollection('content', defineCollection({
type: 'data',
source: '**',
schema: z.object({
customField: z.number().default(13),
otherField: z.string().default('untitled'),
otherField2: z.boolean().default(true),
date: z.date().default(new Date('2022-01-01')),
}),
}))!
const { queries: sql } = generateCollectionInsert(collection, {
id: 'foo.md',
stem: 'foo',
extension: 'md',
meta: {},
customField: 42,
otherField: 'foo',
otherField2: false,
date: new Date('2022-01-02'),
})
expect(sql[0]).toBe([
`INSERT INTO ${getTableName('content', collection.hash)}`,
' VALUES',
' (\'foo.md\', 42, \'2022-01-02T00:00:00.000Z\', \'md\', \'{}\', \'foo\', false, \'foo\', \'R5zX5zuyfvCtvXPcgINuEjEoHmZnse8kATeDd4V7I-c\');',
].join(''))
})
test('Split long values', () => {
const collection = resolveCollection('content', defineCollection({
type: 'data',
source: '**',
schema: z.object({
content: z.string().max(10000),
}),
}))!
const content = (Array.from({ length: 20000 })).fill('lorem ipsum dolor sit amet - ').map((val, i) => val + i.toString()).join(' ')
const { queries: sql } = generateCollectionInsert(collection, {
id: 'foo.md',
stem: 'foo',
extension: 'md',
meta: {},
content,
})
const querySlices: string[] = [content.slice(0, SLICE_SIZE - 1)]
for (let i = 1; i < (content.length / SLICE_SIZE); i++) {
querySlices.push(content.slice((SLICE_SIZE * i) - 1, (SLICE_SIZE * (i + 1)) - 1))
}
// check that the content will be split into multiple queries
expect(content.length).toBeGreaterThan(MAX_SQL_QUERY_SIZE)
// check that concatenated all the values are equal to the original content
expect(content).toEqual(querySlices.join(''))
expect(sql[0]).toBe([
`INSERT INTO ${getTableName('content', collection.hash)}`,
' VALUES',
` ('foo.md', '${querySlices[0]}', 'md', '{}', 'foo', 'QMyFxMru9gVfaNx0fzjs5is7SvAZMEy3tNDANjkdogg');`,
].join(''))
let index = 1
while (index < sql.length - 1) {
expect(sql[index]).toBe([
`UPDATE ${getTableName('content', collection.hash)}`,
' SET',
` content = CONCAT(content, '${querySlices[index]}')`,
' WHERE id = \'foo.md\';',
].join(''))
index++
}
})
})