mirror of
https://github.com/lana-k/sqliteviz.git
synced 2025-12-06 18:18:53 +08:00
add tests for database module #27
This commit is contained in:
@@ -283,8 +283,8 @@ export default {
|
|||||||
}
|
}
|
||||||
let importLoadingIndicator = null
|
let importLoadingIndicator = null
|
||||||
|
|
||||||
const updateProgress = e => {
|
const updateProgress = progress => {
|
||||||
this.$set(importMsg, 'progress', e.detail)
|
this.$set(importMsg, 'progress', progress)
|
||||||
}
|
}
|
||||||
this.newDb = database.getNewDatabase()
|
this.newDb = database.getNewDatabase()
|
||||||
const progressCounterId = this.newDb.createProgressCounter(updateProgress)
|
const progressCounterId = this.newDb.createProgressCounter(updateProgress)
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ class Database {
|
|||||||
createProgressCounter (callback) {
|
createProgressCounter (callback) {
|
||||||
const id = progressCounterIds++
|
const id = progressCounterIds++
|
||||||
this.importProgresses[id] = new EventTarget()
|
this.importProgresses[id] = new EventTarget()
|
||||||
this.importProgresses[id].addEventListener('progress', callback)
|
this.importProgresses[id].addEventListener('progress', e => { callback(e.detail) })
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ describe('DbUploader.vue', () => {
|
|||||||
const db = {
|
const db = {
|
||||||
loadDb: sinon.stub().resolves(schema)
|
loadDb: sinon.stub().resolves(schema)
|
||||||
}
|
}
|
||||||
database.getNewDatabase = sinon.stub().returns(db)
|
sinon.stub(database, 'getNewDatabase').returns(db)
|
||||||
|
|
||||||
// mock router
|
// mock router
|
||||||
const $router = { push: sinon.stub() }
|
const $router = { push: sinon.stub() }
|
||||||
@@ -60,7 +60,7 @@ describe('DbUploader.vue', () => {
|
|||||||
const db = {
|
const db = {
|
||||||
loadDb: sinon.stub().resolves(schema)
|
loadDb: sinon.stub().resolves(schema)
|
||||||
}
|
}
|
||||||
database.getNewDatabase = sinon.stub().returns(db)
|
sinon.stub(database, 'getNewDatabase').returns(db)
|
||||||
|
|
||||||
// mock router
|
// mock router
|
||||||
const $router = { push: sinon.stub() }
|
const $router = { push: sinon.stub() }
|
||||||
@@ -104,7 +104,7 @@ describe('DbUploader.vue', () => {
|
|||||||
const db = {
|
const db = {
|
||||||
loadDb: sinon.stub().resolves(schema)
|
loadDb: sinon.stub().resolves(schema)
|
||||||
}
|
}
|
||||||
database.getNewDatabase = sinon.stub().returns(db)
|
sinon.stub(database, 'getNewDatabase').returns(db)
|
||||||
|
|
||||||
// mock router
|
// mock router
|
||||||
const $router = { push: sinon.stub() }
|
const $router = { push: sinon.stub() }
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ describe('csv.js', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('parse resolves', async () => {
|
it('parse resolves', async () => {
|
||||||
Papa.parse = sinon.stub().callsFake((file, config) => {
|
sinon.stub(Papa, 'parse').callsFake((file, config) => {
|
||||||
config.complete({
|
config.complete({
|
||||||
data: [
|
data: [
|
||||||
[1, 'foo'],
|
[1, 'foo'],
|
||||||
@@ -106,7 +106,7 @@ describe('csv.js', () => {
|
|||||||
|
|
||||||
it('parse rejects', async () => {
|
it('parse rejects', async () => {
|
||||||
const err = new Error('something went wrong')
|
const err = new Error('something went wrong')
|
||||||
Papa.parse = sinon.stub().callsFake((file, config) => {
|
sinon.stub(Papa, 'parse').callsFake((file, config) => {
|
||||||
config.error(err)
|
config.error(err)
|
||||||
})
|
})
|
||||||
const file = {}
|
const file = {}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import chai from 'chai'
|
import chai from 'chai'
|
||||||
|
import sinon from 'sinon'
|
||||||
import chaiAsPromised from 'chai-as-promised'
|
import chaiAsPromised from 'chai-as-promised'
|
||||||
import initSqlJs from 'sql.js'
|
import initSqlJs from 'sql.js'
|
||||||
import database from '@/database.js'
|
import database from '@/database.js'
|
||||||
@@ -6,10 +7,19 @@ chai.use(chaiAsPromised)
|
|||||||
const expect = chai.expect
|
const expect = chai.expect
|
||||||
chai.should()
|
chai.should()
|
||||||
|
|
||||||
const db = database.getNewDatabase()
|
|
||||||
const getSQL = initSqlJs()
|
const getSQL = initSqlJs()
|
||||||
|
let db
|
||||||
|
|
||||||
describe('database.js', () => {
|
describe('database.js', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
db = database.getNewDatabase()
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
db.shutDown()
|
||||||
|
sinon.restore()
|
||||||
|
})
|
||||||
|
|
||||||
it('creates schema', async () => {
|
it('creates schema', async () => {
|
||||||
const SQL = await getSQL
|
const SQL = await getSQL
|
||||||
const tempDb = new SQL.Database()
|
const tempDb = new SQL.Database()
|
||||||
@@ -57,6 +67,19 @@ describe('database.js', () => {
|
|||||||
expect(schema[0].columns[1].type).to.equal('N/A')
|
expect(schema[0].columns[1].type).to.equal('N/A')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('loadDb throws errors', async () => {
|
||||||
|
const SQL = await getSQL
|
||||||
|
const tempDb = new SQL.Database()
|
||||||
|
tempDb.run('CREATE TABLE test (col1, col2)')
|
||||||
|
|
||||||
|
const data = tempDb.export()
|
||||||
|
const buffer = new Blob([data])
|
||||||
|
|
||||||
|
sinon.stub(db.pw, 'postMessage').resolves({ error: new Error('foo') })
|
||||||
|
|
||||||
|
await expect(db.loadDb(buffer)).to.be.rejectedWith('foo')
|
||||||
|
})
|
||||||
|
|
||||||
it('returns a query result', async () => {
|
it('returns a query result', async () => {
|
||||||
const SQL = await getSQL
|
const SQL = await getSQL
|
||||||
const tempDb = new SQL.Database()
|
const tempDb = new SQL.Database()
|
||||||
@@ -110,4 +133,84 @@ describe('database.js', () => {
|
|||||||
await db.loadDb(buffer)
|
await db.loadDb(buffer)
|
||||||
await expect(db.execute('SELECT * from foo')).to.be.rejectedWith(/^no such table: foo$/)
|
await expect(db.execute('SELECT * from foo')).to.be.rejectedWith(/^no such table: foo$/)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('creates db', async () => {
|
||||||
|
const data = {
|
||||||
|
columns: ['id', 'name', 'faculty'],
|
||||||
|
values: [
|
||||||
|
[1, 'Harry Potter', 'Griffindor'],
|
||||||
|
[2, 'Draco Malfoy', 'Slytherin']
|
||||||
|
]
|
||||||
|
}
|
||||||
|
const progressHandler = sinon.spy()
|
||||||
|
const progressCounterId = db.createProgressCounter(progressHandler)
|
||||||
|
const { dbName, schema } = await db.createDb('foo', data, progressCounterId)
|
||||||
|
expect(dbName).to.equal('foo')
|
||||||
|
expect(schema).to.have.lengthOf(1)
|
||||||
|
expect(schema[0].name).to.equal('csv_import')
|
||||||
|
expect(schema[0].columns).to.have.lengthOf(3)
|
||||||
|
expect(schema[0].columns[0].name).to.equal('id')
|
||||||
|
expect(schema[0].columns[0].type).to.equal('real')
|
||||||
|
expect(schema[0].columns[1].name).to.equal('name')
|
||||||
|
expect(schema[0].columns[1].type).to.equal('text')
|
||||||
|
expect(schema[0].columns[2].name).to.equal('faculty')
|
||||||
|
expect(schema[0].columns[2].type).to.equal('text')
|
||||||
|
|
||||||
|
const result = await db.execute('SELECT * from csv_import')
|
||||||
|
expect(result.columns).to.eql(data.columns)
|
||||||
|
expect(result.values).to.eql(data.values)
|
||||||
|
|
||||||
|
expect(progressHandler.calledTwice).to.equal(true)
|
||||||
|
expect(progressHandler.firstCall.calledWith(0)).to.equal(true)
|
||||||
|
expect(progressHandler.secondCall.calledWith(100)).to.equal(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('createDb throws errors', async () => {
|
||||||
|
const data = {
|
||||||
|
columns: ['id', 'name'],
|
||||||
|
values: [
|
||||||
|
[1, 'Harry Potter', 'Griffindor'],
|
||||||
|
[2, 'Draco Malfoy', 'Slytherin']
|
||||||
|
]
|
||||||
|
}
|
||||||
|
const progressHandler = sinon.stub()
|
||||||
|
const progressCounterId = db.createProgressCounter(progressHandler)
|
||||||
|
await expect(db.createDb('foo', data, progressCounterId))
|
||||||
|
.to.be.rejectedWith('column index out of range')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('progressCounters', () => {
|
||||||
|
const firstHandler = sinon.stub()
|
||||||
|
const firstId = db.createProgressCounter(firstHandler)
|
||||||
|
db.worker.dispatchEvent(new MessageEvent('message', {
|
||||||
|
data: {
|
||||||
|
progress: 50,
|
||||||
|
id: firstId
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
expect(firstHandler.calledOnceWith(50)).to.equal(true)
|
||||||
|
|
||||||
|
const secondHandler = sinon.stub()
|
||||||
|
const secondId = db.createProgressCounter(secondHandler)
|
||||||
|
db.worker.dispatchEvent(new MessageEvent('message', {
|
||||||
|
data: {
|
||||||
|
progress: 70,
|
||||||
|
id: secondId
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
expect(firstId).to.not.equals(secondId)
|
||||||
|
expect(secondHandler.calledOnceWith(70)).to.equal(true)
|
||||||
|
|
||||||
|
db.worker.dispatchEvent(new MessageEvent('message', {
|
||||||
|
data: {
|
||||||
|
progress: 80,
|
||||||
|
id: firstId
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
expect(firstHandler.calledTwice).to.equal(true)
|
||||||
|
expect(firstHandler.secondCall.calledWith(80)).to.equal(true)
|
||||||
|
|
||||||
|
db.deleteProgressCounter(firstId)
|
||||||
|
expect(db.importProgresses[firstId]).to.equal(undefined)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user