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

Feature/unit test2 #32260

Closed
wants to merge 16 commits into from
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"test:signoff": "npm run integration-test:node -- --fgrep \"nosignoff\" --invert",
"unit-test": "npm run unit-test:node && npm run unit-test:browser",
"unit-test:browser": "echo skipped",
"unit-test:node": "echo skipped",
"unit-test:node": "dev-tool run vendored cross-env NODE_OPTIONS='--dns-result-order=ipv4first' mocha -r test/mocha.env.ts -r ts-node/register -r dotenv/config -r ./test/public/common/setup.ts --reporter ../../../common/tools/mocha-multi-reporter.js --reporter-option output=test-results.xml \"./test/internal/**/*.spec.ts\" --timeout 100000",
"update-snippets": "echo skipped"
},
"repository": "github:Azure/azure-sdk-for-js",
Expand Down
2 changes: 2 additions & 0 deletions sdk/cosmosdb/cosmos/test/internal/unit/platform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe("getUserAgent", function () {
});

it("should contain the current node version", () => {
console.log(getUserAgent());
console.log(process.version.replace("v", ""));
assert(getUserAgent().includes(process.version.replace("v", "")));
});

Expand Down
43 changes: 28 additions & 15 deletions sdk/cosmosdb/cosmos/test/public/common/TestHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,22 @@ import path from "path";
const defaultRoutingGatewayPort: string = ":8081";
const defaultComputeGatewayPort: string = ":8903";

export const defaultClient = new CosmosClient({
endpoint,
key: masterKey,
connectionPolicy: { enableBackgroundEndpointRefreshing: false },
diagnosticLevel: CosmosDbDiagnosticLevel.info,
});

export const defaultComputeGatewayClient = new CosmosClient({
endpoint: endpoint.replace(defaultRoutingGatewayPort, defaultComputeGatewayPort),
key: masterKey,
connectionPolicy: { enableBackgroundEndpointRefreshing: false },
});
export function getDefaultClient(): CosmosClient {
return new CosmosClient({
endpoint,
key: masterKey,
connectionPolicy: { enableBackgroundEndpointRefreshing: false },
diagnosticLevel: CosmosDbDiagnosticLevel.info,
});
}

export function getDefaultComputeGatewayClient(): CosmosClient {
return new CosmosClient({
endpoint: endpoint.replace(defaultRoutingGatewayPort, defaultComputeGatewayPort),
key: masterKey,
connectionPolicy: { enableBackgroundEndpointRefreshing: false },
});
}

export function addEntropy(name: string): string {
return name + getEntropy();
Expand All @@ -65,8 +69,11 @@ export function getEntropy(): string {
return `${Math.floor(Math.random() * 10000)}`;
}

export async function removeAllDatabases(client: CosmosClient = defaultClient): Promise<void> {
export async function removeAllDatabases(client?: CosmosClient): Promise<void> {
try {
if (!client) {
client = getDefaultClient();
}
const { resources: databases } = await client.databases.readAll().fetchAll();
const length = databases.length;

Expand Down Expand Up @@ -424,9 +431,12 @@ function validateRequestStartTimeForDiagnostics(

export async function getTestDatabase(
testName: string,
client: CosmosClient = defaultClient,
client?: CosmosClient,
attrs?: Partial<DatabaseRequest>,
): Promise<Database> {
if (!client) {
client = getDefaultClient();
}
const entropy = Math.floor(Math.random() * 10000);
const id = `${testName.replace(" ", "").substring(0, 30)}${entropy}`;
await client.databases.create({ id, ...attrs });
Expand All @@ -435,10 +445,13 @@ export async function getTestDatabase(

export async function getTestContainer(
testName: string,
client: CosmosClient = defaultClient,
client?: CosmosClient,
containerDef?: ContainerRequest,
options?: RequestOptions,
): Promise<Container> {
if (!client) {
client = getDefaultClient();
}
const db = await getTestDatabase(testName, client);
const entropy = Math.floor(Math.random() * 10000);
const id = `${testName.replace(" ", "").substring(0, 30)}${entropy}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type { Container, CosmosClient } from "../../../../src";
import {
getTestContainer,
removeAllDatabases,
defaultClient,
defaultComputeGatewayClient,
getDefaultClient,
getDefaultComputeGatewayClient,
} from "../../common/TestHelpers";

interface ItemPayload {
Expand Down Expand Up @@ -36,7 +36,9 @@ const executeTestCase = async function (
scenario: TestScenario,
useComputeGateway: boolean = false,
) {
const client: CosmosClient = useComputeGateway ? defaultComputeGatewayClient : defaultClient;
const client: CosmosClient = useComputeGateway
? getDefaultComputeGatewayClient()
: getDefaultClient();
const container: Container = await getTestContainer(scenario.name, client, {
partitionKey: {
paths: ["/pk"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
/* eslint-disable no-unused-expressions */
import assert from "assert";
import type { Suite } from "mocha";
import type { ClientContext, Container, PluginConfig } from "../../src";
import { PluginOn } from "../../src";
import { OperationType, ResourceType } from "../../src/common";
import { ConsistencyLevel } from "../../src";
import { CosmosClient } from "../../src";
import type { SessionContainer } from "../../src/session/sessionContainer";
import { endpoint } from "../public/common/_testConfig";
import { masterKey } from "../public/common/_fakeTestSecrets";
import { addEntropy, getTestDatabase, removeAllDatabases } from "../public/common/TestHelpers";
import type { RequestContext } from "../../src";
import type { Response } from "../../src/request/Response";
import type { ClientContext, Container, PluginConfig } from "../../../src";
import { PluginOn } from "../../../src";
import { OperationType, ResourceType } from "../../../src/common";
import { ConsistencyLevel } from "../../../src";
import { CosmosClient } from "../../../src";
import type { SessionContainer } from "../../../src/session/sessionContainer";
import { endpoint } from "../../public/common/_testConfig";
import { masterKey } from "../../public/common/_fakeTestSecrets";
import { addEntropy, getTestDatabase, removeAllDatabases } from "../../public/common/TestHelpers";
import type { RequestContext } from "../../../src";
import type { Response } from "../../../src/request/Response";
import { expect } from "chai";

describe("New session token", function () {
Expand Down
4 changes: 2 additions & 2 deletions sdk/cosmosdb/cosmos/tsconfig.strict.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
"src/request/globalStatistics.ts",
"src/request/hybridSearchQueryResult.ts",
"test/public/common/TestHelpers.ts",
"test/internal/session.spec.ts",
"test/public/integration/session.spec.ts",
"test/internal/unit/auth.spec.ts",
"test/internal/unit/defaultQueryExecutionContext.spec.ts",
"test/internal/unit/helper.spec.ts",
Expand All @@ -177,7 +177,7 @@
"test/internal/unit/utils/batch.spec.ts",
"test/internal/unit/utils/checkURL.spec.ts",
"test/internal/unit/utils/offer.spec.ts",
"test/internal/unit/client.spec.ts",
"test/public/integration/client.spec.ts",
"test/internal/unit/timeoutFailoverRetryPolicy.spec.ts",
"test/internal/unit/nonStreamingOrderByMap.spec.ts",
"test/internal/unit/utils/supportedQueryFeaturesBuilder.spec.ts",
Expand Down
Loading