Skip to content

Commit 3e98a8b

Browse files
author
Swapnil Nagar
committed
Adding unit tests
1 parent 4b154d0 commit 3e98a8b

16 files changed

+860
-36
lines changed

src/converters/fromRpcTypedData.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,23 @@ import { HttpRequest } from '../http/HttpRequest';
77
import { isDefined } from '../utils/nonNull';
88

99
export function fromRpcTypedData(data: RpcTypedData | null | undefined): unknown {
10-
console.log('fromRpcTypedData input:', data);
11-
1210
if (!data) {
13-
console.log('Condition: !data - returning undefined');
1411
return undefined;
1512
} else if (isDefined(data.string)) {
1613
const result = tryJsonParse(data.string);
17-
console.log('Condition: data.string - returning:', result);
1814
return result;
1915
} else if (isDefined(data.json)) {
2016
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
2117
const result = JSON.parse(data.json);
22-
console.log('Condition: data.json - returning:', result);
2318
return result;
2419
} else if (isDefined(data.bytes)) {
2520
const result = Buffer.from(data.bytes);
26-
console.log('Condition: data.bytes - returning Buffer of length:', result.length);
2721
return result;
2822
} else if (isDefined(data.stream)) {
2923
const result = Buffer.from(data.stream);
30-
console.log('Condition: data.stream - returning Buffer of length:', result.length);
3124
return result;
3225
} else if (isDefined(data.http)) {
3326
const result = new HttpRequest(data.http);
34-
console.log('Condition: data.http - returning HttpRequest');
3527
return result;
3628
} else if (isDefined(data.int)) {
3729
console.log('Condition: data.int - returning:', data.int);
@@ -41,24 +33,18 @@ export function fromRpcTypedData(data: RpcTypedData | null | undefined): unknown
4133
return data.double;
4234
} else if (data.collectionBytes && isDefined(data.collectionBytes.bytes)) {
4335
const result = data.collectionBytes.bytes.map((d) => Buffer.from(d));
44-
console.log('Condition: data.collectionBytes - returning array of Buffers, count:', result.length);
4536
return result;
4637
} else if (data.collectionString && isDefined(data.collectionString.string)) {
4738
const result = data.collectionString.string.map(tryJsonParse);
48-
console.log('Condition: data.collectionString - returning:', result);
4939
return result;
5040
} else if (data.collectionDouble && isDefined(data.collectionDouble.double)) {
51-
console.log('Condition: data.collectionDouble - returning:', data.collectionDouble.double);
5241
return data.collectionDouble.double;
5342
} else if (data.collectionSint64 && isDefined(data.collectionSint64.sint64)) {
54-
console.log('Condition: data.collectionSint64 - returning:', data.collectionSint64.sint64);
5543
return data.collectionSint64.sint64;
5644
} else if (data.modelBindingData && isDefined(data.modelBindingData.content)) {
57-
console.log('Here at the modelBindinData', data.modelBindingData);
5845
return AzureStorageBlobClientFactory.buildClientFromModelBindingData(data.modelBindingData);
5946
//return data.modelBindingData;
6047
} else {
61-
console.log('Condition: none matched - returning undefined');
6248
return undefined;
6349
}
6450
}
@@ -67,10 +53,8 @@ function tryJsonParse(data: string): unknown {
6753
try {
6854
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
6955
const parsed = JSON.parse(data);
70-
console.log('tryJsonParse: successfully parsed JSON');
7156
return parsed;
7257
} catch {
73-
console.log('tryJsonParse: failed to parse JSON, returning original string');
7458
return data;
7559
}
7660
}

src/converters/toCoreFunctionMetadata.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ export function toCoreFunctionMetadata(name: string, options: GenericFunctionOpt
1212
const bindings: Record<string, coreTypes.RpcBindingInfo> = {};
1313
const bindingNames: string[] = [];
1414
const trigger = options.trigger;
15-
console.log('toCoreFunctionMetadata: Handle', JSON.stringify(options));
16-
console.log('toCoreFunctionMetadata: deferredBindingType', options.trigger.deferredBindingType);
1715

1816
bindings[trigger.name] = {
1917
...trigger,
@@ -80,10 +78,8 @@ export function toCoreFunctionMetadata(name: string, options: GenericFunctionOpt
8078
}
8179

8280
function addDeferredBindingsFlag(deferredBindingType?: boolean | unknown): { [key: string]: string } {
83-
//Ensure that trigger type that is passed is valid and supported, to avoid customer misconfiguration.
84-
console.log('Deferred binding flag value is: ', deferredBindingType);
81+
//Ensure that trigger type that is passed is valid and supported.
8582
if (deferredBindingType !== undefined && deferredBindingType === true) {
86-
console.log('Adding deferred binding propertyu to trigger type:', deferredBindingType);
8783
return { supportsDeferredBinding: 'true' };
8884
}
8985

src/converters/toRpcTypedData.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,32 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
// Copyright (c) .NET Foundation. All rights reserved.
5+
// Licensed under the MIT License.
6+
47
import { RpcTypedData } from '@azure/functions-core';
58

69
export function toRpcTypedData(data: unknown): RpcTypedData | null | undefined {
7-
console.log('toRpcTypedData input:', typeof data, data);
8-
910
if (data === null || data === undefined) {
10-
console.log('Condition: null/undefined - returning:', data);
1111
return data;
1212
} else if (typeof data === 'string') {
13-
console.log('Condition: string - returning:', { string: data });
1413
return { string: data };
1514
} else if (Buffer.isBuffer(data)) {
16-
console.log('Condition: Buffer - returning bytes of length:', data.length);
1715
return { bytes: data };
1816
} else if (ArrayBuffer.isView(data)) {
1917
const bytes = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
20-
console.log('Condition: ArrayBufferView - returning Uint8Array of length:', bytes.length);
2118
return { bytes: bytes };
2219
} else if (data instanceof ArrayBuffer) {
2320
const bytes = new Uint8Array(data);
24-
console.log('Condition: ArrayBuffer - returning Uint8Array of length:', bytes.length);
2521
return { bytes: bytes };
2622
} else if (typeof data === 'number') {
2723
if (Number.isInteger(data)) {
28-
console.log('Condition: integer number - returning:', { int: data });
2924
return { int: data };
3025
} else {
31-
console.log('Condition: floating-point number - returning:', { double: data });
3226
return { double: data };
3327
}
3428
} else {
3529
const jsonString = JSON.stringify(data);
36-
console.log('Condition: other (converted to JSON) - returning:', { json: jsonString });
3730
return { json: jsonString };
3831
}
3932
}

src/deferred-binding/storage-blob/azureStorageBlobClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class AzureStorageBlobClient {
2121
blobName?: string,
2222
options?: StoragePipelineOptions
2323
) {
24-
const storageBlobServiceClient = strategy.createStroageBlobServiceClient(options);
24+
const storageBlobServiceClient = strategy.createStorageBlobServiceClient(options);
2525
// Initialize container and blob clients if names are provided
2626
if (containerName) {
2727
this.containerClient = storageBlobServiceClient.getContainerClient(containerName);

src/deferred-binding/storage-blob/connectionStringStrategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class ConnectionStringStrategy implements StorageBlobServiceClientStrateg
1313
*/
1414
constructor(private connectionString: string) {}
1515

16-
createStroageBlobServiceClient(options?: StoragePipelineOptions): BlobServiceClient {
16+
createStorageBlobServiceClient(options?: StoragePipelineOptions): BlobServiceClient {
1717
return BlobServiceClient.fromConnectionString(this.connectionString, options);
1818
}
1919
}

src/deferred-binding/storage-blob/managedIdentitySystemStrategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class ManagedIdentitySystemStrategy implements StorageBlobServiceClientSt
1111
*/
1212
constructor(private accountUrl: string) {}
1313

14-
createStroageBlobServiceClient(options?: StoragePipelineOptions): BlobServiceClient {
14+
createStorageBlobServiceClient(options?: StoragePipelineOptions): BlobServiceClient {
1515
const credential = new DefaultAzureCredential();
1616
return new BlobServiceClient(this.accountUrl, credential, options);
1717
}

src/deferred-binding/storage-blob/managedIdentityUserStartegy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class ManagedIdentityUserStrategy implements StorageBlobServiceClientStra
1515
*/
1616
constructor(private accountUrl: string, private clientId: string) {}
1717

18-
createStroageBlobServiceClient(options?: StoragePipelineOptions): BlobServiceClient {
18+
createStorageBlobServiceClient(options?: StoragePipelineOptions): BlobServiceClient {
1919
const credential = new ManagedIdentityCredential(this.clientId);
2020
return new BlobServiceClient(this.accountUrl, credential, options);
2121
}

src/deferred-binding/storage-blob/storageBlobServiceClientStrategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ import { BlobServiceClient, StoragePipelineOptions } from '@azure/storage-blob';
77
* Strategy interface for creating StorageBlobServiceClient instances
88
*/
99
export interface StorageBlobServiceClientStrategy {
10-
createStroageBlobServiceClient(options?: StoragePipelineOptions): BlobServiceClient;
10+
createStorageBlobServiceClient(options?: StoragePipelineOptions): BlobServiceClient;
1111
}

test/converters/fromRpcTypedData.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { fromString } from 'long';
77
import { HttpRequest } from '../../src';
88
import { fromRpcTypedData } from '../../src/converters/fromRpcTypedData';
99
import Long = require('long');
10+
import { ModelBindingData, RpcTypedData } from '@azure/functions-core';
11+
import { AzureStorageBlobClientFactory } from '../../src/deferred-binding/storage-blob/azureStorageBlobClientFactory';
1012

1113
describe('fromRpcTypedData', () => {
1214
it('null', () => {
@@ -110,3 +112,74 @@ describe('fromRpcTypedData', () => {
110112
expect(result[1].toString()).to.equal('9007199254740992');
111113
});
112114
});
115+
116+
describe('modelBindingData scenario', () => {
117+
let mockBlobClient: any;
118+
119+
beforeEach(() => {
120+
// Suppress console.log output during tests
121+
console.log = () => {};
122+
123+
// Create mock blob client for testing
124+
mockBlobClient = {
125+
blobClient: { url: 'https://test.blob.core.windows.net/container/blob' },
126+
containerClient: { url: 'https://test.blob.core.windows.net/container' },
127+
};
128+
129+
// Replace the factory method with a mock implementation
130+
AzureStorageBlobClientFactory.buildClientFromModelBindingData = () => {
131+
return mockBlobClient;
132+
};
133+
});
134+
135+
it('should call AzureStorageBlobClientFactory with modelBindingData', () => {
136+
let capturedModelBindingData: ModelBindingData | null = null;
137+
138+
// Override the mock to capture the input parameter
139+
AzureStorageBlobClientFactory.buildClientFromModelBindingData = (modelBindingData: ModelBindingData) => {
140+
capturedModelBindingData = modelBindingData;
141+
return mockBlobClient;
142+
};
143+
144+
const modelBindingData: ModelBindingData = {
145+
content: Buffer.from(
146+
JSON.stringify({
147+
Connection: 'test-connection',
148+
ContainerName: 'test-container',
149+
BlobName: 'test-blob.txt',
150+
})
151+
),
152+
contentType: 'application/json',
153+
source: 'test-source',
154+
version: '1.0',
155+
};
156+
157+
const data: RpcTypedData = { modelBindingData };
158+
159+
const result = fromRpcTypedData(data);
160+
161+
// Verify the factory was called with the correct data
162+
expect(capturedModelBindingData).to.equal(modelBindingData);
163+
164+
// Verify the result is what the factory returned
165+
expect(result).to.equal(mockBlobClient);
166+
});
167+
168+
it('should handle undefined content in modelBindingData', () => {
169+
// Override mock to throw if called with incorrect data
170+
AzureStorageBlobClientFactory.buildClientFromModelBindingData = () => {
171+
throw new Error('Should not be called with undefined content');
172+
};
173+
174+
const modelBindingData: ModelBindingData = {
175+
// content is undefined
176+
contentType: 'application/json',
177+
};
178+
179+
const data: RpcTypedData = { modelBindingData };
180+
181+
// This should not throw because the isDefined check should prevent the factory from being called
182+
const result = fromRpcTypedData(data);
183+
expect(result).to.be.undefined;
184+
});
185+
});

0 commit comments

Comments
 (0)