Skip to content

Commit

Permalink
Update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timovv committed Aug 2, 2024
1 parent 410e295 commit 9e8a859
Show file tree
Hide file tree
Showing 29 changed files with 744 additions and 505 deletions.
139 changes: 81 additions & 58 deletions packages/typespec-ts/test/modularUnit/anonymousModel.spec.ts

Large diffs are not rendered by default.

228 changes: 142 additions & 86 deletions packages/typespec-ts/test/modularUnit/apiOperations.spec.ts

Large diffs are not rendered by default.

54 changes: 38 additions & 16 deletions packages/typespec-ts/test/modularUnit/clientContext.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,40 @@ describe("modular client context type", () => {
await assertEqualContent(
clientContext?.getFullText()!,
`
import { ClientOptions } from "@azure-rest/core-client";
import { ServiceContext } from "../rest/index.js";
import getClient from "../rest/index.js";
import { ClientOptions, Client, getClient } from "@azure-rest/core-client";
import { logger } from "../logger.js";
export interface ServiceContext extends Client {}
/** Optional parameters for the client. */
export interface ServiceClientOptionalParams extends ClientOptions {}
export { ServiceContext } from "../rest/index.js";
export function createService(
endpointParam: string,
clientParam: ClientType,
options: ServiceClientOptionalParams = {}
): ServiceContext {
const endpointUrl =
options.endpoint ??
options.baseUrl ??
\`\${endpointParam}/client/structure/\${clientParam}\`;
const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentPrefix = prefixFromOptions
? \`\$\{prefixFromOptions\} azsdk-js-api\`
: "azsdk-js-api";
const clientContext = getClient(endpointParam, clientParam, {
const updatedOptions = {
...options,
userAgentOptions: { userAgentPrefix },
});
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info }
};
const clientContext = getClient(endpointUrl, undefined, updatedOptions);
clientContext.pipeline.removePolicy({ name: "ApiVersionPolicy" });
if (options.apiVersion) {
logger.warning(
"This client does not support client api-version, please change it at the operation level",
);
}
return clientContext;
}`
);
Expand Down Expand Up @@ -140,29 +151,40 @@ describe("modular client context type", () => {
await assertEqualContent(
clientContext?.getFullText()!,
`
import { ClientOptions } from "@azure-rest/core-client";
import { ServiceContext } from "../rest/index.js";
import getClient from "../rest/index.js";
import { ClientOptions, Client, getClient } from "@azure-rest/core-client";
import { logger } from "../logger.js";
export interface ServiceContext extends Client {}
/** Optional parameters for the client. */
export interface ServiceClientOptionalParams extends ClientOptions {}
export { ServiceContext } from "../rest/index.js";
export function createService(
endpointParam: string,
clientParam: ClientType,
options: ServiceClientOptionalParams = {}
): ServiceContext {
const endpointUrl =
options.endpoint ??
options.baseUrl ??
\`\${endpointParam}/client/structure/\${clientParam}\`;
const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentPrefix = prefixFromOptions
? \`\$\{prefixFromOptions\} azsdk-js-api\`
: "azsdk-js-api";
const clientContext = getClient(endpointParam, clientParam, {
const updatedOptions = {
...options,
userAgentOptions: { userAgentPrefix },
});
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
};
const clientContext = getClient(endpointUrl, undefined, updatedOptions);
clientContext.pipeline.removePolicy({ name: "ApiVersionPolicy" });
if (options.apiVersion) {
logger.warning(
"This client does not support client api-version, please change it at the operation level",
);
}
return clientContext;
}`
);
Expand Down
42 changes: 24 additions & 18 deletions packages/typespec-ts/test/modularUnit/enumUnion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ describe("header parameters", () => {
await assertEqualContent(
paramOutput?.[0]?.getFullText()!,
`
import { DemoServiceContext as Client } from "../rest/index.js";
import { DemoServiceContext as Client } from "./index.js";
import {
StreamableMethod,
operationOptionsToRequestParameters,
PathUncheckedResponse,
createRestError,
} from "@azure-rest/core-client";
Expand All @@ -74,7 +75,7 @@ describe("header parameters", () => {
contentType: SchemaContentTypeValues,
body: string,
options: GetOptionalParams = { requestOptions: {} },
): StreamableMethod<Get204Response> {
): StreamableMethod {
return context
.path("/")
.post({
Expand All @@ -84,8 +85,9 @@ describe("header parameters", () => {
});
}
export async function _getDeserialize(result: Get204Response): Promise<void> {
if (result.status !== "204") {
export async function _getDeserialize(result: PathUncheckedResponse): Promise<void> {
const expectedStatuses = ["204"];
if(!expectedStatuses.includes(result.status)) {
throw createRestError(result);
}
Expand Down Expand Up @@ -463,18 +465,19 @@ describe("header parameters", () => {
await assertEqualContent(
operationContent,
`
import { DemoServiceContext as Client } from "../rest/index.js";
import { DemoServiceContext as Client } from "./index.js";
import {
StreamableMethod,
operationOptionsToRequestParameters,
PathUncheckedResponse,
createRestError,
} from "@azure-rest/core-client";
export function _getSend(
context: Client,
testHeader: "A" | "B",
body: string,
options: GetOptionalParams = { requestOptions: {} },
): StreamableMethod<Get204Response> {
): StreamableMethod {
return context
.path("/")
.post({
Expand All @@ -483,8 +486,9 @@ describe("header parameters", () => {
body: body
});
}
export async function _getDeserialize(result: Get204Response): Promise<void> {
if (result.status !== "204") {
export async function _getDeserialize(result: PathUncheckedResponse): Promise<void> {
const expectedStatuses = ["204"];
if(!expectedStatuses.includes(result.status)) {
throw createRestError(result);
}
return;
Expand Down Expand Up @@ -542,18 +546,19 @@ describe("header parameters", () => {
await assertEqualContent(
operationContent,
`
import { DemoServiceContext as Client } from "../rest/index.js";
import { DemoServiceContext as Client } from "./index.js";
import {
StreamableMethod,
operationOptionsToRequestParameters,
PathUncheckedResponse,
createRestError,
} from "@azure-rest/core-client";
export function _getSend(
context: Client,
testHeader: string | "A" | "B",
body: string,
options: GetOptionalParams = { requestOptions: {} },
): StreamableMethod<Get204Response> {
): StreamableMethod {
return context
.path("/")
.post({
Expand All @@ -562,8 +567,9 @@ describe("header parameters", () => {
body: body
});
}
export async function _getDeserialize(result: Get204Response): Promise<void> {
if (result.status !== "204") {
export async function _getDeserialize(result: PathUncheckedResponse): Promise<void> {
const expectedStatuses = ["204"];
if(!expectedStatuses.includes(result.status)) {
throw createRestError(result);
}
return;
Expand Down Expand Up @@ -803,7 +809,7 @@ describe("model type", () => {
await assertEqualContent(
serializer!,
`
export function testSerializer(item: Test): TestRest {
export function testSerializer(item: Test): Record<string, unknown> {
return {
color: item["color"],
}
Expand Down Expand Up @@ -861,7 +867,7 @@ describe("model type", () => {
await assertEqualContent(
serializer!,
`
export function testSerializer(item: Test): TestRest {
export function testSerializer(item: Test): Record<string, unknown> {
return {
content: item["content"],
}
Expand Down Expand Up @@ -889,7 +895,7 @@ describe("model type", () => {
await assertEqualContent(
serializer!,
`
export function testSerializer(item: Test): TestRest {
export function testSerializer(item: Test): Record<string, unknown> {
return {
content: item["content"],
}
Expand Down Expand Up @@ -919,7 +925,7 @@ describe("model type", () => {
await assertEqualContent(
serializer!,
`
export function testSerializer(item: Test): TestRest {
export function testSerializer(item: Test): Record<string, unknown> {
return {
color: item["color"],
}
Expand Down Expand Up @@ -1115,7 +1121,7 @@ describe("model type", () => {
await assertEqualContent(
serializer!,
`
export function testSerializer(item: Test): TestRest {
export function testSerializer(item: Test): Record<string, unknown> {
return {
content: item["content"],
}
Expand Down Expand Up @@ -1143,7 +1149,7 @@ describe("model type", () => {
await assertEqualContent(
serializer!,
`
export function testSerializer(item: Test): TestRest {
export function testSerializer(item: Test): Record<string, unknown> {
return {
content: item["content"],
}
Expand Down
Loading

0 comments on commit 9e8a859

Please sign in to comment.