Skip to content

Commit

Permalink
[EventGrid] Split tests to enable min/max testing
Browse files Browse the repository at this point in the history
This change splits our tests into two folders:

- `public` which only uses surface area exported by the package.
- `internal` which tests internal methods which we don't export from
   the top level package.

This allows us to test the public surface area independently from the
private one, which is helpful for classes of testing.

Fixes Azure#13813
  • Loading branch information
ellismg committed Mar 23, 2021
1 parent 18b3f30 commit 287fefb
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 181 deletions.
4 changes: 2 additions & 2 deletions sdk/eventgrid/eventgrid/rollup.base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function nodeConfig(test = false) {

if (test) {
// Entry points - test files under the `test` folder(common for both browser and node), node specific test files
baseConfig.input = ["dist-esm/test/*.spec.js", "dist-esm/test/node/*.spec.js"];
baseConfig.input = ["dist-esm/test/internal/**/*.spec.js", "dist-esm/test/public/**/*.spec.js"];
baseConfig.plugins.unshift(multiEntry({ exports: false }));

// different output file
Expand Down Expand Up @@ -104,7 +104,7 @@ export function browserConfig(test = false, production = false) {

if (test) {
// Entry points - test files under the `test` folder(common for both browser and node), browser specific test files
baseConfig.input = ["dist-esm/test/*.spec.js", "dist-esm/test/browser/*.spec.js"];
baseConfig.input = ["dist-esm/test/internal/**/*.spec.js", "dist-esm/test/public/**/*.spec.js"];
baseConfig.plugins.unshift(multiEntry({ exports: false }));
baseConfig.output.file = "dist-test/index.browser.js";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.

import { assert } from "chai";
import { cloudEventDistributedTracingEnricherPolicy } from "../src/cloudEventDistrubtedTracingEnricherPolicy";
import { cloudEventDistributedTracingEnricherPolicy } from "../../src/cloudEventDistrubtedTracingEnricherPolicy";
import {
PipelineRequest,
PipelineResponse,
Expand Down
173 changes: 173 additions & 0 deletions sdk/eventgrid/eventgrid/test/internal/convertToWireModel.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { assert } from "chai";

import {
convertEventGridEventToModelType,
convertCloudEventToModelType
} from "../../src/eventGridClient";

describe("convertEventGridEventToModelType", function() {
it("sets a default ID if one is not provided", () => {
const convertedEvent = convertEventGridEventToModelType({
dataVersion: "1.0",
eventType: "Azure.Sdk.TestEvent",
subject: "Test Event",
data: { hello: "world " }
});

assert.isDefined(convertedEvent.id);
});

it("sets a default event time if one is not provided", () => {
const convertedEvent = convertEventGridEventToModelType({
dataVersion: "1.0",
eventType: "Azure.Sdk.TestEvent",
subject: "Test Event",
data: { hello: "world " }
});

assert.isDefined(convertedEvent.eventTime);
});

it("does not change set values", () => {
const time = new Date();
const id = "272871ba-2496-4750-9a90-bedd1ea10191";

const convertedEvent = convertEventGridEventToModelType({
id: id,
eventTime: time,
dataVersion: "1.0",
eventType: "Azure.Sdk.TestEvent",
subject: "Test Event",
data: { hello: "world " }
});

assert.strictEqual(convertedEvent.id, id);
assert.strictEqual(convertedEvent.eventTime, time);
});
});

describe("convertCloudEventToModelType", function() {
it("sets a default ID if one is not provided", () => {
const convertedEvent = convertCloudEventToModelType({
source: "/azure/sdk/tests",
type: "Azure.Sdk.TestEvent"
});

assert.isDefined(convertedEvent.id);
});

it("sets a default event time if one is not provided", () => {
const convertedEvent = convertCloudEventToModelType({
source: "/azure/sdk/tests",
type: "Azure.Sdk.TestEvent"
});

assert.isDefined(convertedEvent.time);
});

it("does not change set values", () => {
const time = new Date();
const id = "272871ba-2496-4750-9a90-bedd1ea10191";

const convertedEvent = convertCloudEventToModelType({
id: id,
time: time,
source: "/azure/sdk/tests",
type: "Azure.Sdk.TestEvent"
});

assert.strictEqual(convertedEvent.id, id);
assert.strictEqual(convertedEvent.time, time);
});

it("promotes extension attributes", () => {
const traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01";
const tracestate =
"rojo=00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01,congo=lZWRzIHRoNhcm5hbCBwbGVhc3VyZS4";

const convertedEvent = convertCloudEventToModelType({
source: "/azure/sdk/tests",
type: "Azure.Sdk.TestEvent",
extensionAttributes: {
traceparent,
tracestate
}
});

// When converted to a model type to send over the wire, the extension attributes are promoted to be
// properties on the envelope itself.
assert.equal(convertedEvent["traceparent"], traceparent);
assert.equal(convertedEvent["tracestate"], tracestate);
});

it("base64 encodes binary data", () => {
const binaryData = new Uint8Array(10);
for (let i = 0; i < binaryData.length; i++) {
binaryData[i] = i;
}

const convertedEvent = convertCloudEventToModelType({
source: "/azure/sdk/tests",
type: "Azure.Sdk.TestEvent",
data: binaryData,
datacontenttype: "application/binary"
});

assert.isUndefined(convertedEvent.data);
assert.strictEqual(convertedEvent.dataBase64, binaryData);
});

it("fails if data content type is missing for binary data", () => {
const binaryData = new Uint8Array(10);
for (let i = 0; i < binaryData.length; i++) {
binaryData[i] = i;
}

assert.throws(() => {
convertCloudEventToModelType({
source: "/azure/sdk/tests",
type: "Azure.Sdk.TestEvent",
data: binaryData
});
}, /data content type/);
});

it("fails if extenion attributes are invalid", () => {
const binaryData = new Uint8Array(10);
for (let i = 0; i < binaryData.length; i++) {
binaryData[i] = i;
}

assert.throws(() => {
convertCloudEventToModelType({
source: "/azure/sdk/tests",
type: "Azure.Sdk.TestEvent",
extensionAttributes: {
source: "this-is-not-allowed"
}
});
}, /invalid extension attribute name: source/);

assert.throws(() => {
convertCloudEventToModelType({
source: "/azure/sdk/tests",
type: "Azure.Sdk.TestEvent",
extensionAttributes: {
MiXedCasE: "this-is-not-allowed"
}
});
}, /invalid extension attribute name: MiXedCasE/);

assert.throws(() => {
convertCloudEventToModelType({
source: "/azure/sdk/tests",
type: "Azure.Sdk.TestEvent",
extensionAttributes: {
data_base64: "this-is-not-allowed"
}
});
}, /invalid extension attribute name: data_base64/);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { assert } from "chai";

import { dateToServiceTimeString } from "../src/util";
import { dateToServiceTimeString } from "../../src/util";

describe("util", function() {
describe("dateToServiceTimeString", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import { assert, use as chaiUse } from "chai";
import chaiPromises from "chai-as-promised";

import { EventGridDeserializer } from "../src";
import * as testData from "./utils/testData";
import { EventGridDeserializer } from "../../src";
import * as testData from "../utils/testData";

chaiUse(chaiPromises);

Expand Down
Loading

0 comments on commit 287fefb

Please sign in to comment.