Skip to content

Commit

Permalink
test framework structure update
Browse files Browse the repository at this point in the history
  • Loading branch information
soham-bentley committed Dec 17, 2024
1 parent 171420a commit ebd443f
Show file tree
Hide file tree
Showing 32 changed files with 30 additions and 30 deletions.
3 changes: 2 additions & 1 deletion core/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
"node": "^20.0.0 || ^22.0.0"
},
"scripts": {
"build": "npm run -s build:cjs && npm run -s copy:assets && npm run -s copy:test-assets",
"build": "npm run -s build:cjs && npm run -s copy:assets && npm run -s copy:test-assets && npm run -s copy:ecsqltestmdfiles",
"build:cjs": "tsc 1>&2 --outDir lib/cjs",
"clean": "rimraf lib .rush/temp/package-deps*.json ../../tools/internal/ios/core-test-runner/build ../../tools/internal/lib",
"docs": "betools docs --json=../../generated-docs/core/core-backend/file.json --tsIndexFile=./core-backend.ts --onlyJson --excludeGlob=**/*.d.ts",
"copy:assets": "cpx \"./src/assets/**/*\" ./lib/cjs/assets",
"copy:ecsqltestmdfiles": "cpx \"./src/test/ecsql/queries/*\" ./lib/cjs/test/ecsql/queries",
"copy:config": "internal-tools copy-config",
"copy:test-assets": "cpx \"./src/test/assets/**/*\" ./lib/cjs/test/assets",
"cover": "nyc npm -s test",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Readme for ECDb tests

This file is the readme for the ECDB SQL test runner. All files ending in .ecdbtest.md in the directory will be used.
This file is the readme for the ECDB SQL test runner. All files ending in .ecsql.md in the directory will be used.

A h1-header (#) indicates a new test and is at the same time the title of the test. A file may contain multiple tests
Regular text, blockquotes or html comments can be used to describe the test, they do not affect test execution
Expand Down Expand Up @@ -78,7 +78,7 @@ SELECT * from meta.ECSchemaDef LIMIT 5
- bindInt 1, 23
- bindInt param2, 3

For all example related to binders please follow `Binders.ecdbtest.md` file
For all example related to binders please follow `Binders.ecsql.md` file

> If we expect prepare to fail use:
>
Expand Down Expand Up @@ -170,15 +170,15 @@ As an alternative, column infos can be formatted as json in a code block like th
]
```

> Note :- When dealing with binary data always enclose the Uint8Array values within `BIN()`. Follow examples written in `DataTypes.ecdbtest.md`
> Note :- When dealing with binary data always enclose the Uint8Array values within `BIN()`. Follow examples written in `DataTypes.ecsql.md`
Check out the Examples.ecdbtest.md or other files in this directory.
Check out the Examples.ecsql.md or other files in this directory.

## Test generator

There is a script which takes a statement as input and generates a test from it.
Check ECDbMarkdownTestGenerator.ts, it can be called like this:
`node lib\cjs\test\ecdb\ECDbMarkdownTestGenerator.js AllProperties.bim "SELECT * FROM meta.ECSchemaDef LIMIT 2" -t`
`node lib\cjs\test\ecsql\src\ECSqlTestGenerator.js AllProperties.bim "SELECT * FROM meta.ECSchemaDef LIMIT 2" -t`

## Generate coverage

Expand All @@ -188,4 +188,4 @@ Link the native addon into you itwinjs-core directory ( iModelJsNodeAddon\linkna
Build the tests ( npm run build in core/backend )
This is the command to run from the core/backend working dir to generate coverage:

> OpenCppCoverage --sources (pathToNative)\src\imodel-native\iModelCore\ECDb\* --modules iTwin* --cover_children --export_type html:lib/coverage -- "C:\Program Files\nodejs\npm.cmd" run test
> OpenCppCoverage --sources (pathToNative)\src\imodel-native\iModelCore\ECDb\* --modules iTwin\* --cover_children --export_type html:lib/coverage -- "C:\Program Files\nodejs\npm.cmd" run test
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import { assert } from "chai";
import * as path from "path";
import { Id64, Id64String } from "@itwin/core-bentley";
import { _nativeDb, IModelDb, IModelHost, SnapshotDb, SpatialCategory } from "../../core-backend";
import { KnownTestLocations } from "../KnownTestLocations";
import { IModelTestUtils } from "../IModelTestUtils";
import { _nativeDb, IModelDb, IModelHost, SnapshotDb, SpatialCategory } from "../../../core-backend";
import { IModelTestUtils } from "../../IModelTestUtils";
import { Code, ColorDef, ElementAspectProps, GeometryStreamProps, IModel, PhysicalElementProps, RelatedElementProps, SubCategoryAppearance } from "@itwin/core-common";
import { Arc3d, IModelJson, Point2d, Point3d } from "@itwin/core-geometry";
import { KnownTestLocations } from "../../KnownTestLocations";


interface IPrimitiveBase {
Expand Down Expand Up @@ -164,14 +164,14 @@ function createElemWithNavProp(className: string, _iModelName: IModelDb, modId:
} as TestElementWithNavProps;
}

export class ECDbMarkdownDatasets {
export class ECSqlDatasets {
public static async generateFiles(): Promise<void> {
const fileName = "AllProperties.bim";

await IModelHost.startup();
const filePath = IModelTestUtils.prepareOutputFile("ECDbTests", fileName);
const filePath = IModelTestUtils.prepareOutputFile("ECSqlTests", fileName);
const iModel = SnapshotDb.createEmpty(filePath, { rootSubject: { name: "AllPropertiesTest" } });
const testSchemaPath = path.join(KnownTestLocations.assetsDir, "ECDbTests", "AllProperties.ecschema.xml");
const testSchemaPath = path.join(KnownTestLocations.assetsDir, "ECSqlTests", "AllProperties.ecschema.xml");
await iModel.importSchemas([testSchemaPath]);
const [, newModelId] = IModelTestUtils.createAndInsertPhysicalPartitionAndModel(iModel, Code.createEmpty(), true);
let spatialCategoryId = SpatialCategory.queryCategoryIdByName(iModel, IModel.dictionaryId, "MySpatialCategory");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/

import { IModelDb, SnapshotDb } from "../../core-backend";
import { IModelDb, SnapshotDb } from "../../../core-backend";
import { DbResult, ECSqlValueType, QueryOptionsBuilder, QueryPropertyMetaData, QueryRowFormat } from "@itwin/core-common";
import * as path from "path";
import * as fs from "fs";
import * as crypto from "crypto";
import { ECDbMarkdownDatasets } from "./ECDbMarkdownDatasets";
import { KnownTestLocations } from "../KnownTestLocations";
import { ECSqlDatasets } from "../dataset/ECSqlDatasets";
import { KnownTestLocations } from "../../KnownTestLocations";
import { format } from "sql-formatter";

// Call like this:
// node lib\cjs\test\ecdb\ECDbMarkdownTestGenerator.js AllProperties.bim "SELECT * FROM meta.ECSchemaDef LIMIT 2" -t
// node lib\cjs\test\ecdb\ECDbMarkdownTestGenerator.js AllProperties.bim "SELECT te.ECInstanceId [MyId], te.s, te.DT [Date], row_number() over(PARTITION BY te.DT ORDER BY te.ECInstanceId) as [RowNumber] from aps.TestElement te WHERE te.i < 106" -t
// node lib\cjs\test\ecsql\src\ECSqlTestGenerator.js AllProperties.bim "SELECT * FROM meta.ECSchemaDef LIMIT 2" -t
// node lib\cjs\test\ecsql\src\ECSqlTestGenerator.js AllProperties.bim "SELECT te.ECInstanceId [MyId], te.s, te.DT [Date], row_number() over(PARTITION BY te.DT ORDER BY te.ECInstanceId) as [RowNumber] from aps.TestElement te WHERE te.i < 106" -t
async function runConcurrentQuery(imodel: IModelDb, sql: string): Promise<{metadata: any[], rows: any[] }> {
const queryOptions: QueryOptionsBuilder = new QueryOptionsBuilder();
queryOptions.setRowFormat(QueryRowFormat.UseECSqlPropertyNames);
Expand Down Expand Up @@ -109,7 +109,7 @@ ${JSON.stringify(results, null, 2)}
`;
}

const outputFilePath = path.join(__dirname, "generated.ecdbtest.md");
const outputFilePath = path.join(__dirname, "generated.ecsql.md");
fs.appendFileSync(outputFilePath, markdownContent, "utf-8");
// eslint-disable-next-line no-console
console.log(`Results written to ${outputFilePath}`);
Expand All @@ -127,8 +127,8 @@ async function main() {
const useTables = tablesFlag === "-t";
let imodel: IModelDb | undefined;
try {
await ECDbMarkdownDatasets.generateFiles();
const datasetFilePath = path.join(KnownTestLocations.outputDir, "ECDbTests", dataset);
await ECSqlDatasets.generateFiles();
const datasetFilePath = path.join(KnownTestLocations.outputDir, "ECSqlTests", dataset);
imodel = SnapshotDb.openFile(datasetFilePath);
const { metadata, rows } = await runConcurrentQuery(imodel, sql);
pullAdditionalMetadataThroughECSqlStatement(imodel, metadata, sql);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import * as path from "path";
import * as fs from "fs";
import { marked, Tokens } from "marked";
import { IModelTestUtils } from "../IModelTestUtils";

export interface ECDbTestProps {
fileName: string;
Expand Down Expand Up @@ -180,8 +179,8 @@ function understandAndReplaceBinaryData(str: string): any{

export class ECDbMarkdownTestParser {
public static parse(): ECDbTestProps[] {
const testAssetsDir = IModelTestUtils.resolveAssetFile("ECDbTests");
const testFiles = fs.readdirSync(testAssetsDir, "utf-8").filter((fileName) => fileName.toLowerCase().endsWith("ecdbtest.md"));
const testAssetsDir = path.join(__dirname ,"..","queries");
const testFiles = fs.readdirSync(testAssetsDir, "utf-8").filter((fileName) => fileName.toLowerCase().endsWith("ecsql.md"));
const out: ECDbTestProps[] = [];

for (const fileName of testFiles) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
*--------------------------------------------------------------------------------------------*/
import { assert } from "chai";
import { DbResult } from "@itwin/core-bentley";
import { ECSqlRowArg, ECSqlStatement, SnapshotDb } from "../../core-backend";
import { KnownTestLocations } from "../KnownTestLocations";
import { ECSqlRowArg, ECSqlStatement, SnapshotDb } from "../../../core-backend";
import { KnownTestLocations } from "../../KnownTestLocations";
import { ECSqlReader, ECSqlValueType, QueryBinder, QueryOptions, QueryRowFormat } from "@itwin/core-common";
import { buildBinaryData, ECDbMarkdownTestParser, ECDbTestMode, ECDbTestProps, ECDbTestRowFormat } from "./ECDbMarkdownTestParser";
import { buildBinaryData, ECDbMarkdownTestParser, ECDbTestMode, ECDbTestProps, ECDbTestRowFormat } from "./ECSqlTestParser";
import * as path from "path";
import * as fs from "fs";
import { ECDbMarkdownDatasets } from "./ECDbMarkdownDatasets";
import { ECSqlDatasets } from "../dataset/ECSqlDatasets";
import { Point2d, Point3d } from "@itwin/core-geometry";

enum TestDataset {
Expand All @@ -21,8 +21,8 @@ const snapshotDbs: { [key in TestDataset]?: SnapshotDb } = {};

describe("Markdown based ECDb test runner", async () => {
before(async () => {
await ECDbMarkdownDatasets.generateFiles();
const datasetFilePath = path.join(KnownTestLocations.outputDir, "ECDbTests", TestDataset.AllProperties);
await ECSqlDatasets.generateFiles();
const datasetFilePath = path.join(KnownTestLocations.outputDir, "ECSqlTests", TestDataset.AllProperties);
if (!fs.existsSync(datasetFilePath)) {
throw new Error(`Dataset file ${datasetFilePath} does not exist`);
}
Expand Down

0 comments on commit ebd443f

Please sign in to comment.