Skip to content

Commit

Permalink
test: add an integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneRifle committed Sep 6, 2022
1 parent 581928a commit c054d1a
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 4 deletions.
1 change: 1 addition & 0 deletions babel.config.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
presets: ['@babel/preset-env'],
plugins: ['babel-plugin-transform-import-meta'],
}
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const config: Config.InitialOptions = {
],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
'^.+\\.(js|jsx)$': 'babel-jest',
'^.+\\.(mjs|js|jsx)$': 'babel-jest',
},
}

Expand Down
5 changes: 4 additions & 1 deletion jest.localstack.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ module.exports = {
showLog: true,
S3Buckets: [
{
Bucket: 'test',
Bucket: 'unit',
},
{
Bucket: 'integration',
},
],
}
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@types/node": "*",
"@types/uuid": "^8.3.4",
"babel-jest": "^28.1.3",
"babel-plugin-transform-import-meta": "^2.2.0",
"jest": "^28.1.3",
"ts-jest": "^28.0.8",
"ts-node": "^10.9.1",
Expand Down
73 changes: 73 additions & 0 deletions src/__tests__/s3vfs.integration.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { S3 } from '@aws-sdk/client-s3'
import { createRequire } from 'module'
import path from 'path'
import * as SQLite from 'wa-sqlite'
import SQLiteESMFactory from 'wa-sqlite/dist/wa-sqlite-async.mjs'

import { S3VFS } from '../s3vfs'

// @ts-ignore
import * as localstackConfig from '../../jest.localstack.js'

const s3 = new S3({
credentials: {
accessKeyId: 'access-key',
secretAccessKey: 'secret-key',
},
forcePathStyle: true,
endpoint: 'http://localhost:4566',
})

const { Bucket: bucketName } = localstackConfig.S3Buckets[1]


describe('S3VFS with wa-sqlite', function() {
let sqlite3: SQLiteAPI
let db: number

beforeAll(async () => {
await s3.createBucket({ Bucket: bucketName })

const s3vfs = new S3VFS(s3, bucketName)

const currentDirName = globalThis.__dirname
const originalRequire = globalThis.require
const sqliteDistPath = path.resolve(__dirname, '../../node_modules/wa-sqlite/dist')

if (typeof process === 'object') {
globalThis.__dirname = sqliteDistPath + '/wa-sqlite-async.mjs'
globalThis.require = createRequire(sqliteDistPath)
}

// Invoke the ES6 module factory to create the SQLite
// Emscripten module. This will fetch and compile the
// .wasm file.
const module = await SQLiteESMFactory({ locateFile: (path: string) => sqliteDistPath + '/' + path })

globalThis.require = originalRequire
globalThis.__dirname = currentDirName

// Use the module to build the API instance.
sqlite3 = SQLite.Factory(module)
sqlite3.vfs_register(s3vfs, true)
db = await sqlite3.open_v2(bucketName)
})
it('should return a result for SELECT 1+1', async () => {
const { rows, columns } = await new Promise((resolve) => {
sqlite3.exec(db, 'SELECT 1+1', (rows, columns) => resolve({ rows, columns }))
})
expect(rows).toStrictEqual([ 2 ])
expect(columns).toStrictEqual([ '1+1' ])
})
it('should CREATE TABLE, INSERT, UPDATE, SELECT', async () => {
await sqlite3.exec(db, 'CREATE TABLE users (x INTEGER PRIMARY KEY ASC, y TEXT)')
await sqlite3.exec(db, "INSERT INTO users (x,y) VALUES (1,'2');")
await sqlite3.exec(db, "UPDATE users SET y='c' WHERE x=1;")

const { rows, columns } = await new Promise((resolve) => {
sqlite3.exec(db, 'SELECT * from users', (rows, columns) => resolve({ rows, columns }))
})
expect(rows).toStrictEqual([ 1, 'c' ])
expect(columns).toStrictEqual([ 'x', 'y' ])
})
})
4 changes: 2 additions & 2 deletions src/__tests__/s3vfs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const s3 = new S3({
endpoint: 'http://localhost:4566',
})

const bucketName = localstackConfig.S3Buckets[0].Bucket
const { Bucket: bucketName } = localstackConfig.S3Buckets[0]

async function clear() {
const { Contents: objects } = await s3.listObjectsV2({
Expand All @@ -35,5 +35,5 @@ describe('S3VFS', function() {
beforeAll(async () => {
await s3.createBucket({ Bucket: bucketName })
})
configureTests(() => new S3VFS(s3, bucketName), clear, SKIP);
configureTests(() => new S3VFS(s3, bucketName), clear, SKIP)
})

0 comments on commit c054d1a

Please sign in to comment.