mirror of
https://github.com/lana-k/sqliteviz.git
synced 2025-12-07 10:38:54 +08:00
add CSV support #27
This commit is contained in:
@@ -1,129 +1,113 @@
|
||||
import { expect } from 'chai'
|
||||
import chai from 'chai'
|
||||
import chaiAsPromised from 'chai-as-promised'
|
||||
import initSqlJs from 'sql.js'
|
||||
import db from '@/database.js'
|
||||
const config = {
|
||||
locateFile: filename => 'js/sql-wasm.wasm'
|
||||
}
|
||||
import database from '@/database.js'
|
||||
chai.use(chaiAsPromised)
|
||||
const expect = chai.expect
|
||||
chai.should()
|
||||
|
||||
const db = database.getNewDatabase()
|
||||
const getSQL = initSqlJs()
|
||||
|
||||
describe('database.js', () => {
|
||||
it('creates schema', () => {
|
||||
return initSqlJs(config)
|
||||
.then(SQL => {
|
||||
const database = new SQL.Database()
|
||||
database.run(`
|
||||
CREATE TABLE test (
|
||||
col1,
|
||||
col2 integer,
|
||||
col3 decimal(5,2),
|
||||
col4 varchar(30)
|
||||
)
|
||||
`)
|
||||
it('creates schema', async () => {
|
||||
const SQL = await getSQL
|
||||
const tempDb = new SQL.Database()
|
||||
tempDb.run(`CREATE TABLE test (
|
||||
col1,
|
||||
col2 integer,
|
||||
col3 decimal(5,2),
|
||||
col4 varchar(30)
|
||||
)`)
|
||||
|
||||
const data = database.export()
|
||||
const buffer = new Blob([data])
|
||||
return db.loadDb(buffer)
|
||||
})
|
||||
.then(({ dbName, schema }) => {
|
||||
expect(schema).to.have.lengthOf(1)
|
||||
expect(schema[0].name).to.equal('test')
|
||||
expect(schema[0].columns[0].name).to.equal('col1')
|
||||
expect(schema[0].columns[0].type).to.equal('N/A')
|
||||
expect(schema[0].columns[1].name).to.equal('col2')
|
||||
expect(schema[0].columns[1].type).to.equal('integer')
|
||||
expect(schema[0].columns[2].name).to.equal('col3')
|
||||
expect(schema[0].columns[2].type).to.equal('decimal(5, 2)')
|
||||
expect(schema[0].columns[3].name).to.equal('col4')
|
||||
expect(schema[0].columns[3].type).to.equal('varchar(30)')
|
||||
})
|
||||
const data = tempDb.export()
|
||||
const buffer = new Blob([data])
|
||||
|
||||
const { schema } = await db.loadDb(buffer)
|
||||
expect(schema).to.have.lengthOf(1)
|
||||
expect(schema[0].name).to.equal('test')
|
||||
expect(schema[0].columns[0].name).to.equal('col1')
|
||||
expect(schema[0].columns[0].type).to.equal('N/A')
|
||||
expect(schema[0].columns[1].name).to.equal('col2')
|
||||
expect(schema[0].columns[1].type).to.equal('integer')
|
||||
expect(schema[0].columns[2].name).to.equal('col3')
|
||||
expect(schema[0].columns[2].type).to.equal('decimal(5, 2)')
|
||||
expect(schema[0].columns[3].name).to.equal('col4')
|
||||
expect(schema[0].columns[3].type).to.equal('varchar(30)')
|
||||
})
|
||||
|
||||
it('creates schema with virtual table', () => {
|
||||
return initSqlJs(config)
|
||||
.then(SQL => {
|
||||
const database = new SQL.Database()
|
||||
database.run(`
|
||||
CREATE VIRTUAL TABLE test_virtual USING fts4(
|
||||
col1, col2,
|
||||
notindexed=col1, notindexed=col2,
|
||||
tokenize=unicode61 "tokenchars=.+#")
|
||||
`)
|
||||
it('creates schema with virtual table', async () => {
|
||||
const SQL = await getSQL
|
||||
const tempDb = new SQL.Database()
|
||||
tempDb.run(`
|
||||
CREATE VIRTUAL TABLE test_virtual USING fts4(
|
||||
col1, col2,
|
||||
notindexed=col1, notindexed=col2,
|
||||
tokenize=unicode61 "tokenchars=.+#")
|
||||
`)
|
||||
|
||||
const data = database.export()
|
||||
const buffer = new Blob([data])
|
||||
return db.loadDb(buffer)
|
||||
})
|
||||
.then(({ dbName, schema }) => {
|
||||
expect(schema[0].name).to.equal('test_virtual')
|
||||
expect(schema[0].columns[0].name).to.equal('col1')
|
||||
expect(schema[0].columns[0].type).to.equal('N/A')
|
||||
expect(schema[0].columns[1].name).to.equal('col2')
|
||||
expect(schema[0].columns[1].type).to.equal('N/A')
|
||||
})
|
||||
const data = tempDb.export()
|
||||
const buffer = new Blob([data])
|
||||
|
||||
const { schema } = await db.loadDb(buffer)
|
||||
expect(schema[0].name).to.equal('test_virtual')
|
||||
expect(schema[0].columns[0].name).to.equal('col1')
|
||||
expect(schema[0].columns[0].type).to.equal('N/A')
|
||||
expect(schema[0].columns[1].name).to.equal('col2')
|
||||
expect(schema[0].columns[1].type).to.equal('N/A')
|
||||
})
|
||||
|
||||
it('returns a query result', () => {
|
||||
return initSqlJs(config)
|
||||
.then(SQL => {
|
||||
const database = new SQL.Database()
|
||||
database.run(`
|
||||
CREATE TABLE test (
|
||||
id integer,
|
||||
name varchar(100),
|
||||
faculty varchar(100)
|
||||
);
|
||||
INSERT INTO test (id, name, faculty)
|
||||
VALUES
|
||||
( 1, 'Harry Potter', 'Griffindor'),
|
||||
( 2, 'Draco Malfoy', 'Slytherin');
|
||||
`)
|
||||
it('returns a query result', async () => {
|
||||
const SQL = await getSQL
|
||||
const tempDb = new SQL.Database()
|
||||
tempDb.run(`
|
||||
CREATE TABLE test (
|
||||
id integer,
|
||||
name varchar(100),
|
||||
faculty varchar(100)
|
||||
);
|
||||
INSERT INTO test (id, name, faculty)
|
||||
VALUES
|
||||
( 1, 'Harry Potter', 'Griffindor'),
|
||||
( 2, 'Draco Malfoy', 'Slytherin');
|
||||
`)
|
||||
|
||||
const data = database.export()
|
||||
const buffer = new Blob([data])
|
||||
return db.loadDb(buffer)
|
||||
})
|
||||
.then(({ dbName, schema }) => {
|
||||
return db.execute('SELECT * from test')
|
||||
})
|
||||
.then(result => {
|
||||
expect(result.columns).to.have.lengthOf(3)
|
||||
expect(result.columns[0]).to.equal('id')
|
||||
expect(result.columns[1]).to.equal('name')
|
||||
expect(result.columns[2]).to.equal('faculty')
|
||||
expect(result.values).to.have.lengthOf(2)
|
||||
expect(result.values[0][0]).to.equal(1)
|
||||
expect(result.values[0][1]).to.equal('Harry Potter')
|
||||
expect(result.values[0][2]).to.equal('Griffindor')
|
||||
expect(result.values[1][0]).to.equal(2)
|
||||
expect(result.values[1][1]).to.equal('Draco Malfoy')
|
||||
expect(result.values[1][2]).to.equal('Slytherin')
|
||||
})
|
||||
const data = tempDb.export()
|
||||
const buffer = new Blob([data])
|
||||
|
||||
await db.loadDb(buffer)
|
||||
const result = await db.execute('SELECT * from test')
|
||||
expect(result.columns).to.have.lengthOf(3)
|
||||
expect(result.columns[0]).to.equal('id')
|
||||
expect(result.columns[1]).to.equal('name')
|
||||
expect(result.columns[2]).to.equal('faculty')
|
||||
expect(result.values).to.have.lengthOf(2)
|
||||
expect(result.values[0][0]).to.equal(1)
|
||||
expect(result.values[0][1]).to.equal('Harry Potter')
|
||||
expect(result.values[0][2]).to.equal('Griffindor')
|
||||
expect(result.values[1][0]).to.equal(2)
|
||||
expect(result.values[1][1]).to.equal('Draco Malfoy')
|
||||
expect(result.values[1][2]).to.equal('Slytherin')
|
||||
})
|
||||
|
||||
it('returns an error', () => {
|
||||
return initSqlJs(config)
|
||||
.then(SQL => {
|
||||
const database = new SQL.Database()
|
||||
database.run(`
|
||||
CREATE TABLE test (
|
||||
id integer,
|
||||
name varchar(100),
|
||||
faculty varchar(100)
|
||||
);
|
||||
INSERT INTO test (id, name, faculty)
|
||||
VALUES
|
||||
( 1, 'Harry Potter', 'Griffindor'),
|
||||
( 2, 'Draco Malfoy', 'Slytherin');
|
||||
`)
|
||||
it('returns an error', async () => {
|
||||
const SQL = await getSQL
|
||||
const tempDb = new SQL.Database()
|
||||
tempDb.run(`
|
||||
CREATE TABLE test (
|
||||
id integer,
|
||||
name varchar(100),
|
||||
faculty varchar(100)
|
||||
);
|
||||
INSERT INTO test (id, name, faculty)
|
||||
VALUES
|
||||
( 1, 'Harry Potter', 'Griffindor'),
|
||||
( 2, 'Draco Malfoy', 'Slytherin');
|
||||
`)
|
||||
|
||||
const data = database.export()
|
||||
const buffer = new Blob([data])
|
||||
return db.loadDb(buffer)
|
||||
})
|
||||
.then(() => {
|
||||
return db.execute('SELECT * from foo')
|
||||
})
|
||||
.catch(result => {
|
||||
expect(result).to.equal('no such table: foo')
|
||||
})
|
||||
const data = tempDb.export()
|
||||
const buffer = new Blob([data])
|
||||
await db.loadDb(buffer)
|
||||
await expect(db.execute('SELECT * from foo')).to.be.rejectedWith(/^no such table: foo$/)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user