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

Pivot implementation and redesign (#69)

- Pivot support implementation 
- Rename queries into inquiries
- Rename editor into workspace
- Change result set format
- New JSON format for inquiries
- Redesign panels
This commit is contained in:
lana-k
2021-08-04 22:20:51 +02:00
committed by GitHub
parent 8d0bc6affe
commit 5017b55944
105 changed files with 4659 additions and 2021 deletions

View File

@@ -34,10 +34,11 @@ describe('_sql.js', () => {
sql.open(data)
const result = sql.exec('SELECT * from test')
expect(result).to.have.lengthOf(1)
expect(result[0].columns).to.eql(['id', 'name', 'faculty'])
expect(result[0].values).to.have.lengthOf(2)
expect(result[0].values[0]).to.eql([1, 'Harry Potter', 'Griffindor'])
expect(result[0].values[1]).to.eql([2, 'Draco Malfoy', 'Slytherin'])
expect(result[0]).to.eql({
id: [1, 2],
name: ['Harry Potter', 'Draco Malfoy'],
faculty: ['Griffindor', 'Slytherin']
})
})
it('throws an error if query is empty', async () => {
@@ -63,26 +64,21 @@ describe('_sql.js', () => {
it('imports', async () => {
const data = {
columns: ['id', 'name'],
values: [
[1, 'Harry Potter'],
[2, 'Draco Malfoy'],
[3, 'Hermione Granger'],
[4, 'Ron Weasley']
id: [1, 2, 3, 4],
name: [
'Harry Potter',
'Draco Malfoy',
'Hermione Granger',
'Ron Weasley'
]
}
const progressCallback = sinon.stub()
const progressCounterId = 1
const sql = await Sql.build()
sql.import('foo', data.columns, data.values, progressCounterId, progressCallback, 2)
sql.import('foo', data, progressCounterId, progressCallback, 2)
const result = sql.exec('SELECT * from foo')
expect(result).to.have.lengthOf(1)
expect(result[0].columns).to.eql(['id', 'name'])
expect(result[0].values).to.have.lengthOf(4)
expect(result[0].values[0]).to.eql([1, 'Harry Potter'])
expect(result[0].values[1]).to.eql([2, 'Draco Malfoy'])
expect(result[0].values[2]).to.eql([3, 'Hermione Granger'])
expect(result[0].values[3]).to.eql([4, 'Ron Weasley'])
expect(result[0]).to.eql(data)
expect(progressCallback.calledThrice).to.equal(true)
expect(progressCallback.getCall(0).args[0]).to.eql({ progress: 0, id: 1 })
@@ -108,10 +104,11 @@ describe('_sql.js', () => {
anotherSql.open(data)
const result = anotherSql.exec('SELECT * from test')
expect(result).to.have.lengthOf(1)
expect(result[0].columns).to.eql(['id', 'name', 'faculty'])
expect(result[0].values).to.have.lengthOf(2)
expect(result[0].values[0]).to.eql([1, 'Harry Potter', 'Griffindor'])
expect(result[0].values[1]).to.eql([2, 'Draco Malfoy', 'Slytherin'])
expect(result[0]).to.eql({
id: [1, 2],
name: ['Harry Potter', 'Draco Malfoy'],
faculty: ['Griffindor', 'Slytherin']
})
})
it('closes', async () => {
@@ -149,22 +146,28 @@ describe('_sql.js', () => {
`)
let result = sql.exec('SELECT * from test')
expect(result[0].values).to.have.lengthOf(2)
expect(result[0]).to.eql({
id: [1, 2],
name: ['foo', 'bar']
})
const data = {
columns: ['id', 'name'],
values: [
[1, 'Harry Potter'],
[2, 'Draco Malfoy'],
[3, 'Hermione Granger'],
[4, 'Ron Weasley']
id: [1, 2, 3, 4],
name: [
'Harry Potter',
'Draco Malfoy',
'Hermione Granger',
'Ron Weasley'
]
}
// import adds table
sql.import('foo', data.columns, data.values, 1, sinon.stub(), 2)
sql.import('foo', data, 1, sinon.stub(), 2)
result = sql.exec('SELECT * from foo')
expect(result[0].values).to.have.lengthOf(4)
expect(result[0]).to.eql(data)
result = sql.exec('SELECT * from test')
expect(result[0].values).to.have.lengthOf(2)
expect(result[0]).to.eql({
id: [1, 2],
name: ['foo', 'bar']
})
})
})

View File

@@ -3,16 +3,18 @@ import stmts from '@/lib/database/_statements'
describe('_statements.js', () => {
it('generateChunks', () => {
const arr = ['1', '2', '3', '4', '5']
const source = {
id: ['1', '2', '3', '4', '5']
}
const size = 2
const chunks = stmts.generateChunks(arr, size)
const chunks = stmts.generateChunks(source, size)
const output = []
for (const chunk of chunks) {
output.push(chunk)
}
expect(output[0]).to.eql(['1', '2'])
expect(output[1]).to.eql(['3', '4'])
expect(output[2]).to.eql(['5'])
expect(output[0]).to.eql([['1'], ['2']])
expect(output[1]).to.eql([['3'], ['4']])
expect(output[2]).to.eql([['5']])
})
it('getInsertStmt', () => {
@@ -22,12 +24,14 @@ describe('_statements.js', () => {
})
it('getCreateStatement', () => {
const columns = ['id', 'name', 'isAdmin', 'startDate']
const values = [
[1, 'foo', true, new Date()],
[2, 'bar', false, new Date()]
]
expect(stmts.getCreateStatement('foo', columns, values)).to.equal(
const data = {
id: [1, 2],
name: ['foo', 'bar'],
isAdmin: [true, false],
startDate: [new Date(), new Date()]
}
expect(stmts.getCreateStatement('foo', data)).to.equal(
'CREATE table "foo"("id" REAL, "name" TEXT, "isAdmin" INTEGER, "startDate" TEXT);'
)
})

View File

@@ -25,10 +25,7 @@ describe('database.js', () => {
it('creates schema', async () => {
const SQL = await getSQL
const tempDb = new SQL.Database()
tempDb.run(`CREATE TABLE test (
col1,
col2 integer
)`)
tempDb.run('CREATE TABLE test (col1, col2 integer)')
const data = tempDb.export()
const buffer = new Blob([data])
@@ -88,11 +85,11 @@ describe('database.js', () => {
await db.loadDb(buffer)
const result = await db.execute('SELECT * from test limit 1; SELECT * from test;')
expect(result.columns).to.have.lengthOf(3)
expect(result.columns).to.eql(['id', 'name', 'faculty'])
expect(result.values).to.have.lengthOf(2)
expect(result.values[0]).to.eql([1, 'Harry Potter', 'Griffindor'])
expect(result.values[1]).to.eql([2, 'Draco Malfoy', 'Slytherin'])
expect(result).to.eql({
id: [1, 2],
name: ['Harry Potter', 'Draco Malfoy'],
faculty: ['Griffindor', 'Slytherin']
})
})
it('returns an error', async () => {
@@ -119,11 +116,9 @@ describe('database.js', () => {
it('adds table from csv', async () => {
const data = {
columns: ['id', 'name', 'faculty'],
values: [
[1, 'Harry Potter', 'Griffindor'],
[2, 'Draco Malfoy', 'Slytherin']
]
id: [1, 2],
name: ['Harry Potter', 'Draco Malfoy'],
faculty: ['Griffindor', 'Slytherin']
}
const progressHandler = sinon.spy()
const progressCounterId = db.createProgressCounter(progressHandler)
@@ -140,8 +135,7 @@ describe('database.js', () => {
expect(db.schema[0].columns[2]).to.eql({ name: 'faculty', type: 'text' })
const result = await db.execute('SELECT * from foo')
expect(result.columns).to.eql(data.columns)
expect(result.values).to.eql(data.values)
expect(result).to.eql(data)
expect(progressHandler.calledTwice).to.equal(true)
expect(progressHandler.firstCall.calledWith(0)).to.equal(true)
@@ -150,16 +144,13 @@ describe('database.js', () => {
it('addTableFromCsv throws errors', async () => {
const data = {
columns: ['id', 'name'],
values: [
[1, 'Harry Potter', 'Griffindor'],
[2, 'Draco Malfoy', 'Slytherin']
]
id: [1, 2],
name: ['Harry Potter', 'Draco Malfoy'],
faculty: null
}
const progressHandler = sinon.stub()
const progressCounterId = db.createProgressCounter(progressHandler)
await expect(db.addTableFromCsv('foo', data, progressCounterId))
.to.be.rejectedWith('column index out of range')
await expect(db.addTableFromCsv('foo', data, progressCounterId)).to.be.rejected
})
it('progressCounters', () => {
@@ -222,9 +213,10 @@ describe('database.js', () => {
// check that new db works and has the same table and data
result = await anotherDb.execute('SELECT * from foo')
expect(result.columns).to.eql(['id', 'name'])
expect(result.values).to.have.lengthOf(1)
expect(result.values[0]).to.eql([1, 'Harry Potter'])
expect(result).to.eql({
id: [1],
name: ['Harry Potter']
})
})
it('sanitizeTableName', () => {

View File

@@ -36,7 +36,27 @@ describe('SQLite extensions', function () {
abs(pi() - radians(180)) < 0.000001,
abs(pi() / 2 - atan2(1, 0)) < 0.000001
`)
expect(actual.values).to.eql([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
expect(actual).to.eql({
'abs(3.1415926 - pi()) < 0.000001': [1],
'abs(1 - cos(2 * pi())) < 0.000001': [1],
'abs(0 - sin(pi())) < 0.000001': [1],
'abs(0 - tan(0)) < 0.000001': [1],
'abs(0 - cot(pi() / 2)) < 0.000001': [1],
'abs(1 - acos(cos(1))) < 0.000001': [1],
'abs(1 - asin(sin(1))) < 0.000001': [1],
'abs(1 - atan(tan(1))) < 0.000001': [1],
'abs(1 - cosh(0)) < 0.000001': [1],
'abs(0 - sinh(0)) < 0.000001': [1],
'abs(tanh(1) + tanh(-1)) < 0.000001': [1],
'abs(coth(1) + coth(-1)) < 0.000001': [1],
'abs(1 - acosh(cosh(1))) < 0.000001': [1],
'abs(1 - asinh(sinh(1))) < 0.000001': [1],
'abs(1 - atanh(tanh(1))) < 0.000001': [1],
'abs(180 - degrees(pi())) < 0.000001': [1],
'abs(pi() - radians(180)) < 0.000001': [1],
'abs(pi() / 2 - atan2(1, 0)) < 0.000001': [1]
})
})
it('supports contrib math functions', async function () {
@@ -51,7 +71,17 @@ describe('SQLite extensions', function () {
ceil(-1.95) + ceil(1.95),
floor(-1.95) + floor(1.95)
`)
expect(actual.values).to.eql([[1, 1, 4, 8, 0, 16, 1, -1]])
expect(actual).to.eql({
'exp(0)': [1],
'log(exp(1))': [1],
'log10(10000)': [4],
'power(2, 3)': [8],
'sign(-10) + sign(20)': [0],
'sqrt(square(16))': [16],
'ceil(-1.95) + ceil(1.95)': [1],
'floor(-1.95) + floor(1.95)': [-1]
})
})
it('supports contrib string functions', async function () {
@@ -69,9 +99,19 @@ describe('SQLite extensions', function () {
padc('foo', 5),
strfilter('abcba', 'bc')
`)
expect(actual.values).to.eql([
['abababab', 7, 0, 'fo', 'ar', 'raboof', 'Foobar', ' foo', 'foo ', ' foo ', 'bcb']
])
expect(actual).to.eql({
"replicate('ab', 4)": ['abababab'],
"charindex('ab', 'foobarabbarfoo')": [7],
"charindex('ab', 'foobarabbarfoo', 8)": [0],
"leftstr('foobar', 2)": ['fo'],
"rightstr('foobar', 2)": ['ar'],
"reverse('foobar')": ['raboof'],
"proper('fooBar')": ['Foobar'],
"padl('foo', 5)": [' foo'],
"padr('foo', 5)": ['foo '],
"padc('foo', 5)": [' foo '],
"strfilter('abcba', 'bc')": ['bcb']
})
})
it('supports contrib aggregate functions', async function () {
@@ -97,7 +137,14 @@ describe('SQLite extensions', function () {
VALUES (1)
)
`)
expect(actual.values).to.eql([[1, 1, 1, 6, 3, 9]])
expect(actual).to.eql({
'abs( 3.77406806 - stdev(x)) < 0.000001': [1],
'abs(14.24358974 - variance(x)) < 0.000001': [1],
'mode(x)': [1],
'median(x)': [6],
'lower_quartile(x)': [3],
'upper_quartile(x)': [9]
})
})
it('supports generate_series', async function () {
@@ -105,7 +152,9 @@ describe('SQLite extensions', function () {
SELECT value
FROM generate_series(5, 20, 5)
`)
expect(actual.values).to.eql([[5], [10], [15], [20]])
expect(actual).to.eql({
value: [5, 10, 15, 20]
})
})
it('supports transitive_closure', async function () {
@@ -145,33 +194,42 @@ describe('SQLite extensions', function () {
WHERE nc.root = 2 AND nc.depth = 2
);
`)
expect(actual.values).to.eql([
['_sql.spec.js'],
['_statements.spec.js'],
['database.spec.js'],
['sqliteExtensions.spec.js'],
['fileIo.spec.js'],
['time.spec.js']
])
expect(actual).to.eql({
name: [
'_sql.spec.js',
'_statements.spec.js',
'database.spec.js',
'sqliteExtensions.spec.js',
'fileIo.spec.js',
'time.spec.js'
]
})
})
it('supports UUID functions', async function () {
const actual = await db.execute(`
SELECT
length(uuid()),
uuid_str(uuid_blob('26a8349c8a7f4cbeb519bf792c3d7ac6'))
length(uuid()) as length,
uuid_str(uuid_blob('26a8349c8a7f4cbeb519bf792c3d7ac6')) as uid
`)
expect(actual.values).to.eql([[36, '26a8349c-8a7f-4cbe-b519-bf792c3d7ac6']])
expect(actual).to.eql({
length: [36],
uid: ['26a8349c-8a7f-4cbe-b519-bf792c3d7ac6']
})
})
it('supports regexp', async function () {
const actual = await db.execute(`
SELECT
regexp('=\\s?\\d+', 'const foo = 123; const bar = "bar"'),
regexpi('=\\s?\\d+', 'const foo = 123; const bar = "bar"'),
'const foo = 123; const bar = "bar"' REGEXP '=\\s?\\d+'
regexp('=\\s?\\d+', 'const foo = 123; const bar = "bar"') as one,
regexpi('=\\s?\\d+', 'const foo = 123; const bar = "bar"') as two,
'const foo = 123; const bar = "bar"' REGEXP '=\\s?\\d+' as three
`)
expect(actual.values).to.eql([[1, 1, 1]])
expect(actual).to.eql({
one: [1],
two: [1],
three: [1]
})
})
it('supports pivot virtual table', async function () {
@@ -202,12 +260,13 @@ describe('SQLite extensions', function () {
ALTER TABLE surface DROP COLUMN rownum;
SELECT * FROM surface;
`)
expect(actual.columns).to.eql(['x', 'y', '5.0', '10.0', '15.0'])
expect(actual.values).to.eql([
[5, 3, 3.2, 4, 4.8],
[10, 6, 4.3, 3.8, 4],
[15, 9, 5.4, 3.6, 3.5]
])
expect(actual).to.eql({
x: [5, 10, 15],
y: [3, 6, 9],
'5.0': [3.2, 4.3, 5.4],
'10.0': [4, 3.8, 3.6],
'15.0': [4.8, 4, 3.5]
})
})
it('supports FTS5', async function () {
@@ -233,6 +292,8 @@ describe('SQLite extensions', function () {
WHERE body MATCH '"full-text" NOT document'
ORDER BY rank;
`)
expect(actual.values).to.eql([['bar@localhost']])
expect(actual).to.eql({
sender: ['bar@localhost']
})
})
})

View File

@@ -0,0 +1,42 @@
import { expect } from 'chai'
import migrations from '@/lib/storedInquiries/_migrations'
describe('_migrations.js', () => {
it('migrates from version 1 to the current', () => {
const oldInquiries = [
{
id: '123',
name: 'foo',
query: 'SELECT * FROM foo',
chart: { here_are: 'foo chart settings' },
createdAt: '2021-05-06T11:05:50.877Z'
},
{
id: '456',
name: 'bar',
query: 'SELECT * FROM bar',
chart: { here_are: 'bar chart settings' },
createdAt: '2021-05-07T11:05:50.877Z'
}
]
expect(migrations._migrate(1, oldInquiries)).to.eql([
{
id: '123',
name: 'foo',
query: 'SELECT * FROM foo',
viewType: 'chart',
viewOptions: { here_are: 'foo chart settings' },
createdAt: '2021-05-06T11:05:50.877Z'
},
{
id: '456',
name: 'bar',
query: 'SELECT * FROM bar',
viewType: 'chart',
viewOptions: { here_are: 'bar chart settings' },
createdAt: '2021-05-07T11:05:50.877Z'
}
])
})
})

View File

@@ -0,0 +1,432 @@
import { expect } from 'chai'
import sinon from 'sinon'
import storedInquiries from '@/lib/storedInquiries'
import fu from '@/lib/utils/fileIo'
describe('storedInquiries.js', () => {
beforeEach(() => {
localStorage.removeItem('myInquiries')
localStorage.removeItem('myQueries')
})
afterEach(() => {
sinon.restore()
})
it('getStoredInquiries returns emplty array when storage is empty', () => {
const inquiries = storedInquiries.getStoredInquiries()
expect(inquiries).to.eql([])
})
it('getStoredInquiries migrate and returns inquiries of v1', () => {
localStorage.setItem('myQueries', JSON.stringify([
{
id: '123',
name: 'foo',
query: 'SELECT * FROM foo',
chart: { here_are: 'foo chart settings' }
},
{
id: '456',
name: 'bar',
query: 'SELECT * FROM bar',
chart: { here_are: 'bar chart settings' }
}
]))
const inquiries = storedInquiries.getStoredInquiries()
expect(inquiries).to.eql([
{
id: '123',
name: 'foo',
query: 'SELECT * FROM foo',
viewType: 'chart',
viewOptions: { here_are: 'foo chart settings' }
},
{
id: '456',
name: 'bar',
query: 'SELECT * FROM bar',
viewType: 'chart',
viewOptions: { here_are: 'bar chart settings' }
}
])
})
it('updateStorage and getStoredInquiries', () => {
const data = [
{ id: 1 },
{ id: 2 }
]
storedInquiries.updateStorage(data)
const inquiries = storedInquiries.getStoredInquiries()
expect(inquiries).to.eql(data)
})
it('duplicateInquiry', () => {
const now = new Date()
const nowPlusMinute = new Date(now.getTime() + 60 * 1000)
const base = {
id: 1,
name: 'foo',
query: 'SELECT * from foo',
viewType: 'chart',
viewOptions: [],
createdAt: new Date(2021, 0, 1),
isPredefined: true
}
const copy = storedInquiries.duplicateInquiry(base)
expect(copy).to.have.property('id').which.not.equal(base.id)
expect(copy).to.have.property('name').which.equal(base.name + ' Copy')
expect(copy).to.have.property('query').which.equal(base.query)
expect(copy).to.have.property('viewType').which.equal(base.viewType)
expect(copy).to.have.property('viewOptions').which.eql(base.viewOptions)
expect(copy).to.have.property('createdAt').which.within(now, nowPlusMinute)
expect(copy).to.not.have.property('isPredefined')
})
it('isTabNeedName returns false when the inquiry has a name and is not predefined', () => {
const tab = {
initName: 'foo'
}
expect(storedInquiries.isTabNeedName(tab)).to.equal(false)
})
it('isTabNeedName returns true when the inquiry has no name and is not predefined', () => {
const tab = {
initName: null,
tempName: 'Untitled'
}
expect(storedInquiries.isTabNeedName(tab)).to.equal(true)
})
it('isTabNeedName returns true when the inquiry is predefined', () => {
const tab = {
initName: 'foo',
isPredefined: true
}
expect(storedInquiries.isTabNeedName(tab)).to.equal(true)
})
it('serialiseInquiries', () => {
const inquiryList = [
{
id: 1,
name: 'foo',
query: 'SELECT from foo',
viewType: 'chart',
viewOptions: [],
createdAt: '2020-11-03T14:17:49.524Z',
isPredefined: true
},
{
id: 2,
name: 'bar',
query: 'SELECT from bar',
viewType: 'chart',
viewOptions: [],
createdAt: '2020-12-03T14:17:49.524Z'
}
]
const str = storedInquiries.serialiseInquiries(inquiryList)
const parsedJson = JSON.parse(str)
expect(parsedJson.version).to.equal(2)
expect(parsedJson.inquiries).to.have.lengthOf(2)
expect(parsedJson.inquiries[1]).to.eql(inquiryList[1])
expect(parsedJson.inquiries[0]).to.eql({
id: 1,
name: 'foo',
query: 'SELECT from foo',
viewType: 'chart',
viewOptions: [],
createdAt: '2020-11-03T14:17:49.524Z'
})
})
it('deserialiseInquiries migrates inquiries', () => {
const str = `[
{
"id": 1,
"name": "foo",
"query": "select * from foo",
"chart": [],
"createdAt": "2020-11-03T14:17:49.524Z"
},
{
"id": 2,
"name": "bar",
"query": "select * from bar",
"chart": [],
"createdAt": "2020-11-04T14:17:49.524Z"
}
]`
const inquiry = storedInquiries.deserialiseInquiries(str)
expect(inquiry).to.eql([
{
id: 1,
name: 'foo',
query: 'select * from foo',
viewType: 'chart',
viewOptions: [],
createdAt: '2020-11-03T14:17:49.524Z'
},
{
id: 2,
name: 'bar',
query: 'select * from bar',
viewType: 'chart',
viewOptions: [],
createdAt: '2020-11-04T14:17:49.524Z'
}
])
})
it('deserialiseInquiries return array for one inquiry of v1', () => {
const str = `
{
"id": 1,
"name": "foo",
"query": "select * from foo",
"chart": [],
"createdAt": "2020-11-03T14:17:49.524Z"
}
`
const inquiry = storedInquiries.deserialiseInquiries(str)
expect(inquiry).to.eql([{
id: 1,
name: 'foo',
query: 'select * from foo',
viewType: 'chart',
viewOptions: [],
createdAt: '2020-11-03T14:17:49.524Z'
}])
})
it('deserialiseInquiries generates new id to avoid duplication', () => {
storedInquiries.updateStorage([{ id: 1 }])
const str = `{
"version": 2,
"inquiries": [
{
"id": 1,
"name": "foo",
"query": "select * from foo",
"viewType": "chart",
"viewOptions": [],
"createdAt": "2020-11-03T14:17:49.524Z"
},
{
"id": 2,
"name": "bar",
"query": "select * from bar",
"viewType": "chart",
"viewOptions": [],
"createdAt": "2020-11-04T14:17:49.524Z"
}
]
}`
const inquiries = storedInquiries.deserialiseInquiries(str)
const parsedStr = JSON.parse(str)
expect(inquiries[1]).to.eql(parsedStr.inquiries[1])
expect(inquiries[0].id).to.not.equal(parsedStr.inquiries[0].id)
expect(inquiries[0].id).to.not.equal(parsedStr.inquiries[0].id)
expect(inquiries[0].name).to.equal(parsedStr.inquiries[0].name)
expect(inquiries[0].query).to.equal(parsedStr.inquiries[0].query)
expect(inquiries[0].viewType).to.equal(parsedStr.inquiries[0].viewType)
expect(inquiries[0].viewOptions).to.eql(parsedStr.inquiries[0].viewOptions)
expect(inquiries[0].createdAt).to.equal(parsedStr.inquiries[0].createdAt)
})
it('importInquiries v1', async () => {
const str = `
{
"id": 1,
"name": "foo",
"query": "select * from foo",
"chart": [],
"createdAt": "2020-11-03T14:17:49.524Z"
}
`
sinon.stub(fu, 'importFile').returns(Promise.resolve(str))
const inquiries = await storedInquiries.importInquiries()
expect(inquiries).to.eql([{
id: 1,
name: 'foo',
query: 'select * from foo',
viewType: 'chart',
viewOptions: [],
createdAt: '2020-11-03T14:17:49.524Z'
}])
})
it('importInquiries', async () => {
const str = `{
"version": 2,
"inquiries": [{
"id": 1,
"name": "foo",
"query": "select * from foo",
"viewType": "chart",
"viewOptions": [],
"createdAt": "2020-11-03T14:17:49.524Z"
}]
}`
sinon.stub(fu, 'importFile').returns(Promise.resolve(str))
const inquiries = await storedInquiries.importInquiries()
expect(inquiries).to.eql([{
id: 1,
name: 'foo',
query: 'select * from foo',
viewType: 'chart',
viewOptions: [],
createdAt: '2020-11-03T14:17:49.524Z'
}])
})
it('readPredefinedInquiries old', async () => {
const str = `[
{
"id": 1,
"name": "foo",
"query": "select * from foo",
"chart": [],
"createdAt": "2020-11-03T14:17:49.524Z"
}]
`
sinon.stub(fu, 'readFile').returns(Promise.resolve(new Response(str)))
const inquiries = await storedInquiries.readPredefinedInquiries()
expect(fu.readFile.calledOnceWith('./inquiries.json')).to.equal(true)
expect(inquiries).to.eql([
{
id: 1,
name: 'foo',
query: 'select * from foo',
viewType: 'chart',
viewOptions: [],
createdAt: '2020-11-03T14:17:49.524Z'
}])
})
it('readPredefinedInquiries', async () => {
const str = `{
"version": 2,
"inquiries": [
{
"id": 1,
"name": "foo",
"query": "select * from foo",
"viewType": "chart",
"viewOptions": [],
"createdAt": "2020-11-03T14:17:49.524Z"
}]
}
`
sinon.stub(fu, 'readFile').returns(Promise.resolve(new Response(str)))
const inquiries = await storedInquiries.readPredefinedInquiries()
expect(fu.readFile.calledOnceWith('./inquiries.json')).to.equal(true)
expect(inquiries).to.eql([
{
id: 1,
name: 'foo',
query: 'select * from foo',
viewType: 'chart',
viewOptions: [],
createdAt: '2020-11-03T14:17:49.524Z'
}])
})
it('save adds new inquiry in the storage', () => {
const now = new Date()
const nowPlusMinute = new Date(now.getTime() + 60 * 1000)
const tab = {
id: 1,
query: 'select * from foo',
viewType: 'chart',
viewOptions: [],
initName: null,
$refs: {
dataView: {
getOptionsForSave () {
return ['chart']
}
}
}
}
const value = storedInquiries.save(tab, 'foo')
expect(value.id).to.equal(tab.id)
expect(value.name).to.equal('foo')
expect(value.query).to.equal(tab.query)
expect(value.viewOptions).to.eql(['chart'])
expect(value).to.have.property('createdAt').which.within(now, nowPlusMinute)
const inquiries = storedInquiries.getStoredInquiries()
expect(JSON.stringify(inquiries)).to.equal(JSON.stringify([value]))
})
it('save updates existing inquiry in the storage', () => {
const tab = {
id: 1,
query: 'select * from foo',
viewType: 'chart',
viewOptions: [],
initName: null,
$refs: {
dataView: {
getOptionsForSave () {
return ['chart']
}
}
}
}
const first = storedInquiries.save(tab, 'foo')
tab.initName = 'foo'
tab.query = 'select * from foo'
storedInquiries.save(tab)
const inquiries = storedInquiries.getStoredInquiries()
const second = inquiries[0]
expect(inquiries).has.lengthOf(1)
expect(second.id).to.equal(first.id)
expect(second.name).to.equal(first.name)
expect(second.query).to.equal(tab.query)
expect(second.viewOptions).to.eql(['chart'])
expect(new Date(second.createdAt).getTime()).to.equal(first.createdAt.getTime())
})
it("save adds a new inquiry with new id if it's based on predefined inquiry", () => {
const now = new Date()
const nowPlusMinute = new Date(now.getTime() + 60 * 1000)
const tab = {
id: 1,
query: 'select * from foo',
viewType: 'chart',
viewOptions: [],
initName: 'foo predefined',
$refs: {
dataView: {
getOptionsForSave () {
return ['chart']
}
}
},
isPredefined: true
}
storedInquiries.save(tab, 'foo')
const inquiries = storedInquiries.getStoredInquiries()
expect(inquiries).has.lengthOf(1)
expect(inquiries[0]).to.have.property('id').which.not.equal(tab.id)
expect(inquiries[0].name).to.equal('foo')
expect(inquiries[0].query).to.equal(tab.query)
expect(inquiries[0].viewOptions).to.eql(['chart'])
expect(new Date(inquiries[0].createdAt)).to.be.within(now, nowPlusMinute)
})
})

View File

@@ -1,267 +0,0 @@
import { expect } from 'chai'
import sinon from 'sinon'
import storedQueries from '@/lib/storedQueries'
import fu from '@/lib/utils/fileIo'
describe('storedQueries.js', () => {
beforeEach(() => {
localStorage.removeItem('myQueries')
})
afterEach(() => {
sinon.restore()
})
it('getStoredQueries returns emplty array when storage is empty', () => {
const queries = storedQueries.getStoredQueries()
expect(queries).to.eql([])
})
it('updateStorage and getStoredQueries', () => {
const data = [
{ id: 1 },
{ id: 2 }
]
storedQueries.updateStorage(data)
const queries = storedQueries.getStoredQueries()
expect(queries).to.eql(data)
})
it('duplicateQuery', () => {
const now = new Date()
const nowPlusMinute = new Date(now.getTime() + 60 * 1000)
const base = {
id: 1,
name: 'foo',
query: 'SELECT * from foo',
chart: [],
createdAt: new Date(2021, 0, 1),
isPredefined: true
}
const copy = storedQueries.duplicateQuery(base)
expect(copy).to.have.property('id').which.not.equal(base.id)
expect(copy).to.have.property('name').which.equal(base.name + ' Copy')
expect(copy).to.have.property('query').which.equal(base.query)
expect(copy).to.have.property('chart').which.eql(base.chart)
expect(copy).to.have.property('createdAt').which.within(now, nowPlusMinute)
expect(copy).to.not.have.property('isPredefined')
})
it('isTabNeedName returns false when the query has a name and is not predefined', () => {
const tab = {
initName: 'foo'
}
expect(storedQueries.isTabNeedName(tab)).to.equal(false)
})
it('isTabNeedName returns true when the query has no name and is not predefined', () => {
const tab = {
initName: null,
tempName: 'Untitled'
}
expect(storedQueries.isTabNeedName(tab)).to.equal(true)
})
it('isTabNeedName returns true when the qiery is predefined', () => {
const tab = {
initName: 'foo',
isPredefined: true
}
expect(storedQueries.isTabNeedName(tab)).to.equal(true)
})
it('serialiseQueries', () => {
const queryList = [
{
id: 1,
name: 'foo',
query: 'SELECT from foo',
chart: [],
createdAt: '2020-11-03T14:17:49.524Z',
isPredefined: true
},
{
id: 2,
name: 'bar',
query: 'SELECT from bar',
chart: [],
createdAt: '2020-12-03T14:17:49.524Z'
}
]
const str = storedQueries.serialiseQueries(queryList)
const parsedJson = JSON.parse(str)
expect(parsedJson).to.have.lengthOf(2)
expect(parsedJson[1]).to.eql(queryList[1])
expect(parsedJson[0].id).to.equal(queryList[0].id)
expect(parsedJson[0].name).to.equal(queryList[0].name)
expect(parsedJson[0].query).to.equal(queryList[0].query)
expect(parsedJson[0].chart).to.eql(queryList[0].chart)
expect(parsedJson[0].createdAt).to.eql(queryList[0].createdAt)
expect(parsedJson[0].chart).to.not.have.property('isPredefined')
})
it('deserialiseQueries return array for one query', () => {
const str = `
{
"id": 1,
"name": "foo",
"query": "select * from foo",
"chart": [],
"createdAt": "2020-11-03T14:17:49.524Z"
}
`
const query = storedQueries.deserialiseQueries(str)
expect(query).to.eql([JSON.parse(str)])
})
it('deserialiseQueries generates new id to avoid duplication', () => {
storedQueries.updateStorage([{ id: 1 }])
const str = `[
{
"id": 1,
"name": "foo",
"query": "select * from foo",
"chart": [],
"createdAt": "2020-11-03T14:17:49.524Z"
},
{
"id": 2,
"name": "bar",
"query": "select * from bar",
"chart": [],
"createdAt": "2020-11-04T14:17:49.524Z"
}
]`
const queries = storedQueries.deserialiseQueries(str)
const parsedStr = JSON.parse(str)
expect(queries[1]).to.eql(parsedStr[1])
expect(queries[0].id).to.not.equal(parsedStr[0].id)
expect(queries[0]).to.have.property('id')
expect(queries[0].id).to.not.equal(parsedStr[0].id)
expect(queries[0].name).to.equal(parsedStr[0].name)
expect(queries[0].query).to.equal(parsedStr[0].query)
expect(queries[0].chart).to.eql(parsedStr[0].chart)
expect(queries[0].createdAt).to.equal(parsedStr[0].createdAt)
})
it('importQueries', async () => {
const str = `
{
"id": 1,
"name": "foo",
"query": "select * from foo",
"chart": [],
"createdAt": "2020-11-03T14:17:49.524Z"
}
`
sinon.stub(fu, 'importFile').returns(Promise.resolve(str))
const queries = await storedQueries.importQueries()
expect(queries).to.eql([JSON.parse(str)])
})
it('readPredefinedQueries', async () => {
const str = `
{
"id": 1,
"name": "foo",
"query": "select * from foo",
"chart": [],
"createdAt": "2020-11-03T14:17:49.524Z"
}
`
sinon.stub(fu, 'readFile').returns(Promise.resolve(new Response(str)))
const queries = await storedQueries.readPredefinedQueries()
expect(fu.readFile.calledOnceWith('./queries.json')).to.equal(true)
expect(queries).to.eql(JSON.parse(str))
})
it('save adds new query in the storage', () => {
const now = new Date()
const nowPlusMinute = new Date(now.getTime() + 60 * 1000)
const tab = {
id: 1,
query: 'select * from foo',
chart: [],
initName: null,
$refs: {
chart: {
getChartStateForSave () {
return ['chart']
}
}
}
}
const value = storedQueries.save(tab, 'foo')
expect(value.id).to.equal(tab.id)
expect(value.name).to.equal('foo')
expect(value.query).to.equal(tab.query)
expect(value.chart).to.eql(['chart'])
expect(value).to.have.property('createdAt').which.within(now, nowPlusMinute)
const queries = storedQueries.getStoredQueries()
expect(JSON.stringify(queries)).to.equal(JSON.stringify([value]))
})
it('save updates existing query in the storage', () => {
const tab = {
id: 1,
query: 'select * from foo',
chart: [],
initName: null,
$refs: {
chart: {
getChartStateForSave () {
return ['chart']
}
}
}
}
const first = storedQueries.save(tab, 'foo')
tab.initName = 'foo'
tab.query = 'select * from foo'
storedQueries.save(tab)
const queries = storedQueries.getStoredQueries()
const second = queries[0]
expect(queries).has.lengthOf(1)
expect(second.id).to.equal(first.id)
expect(second.name).to.equal(first.name)
expect(second.query).to.equal(tab.query)
expect(second.chart).to.eql(['chart'])
expect(new Date(second.createdAt).getTime()).to.equal(first.createdAt.getTime())
})
it("save adds a new query with new id if it's based on predefined query", () => {
const now = new Date()
const nowPlusMinute = new Date(now.getTime() + 60 * 1000)
const tab = {
id: 1,
query: 'select * from foo',
chart: [],
initName: 'foo predefined',
$refs: {
chart: {
getChartStateForSave () {
return ['chart']
}
}
},
isPredefined: true
}
storedQueries.save(tab, 'foo')
const queries = storedQueries.getStoredQueries()
expect(queries).has.lengthOf(1)
expect(queries[0]).to.have.property('id').which.not.equal(tab.id)
expect(queries[0].name).to.equal('foo')
expect(queries[0].query).to.equal(tab.query)
expect(queries[0].chart).to.eql(['chart'])
expect(new Date(queries[0].createdAt)).to.be.within(now, nowPlusMinute)
})
})