0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/db/test/basics.test.js
Ben Holmes 3488be9b59
finalize WIP API (#10280)
* feat: no more readable / writable

* fix: table typegen

* wip: move data seeding

* chore: add scripts to basics

* feat: data() -> seed file

* refactor: ensure precedence of file name

* feat: db execute command

* fix: test imports

* chore: remove old readable error tests

* feat: support local db with `db execute`

* refactor: remove integrations from test for now

* chore: stray comment

* chore: remove `table` config object

* feat: `db.batch`!

* refactor: move migrations/ inside db/

* fix: move ticketing-example to seed file

* fix: disable foreign keys when recreating tables

* refactor: standardize migrations dir

* feat: move to db/config.ts

* feat: file watching for db/config.ts dependencies

* feat: remove unsafeDisableStudio

* chroe: remove bad import

* feat: parse config.ts from cli

* chore: remove async from localDatabaseClient

* fix: update recipes config and seed

* chore: update unit tests

* chore: update tests to dev server

* refactor: collectionToTable -> asDrizzleTable

* chore: tidy up collection -> table error states

* refactor: regexp -> endsWith

* feat: pretty error inserting into table

* refactor: try/catch -> catch()

* feat: expose utils for integration seed files

* fix: add config import to db client modules

* fix: just use generic "seeding database" error

* chore: remove unused link args

* fix: migration queries im,port

* chore: remove irrelevant glob/ example

* feat: format migration file path

* feat: support all config file names

* chore: remove db.batch() for now

* chore: remove `db` object

* core: remove unused integration file

* chore: changeset

* fix: foreign key empty error message

* chore: remove old TODO

* fix: bad context reference

* refactor: seedDev -> seedLocal

* wip: throw some console logs at github

* wip: avoid seeding astro:db imported by seed file

* wip: use anything in db/

* refactor: only seed when loaded within srcDir

* refactor: avoid resolution when not seeding

* chore: remove logs

* refactor: seed within create local db client

* refactor: use normalizePath

* wip: logs

* wip: logs

* refactor: early return

* chore: more logs

* refactor: no batch

* fix: use beforeAll

* refactor: move all tests to base block

* wip: log dev server starting

* chore: remove logs

* wip: demo ready

* chore: remove duplicate recreateTables() call

* Revert "wip: demo ready"

This reverts commit 37585ce5cb.

* refactor: beforeEach to isolate dev servers

* chore: remove useBundledDbUrl

* refactor: naming and seed scope

* chore: remove stray console logs

* wip: fix windows file import

* wip: try fileURLToPath

* Revert "wip: try fileURLToPath"

This reverts commit 46fd65d61a.

* Revert "wip: fix windows file import"

This reverts commit 1a669ea646.

* refactor: dir -> directory

* refactor: move execute file to cli

* refactor: remove seed.dev convention

* wip: attempt fileURLToPath

* wip: debug the file exists

* fix: use mjs??

* chore: remove duplicate seedLocal

* chore: remove log check

* refactor: use in memory db for tests

* chore: clean up test comment

* fix: avoid file writes for db setup on in memory db

* chore: bump db changeset to minor

---------

Co-authored-by: Nate Moore <nate@astro.build>
2024-03-01 19:29:55 -05:00

74 lines
2.3 KiB
JavaScript

import { expect } from 'chai';
import { load as cheerioLoad } from 'cheerio';
import testAdapter from '../../astro/test/test-adapter.js';
import { loadFixture } from '../../astro/test/test-utils.js';
describe('astro:db', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
root: new URL('./fixtures/basics/', import.meta.url),
output: 'server',
adapter: testAdapter(),
});
});
// Note(bholmesdev): Use in-memory db to avoid
// Multiple dev servers trying to unlink and remount
// the same database file.
process.env.TEST_IN_MEMORY_DB = 'true';
describe('development', () => {
let devServer;
before(async () => {
console.log('starting dev server');
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
process.env.TEST_IN_MEMORY_DB = undefined;
});
it('Prints the list of authors', async () => {
const html = await fixture.fetch('/').then((res) => res.text());
const $ = cheerioLoad(html);
const ul = $('.authors-list');
expect(ul.children()).to.have.a.lengthOf(5);
expect(ul.children().eq(0).text()).to.equal('Ben');
});
it('Allows expression defaults for date columns', async () => {
const html = await fixture.fetch('/').then((res) => res.text());
const $ = cheerioLoad(html);
const themeAdded = $($('.themes-list .theme-added')[0]).text();
expect(new Date(themeAdded).getTime()).to.not.be.NaN;
});
it('Defaults can be overridden for dates', async () => {
const html = await fixture.fetch('/').then((res) => res.text());
const $ = cheerioLoad(html);
const themeAdded = $($('.themes-list .theme-added')[1]).text();
expect(new Date(themeAdded).getTime()).to.not.be.NaN;
});
it('Allows expression defaults for text columns', async () => {
const html = await fixture.fetch('/').then((res) => res.text());
const $ = cheerioLoad(html);
const themeOwner = $($('.themes-list .theme-owner')[0]).text();
expect(themeOwner).to.equal('');
});
it('Allows expression defaults for boolean columns', async () => {
const html = await fixture.fetch('/').then((res) => res.text());
const $ = cheerioLoad(html);
const themeDark = $($('.themes-list .theme-dark')[0]).text();
expect(themeDark).to.equal('dark mode');
});
});
});