UNPKG

@itwin/ecschema-rpcinterface-common

Version:

Schema RPC Interface common interface

130 lines • 7.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ const ecschema_metadata_1 = require("@itwin/ecschema-metadata"); const RpcIncrementalSchemaLocater_1 = require("../RpcIncrementalSchemaLocater"); const core_common_1 = require("@itwin/core-common"); const ECSchemaRpcInterface_1 = require("../ECSchemaRpcInterface"); const chai_1 = require("chai"); const sinon = require("sinon"); // RpcIncrementalSchemaLocater derives from IncrementalSchemaLocater and ECSqlSchemaLocater, so // the basic functionality is already tested in their tests. This test suite verifies the expected // behaviour of the RPC specific implementation. describe("RpcIncrementalSchemaLocater Tests", () => { let imodelReadInterface; let ecschemaRpcInterface; const schemaInfoColumns = ["name", "version", "references", "alias"]; beforeEach(() => { sinon.stub(core_common_1.IModelReadRpcInterface, "getClient").returns(imodelReadInterface = { queryRows: async () => { throw new Error("Method must be implemented in the tests."); }, }); sinon.stub(ECSchemaRpcInterface_1.ECSchemaRpcInterface, "getClient").returns(ecschemaRpcInterface = { getSchemaJSON: async () => { throw new Error("Method must be implemented in the tests."); }, }); }); afterEach(() => { sinon.restore(); }); it("get schema info, schema found", async () => { const schemaKey = ecschema_metadata_1.SchemaKey.parseString("TestSchema.1.0.0"); sinon.stub(imodelReadInterface, "queryRows").returns(toQueryResult(schemaInfoColumns, [ { name: schemaKey.name, version: schemaKey.version.toString(), references: "[]", alias: "ts" } ])); const locater = new RpcIncrementalSchemaLocater_1.RpcIncrementalSchemaLocater({ key: "rpc-test-imodel" }); const schemaInfo = await locater.getSchemaInfo(schemaKey, ecschema_metadata_1.SchemaMatchType.Latest, new ecschema_metadata_1.SchemaContext()); (0, chai_1.expect)(schemaInfo).is.not.undefined; (0, chai_1.expect)(schemaInfo).has.property("schemaKey").that.satisfies((key) => key.matches(schemaKey)); (0, chai_1.expect)(schemaInfo).has.property("alias").that.equals("ts"); (0, chai_1.expect)(schemaInfo).has.property("references").that.is.empty; }); it("get schema info, schema not found, return undefined", async () => { sinon.stub(imodelReadInterface, "queryRows").returns(toQueryResult(schemaInfoColumns, [])); const schemaKey = ecschema_metadata_1.SchemaKey.parseString("TestSchema.1.0.0"); const locater = new RpcIncrementalSchemaLocater_1.RpcIncrementalSchemaLocater({ key: "rpc-test-imodel" }); const schemaInfo = await locater.getSchemaInfo(schemaKey, ecschema_metadata_1.SchemaMatchType.Latest, new ecschema_metadata_1.SchemaContext()); (0, chai_1.expect)(schemaInfo).to.be.undefined; }); it("get schema, incrementally", async () => { const context = new ecschema_metadata_1.SchemaContext(); const locater = new RpcIncrementalSchemaLocater_1.RpcIncrementalSchemaLocater({ key: "rpc-test-imodel" }); context.addLocater(locater); const schemaKey = ecschema_metadata_1.SchemaKey.parseString("TestSchema.1.0.0"); sinon.stub(imodelReadInterface, "queryRows") .onCall(0).returns(toQueryResult(schemaInfoColumns, [ { name: schemaKey.name, version: schemaKey.version.toString(), references: "[]", alias: "ts" }, { name: "ECDbMeta", version: "4.0.3", references: "[]", alias: "ecdb" }, ])) .onCall(1).returns(toQueryResult(["name", "version", "alias", "references", "items"], [ { name: schemaKey.name, version: schemaKey.version.toString(), alias: "ts", references: "[]", items: "[]", } ])) .returns(toQueryResult(["schema", "items"], [ { schema: `{ "name": "${schemaKey.name}", "version": "${schemaKey.version.toString()}", "alias": "ts", "label": "Test Schema", "description": "This is a test schema." }`, } ])); const schema = await context.getSchema(schemaKey, ecschema_metadata_1.SchemaMatchType.Latest); (0, chai_1.expect)(schema).is.not.undefined; (0, chai_1.expect)(schema).has.property("schemaKey").that.satisfies((key) => key.matches(schemaKey)); (0, chai_1.expect)(schema).has.property("alias").that.equals("ts"); (0, chai_1.expect)(schema).has.property("loadingController").that.is.not.undefined; (0, chai_1.expect)(schema).has.a.nested.property("loadingController.inProgress").that.is.true; await schema.loadingController?.wait(); (0, chai_1.expect)(schema).has.property("label", "Test Schema"); (0, chai_1.expect)(schema).has.property("description", "This is a test schema."); }); it("get schema, unsupported metaschema", async () => { const schemaKey = ecschema_metadata_1.SchemaKey.parseString("TestSchema.1.0.0"); sinon.stub(imodelReadInterface, "queryRows").returns(toQueryResult(schemaInfoColumns, [ { name: schemaKey.name, version: schemaKey.version.toString(), references: "[]", alias: "ts" }, { name: "ECDbMeta", version: "4.0.1", references: "[]", alias: "ecdb" }, ])); sinon.stub(ecschemaRpcInterface, "getSchemaJSON").returns(Promise.resolve({ $schema: ecschema_metadata_1.ECSchemaNamespaceUris.SCHEMAURL3_2_JSON, name: schemaKey.name, version: schemaKey.version.toString(), alias: "ts" })); const locater = new RpcIncrementalSchemaLocater_1.RpcIncrementalSchemaLocater({ key: "rpc-test-imodel" }); const schema = await locater.getSchema(schemaKey, ecschema_metadata_1.SchemaMatchType.Latest, new ecschema_metadata_1.SchemaContext()); (0, chai_1.expect)(schema).is.not.undefined; (0, chai_1.expect)(schema).has.property("schemaKey").that.satisfies((key) => key.matches(schemaKey)); (0, chai_1.expect)(schema).has.property("alias").that.equals("ts"); }); it("get schema, schema not found, return undefined", async () => { const schemaKey = ecschema_metadata_1.SchemaKey.parseString("TestSchema.1.0.0"); sinon.stub(imodelReadInterface, "queryRows").returns(toQueryResult(schemaInfoColumns, [])); const locater = new RpcIncrementalSchemaLocater_1.RpcIncrementalSchemaLocater({ key: "rpc-test-imodel" }); const schema = await locater.getSchema(schemaKey, ecschema_metadata_1.SchemaMatchType.Latest, new ecschema_metadata_1.SchemaContext()); (0, chai_1.expect)(schema).is.undefined; }); }); /** * This is a very simple helper function to translate a set of rows into a DbQueryResponse. * It does not handle different row formats or property order. */ async function toQueryResult(columns, rows) { return { kind: core_common_1.DbResponseKind.ECSql, status: core_common_1.DbResponseStatus.Done, stats: {}, rowCount: rows.length, meta: columns.map((column, i) => ({ name: column, jsonName: column, index: i })), data: rows.map(row => columns.map(k => row[k])), }; } //# sourceMappingURL=RpcIncrementalSchemaLocater.test.js.map