From 555b2fb5fe38089de1965171c3e73c730b48b808 Mon Sep 17 00:00:00 2001 From: Jaco Date: Sun, 1 Aug 2021 10:35:01 +0300 Subject: [PATCH] Flatten metadata tests & imports (#3830) --- .../metadata/decorate/constants/index.spec.ts | 2 +- .../types/src/metadata/static-polkadot.ts | 2 +- .../types/src/metadata/static-substrate.ts | 2 +- packages/types/src/metadata/util/testUtil.ts | 25 ++++++++++-- packages/types/src/metadata/util/types.ts | 8 ++++ .../types/src/metadata/v10/Metadata.spec.ts | 22 +++++------ .../static-substrate.ts => substrate-hex.ts} | 0 ...tic-substrate.json => substrate-json.json} | 0 .../types/src/metadata/v11/Metadata.spec.ts | 22 +++++------ .../static-substrate.ts => substrate-hex.ts} | 0 ...tic-substrate.json => substrate-json.json} | 0 .../types/src/metadata/v12/Metadata.spec.ts | 20 ++++------ .../static-substrate.ts => substrate-hex.ts} | 0 ...tic-substrate.json => substrate-json.json} | 0 .../types/src/metadata/v13/Metadata.spec.ts | 38 +++++++------------ .../static-polkadot.ts => polkadot-hex.ts} | 0 ...tatic-polkadot.json => polkadot-json.json} | 0 .../static-substrate.ts => substrate-hex.ts} | 0 ...tic-substrate.json => substrate-json.json} | 0 .../types/src/metadata/v14/Metadata.spec.ts | 12 +++--- .../static-polkadot.ts => polkadot-hex.ts} | 0 ...tatic-polkadot.json => polkadot-json.json} | 0 ...olkadot-types.json => polkadot-types.json} | 0 .../static-substrate.ts => substrate-hex.ts} | 0 ...tic-substrate.json => substrate-json.json} | 0 ...strate-types.json => substrate-types.json} | 0 .../types/src/metadata/v9/Metadata.spec.ts | 22 +++++------ .../static-substrate.ts => substrate-hex.ts} | 0 ...tic-substrate.json => substrate-json.json} | 0 .../types/src/primitive/StorageKey.spec.ts | 2 +- 30 files changed, 88 insertions(+), 89 deletions(-) create mode 100644 packages/types/src/metadata/util/types.ts rename packages/types/src/metadata/v10/{hex/static-substrate.ts => substrate-hex.ts} (100%) rename packages/types/src/metadata/v10/{json/static-substrate.json => substrate-json.json} (100%) rename packages/types/src/metadata/v11/{hex/static-substrate.ts => substrate-hex.ts} (100%) rename packages/types/src/metadata/v11/{json/static-substrate.json => substrate-json.json} (100%) rename packages/types/src/metadata/v12/{hex/static-substrate.ts => substrate-hex.ts} (100%) rename packages/types/src/metadata/v12/{json/static-substrate.json => substrate-json.json} (100%) rename packages/types/src/metadata/v13/{hex/static-polkadot.ts => polkadot-hex.ts} (100%) rename packages/types/src/metadata/v13/{json/static-polkadot.json => polkadot-json.json} (100%) rename packages/types/src/metadata/v13/{hex/static-substrate.ts => substrate-hex.ts} (100%) rename packages/types/src/metadata/v13/{json/static-substrate.json => substrate-json.json} (100%) rename packages/types/src/metadata/v14/{hex/static-polkadot.ts => polkadot-hex.ts} (100%) rename packages/types/src/metadata/v14/{json/static-polkadot.json => polkadot-json.json} (100%) rename packages/types/src/metadata/v14/{json/static-polkadot-types.json => polkadot-types.json} (100%) rename packages/types/src/metadata/v14/{hex/static-substrate.ts => substrate-hex.ts} (100%) rename packages/types/src/metadata/v14/{json/static-substrate.json => substrate-json.json} (100%) rename packages/types/src/metadata/v14/{json/static-substrate-types.json => substrate-types.json} (100%) rename packages/types/src/metadata/v9/{hex/static-substrate.ts => substrate-hex.ts} (100%) rename packages/types/src/metadata/v9/{json/static-substrate.json => substrate-json.json} (100%) diff --git a/packages/types/src/metadata/decorate/constants/index.spec.ts b/packages/types/src/metadata/decorate/constants/index.spec.ts index 48b1cbefd3..c772cce201 100644 --- a/packages/types/src/metadata/decorate/constants/index.spec.ts +++ b/packages/types/src/metadata/decorate/constants/index.spec.ts @@ -7,7 +7,7 @@ import type { Constants } from '../types'; import { TypeRegistry } from '../../../create'; import { Metadata } from '../../Metadata'; import rpcMetadata from '../../static-substrate'; -import rpcMetadataV10 from '../../v10/hex/static-substrate'; +import rpcMetadataV10 from '../../v10/substrate-hex'; import { decorateConstants } from '..'; function init (meta: string): [Constants, TypeRegistry] { diff --git a/packages/types/src/metadata/static-polkadot.ts b/packages/types/src/metadata/static-polkadot.ts index 3071ef959b..6ba9ce09eb 100644 --- a/packages/types/src/metadata/static-polkadot.ts +++ b/packages/types/src/metadata/static-polkadot.ts @@ -1,6 +1,6 @@ // Copyright 2017-2021 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import metadata from './v13/hex/static-polkadot'; +import metadata from './v13/polkadot-hex'; export default metadata; diff --git a/packages/types/src/metadata/static-substrate.ts b/packages/types/src/metadata/static-substrate.ts index 2b3cdcfbad..edfb6fa403 100644 --- a/packages/types/src/metadata/static-substrate.ts +++ b/packages/types/src/metadata/static-substrate.ts @@ -1,6 +1,6 @@ // Copyright 2017-2021 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import metadata from './v13/hex/static-substrate'; +import metadata from './v13/substrate-hex'; export default metadata; diff --git a/packages/types/src/metadata/util/testUtil.ts b/packages/types/src/metadata/util/testUtil.ts index c28ccb8299..15dd1215c0 100644 --- a/packages/types/src/metadata/util/testUtil.ts +++ b/packages/types/src/metadata/util/testUtil.ts @@ -3,24 +3,26 @@ import type { Codec, Registry } from '../../types'; import type { MetadataInterface } from '../types'; +import type { Check } from './types'; import { assert, hexToU8a, stringify, u8aToHex } from '@polkadot/util'; +import { TypeRegistry } from '../../create'; import { unwrapStorageType } from '../../primitive/StorageKey'; import { Metadata } from '../Metadata'; import { getUniqTypes } from './getUniqTypes'; /** @internal */ -export function decodeLatestMeta (registry: Registry, version: number, staticMeta: string, staticJson: Record): void { +export function decodeLatestMeta (registry: Registry, version: number, { compare, data }: Check): void { it('decodes metadata properly', (): void => { - const metadata = new Metadata(registry, staticMeta); + const metadata = new Metadata(registry, data); registry.setMetadata(metadata); try { expect(metadata.version).toBe(version); expect((metadata[`asV${version}` as keyof Metadata] as unknown as MetadataInterface).modules.length).not.toBe(0); - expect(metadata.toJSON()).toEqual(staticJson); + expect(metadata.toJSON()).toEqual(compare); } catch (error) { console.error(stringify(metadata.toJSON())); @@ -57,7 +59,7 @@ export function defaultValues (registry: Registry, rpcData: string, withThrow = const inner = unwrapStorageType(registry, type, modifier.isOptional); const location = `${mod.name.toString()}.${name.toString()}: ${inner}`; - it(`creates default types for ${location}`, (): void => { + it(location, (): void => { expect((): void => { try { const type = registry.createType(inner, hexToU8a(fallback.toHex())); @@ -82,3 +84,18 @@ export function defaultValues (registry: Registry, rpcData: string, withThrow = }); }); } + +export function testMeta (version: number, matchers: Record, withFallback = true): void { + describe(`MetadataV${version}`, (): void => { + describe.each(Object.keys(matchers))('%s', (type): void => { + const matcher = matchers[type]; + const registry = new TypeRegistry(); + + decodeLatestMeta(registry, version, matcher); + + toLatest(registry, version, matcher.data); + + defaultValues(registry, matcher.data, true, withFallback); + }); + }); +} diff --git a/packages/types/src/metadata/util/types.ts b/packages/types/src/metadata/util/types.ts new file mode 100644 index 0000000000..0de8b2b448 --- /dev/null +++ b/packages/types/src/metadata/util/types.ts @@ -0,0 +1,8 @@ +// Copyright 2017-2021 @polkadot/types authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +export interface Check { + compare: Record; + data: string; + types?: Record; +} diff --git a/packages/types/src/metadata/v10/Metadata.spec.ts b/packages/types/src/metadata/v10/Metadata.spec.ts index f464743e7c..5a9f2142a1 100644 --- a/packages/types/src/metadata/v10/Metadata.spec.ts +++ b/packages/types/src/metadata/v10/Metadata.spec.ts @@ -1,17 +1,13 @@ // Copyright 2017-2021 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { TypeRegistry } from '../../create'; -import { decodeLatestMeta, defaultValues, toLatest } from '../util/testUtil'; -import substrateData from './hex/static-substrate'; -import substrateJson from './json/static-substrate.json'; +import { testMeta } from '../util/testUtil'; +import substrateData from './substrate-hex'; +import substrateJson from './substrate-json.json'; -describe('MetadataV10 (substrate)', (): void => { - const registry = new TypeRegistry(); - - decodeLatestMeta(registry, 10, substrateData, substrateJson); - - toLatest(registry, 10, substrateData); - - defaultValues(registry, substrateData); -}); +testMeta(10, { + substrate: { + compare: substrateJson as Record, + data: substrateData + } +}, false); diff --git a/packages/types/src/metadata/v10/hex/static-substrate.ts b/packages/types/src/metadata/v10/substrate-hex.ts similarity index 100% rename from packages/types/src/metadata/v10/hex/static-substrate.ts rename to packages/types/src/metadata/v10/substrate-hex.ts diff --git a/packages/types/src/metadata/v10/json/static-substrate.json b/packages/types/src/metadata/v10/substrate-json.json similarity index 100% rename from packages/types/src/metadata/v10/json/static-substrate.json rename to packages/types/src/metadata/v10/substrate-json.json diff --git a/packages/types/src/metadata/v11/Metadata.spec.ts b/packages/types/src/metadata/v11/Metadata.spec.ts index 42dafc8be3..a77be473bf 100644 --- a/packages/types/src/metadata/v11/Metadata.spec.ts +++ b/packages/types/src/metadata/v11/Metadata.spec.ts @@ -1,17 +1,13 @@ // Copyright 2017-2021 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { TypeRegistry } from '../../create'; -import { decodeLatestMeta, defaultValues, toLatest } from '../util/testUtil'; -import substrateData from './hex/static-substrate'; -import substrateJson from './json/static-substrate.json'; +import { testMeta } from '../util/testUtil'; +import substrateData from './substrate-hex'; +import substrateJson from './substrate-json.json'; -describe('MetadataV11 (substrate)', (): void => { - const registry = new TypeRegistry(); - - decodeLatestMeta(registry, 11, substrateData, substrateJson); - - toLatest(registry, 11, substrateData); - - defaultValues(registry, substrateData); -}); +testMeta(11, { + substrate: { + compare: substrateJson as Record, + data: substrateData + } +}, false); diff --git a/packages/types/src/metadata/v11/hex/static-substrate.ts b/packages/types/src/metadata/v11/substrate-hex.ts similarity index 100% rename from packages/types/src/metadata/v11/hex/static-substrate.ts rename to packages/types/src/metadata/v11/substrate-hex.ts diff --git a/packages/types/src/metadata/v11/json/static-substrate.json b/packages/types/src/metadata/v11/substrate-json.json similarity index 100% rename from packages/types/src/metadata/v11/json/static-substrate.json rename to packages/types/src/metadata/v11/substrate-json.json diff --git a/packages/types/src/metadata/v12/Metadata.spec.ts b/packages/types/src/metadata/v12/Metadata.spec.ts index 938f09dcb2..76120de21d 100644 --- a/packages/types/src/metadata/v12/Metadata.spec.ts +++ b/packages/types/src/metadata/v12/Metadata.spec.ts @@ -1,17 +1,13 @@ // Copyright 2017-2021 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { TypeRegistry } from '../../create'; -import { decodeLatestMeta, defaultValues, toLatest } from '../util/testUtil'; -import substrateData from './hex/static-substrate'; -import substrateJson from './json/static-substrate.json'; +import { testMeta } from '../util/testUtil'; +import substrateData from './substrate-hex'; +import substrateJson from './substrate-json.json'; -describe('MetadataV12 (substrate)', (): void => { - const registry = new TypeRegistry(); - - decodeLatestMeta(registry, 12, substrateData, substrateJson); - - toLatest(registry, 12, substrateData); - - defaultValues(registry, substrateData, true, true); +testMeta(12, { + substrate: { + compare: substrateJson as Record, + data: substrateData + } }); diff --git a/packages/types/src/metadata/v12/hex/static-substrate.ts b/packages/types/src/metadata/v12/substrate-hex.ts similarity index 100% rename from packages/types/src/metadata/v12/hex/static-substrate.ts rename to packages/types/src/metadata/v12/substrate-hex.ts diff --git a/packages/types/src/metadata/v12/json/static-substrate.json b/packages/types/src/metadata/v12/substrate-json.json similarity index 100% rename from packages/types/src/metadata/v12/json/static-substrate.json rename to packages/types/src/metadata/v12/substrate-json.json diff --git a/packages/types/src/metadata/v13/Metadata.spec.ts b/packages/types/src/metadata/v13/Metadata.spec.ts index 52cfb80835..1b10ef4dd1 100644 --- a/packages/types/src/metadata/v13/Metadata.spec.ts +++ b/packages/types/src/metadata/v13/Metadata.spec.ts @@ -1,29 +1,19 @@ // Copyright 2017-2021 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { TypeRegistry } from '../../create'; -import { decodeLatestMeta, defaultValues, toLatest } from '../util/testUtil'; -import polkadotData from './hex/static-polkadot'; -import substrateData from './hex/static-substrate'; -import polkadotJson from './json/static-polkadot.json'; -import substrateJson from './json/static-substrate.json'; +import { testMeta } from '../util/testUtil'; +import polkadotData from './polkadot-hex'; +import polkadotJson from './polkadot-json.json'; +import substrateData from './substrate-hex'; +import substrateJson from './substrate-json.json'; -describe('MetadataV13 (substrate)', (): void => { - const registry = new TypeRegistry(); - - decodeLatestMeta(registry, 13, substrateData, substrateJson); - - toLatest(registry, 13, substrateData); - - defaultValues(registry, substrateData, true, true); -}); - -describe('MetadataV13 (polkadot)', (): void => { - const registry = new TypeRegistry(); - - decodeLatestMeta(registry, 13, polkadotData, polkadotJson); - - toLatest(registry, 13, polkadotData); - - defaultValues(registry, polkadotData, true, true); +testMeta(13, { + polkadot: { + compare: polkadotJson as Record, + data: polkadotData + }, + substrate: { + compare: substrateJson as Record, + data: substrateData + } }); diff --git a/packages/types/src/metadata/v13/hex/static-polkadot.ts b/packages/types/src/metadata/v13/polkadot-hex.ts similarity index 100% rename from packages/types/src/metadata/v13/hex/static-polkadot.ts rename to packages/types/src/metadata/v13/polkadot-hex.ts diff --git a/packages/types/src/metadata/v13/json/static-polkadot.json b/packages/types/src/metadata/v13/polkadot-json.json similarity index 100% rename from packages/types/src/metadata/v13/json/static-polkadot.json rename to packages/types/src/metadata/v13/polkadot-json.json diff --git a/packages/types/src/metadata/v13/hex/static-substrate.ts b/packages/types/src/metadata/v13/substrate-hex.ts similarity index 100% rename from packages/types/src/metadata/v13/hex/static-substrate.ts rename to packages/types/src/metadata/v13/substrate-hex.ts diff --git a/packages/types/src/metadata/v13/json/static-substrate.json b/packages/types/src/metadata/v13/substrate-json.json similarity index 100% rename from packages/types/src/metadata/v13/json/static-substrate.json rename to packages/types/src/metadata/v13/substrate-json.json diff --git a/packages/types/src/metadata/v14/Metadata.spec.ts b/packages/types/src/metadata/v14/Metadata.spec.ts index 2dc5084ba0..e9a2090b8d 100644 --- a/packages/types/src/metadata/v14/Metadata.spec.ts +++ b/packages/types/src/metadata/v14/Metadata.spec.ts @@ -5,12 +5,12 @@ import type { MetadataAll } from '../../interfaces/metadata'; import { TypeRegistry } from '../../create'; import { Metadata } from '../Metadata'; -import polkadotData from './hex/static-polkadot'; -import substrateData from './hex/static-substrate'; -import polkadotJson from './json/static-polkadot.json'; -import polkadotTypes from './json/static-polkadot-types.json'; -import substrateJson from './json/static-substrate.json'; -import substrateTypes from './json/static-substrate-types.json'; +import polkadotData from './polkadot-hex'; +import polkadotJson from './polkadot-json.json'; +import polkadotTypes from './polkadot-types.json'; +import substrateData from './substrate-hex'; +import substrateJson from './substrate-json.json'; +import substrateTypes from './substrate-types.json'; describe('MetadataV14 (substrate)', (): void => { // FIXME This is to be replaced with the proper version (here as a stop-gap) diff --git a/packages/types/src/metadata/v14/hex/static-polkadot.ts b/packages/types/src/metadata/v14/polkadot-hex.ts similarity index 100% rename from packages/types/src/metadata/v14/hex/static-polkadot.ts rename to packages/types/src/metadata/v14/polkadot-hex.ts diff --git a/packages/types/src/metadata/v14/json/static-polkadot.json b/packages/types/src/metadata/v14/polkadot-json.json similarity index 100% rename from packages/types/src/metadata/v14/json/static-polkadot.json rename to packages/types/src/metadata/v14/polkadot-json.json diff --git a/packages/types/src/metadata/v14/json/static-polkadot-types.json b/packages/types/src/metadata/v14/polkadot-types.json similarity index 100% rename from packages/types/src/metadata/v14/json/static-polkadot-types.json rename to packages/types/src/metadata/v14/polkadot-types.json diff --git a/packages/types/src/metadata/v14/hex/static-substrate.ts b/packages/types/src/metadata/v14/substrate-hex.ts similarity index 100% rename from packages/types/src/metadata/v14/hex/static-substrate.ts rename to packages/types/src/metadata/v14/substrate-hex.ts diff --git a/packages/types/src/metadata/v14/json/static-substrate.json b/packages/types/src/metadata/v14/substrate-json.json similarity index 100% rename from packages/types/src/metadata/v14/json/static-substrate.json rename to packages/types/src/metadata/v14/substrate-json.json diff --git a/packages/types/src/metadata/v14/json/static-substrate-types.json b/packages/types/src/metadata/v14/substrate-types.json similarity index 100% rename from packages/types/src/metadata/v14/json/static-substrate-types.json rename to packages/types/src/metadata/v14/substrate-types.json diff --git a/packages/types/src/metadata/v9/Metadata.spec.ts b/packages/types/src/metadata/v9/Metadata.spec.ts index 2912f21f67..846adbb08f 100644 --- a/packages/types/src/metadata/v9/Metadata.spec.ts +++ b/packages/types/src/metadata/v9/Metadata.spec.ts @@ -1,17 +1,13 @@ // Copyright 2017-2021 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { TypeRegistry } from '../../create'; -import { decodeLatestMeta, defaultValues, toLatest } from '../util/testUtil'; -import substrateData from './hex/static-substrate'; -import substrateJson from './json/static-substrate.json'; +import { testMeta } from '../util/testUtil'; +import substrateData from './substrate-hex'; +import substrateJson from './substrate-json.json'; -describe('MetadataV9 (substrate)', (): void => { - const registry = new TypeRegistry(); - - decodeLatestMeta(registry, 9, substrateData, substrateJson); - - toLatest(registry, 9, substrateData); - - defaultValues(registry, substrateData); -}); +testMeta(9, { + substrate: { + compare: substrateJson as Record, + data: substrateData + } +}, false); diff --git a/packages/types/src/metadata/v9/hex/static-substrate.ts b/packages/types/src/metadata/v9/substrate-hex.ts similarity index 100% rename from packages/types/src/metadata/v9/hex/static-substrate.ts rename to packages/types/src/metadata/v9/substrate-hex.ts diff --git a/packages/types/src/metadata/v9/json/static-substrate.json b/packages/types/src/metadata/v9/substrate-json.json similarity index 100% rename from packages/types/src/metadata/v9/json/static-substrate.json rename to packages/types/src/metadata/v9/substrate-json.json diff --git a/packages/types/src/primitive/StorageKey.spec.ts b/packages/types/src/primitive/StorageKey.spec.ts index 25fa15893c..0e8abbc1da 100644 --- a/packages/types/src/primitive/StorageKey.spec.ts +++ b/packages/types/src/primitive/StorageKey.spec.ts @@ -3,7 +3,7 @@ import { TypeRegistry } from '../create'; import { decorateStorage, Metadata } from '../metadata'; -import rpcDataV11 from '../metadata/v11/hex/static-substrate'; +import rpcDataV11 from '../metadata/v11/substrate-hex'; import { StorageKey } from '.'; describe('StorageKey', (): void => {