/** * Copyright (c) 2022 EdgerOS Team. * All rights reserved. * * Detailed license information can be found in the LICENSE file. * * Author : xueqiang * Date : 2024-08-07 17:37:04 * LastEditors : xueqiang * LastEditTime : 2024-08-14 13:57:00 */ import { type DBClient, type Namespace, Range } from '..' import { createTestClientAndKeys, tearDownTestClient } from './util' import { test } from '@edgeros/tapes' import assert from 'assert' const start = async function () { const client = await createTestClientAndKeys() const ns = client.namespace('user1/') return { client, ns } } const end = async function (client: DBClient) { await tearDownTestClient(client) } const assertEqualInNamespace = async (client: DBClient, ns: Namespace, key: string, value: string | Buffer) => { assert.equal(await ns.get(key), value) assert.equal(await client.get(`user1/${key}`), value) } test('namespacing - puts and gets values in the namespace', async (t) => { const { client, ns } = await start() await ns.put('foo').value('') await assertEqualInNamespace(client, ns, 'foo', '') t.deepEqual(await ns.getAll().strings(), { foo: '' }, 'namespacing - puts and gets values in the namespace - ') await end(client) t.end() }) test('namespacing - deletes values in the namespace', async (t) => { const { client, ns } = await start() await ns.put('foo1').value('') await ns.put('foo2').value('') await ns.delete().key('foo1') t.deepEqual(await ns.getAll().strings(), { foo2: '' }) await ns.delete().all() t.deepEqual(await ns.getAll().strings(), {}) t.ok((await client.getAll().keys()).length > 0) t.pass('namespacing - deletes values in the namespace - ') await end(client) t.end() }) test('namespacing - contains leases in the namespace', async (t) => { const { client, ns } = await start() const lease = ns.lease(100) await lease.put('leased').value('') await assertEqualInNamespace(client, ns, 'leased', '') await lease.revoke() t.pass('namespacing - contains leases in the namespace - ') await end(client) t.end() }) test('namespacing - contains locks in the namespace', async (t) => { const { client, ns } = await start() const lock = ns.lock('mylock') await lock.acquire() assert.ok(await ns.get('mylock') !== null) assert.ok(await client.get('user1/mylock') !== null) await lock.release() t.pass('namespacing - contains locks in the namespace - ') await end(client) t.end() }) test('namespacing - runs a simple if', async (t) => { const { client, ns } = await start() await ns.put('foo1').value('potatoes') await ns.if('foo1', 'Value', '==', 'potatoes').then(ns.put('foo1').value('tomatoes')).commit() await assertEqualInNamespace(client, ns, 'foo1', 'tomatoes') t.pass('namespacing - runs a simple if - ') await end(client) t.end() }) /** * RangeBuilder 测试 * */ test('RangeBuilder - options ', async (t) => { const { client, ns } = await start() const key = Date.now() + '' await ns.put(key).value('111') await ns.put(key).ignoreLease().options(undefined).value('222') const value = await ns.get(key) .revision(0) .minModRevision(0) .maxModRevision(0) .maxCreateRevision(0) .minCreateRevision(0) .serializable(true) .string() t.equal(value, '222', 'RangeBuilder - options') await end(client) t.end() }) test('MultiRangeBuilder-sort-buffers', async (t) => { const { client, ns } = await start() await ns.put('m1').value('m1') await ns.put('m2').value('m2') await ns.put('m3').value('m3') await ns.put('m4').value('m4') const keys = await ns.getAll().sort('Key', 'Descend').keys() t.equal(keys[0].toString(), 'm4', 'MultiRangeBuilder - sort - Descend') const data = await ns.getAll().all().buffers() t.ok(Buffer.isBuffer(data.m1), 'MultiRangeBuilder - buffer') await end(client) t.end() }) test('MultiRangeBuilder-json', async (t) => { const { client, ns } = await start() await ns.put('m').value('{"m1":123}') await ns.getAll().json() t.pass('MultiRangeBuilder - json') await end(client) t.end() }) test('DeleteRangeBuilder-inRange', async (t) => { const { client, ns } = await start() await ns.put('1').value('m1') await ns.put('2').value('m1') await ns.put('3').value('m1') const del = ns.delete().inRange(Range.from({ start: '1', end: '2' })).options(undefined) t.ok(await del.op(), 'DeleteRangeBuilder-inRange delete op') await del.exec() const all = await ns.getAll().exec() t.equal(all.kvs[0].key.toString(), '2', 'DeleteRangeBuilder-inRange - getAll') await end(client) t.end() })