mirror of
https://github.com/withastro/astro.git
synced 2024-12-30 22:03:56 -05:00
feat: index query tests
This commit is contained in:
parent
d2b9cb3cf5
commit
9e3ff8b462
1 changed files with 98 additions and 0 deletions
98
packages/db/test/unit/index-queries.test.js
Normal file
98
packages/db/test/unit/index-queries.test.js
Normal file
|
@ -0,0 +1,98 @@
|
|||
import { expect } from 'chai';
|
||||
import { describe, it } from 'mocha';
|
||||
import { collectionSchema } from '../../dist/types.js';
|
||||
import { getCollectionChangeQueries } from '../../dist/cli/queries.js';
|
||||
import { field } from '../../dist/config.js';
|
||||
|
||||
const userInitial = collectionSchema.parse({
|
||||
fields: {
|
||||
name: field.text(),
|
||||
age: field.number(),
|
||||
email: field.text({ unique: true }),
|
||||
mi: field.text({ optional: true }),
|
||||
},
|
||||
indexes: {},
|
||||
writable: false,
|
||||
});
|
||||
|
||||
describe('index queries', () => {
|
||||
it('adds indexes', async () => {
|
||||
/** @type {import('../../dist/types.js').DBCollection} */
|
||||
const userFinal = {
|
||||
...userInitial,
|
||||
indexes: {
|
||||
nameIdx: { on: ['name'], unique: false },
|
||||
emailIdx: { on: ['email'], unique: true },
|
||||
},
|
||||
};
|
||||
|
||||
const queries = await getCollectionChangeQueries({
|
||||
collectionName: 'user',
|
||||
oldCollection: userInitial,
|
||||
newCollection: userFinal,
|
||||
});
|
||||
|
||||
expect(queries).to.deep.equal([
|
||||
'CREATE INDEX "nameIdx" ON "user" ("name")',
|
||||
'CREATE UNIQUE INDEX "emailIdx" ON "user" ("email")',
|
||||
]);
|
||||
});
|
||||
|
||||
it('drops indexes', async () => {
|
||||
/** @type {import('../../dist/types.js').DBCollection} */
|
||||
const initial = {
|
||||
...userInitial,
|
||||
indexes: {
|
||||
nameIdx: { on: ['name'], unique: false },
|
||||
emailIdx: { on: ['email'], unique: true },
|
||||
},
|
||||
};
|
||||
|
||||
/** @type {import('../../dist/types.js').DBCollection} */
|
||||
const final = {
|
||||
...userInitial,
|
||||
indexes: {},
|
||||
};
|
||||
|
||||
const queries = await getCollectionChangeQueries({
|
||||
collectionName: 'user',
|
||||
oldCollection: initial,
|
||||
newCollection: final,
|
||||
});
|
||||
|
||||
expect(queries).to.deep.equal(['DROP INDEX "nameIdx"', 'DROP INDEX "emailIdx"']);
|
||||
});
|
||||
|
||||
it('drops and recreates modified indexes', async () => {
|
||||
/** @type {import('../../dist/types.js').DBCollection} */
|
||||
const initial = {
|
||||
...userInitial,
|
||||
indexes: {
|
||||
nameIdx: { on: ['name'], unique: false },
|
||||
emailIdx: { on: ['email'], unique: true },
|
||||
},
|
||||
};
|
||||
|
||||
/** @type {import('../../dist/types.js').DBCollection} */
|
||||
const final = {
|
||||
...userInitial,
|
||||
indexes: {
|
||||
nameIdx: { on: ['name'], unique: true },
|
||||
emailIdx: { on: ['email'] },
|
||||
},
|
||||
};
|
||||
|
||||
const queries = await getCollectionChangeQueries({
|
||||
collectionName: 'user',
|
||||
oldCollection: initial,
|
||||
newCollection: final,
|
||||
});
|
||||
|
||||
expect(queries).to.deep.equal([
|
||||
'DROP INDEX "nameIdx"',
|
||||
'DROP INDEX "emailIdx"',
|
||||
'CREATE UNIQUE INDEX "nameIdx" ON "user" ("name")',
|
||||
'CREATE INDEX "emailIdx" ON "user" ("email")',
|
||||
]);
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue