Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for computeSchemaChecksum #3733

Merged
merged 13 commits into from
Jun 10, 2022
Merged
3 changes: 3 additions & 0 deletions common/api/core-backend.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3594,6 +3594,9 @@ export class Schema {
static toSemverString(paddedVersion: string): string;
}

// @public
export function generateSchemaSha1Hash(schemaXmlPath: string, referencePaths: string[], isExactMatch: boolean = false): string;

// @internal (undocumented)
export type SchemaKey = IModelJsNative.ECSchemaXmlContext.SchemaKey;

Expand Down
27 changes: 27 additions & 0 deletions core/backend/src/ECSchemaOps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module ECSchemaOps
*/

import { IModelHost } from "./IModelHost";

/**
* Generate SHA1 Hash of Schema XML
* @param schemaXmlPath: Path where schema XML file is located
* @param referencePaths: Schema reference paths
* @param isExactMatch: Schema references are located by exact scheme version comparisons
* @public
*/
export function generateSchemaSha1Hash(schemaXmlPath: string, referencePaths: string[], isExactMatch: boolean = false): string {
kabentley marked this conversation as resolved.
Show resolved Hide resolved
try {
if (isExactMatch)
return IModelHost.platform.computeSchemaChecksumWithExactRefMatch(schemaXmlPath, referencePaths);
else
return IModelHost.platform.computeSchemaChecksum(schemaXmlPath, referencePaths);
} catch (err) {
throw Error(`Error while generating SHA1 Hash: ${err}`);
}
}
1 change: 0 additions & 1 deletion core/backend/src/test/TestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,3 @@ before(async () => {
after(async () => {
await TestUtils.shutdownBackend();
});

kabentley marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ECSchema schemaName="SchemaA" alias="a" version="1.1.1" xmlns="http://www.bentley.com/schemas/Bentley.ECXML.3.2" displayLabel="SchemaA" description="This is a test Schema.">
<ECSchemaReference name="SchemaC" version="3.3.3" alias="c"/>
<ECSchemaReference name="SchemaB" version="2.2.2" alias="b"/>
</ECSchema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ECSchema schemaName="SchemaB" alias="b" version="2.2.3" xmlns="http://www.bentley.com/schemas/Bentley.ECXML.3.2" displayLabel="SchemaB" description="This is a test Schema.">
<ECSchemaReference name="SchemaC" version="3.3.3" alias="c"/>
<ECSchemaReference name="SchemaD" version="4.4.4" alias="d"/>
</ECSchema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<ECSchema schemaName="SchemaC" alias="c" version="3.3.4" xmlns="http://www.bentley.com/schemas/Bentley.ECXML.3.2" displayLabel="SchemaC" description="This is a test Schema.">
<ECSchemaReference name="SchemaD" version="4.4.4" alias="d"/>
</ECSchema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<ECSchema schemaName="SchemaD" alias="d" version="4.4.4" xmlns="http://www.bentley.com/schemas/Bentley.ECXML.3.2" displayLabel="SchemaD" description="This is a test Schema.">
</ECSchema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ECSchema schemaName="SchemaB" alias="b" version="2.2.2" xmlns="http://www.bentley.com/schemas/Bentley.ECXML.3.2" displayLabel="SchemaB" description="This is a test Schema.">
<ECSchemaReference name="SchemaC" version="3.3.3" alias="c"/>
<ECSchemaReference name="SchemaD" version="4.4.4" alias="d"/>
</ECSchema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<ECSchema schemaName="SchemaC" alias="c" version="3.3.3" xmlns="http://www.bentley.com/schemas/Bentley.ECXML.3.2" displayLabel="SchemaC" description="This is a test Schema.">
<ECSchemaReference name="SchemaD" version="4.4.4" alias="d"/>
</ECSchema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<ECSchema schemaName="SchemaD" alias="d" version="4.4.4" xmlns="http://www.bentley.com/schemas/Bentley.ECXML.3.2" displayLabel="SchemaD" description="This is a test Schema.">
</ECSchema>
37 changes: 37 additions & 0 deletions core/backend/src/test/native/ECSchemaOps.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/

import * as path from "path";
import { assert } from "chai";
import { generateSchemaSha1Hash } from "../../ECSchemaOps";
import { KnownTestLocations } from "../KnownTestLocations";

describe("ECSchemaOps", () => {
let assetsDir: string;

before(async () => {
assetsDir = path.join(KnownTestLocations.assetsDir, "ECSchemaOps");
});

after(async () => {
});

it("computeChecksum", async () => {
const schemaPath = path.join(assetsDir, "SchemaA.ecschema.xml");
const refPath = path.dirname(schemaPath);
const sha1 = generateSchemaSha1Hash(schemaPath, [refPath]);
assert.isDefined(sha1);
assert.equal(sha1, "3ac6578060902aa0b8426b61d62045fdf7fa0b2b", "Expected sha1 hash values to match");
});

it("computeChecksumWithExactRefMatch", async () => {
const schemaPath = path.join(assetsDir, "SchemaA.ecschema.xml");
let refPath = path.dirname(schemaPath);
refPath = path.join(refPath, "exact-match");
const sha1 = generateSchemaSha1Hash(schemaPath, [refPath], true);
kabentley marked this conversation as resolved.
Show resolved Hide resolved
assert.isDefined(sha1);
assert.equal(sha1, "2a618664fbba1df7c05f27d7c0e8f58de250003b", "Expected sha1 hash values to match");
});
});