Skip to content

Commit b3644bf

Browse files
authored
Merge pull request #68 from firstbatchxyz/erhant/set-test
Slightly better `set` tests
2 parents 8838c15 + e12c47c commit b3644bf

File tree

5 files changed

+48
-12
lines changed

5 files changed

+48
-12
lines changed

src/base/base.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@ import type {ContractInputGeneric, ContractMode, ContractState} from '../contrac
66
export class Base<M extends ContractMode> {
77
readonly contract: Contract<ContractState<M>>;
88
readonly warp: Warp;
9-
readonly contractTxId: string;
109
readonly signer: ArWallet | CustomSignature;
1110

1211
constructor(signer: ArWallet | CustomSignature, contractTxId: string, warp: Warp) {
1312
this.signer = signer;
14-
this.contractTxId = contractTxId;
1513
this.warp = warp
1614
// required for proof verification
1715
.use(new SnarkjsExtension())
1816
// required for hashing
1917
.use(new EthersExtension());
2018

2119
this.contract = this.warp
22-
.contract<ContractState<M>>(this.contractTxId)
20+
.contract<ContractState<M>>(contractTxId)
2321
.setEvaluationOptions({
2422
allowBigInt: true, // bigInt is required for circuits
2523
useKVStorage: true,

src/base/sdk.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ export class SDK<V = unknown, M extends ContractMode = ContractMode> {
4040
return this.base.warp;
4141
}
4242

43+
/** Contract transaction id. */
44+
get contractTxId(): string {
45+
return this.base.contract.txId();
46+
}
47+
4348
/** Signer. */
4449
get signer(): ArWallet | CustomSignature {
4550
return this.base.signer;
@@ -100,7 +105,7 @@ export class SDK<V = unknown, M extends ContractMode = ContractMode> {
100105
* @param options optional range
101106
* @returns a key-value `Map`
102107
*/
103-
async getKVMap(options?: SortKeyCacheRangeOptions): Promise<Map<string, V>> {
108+
async getKVMap(options?: SortKeyCacheRangeOptions): Promise<Map<string, V | null>> {
104109
return await this.base.safeReadInteraction<GetKVMapInput, Map<string, V>>({
105110
function: 'getKVMap',
106111
value: {
@@ -114,7 +119,7 @@ export class SDK<V = unknown, M extends ContractMode = ContractMode> {
114119
* @param key the key of the value to be returned
115120
* @returns the value of the given key
116121
*/
117-
async get(key: string): Promise<V> {
122+
async get(key: string): Promise<V | null> {
118123
return await this.base.safeReadInteraction<GetInput, V>({
119124
function: 'get',
120125
value: {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"version": "0.0.0",
3+
"owner": "",
4+
"verificationKeys": {
5+
"auth": null
6+
},
7+
"isProofRequired": {
8+
"auth": false
9+
},
10+
"canEvolve": true,
11+
"whitelists": {
12+
"put": {},
13+
"update": {},
14+
"set": {}
15+
},
16+
"isWhitelistRequired": {
17+
"put": false,
18+
"update": false,
19+
"set": false
20+
}
21+
}

src/hollowdb-set.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class SetSDK<V = unknown> extends BaseSDK<V, {proofs: ['auth']; whitelist
1010
/**
1111
* Inserts the given value into database.
1212
*
13-
* There must not be a value at the given key.
13+
* Overwrites the existing values at the given key.
1414
*
1515
* @param key the key of the value to be inserted
1616
* @param value the value to be inserted
@@ -28,7 +28,7 @@ export class SetSDK<V = unknown> extends BaseSDK<V, {proofs: ['auth']; whitelist
2828
/**
2929
* Inserts an array of value into database.
3030
*
31-
* There must not be a value at the given key.
31+
* Overwrites the existing values at the given keys.
3232
*
3333
* @param keys the keys of the values to be inserted
3434
* @param values the values to be inserted

tests/set.test.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,38 @@ describe('set tests', () => {
2121

2222
it('should allow putting a value', async () => {
2323
await owner.put(KEY, VALUE);
24+
const value = await owner.get(KEY);
25+
expect(value?.val).toBe(VALUE.val);
2426
});
2527

2628
it('should NOT allow putting a value again', async () => {
2729
await expect(owner.put(KEY, NEXT_VALUE)).rejects.toThrow('Contract Error [put]: Key already exists.');
2830
});
2931

3032
it('should allow setting a value at an existing key', async () => {
31-
await owner.set(KEY, VALUE);
33+
await owner.set(KEY, NEXT_VALUE);
34+
const value = await owner.get(KEY);
35+
expect(value?.val).toBe(NEXT_VALUE.val);
3236
});
3337

3438
it('should allow setting a value at a new key', async () => {
3539
const newKV = createValues();
3640
await owner.set(newKV.KEY, newKV.VALUE);
41+
const value = await owner.get(newKV.KEY);
42+
expect(value?.val).toBe(newKV.VALUE.val);
3743
});
3844

3945
it('should allow setting many values', async () => {
4046
const kvs = Array.from({length: 5}, () => createValues());
41-
await owner.setMany(
42-
kvs.map(kv => kv.KEY),
43-
kvs.map(kv => kv.VALUE)
44-
);
47+
48+
const keys = kvs.map(kv => kv.KEY);
49+
const values = kvs.map(kv => kv.VALUE);
50+
await owner.setMany(keys, values);
51+
52+
const results = await owner.getMany(keys);
53+
expect(results.length).toBe(values.length);
54+
for (let i = 0; i < results.length; i++) {
55+
expect(results[i]?.val).toBe(values[i].val);
56+
}
4557
});
4658
});

0 commit comments

Comments
 (0)