/** * Copyright (c) 2022 EdgerOS Team. * All rights reserved. * * Detailed license information can be found in the LICENSE file. * * Author : xueqiang * Date : 2024-08-06 18:36:20 * LastEditors : xueqiang * LastEditTime : 2024-08-12 16:47:13 */ import { createTestClientAndKeys, tearDownTestClient, containSubset } from './util' import { test } from '@edgeros/tapes' import assert from 'assert' test('crud - get() / getAll()', async (t) => { const client = await createTestClientAndKeys() const listData = await client.getAll().strings() t.deepEqual(listData, { foo1: 'bar1', foo2: 'bar2', foo3: '{"value":"bar3"}', baz: 'bar5', }, 'crud - get() / getAll() - lists all value - ') assert.equal(await client.get('foo1').string(), 'bar1') t.deepEqual(await client.get('foo2').buffer(), Buffer.from('bar2')) t.deepEqual(await client.get('foo3').json(), { value: 'bar3' }) assert.equal(await client.get('wut').string(), null) t.pass('crud - get() / getAll() - gets single keys with various encoding - ') assert.ok(await client.get('foo1').exists()) assert.ok(!await client.get('wut').exists()) t.pass('crud - get() / getAll() - gets whether keys exist - ') t.deepEqual(await client.getAll().prefix('fo').strings(), { foo1: 'bar1', foo2: 'bar2', foo3: '{"value":"bar3"}', }, 'crud - get() / getAll() - queries prefixes - ') const keys = await client.getAll().keys() assert.ok(haveAllMembers(keys, ['foo1', 'foo2', 'foo3', 'baz'])) const keysBuf = await client.getAll().keyBuffers() assert.ok(containSubset(keysBuf, [ Buffer.from('foo1'), Buffer.from('foo2'), Buffer.from('foo3'), Buffer.from('baz'), ])) t.pass('crud - get() / getAll() - get keys - ') t.equal(await client.getAll().count(), 4, 'crud - get() / getAll() - counts - ') t.deepEqual(await client.getAll().prefix('foo').sort('Key', 'Ascend').limit(2).keys(), ['foo1', 'foo2'], 'crud - get() / getAll() - sorts - ') t.deepEqual(await client.getAll().prefix('foo').sort('Key', 'Descend').limit(2).keys(), ['foo3', 'foo2'], 'crud - get() / getAll() - sorts - ') await tearDownTestClient(client) t.end() }) test('delete() - crud - delete - delete all', async (t) => { const client = await createTestClientAndKeys() await client.delete().all() t.equal(await client.getAll().count(), 0, 'crud - delete - delete all - ') await tearDownTestClient(client) t.end() }) test('delete() - crud - delete - deletes prefix -', async (t) => { const client = await createTestClientAndKeys() await client.delete().prefix('foo') t.deepEqual(await client.getAll().keys(), ['baz'], 'crud - delete - deletes prefix - ') await tearDownTestClient(client) t.end() }) test('delete() - crud - delete - gets previous -', async (t) => { const client = await createTestClientAndKeys() const previous = await client.delete().key('foo1').getPrevious() t.ok(containSubset(previous, { key: Buffer.from('foo1'), value: Buffer.from('bar1'), }), 'crud - delete - gets previous - ') await tearDownTestClient(client) t.end() }) test('put()', async (t) => { const client = await createTestClientAndKeys() const original = (await client.get('foo1').exec()).kvs[0].mod_revision await client.put('foo1').touch() const updated = (await client.get('foo1').exec()).kvs[0].mod_revision t.ok(Number(updated) > Number(original), 'put() - allows touching key revisions - ') await client.put('foo1').value('updated') t.equal(await client.get('foo1').string(), 'updated', 'put() updates key values - ') await client.put('foo1').value('bar1') const previous = await client.put('foo1').value('updated').getPrevious() t.ok(containSubset([previous], { key: Buffer.from('foo1'), value: Buffer.from('bar1'), }), 'put() includes previous values - ') await tearDownTestClient(client) t.end() }) function haveAllMembers(source: string[], checkArr: string[]) { for (const item of checkArr) { if (!source.includes(item)) { return false } } return true }