0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-04-07 23:41:43 -05:00

Add basic test and fixture for integrations API

This commit is contained in:
Chris Swithinbank 2024-03-04 17:00:33 +01:00
parent b80674d8a2
commit 3ad617a3c0
No known key found for this signature in database
GPG key ID: 52DB15DC07051619
10 changed files with 154 additions and 0 deletions

View file

@ -0,0 +1,8 @@
import db from '@astrojs/db';
import { defineConfig } from 'astro/config';
import testIntegration from './integration';
// https://astro.build/config
export default defineConfig({
integrations: [db(), testIntegration()],
});

View file

@ -0,0 +1,12 @@
import { column, defineDB, defineTable } from 'astro:db';
const Author = defineTable({
columns: {
name: column.text(),
age2: column.number({ optional: true }),
},
});
export default defineDB({
tables: { Author },
});

View file

@ -0,0 +1,11 @@
import { Author, db } from 'astro:db';
await db
.insert(Author)
.values([
{ name: 'Ben' },
{ name: 'Nate' },
{ name: 'Erika' },
{ name: 'Bjorn' },
{ name: 'Sarah' },
]);

View file

@ -0,0 +1,8 @@
import { defineDB } from '@astrojs/db';
import { menu } from './shared';
export default defineDB({
tables: {
menu,
},
});

View file

@ -0,0 +1,15 @@
import type { AstroIntegration } from 'astro';
export default function testIntegration(): AstroIntegration {
return {
name: 'db-test-integration',
hooks: {
'astro:db:setup'({ extendDb }) {
extendDb({
configEntrypoint: './integration/config.ts',
seedEntrypoint: './integration/seed.ts',
});
},
},
};
}

View file

@ -0,0 +1,12 @@
import { db } from 'astro:db';
import { menu } from './shared';
import { asDrizzleTable } from '@astrojs/db/utils';
const table = asDrizzleTable('menu', menu);
await db.insert(table).values([
{ name: 'Pancakes', price: 9.5, type: 'Breakfast' },
{ name: 'French Toast', price: 11.25, type: 'Breakfast' },
{ name: 'Coffee', price: 3, type: 'Beverages' },
{ name: 'Cappuccino', price: 4.5, type: 'Beverages' },
]);

View file

@ -0,0 +1,9 @@
import { defineTable, column } from '@astrojs/db';
export const menu = defineTable({
columns: {
name: column.text(),
type: column.text(),
price: column.number(),
},
});

View file

@ -0,0 +1,14 @@
{
"name": "@test/db-integration",
"version": "0.0.0",
"private": true,
"scripts": {
"dev": "astro dev",
"build": "astro build",
"preview": "astro preview"
},
"dependencies": {
"@astrojs/db": "workspace:*",
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,17 @@
---
/// <reference path="../../.astro/db-types.d.ts" />
import { Author, db, menu } from 'astro:db';
const authors = await db.select().from(Author);
const menuItems = await db.select().from(menu);
---
<h2>Authors</h2>
<ul class="authors-list">
{authors.map((author) => <li>{author.name}</li>)}
</ul>
<h2>Menu</h2>
<ul class="menu">
{menuItems.map((item) => <li>{item.name}</li>)}
</ul>

View file

@ -0,0 +1,48 @@
import { expect } from 'chai';
import { load as cheerioLoad } from 'cheerio';
import { loadFixture } from '../../astro/test/test-utils.js';
describe('astro:db with integrations', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
root: new URL('./fixtures/integrations/', import.meta.url),
});
});
// 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 from user-defined table', 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('Prints the list of menu items from integration-defined table', async () => {
const html = await fixture.fetch('/').then((res) => res.text());
const $ = cheerioLoad(html);
const ul = $('ul.menu');
expect(ul.children()).to.have.a.lengthOf(4);
expect(ul.children().eq(0).text()).to.equal('Pancakes');
});
});
});