feat: Принес класс хранилища SQLite и зависимости

This commit is contained in:
Nikita Dezzpil Orlov committed 2022-01-31 16:11:08 +03:00
1 parent e799da6a48
commit 79cdca410e
6 files changed
+1938 -203

No files matched your search

+137
View File
@@ -0,0 +1,137 @@
import { assert } from 'chai'
import { StorageSQLite } from '../../../app/storage/sqlite'
const abbrList = [
['bar', '{}', 2],
['pew', '{}', 2],
['baz', '{}', 2],
['xyz', '{}', 1],
['yes', '{}', 1],
['lol', JSON.stringify({ hello: 'world' }), 1],
['idk', JSON.stringify({ pew: 1 }), 1],
['gg', JSON.stringify({ pew: 100 }), 1],
]
const dsn = __dirname + `/sqlite.test.db`
describe('StorageSQLite', () => {
interface Item {
abbr: string
foo: string
n: number
}
let storage: StorageSQLite
beforeAll(async () => {
storage = new StorageSQLite({ dsn })
await storage.connect()
storage.db.on('trace', (data: string) => {
console.log(data)
})
await storage.db.run('drop table if exists items')
// UNIQUE нужно, чтобы работал UPSERT в методе save()
await storage.db.run(
'create table items (abbr varchar(4) not null unique, foo text null, num integer not null default 0)'
)
const stmt = await storage.db.prepare('insert into "items" (abbr, foo, num) values (?, ?, ?)')
for (const values of abbrList) {
await stmt.run(values)
}
})
describe('#find', () => {
it('should find all', async () => {
const cursor = await storage.find('items', {})
const items = await cursor.toArray()
assert.equal(items.length, abbrList.length)
})
it('should find only one', async () => {
const cursor = await storage.find('items', { 'abbr = ?': ['bar'] })
const items = await cursor.toArray()
assert.equal(items.length, 1)
})
it('should find 2 with offset', async () => {
const cursor = await storage.find('items', {})
const items = await cursor.limit(2).toArray()
assert.equal(items.length, 2)
const nextItems = await cursor.skip(2).toArray()
assert.equal(nextItems.length, 2)
assert.notDeepEqual(items[0], nextItems[0])
assert.notDeepEqual(items[1], nextItems[1])
})
it('should find reversed order ', async () => {
const cursor = await storage.find('items', {})
const items = await cursor.sort({ abbr: 'DESC' }).toArray()
assert.equal(items.length, abbrList.length)
assert.equal(items[0].abbr, 'yes')
})
it('should find and get string from string field for json', async () => {
const cursor = await storage.find('items', { 'abbr = ?': ['gg'] })
const items = await cursor.toArray()
assert.lengthOf(items, 1)
const item = items[0] as Item
assert.equal(typeof item.foo, 'string')
assert.deepEqual(JSON.parse(item.foo), { pew: 100 })
})
it('should find with criteria with many params 1', async () => {
const cursor = await storage.find('items', { 'num = ?': [1], 'foo != ?': ['{}'] })
const items = await cursor.toArray()
assert.lengthOf(items, 3)
})
it('should find with criteria with many params 2', async () => {
const cursor = await storage.find('items', { 'num = ?': [2], 'foo = ?': ['{}'] })
const items = await cursor.toArray()
assert.lengthOf(items, 3)
})
})
describe('#count', () => {
it('should return total count', async () => {
const count = await storage.count('items', {})
assert.equal(count, abbrList.length)
})
it('should return count with criteria', async () => {
const count1 = await storage.count('items', { "abbr LIKE '%'": [] })
assert.equal(count1, abbrList.length)
const count2 = await storage.count('items', { 'abbr = ? OR abbr = ?': ['bar', 'pew'] })
assert.equal(count2, 2)
})
})
describe('#save', () => {
it('should save new item', async () => {
let lastId = await storage.save('items', 'abbr', { abbr: 'banzay' })
assert.equal(lastId, abbrList.length + 1 + '')
abbrList.push(['banzay', '{}'])
lastId = await storage.save('items', 'abbr', { abbr: 'babam' })
assert.equal(lastId, abbrList.length + 1 + '')
abbrList.push(['babam', '{}'])
})
it('should update item', async () => {
let lastId = await storage.save('items', 'abbr', { abbr: 'kish' })
assert.equal(lastId, abbrList.length + 1 + '')
abbrList.push(['kish', '{}'])
lastId = await storage.save('items', 'abbr', { abbr: 'kish' })
assert.equal(lastId, abbrList.length + '')
})
})
describe('#remove', () => {
it('should remove item', async () => {
const lastId = await storage.save('items', 'abbr', { abbr: 'thx' })
assert.equal(lastId, abbrList.length + 1 + '')
const deleted = await storage.remove('items', 'abbr', 'thx')
assert.isTrue(deleted)
const count = await storage.count('items')
assert.equal(abbrList.length, count)
})
})
})
@@ -0,0 +1,46 @@
import { assert } from 'chai'
import { StorageSQLite } from '../../../app/storage/sqlite'
const itemsData = ['zero', 'one', 'two', 'three', 'four', 'five']
const dsn = __dirname + '/sqlsession.test.db'
describe('SQLiteSession', () => {
let storage: StorageSQLite
beforeAll(async () => {
storage = new StorageSQLite({ dsn })
await storage.connect()
storage.db.on('trace', (data: string) => {
console.log(data)
})
await storage.db.run('DROP TABLE IF EXISTS items')
// UNIQUE нужно, чтобы работал UPSERT в методе save()
await storage.db.run(
'CREATE TABLE items (key varchar(8) NOT NULL UNIQUE, val integer NOT NULL DEFAULT 0)'
)
const stmt = await storage.db.prepare('INSERT INTO items (key, val) VALUES (?, ?)')
for (let k = 0; k < itemsData.length; k++) {
await stmt.run([k, itemsData[k]])
}
})
describe('#commit', () => {
it('should commit save', async () => {
const session = storage.createSession()
await session.start()
await session.commit(async () => {
await storage.save('items', 'key', { key: 'ten', val: 10 })
await storage.save('items', 'key', { key: 'eleven', val: 11 })
await storage.remove('items', 'key', 'ten')
})
const cursor = await storage.find('items', {})
const items = await cursor.toArray()
assert.equal(itemsData.length + 1, items.length)
for (const item of items) {
if (item['key'] === 'ten') assert.fail('ten had to be removed in session')
}
})
})
})