-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: add test for sql-parser/save-helper/core #24
Changes from 11 commits
d0163c2
35267b0
0435a6c
bfc859f
b5f793a
232e492
081612e
6bc58ed
98fc9c9
5c6cbac
9390a7d
1d49a8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { createSQLEditorInstance, EditorCache } from '..' | ||
|
||
test('editor instance cache works fine', () => { | ||
const cache = new EditorCache() | ||
const editorInst = createSQLEditorInstance({ | ||
editorId: '111', | ||
doc: 'select * from test;' | ||
}) | ||
|
||
expect(cache.getEditor('111')).toBe(undefined) | ||
|
||
cache.addEditor('111', editorInst) | ||
expect(cache.getEditor('111')).toBe(editorInst) | ||
|
||
cache.deleteEditor('222') | ||
expect(cache.getEditor('111')).toBe(editorInst) | ||
|
||
cache.deleteEditor('111') | ||
expect(cache.getEditor('111')).toBe(undefined) | ||
|
||
cache.addEditor('111', editorInst) | ||
expect(cache.getEditor('111')).toBe(editorInst) | ||
|
||
cache.clearEditors() | ||
expect(cache.getEditor('111')).toBe(undefined) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,64 @@ | ||
import { createSQLEditorInstance } from '../editor-instance' | ||
import { createSQLEditorInstance } from '..' | ||
|
||
function sum(a: number, b: number) { | ||
return a + b | ||
} | ||
const LINE_1 = `USE sp500insight;` | ||
const LINE_2 = `SELECT sector, industry, COUNT(*) AS companies | ||
FROM companies c | ||
WHERE c.stock_symbol IN (SELECT stock_symbol FROM index_compositions WHERE index_symbol = "SP500") | ||
GROUP BY sector, industry | ||
ORDER BY sector, companies DESC;` | ||
|
||
test('adds 1 + 2 to equal 3', () => { | ||
expect(sum(1, 2)).toBe(3) | ||
}) | ||
const DOC = `\n${LINE_1}\n\n${LINE_2}\n\n` | ||
|
||
test('create instance successfully', () => { | ||
const instance = createSQLEditorInstance({ | ||
test('editor instance works fine', () => { | ||
const editorInst = createSQLEditorInstance({ | ||
editorId: '111', | ||
doc: '222' | ||
doc: DOC | ||
}) | ||
|
||
expect(editorInst.editorId).toBe('111') | ||
|
||
editorInst.editorView.dispatch({ selection: { anchor: 0, head: 0 } }) | ||
|
||
const allSql = editorInst.getAllStatements() | ||
expect(allSql.length).toBe(2) | ||
expect(allSql[0].content).toBe(LINE_1) | ||
expect(allSql[1].content).toBe(LINE_2) | ||
|
||
let curSql = editorInst.getCurStatements() | ||
expect(curSql[0].content).toBe('') | ||
|
||
let nearbySql = editorInst.getNearbyStatement() | ||
expect(nearbySql?.content).toBe(LINE_1) | ||
|
||
editorInst.editorView.dispatch({ | ||
selection: { anchor: 1, head: 2 } | ||
}) | ||
|
||
curSql = editorInst.getCurStatements() | ||
expect(curSql[0].content).toBe(LINE_1) | ||
|
||
nearbySql = editorInst.getNearbyStatement() | ||
expect(nearbySql?.content).toBe(LINE_1) | ||
|
||
editorInst.editorView.dispatch({ | ||
selection: { anchor: DOC.length, head: DOC.length } | ||
}) | ||
expect(instance.editorId).toBe('111') | ||
|
||
curSql = editorInst.getCurStatements() | ||
expect(curSql[0].content).toBe('') | ||
|
||
nearbySql = editorInst.getNearbyStatement() | ||
expect(nearbySql?.content).toBe(LINE_2) | ||
|
||
editorInst.editorView.dispatch({ | ||
selection: { anchor: 2, head: 2 + LINE_1.length + 5 } | ||
}) | ||
|
||
curSql = editorInst.getCurStatements() | ||
expect(curSql.length).toBe(2) | ||
expect(curSql[0].content).toBe(LINE_1) | ||
expect(curSql[1].content).toBe(LINE_2) | ||
|
||
nearbySql = editorInst.getNearbyStatement() | ||
expect(nearbySql?.content).toBe(LINE_1) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
import { EditorView } from '@codemirror/view' | ||
import { EditorState } from '@codemirror/state' | ||
|
||
import { saveHelper } from '..' | ||
|
||
const LINE_1 = `USE sp500insight;` | ||
const LINE_2 = `SELECT * from companies LIMIT 10;` | ||
|
||
const INIT_DOC = `\n${LINE_1}\n\n` | ||
|
||
function delay(ms: number) { | ||
return new Promise((resolve) => setTimeout(resolve, ms)) | ||
} | ||
|
||
test('test auto save after content changes without any delay', async () => { | ||
let latestContent = '' | ||
const editorView = new EditorView({ | ||
state: EditorState.create({ | ||
doc: INIT_DOC, | ||
extensions: [ | ||
saveHelper({ | ||
save(view) { | ||
latestContent = view.state.doc.toString() | ||
}, | ||
delay: 0 | ||
}) | ||
] | ||
}) | ||
}) | ||
|
||
// dispatch a change transaction to update the content | ||
editorView.dispatch({ changes: { from: 0, insert: LINE_2 } }) | ||
|
||
expect(latestContent).toBe(``) | ||
|
||
await delay(0) | ||
expect(latestContent).toBe(`${LINE_2}${INIT_DOC}`) | ||
}) | ||
|
||
test('test auto save after content changes without 1s delay', async () => { | ||
let latestContent = '' | ||
const editorView = new EditorView({ | ||
state: EditorState.create({ | ||
doc: INIT_DOC, | ||
extensions: [ | ||
saveHelper({ | ||
save(view) { | ||
latestContent = view.state.doc.toString() | ||
}, | ||
delay: 1000 | ||
}) | ||
] | ||
}) | ||
}) | ||
|
||
// dispatch a change transaction to update the content | ||
editorView.dispatch({ changes: { from: 0, insert: LINE_2 } }) | ||
|
||
expect(latestContent).toBe(``) | ||
|
||
await delay(100) | ||
expect(latestContent).toBe(``) | ||
|
||
await delay(1000) | ||
expect(latestContent).toBe(`${LINE_2}${INIT_DOC}`) | ||
}) | ||
|
||
test('test turn off auto save', async () => { | ||
let latestContent = '' | ||
const editorView = new EditorView({ | ||
state: EditorState.create({ | ||
doc: INIT_DOC, | ||
extensions: [ | ||
saveHelper({ | ||
auto: false, | ||
save(view) { | ||
latestContent = view.state.doc.toString() | ||
}, | ||
delay: 0 | ||
}) | ||
] | ||
}) | ||
}) | ||
|
||
// dispatch a change transaction to update the content | ||
editorView.dispatch({ changes: { from: 0, insert: LINE_2 } }) | ||
|
||
await delay(100) | ||
expect(latestContent).toBe(``) | ||
}) | ||
|
||
test('test manual save with default hotkey', () => { | ||
let latestContent = '' | ||
const editorView = new EditorView({ | ||
state: EditorState.create({ | ||
doc: INIT_DOC, | ||
extensions: [ | ||
saveHelper({ | ||
auto: false, | ||
save(view) { | ||
latestContent = view.state.doc.toString() | ||
} | ||
}) | ||
] | ||
}) | ||
}) | ||
|
||
// dispatch a change transaction to update the content | ||
editorView.dispatch({ changes: { from: 0, insert: LINE_2 } }) | ||
|
||
// TODO | ||
// press Mod-s | ||
|
||
expect(latestContent).toBe(``) | ||
}) | ||
|
||
test('test manual save with a non-default hotkey', () => { | ||
let latestContent = '' | ||
const editorView = new EditorView({ | ||
state: EditorState.create({ | ||
doc: INIT_DOC, | ||
extensions: [ | ||
saveHelper({ | ||
auto: false, | ||
hotkey: 'Mod-i', | ||
save(view) { | ||
latestContent = view.state.doc.toString() | ||
} | ||
}) | ||
] | ||
}) | ||
}) | ||
|
||
// dispatch a change transaction to update the content | ||
editorView.dispatch({ changes: { from: 0, insert: LINE_2 } }) | ||
|
||
// TODO | ||
// press Mod-i | ||
|
||
expect(latestContent).toBe(``) | ||
}) | ||
|
||
test('test turn off manual save', () => { | ||
let latestContent = '' | ||
const editorView = new EditorView({ | ||
state: EditorState.create({ | ||
doc: INIT_DOC, | ||
extensions: [ | ||
saveHelper({ | ||
auto: false, | ||
hotkey: '', | ||
save(view) { | ||
latestContent = view.state.doc.toString() | ||
} | ||
}) | ||
] | ||
}) | ||
}) | ||
|
||
// dispatch a change transaction to update the content | ||
editorView.dispatch({ changes: { from: 0, insert: LINE_2 } }) | ||
|
||
// TODO | ||
// press Mod-i | ||
|
||
expect(latestContent).toBe(``) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
"author": "", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@codemirror/lang-sql": "^6.6.4", | ||
"@codemirror/language": "^6.10.2", | ||
"@codemirror/state": "^6.4.1", | ||
"@codemirror/view": "^6.26.3", | ||
|
@@ -37,6 +38,7 @@ | |
"typescript": "^5.4.5" | ||
}, | ||
"peerDependencies": { | ||
"@codemirror/lang-sql": "^6.6.4", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the test, we need to install |
||
"@codemirror/language": "^6.10.2", | ||
"@codemirror/state": "^6.4.1", | ||
"@codemirror/view": "^6.26.3" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { EditorView } from '@codemirror/view' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please change the file name to a meaningful one There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because this extension only has one file, I have no better idea, do you have any recommendation? |
||
import { EditorState } from '@codemirror/state' | ||
import { MySQL, sql } from '@codemirror/lang-sql' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use |
||
|
||
import { sqlParser, getSqlStatements, getNearbyStatement } from '..' | ||
|
||
const LINE_1 = `USE sp500insight;` | ||
const LINE_2 = `SELECT sector, industry, COUNT(*) AS companies | ||
FROM companies c | ||
WHERE c.stock_symbol IN (SELECT stock_symbol FROM index_compositions WHERE index_symbol = "SP500") | ||
GROUP BY sector, industry | ||
ORDER BY sector, companies DESC;` | ||
|
||
const DOC = `\n${LINE_1}\n\n${LINE_2}\n\n` | ||
|
||
test('test getSqlStatements', () => { | ||
const editorView = new EditorView({ | ||
state: EditorState.create({ | ||
doc: DOC, | ||
extensions: [sql({ dialect: MySQL }), sqlParser()] | ||
}) | ||
}) | ||
|
||
// dispatch any a transaction to trigger update event for editor | ||
editorView.dispatch({ selection: { anchor: 0, head: 0 } }) | ||
|
||
const allStatements = getSqlStatements(editorView.state) | ||
expect(allStatements.length).toBe(2) | ||
|
||
const firstStatement = allStatements[0] | ||
expect(firstStatement.content).toBe(LINE_1) | ||
expect(firstStatement.database).toBe('sp500insight') | ||
expect(firstStatement.type).toBe('use') | ||
expect(firstStatement.from).toBe(1) | ||
expect(firstStatement.to).toBe(1 + LINE_1.length) | ||
expect(firstStatement.lineFrom).toBe(2) | ||
expect(firstStatement.lineTo).toBe(2) | ||
|
||
const secondStatement = allStatements[1] | ||
expect(secondStatement.content).toBe(LINE_2) | ||
expect(secondStatement.database).toBe('sp500insight') | ||
expect(secondStatement.type).toBe('other') | ||
expect(secondStatement.from).toBe(1 + LINE_1.length + 2) | ||
expect(secondStatement.to).toBe(1 + LINE_1.length + 2 + LINE_2.length) | ||
expect(secondStatement.lineFrom).toBe(4) | ||
expect(secondStatement.lineTo).toBe(8) | ||
}) | ||
|
||
test('test getNearbyStatement', () => { | ||
const editorView = new EditorView({ | ||
state: EditorState.create({ | ||
doc: DOC, | ||
extensions: [sql({ dialect: MySQL }), sqlParser()] | ||
}) | ||
}) | ||
|
||
// dispatch any a transaction to trigger update event for editor | ||
editorView.dispatch({ selection: { anchor: 0, head: 0 } }) | ||
|
||
let nearestStatement = getNearbyStatement(editorView.state, 0) | ||
expect(nearestStatement?.content).toBe(LINE_1) | ||
|
||
nearestStatement = getNearbyStatement(editorView.state, 10) | ||
expect(nearestStatement?.content).toBe(LINE_1) | ||
|
||
nearestStatement = getNearbyStatement(editorView.state, 1 + LINE_1.length) // 18 | ||
expect(nearestStatement?.content).toBe(LINE_1) | ||
|
||
nearestStatement = getNearbyStatement(editorView.state, 1 + LINE_1.length + 1) // 19 | ||
expect(nearestStatement?.content).toBe(LINE_1) | ||
|
||
nearestStatement = getNearbyStatement( | ||
editorView.state, | ||
1 + LINE_1.length + 10 | ||
) // 28 | ||
expect(nearestStatement?.content).toBe(LINE_1) | ||
|
||
nearestStatement = getNearbyStatement( | ||
editorView.state, | ||
1 + LINE_1.length + 2 + LINE_2.length | ||
) // 241 | ||
expect(nearestStatement?.content).toBe(LINE_2) | ||
|
||
nearestStatement = getNearbyStatement( | ||
editorView.state, | ||
1 + LINE_1.length + 2 + LINE_2.length | ||
) // 241 | ||
expect(nearestStatement?.content).toBe(LINE_2) | ||
|
||
nearestStatement = getNearbyStatement( | ||
editorView.state, | ||
1 + LINE_1.length + 2 + LINE_2.length + 2 | ||
) // 243 | ||
expect(nearestStatement?.content).toBe(LINE_2) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please change the file name to a meaningful one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because this extension only has one file, I have no better idea, do you have any recommendation?