Skip to content

Commit

Permalink
Merge pull request #24 from tidbcloud/chore/add-test-case
Browse files Browse the repository at this point in the history
chore: add test for sql-parser/save-helper/core
  • Loading branch information
baurine authored Jun 27, 2024
2 parents 4a52d4f + 1d49a8d commit aa27f58
Show file tree
Hide file tree
Showing 11 changed files with 366 additions and 18 deletions.
26 changes: 26 additions & 0 deletions packages/core/src/test/cache.test.ts
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)
})
69 changes: 58 additions & 11 deletions packages/core/src/test/instance.test.ts
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)
})
3 changes: 2 additions & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"compilerOptions": {
"outDir": "./dist"
},
"include": ["./src"]
"include": ["./src"],
"exclude": ["./src/**/*.test.ts"]
}
8 changes: 6 additions & 2 deletions packages/extensions/cur-sql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,27 @@ This extension is installed internally inside the `SQLEditorInstance`.
npm install @tidbcloud/codemirror-extension-cur-sql
```

It will auto install `@tidbcloud/codemirror-extension-sql-parser` as its dependency.

You need to install its peer dependencies as well:

```shell
npm install @codemirror/view @codemirror/state
npm install @codemirror/view @codemirror/state @codemirror/language @codemirro/lang-sql
```

## Usage

```ts
import { EditorView } from '@codemirror/view'
import { EditorState } from '@codemirror/state'
import { sql, MySQL } from '@codemirror/lang-sql'
import { sqlParser } from '@tidbcloud/codemirror-extension-sql-parser'
import { curSql } from '@tidbcloud/codemirror-extension-cur-sql'

const editorView = new EditorView({
state: EditorState.create({
doc,
extensions: [curSql()]
extensions: [sql({ dialect: MySQL }), sqlParser(), curSql()]
})
})
```
Expand Down
167 changes: 167 additions & 0 deletions packages/extensions/save-helper/src/test/1.test.ts
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(``)
})
3 changes: 2 additions & 1 deletion packages/extensions/save-helper/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"compilerOptions": {
"outDir": "./dist"
},
"include": ["./src"]
"include": ["./src"],
"exclude": ["./src/**/*.test.ts"]
}
5 changes: 3 additions & 2 deletions packages/extensions/sql-parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@ npm install @tidbcloud/codemirror-extension-sql-parser
You need to install its peer dependencies as well:

```shell
npm install @codemirror/view @codemirror/state @codemirror/language
npm install @codemirror/view @codemirror/state @codemirror/language @codemirror/lang-sql
```

## Usage

```ts
import { EditorView } from '@codemirror/view'
import { EditorState } from '@codemirror/state'
import { sql, MySQL } from '@codemirror/lang-sql'
import { sqlParser } from '@tidbcloud/codemirror-extension-sql-parser'

const editorView = new EditorView({
state: EditorState.create({
doc,
extensions: [sqlParser()]
extensions: [sql({ dialect: MySQL }), sqlParser()]
})
})
```
Expand Down
2 changes: 2 additions & 0 deletions packages/extensions/sql-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -37,6 +38,7 @@
"typescript": "^5.4.5"
},
"peerDependencies": {
"@codemirror/lang-sql": "^6.6.4",
"@codemirror/language": "^6.10.2",
"@codemirror/state": "^6.4.1",
"@codemirror/view": "^6.26.3"
Expand Down
Loading

0 comments on commit aa27f58

Please sign in to comment.