|
| 1 | +// Copyright 2017-2019 @polkadot/api authors & contributors |
| 2 | +// This software may be modified and distributed under the terms |
| 3 | +// of the Apache-2.0 license. See the LICENSE file for details. |
| 4 | + |
| 5 | +import { Storage } from '@polkadot/metadata/Decorated/types'; |
| 6 | + |
| 7 | +import storageFromMeta from '@polkadot/metadata/Decorated/storage/fromMetadata'; |
| 8 | +import Metadata from '@polkadot/metadata/Metadata'; |
| 9 | +import metaStatic from '@polkadot/metadata/Metadata/static'; |
| 10 | +import { TypeRegistry } from '@polkadot/types'; |
| 11 | + |
| 12 | +import { extractStorageArgs } from './validate'; |
| 13 | + |
| 14 | +describe('extractStorageArgs', (): void => { |
| 15 | + const registry = new TypeRegistry(); |
| 16 | + let storage: Storage; |
| 17 | + |
| 18 | + beforeEach((): void => { |
| 19 | + const metadata = new Metadata(registry, metaStatic); |
| 20 | + |
| 21 | + storage = storageFromMeta(registry, metadata); |
| 22 | + }); |
| 23 | + |
| 24 | + it('validates no-arg plain', (): void => { |
| 25 | + expect( |
| 26 | + extractStorageArgs(storage.timestamp.now, []) |
| 27 | + ).toEqual([storage.timestamp.now]); |
| 28 | + }); |
| 29 | + |
| 30 | + it('validates no-arg plain (failing when there are args)', (): void => { |
| 31 | + expect( |
| 32 | + (): any[] => |
| 33 | + extractStorageArgs(storage.timestamp.now, [123, 456]) |
| 34 | + ).toThrow('timestamp.now() does not take any arguments, 2 found'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('validates map, 1 arg', (): void => { |
| 38 | + expect( |
| 39 | + extractStorageArgs(storage.staking.payee, ['abc']) |
| 40 | + ).toEqual([storage.staking.payee, 'abc']); |
| 41 | + }); |
| 42 | + |
| 43 | + it('validates map, 1 arg (failing with no args)', (): void => { |
| 44 | + expect( |
| 45 | + (): any => |
| 46 | + extractStorageArgs(storage.staking.payee, []) |
| 47 | + ).toThrow('staking.payee(AccountId) is a map, requiring 1 argument, 0 found'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('validates map, 1 arg (failing with no args)', (): void => { |
| 51 | + expect( |
| 52 | + (): any => |
| 53 | + extractStorageArgs(storage.staking.payee, ['abc', 'def']) |
| 54 | + ).toThrow('staking.payee(AccountId) is a map, requiring 1 argument, 2 found'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('validates doublemap, 2 args', (): void => { |
| 58 | + expect( |
| 59 | + extractStorageArgs(storage.staking.erasStakers, [1, '0x1234']) |
| 60 | + ).toEqual([storage.staking.erasStakers, [1, '0x1234']]); |
| 61 | + }); |
| 62 | + |
| 63 | + it('validates doublemap, 2 args (failing with no args)', (): void => { |
| 64 | + expect( |
| 65 | + (): any => |
| 66 | + extractStorageArgs(storage.staking.erasStakers, []) |
| 67 | + ).toThrow('staking.erasStakers(EraIndex, AccountId) is a doublemap, requiring 2 arguments, 0 found'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('validates doublemap, 2 args (failing with 1 arg)', (): void => { |
| 71 | + expect( |
| 72 | + (): any => |
| 73 | + extractStorageArgs(storage.staking.erasStakers, [123]) |
| 74 | + ).toThrow('staking.erasStakers(EraIndex, AccountId) is a doublemap, requiring 2 arguments, 1 found'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('validates linked map, no args', (): void => { |
| 78 | + expect( |
| 79 | + extractStorageArgs(storage.staking.validators, []) |
| 80 | + ).toEqual([storage.staking.validators]); |
| 81 | + }); |
| 82 | + |
| 83 | + it('validates linked map, single arg', (): void => { |
| 84 | + expect( |
| 85 | + extractStorageArgs(storage.staking.validators, [123]) |
| 86 | + ).toEqual([storage.staking.validators, 123]); |
| 87 | + }); |
| 88 | + |
| 89 | + it('validates linked map (failing with extra args)', (): void => { |
| 90 | + expect( |
| 91 | + (): any[] => |
| 92 | + extractStorageArgs(storage.staking.validators, [123, 456]) |
| 93 | + ).toThrow('staking.validators(AccountId) is a linked map, requiring either 0 arguments (retrieving all) or 1 argument, 2 found'); |
| 94 | + }); |
| 95 | +}); |
0 commit comments