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:
parent
b80674d8a2
commit
3ad617a3c0
10 changed files with 154 additions and 0 deletions
8
packages/db/test/fixtures/integrations/astro.config.mjs
vendored
Normal file
8
packages/db/test/fixtures/integrations/astro.config.mjs
vendored
Normal 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()],
|
||||
});
|
12
packages/db/test/fixtures/integrations/db/config.ts
vendored
Normal file
12
packages/db/test/fixtures/integrations/db/config.ts
vendored
Normal 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 },
|
||||
});
|
11
packages/db/test/fixtures/integrations/db/seed.ts
vendored
Normal file
11
packages/db/test/fixtures/integrations/db/seed.ts
vendored
Normal 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' },
|
||||
]);
|
8
packages/db/test/fixtures/integrations/integration/config.ts
vendored
Normal file
8
packages/db/test/fixtures/integrations/integration/config.ts
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
import { defineDB } from '@astrojs/db';
|
||||
import { menu } from './shared';
|
||||
|
||||
export default defineDB({
|
||||
tables: {
|
||||
menu,
|
||||
},
|
||||
});
|
15
packages/db/test/fixtures/integrations/integration/index.ts
vendored
Normal file
15
packages/db/test/fixtures/integrations/integration/index.ts
vendored
Normal 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',
|
||||
});
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
12
packages/db/test/fixtures/integrations/integration/seed.ts
vendored
Normal file
12
packages/db/test/fixtures/integrations/integration/seed.ts
vendored
Normal 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' },
|
||||
]);
|
9
packages/db/test/fixtures/integrations/integration/shared.ts
vendored
Normal file
9
packages/db/test/fixtures/integrations/integration/shared.ts
vendored
Normal 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(),
|
||||
},
|
||||
});
|
14
packages/db/test/fixtures/integrations/package.json
vendored
Normal file
14
packages/db/test/fixtures/integrations/package.json
vendored
Normal 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:*"
|
||||
}
|
||||
}
|
17
packages/db/test/fixtures/integrations/src/pages/index.astro
vendored
Normal file
17
packages/db/test/fixtures/integrations/src/pages/index.astro
vendored
Normal 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>
|
48
packages/db/test/integrations.test.js
Normal file
48
packages/db/test/integrations.test.js
Normal 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');
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue