1
0
mirror of https://github.com/lana-k/sqliteviz.git synced 2025-12-06 18:18:53 +08:00

Update schema view after script running #38

This commit is contained in:
lana-k
2021-04-27 15:54:51 +02:00
parent 453098b410
commit 35baaf2722
2 changed files with 79 additions and 17 deletions

View File

@@ -108,8 +108,11 @@ export default {
this.isGettingResults = true this.isGettingResults = true
this.result = null this.result = null
this.error = null this.error = null
const state = this.$store.state
try { try {
this.result = await this.$store.state.db.execute(this.query + ';') this.result = await state.db.execute(this.query + ';')
const schema = await state.db.getSchema(state.dbName)
this.$store.commit('saveSchema', schema)
} catch (err) { } catch (err) {
this.error = err this.error = err
} }

View File

@@ -6,6 +6,10 @@ import Vuex from 'vuex'
import Tab from '@/components/Tab.vue' import Tab from '@/components/Tab.vue'
describe('Tab.vue', () => { describe('Tab.vue', () => {
afterEach(() => {
sinon.restore()
})
it('Renders passed query', () => { it('Renders passed query', () => {
// mock store state // mock store state
const state = { const state = {
@@ -142,7 +146,7 @@ describe('Tab.vue', () => {
expect(state.tabs[0].isUnsaved).to.equal(true) expect(state.tabs[0].isUnsaved).to.equal(true)
}) })
it('Shows .result-in-progress message when executing query', (done) => { it('Shows .result-in-progress message when executing query', async () => {
// mock store state // mock store state
const state = { const state = {
currentTabId: 1, currentTabId: 1,
@@ -167,19 +171,17 @@ describe('Tab.vue', () => {
}) })
wrapper.vm.execute() wrapper.vm.execute()
wrapper.vm.$nextTick(() => { await wrapper.vm.$nextTick()
expect(wrapper.find('.table-view .result-before').isVisible()).to.equal(false) expect(wrapper.find('.table-view .result-before').isVisible()).to.equal(false)
expect(wrapper.find('.table-view .result-in-progress').isVisible()).to.equal(true) expect(wrapper.find('.table-view .result-in-progress').isVisible()).to.equal(true)
}) })
done()
})
it('Shows error when executing query ends with error', async () => { it('Shows error when executing query ends with error', async () => {
// mock store state // mock store state
const state = { const state = {
currentTabId: 1, currentTabId: 1,
db: { db: {
execute () { return Promise.reject(new Error('There is no table foo')) } execute: sinon.stub().rejects(new Error('There is no table foo'))
} }
} }
@@ -206,15 +208,6 @@ describe('Tab.vue', () => {
}) })
it('Passes result to sql-table component', async () => { it('Passes result to sql-table component', async () => {
// mock store state
const state = {
currentTabId: 1,
db: {
execute () { return Promise.resolve(result) }
}
}
const store = new Vuex.Store({ state, mutations })
const result = { const result = {
columns: ['id', 'name'], columns: ['id', 'name'],
values: [ values: [
@@ -222,6 +215,16 @@ describe('Tab.vue', () => {
[2, 'bar'] [2, 'bar']
] ]
} }
// mock store state
const state = {
currentTabId: 1,
db: {
execute: sinon.stub().resolves(result),
getSchema: sinon.stub().resolves({ dbName: '', schema: [] })
}
}
const store = new Vuex.Store({ state, mutations })
// mount the component // mount the component
const wrapper = mount(Tab, { const wrapper = mount(Tab, {
@@ -243,4 +246,60 @@ describe('Tab.vue', () => {
expect(wrapper.find('.table-preview.error').isVisible()).to.equal(false) expect(wrapper.find('.table-preview.error').isVisible()).to.equal(false)
expect(wrapper.findComponent({ name: 'SqlTable' }).vm.dataSet).to.eql(result) expect(wrapper.findComponent({ name: 'SqlTable' }).vm.dataSet).to.eql(result)
}) })
it('Updates schema after query execution', async () => {
const result = {
columns: ['id', 'name'],
values: []
}
const newSchema = {
dbName: 'fooDb',
schema: [
{
name: 'foo',
columns: [
{ name: 'id', type: 'INTEGER' },
{ name: 'title', type: 'NVARCHAR(30)' }
]
},
{
name: 'bar',
columns: [
{ name: 'a', type: 'N/A' },
{ name: 'b', type: 'N/A' }
]
}
]
}
// mock store state
const state = {
currentTabId: 1,
dbName: 'fooDb',
db: {
execute: sinon.stub().resolves(result),
getSchema: sinon.stub().resolves(newSchema)
}
}
sinon.spy(mutations, 'saveSchema')
const store = new Vuex.Store({ state, mutations })
// mount the component
const wrapper = mount(Tab, {
store,
stubs: ['chart'],
propsData: {
id: 1,
initName: 'foo',
initQuery: 'SELECT * FROM foo; CREATE TABLE bar(a,b);',
initChart: [],
tabIndex: 0,
isPredefined: false
}
})
await wrapper.vm.execute()
expect(state.db.getSchema.calledOnceWith('fooDb')).to.equal(true)
expect(mutations.saveSchema.calledOnceWith(state, newSchema)).to.equal(true)
})
}) })