github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/testing/test/testdata/output-funcs/nodejs-extras/tests/codegen.spec.ts (about)

     1  // Copyright 2016-2021, Pulumi Corporation.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  import "mocha";
    16  import * as assert from "assert";
    17  import * as pulumi from "@pulumi/pulumi";
    18  import * as sut from "..";
    19  
    20  pulumi.runtime.setMocks({
    21      newResource: function(_: pulumi.runtime.MockResourceArgs): {id: string, state: any} {
    22          throw new Error("newResource not implemented");
    23      },
    24      call: function(args: pulumi.runtime.MockCallArgs) {
    25          if (args.token == "mypkg::listStorageAccountKeys") {
    26              return {
    27                  "keys": [{
    28                      "creationTime": "my-creation-time",
    29                      "keyName": "my-key-name",
    30                      "permissions": "my-permissions",
    31                      "value": JSON.stringify(args.inputs),
    32                  }]
    33              };
    34          }
    35          if (args.token == "mypkg::getIntegrationRuntimeObjectMetadatum") {
    36              return {nextLink: JSON.stringify(args.inputs)};
    37          }
    38          if (args.token == "mypkg::funcWithAllOptionalInputs" ||
    39              args.token == "mypkg::funcWithDefaultValue" ||
    40              args.token == "mypkg::funcWithListParam" ||
    41              args.token == "mypkg::funcWithDictParam")
    42          {
    43              return {r: JSON.stringify(args.inputs)};
    44          }
    45          throw new Error("call not implemented for " + args.token);
    46      },
    47  });
    48  
    49  function checkTable(done: any, transform: (res: any) => any, table: {given: pulumi.Output<any>, expect: any}[]) {
    50      checkOutput(done, pulumi.all(table.map(x => x.given)), res => {
    51          res.map((actual, i) => {
    52              assert.deepStrictEqual(transform(actual), table[i].expect);
    53          });
    54      });
    55  }
    56  
    57  describe("output-funcs", () => {
    58      it("funcWithAllOptionalInputsOutput", (done) => {
    59          checkTable(done, res => JSON.parse(res.r), [
    60              {given: sut.funcWithAllOptionalInputsOutput({}),
    61               expect: {}},
    62              {given: sut.funcWithAllOptionalInputsOutput({a: pulumi.output("my-a")}),
    63               expect: {"a": "my-a"}},
    64              {given: sut.funcWithAllOptionalInputsOutput({a: pulumi.output("my-a"),
    65                                                           b: pulumi.output("my-b")}),
    66               expect: {"a": "my-a", "b": "my-b"}}
    67          ]);
    68      });
    69  
    70      // TODO[pulumi/pulumi#7973] Node codegen does not respect default
    71      // values at the moment, otherwise "b" parameter would receive the
    72      // default value from the schema.
    73      it("funcWithDefaultValueOutput", (done) => {
    74          checkTable(done, res => JSON.parse(res.r), [
    75              {given: sut.funcWithDefaultValueOutput({"a": pulumi.output("my-a")}),
    76               expect: {"a": "my-a"}},
    77              {given: sut.funcWithDefaultValueOutput({"a": pulumi.output("my-a"),
    78                                                      "b": pulumi.output("my-b")}),
    79               expect: {"a": "my-a", "b": "my-b"}}
    80          ]);
    81      });
    82  
    83      it("funcWithListParamOutput", (done) => {
    84          const l = ["a", "b", "c"];
    85          checkTable(done, res => JSON.parse(res.r), [
    86              {given: sut.funcWithListParamOutput({}),
    87               expect: {}},
    88              {given: sut.funcWithListParamOutput({"a": pulumi.output(l)}),
    89               expect: {"a": l}},
    90              {given: sut.funcWithListParamOutput({"a": pulumi.output(l),
    91                                                   "b": pulumi.output("my-b")}),
    92               expect: {"a": l, "b": "my-b"}},
    93          ]);
    94      });
    95  
    96      it("funcWithDictParamOutput", (done) => {
    97          const d = {"key-a": "value-a", "key-b": "value-b"};
    98          checkTable(done, res => JSON.parse(res.r), [
    99              {given: sut.funcWithDictParamOutput({}),
   100               expect: {}},
   101              {given: sut.funcWithDictParamOutput({"a": pulumi.output(d)}),
   102               expect: {"a": d}},
   103              {given: sut.funcWithDictParamOutput({"a": pulumi.output(d),
   104                                                   "b": pulumi.output("my-b")}),
   105               expect: {"a": d, "b": "my-b"}},
   106          ]);
   107      });
   108  
   109      it("listStorageAccountKeysOutput", (done) => {
   110          const output = sut.listStorageAccountKeysOutput({
   111              accountName: pulumi.output("my-account-name"),
   112              resourceGroupName: pulumi.output("my-resource-group-name"),
   113          });
   114          checkOutput(done, output, (res: sut.ListStorageAccountKeysResult) => {
   115              assert.equal(res.keys.length, 1);
   116              const k = res.keys[0];
   117              assert.equal(k.creationTime, "my-creation-time");
   118              assert.equal(k.keyName, "my-key-name");
   119              assert.equal(k.permissions, "my-permissions");
   120              assert.deepStrictEqual(JSON.parse(k.value), {
   121                  "accountName": "my-account-name",
   122                  "resourceGroupName": "my-resource-group-name"
   123              });
   124          });
   125      });
   126  
   127      it("listStorageAccountKeysOutput with optional arg set", (done) => {
   128          const output = sut.listStorageAccountKeysOutput({
   129              accountName: pulumi.output("my-account-name"),
   130              resourceGroupName: pulumi.output("my-resource-group-name"),
   131              expand: pulumi.output("my-expand"),
   132          });
   133          checkOutput(done, output, (res: sut.ListStorageAccountKeysResult) => {
   134              assert.equal(res.keys.length, 1);
   135              const k = res.keys[0];
   136              assert.equal(k.creationTime, "my-creation-time");
   137              assert.equal(k.keyName, "my-key-name");
   138              assert.equal(k.permissions, "my-permissions");
   139              assert.deepStrictEqual(JSON.parse(k.value), {
   140                  "accountName": "my-account-name",
   141                  "resourceGroupName": "my-resource-group-name",
   142                  "expand": "my-expand"
   143              });
   144          });
   145      });
   146  
   147      it("getIntegrationRuntimeObjectMetadatumOutput", (done) => {
   148          checkTable(
   149              done,
   150              (res: sut.GetIntegrationRuntimeObjectMetadatumResult) =>
   151                  JSON.parse(res.nextLink || "{}"),
   152              [{given: sut.getIntegrationRuntimeObjectMetadatumOutput({
   153                  factoryName: pulumi.output("my-factory-name"),
   154                  integrationRuntimeName: pulumi.output("my-integration-runtime-name"),
   155                  metadataPath: pulumi.output("my-metadata-path"),
   156                  resourceGroupName: pulumi.output("my-resource-group-name")}),
   157                expect: {"factoryName": "my-factory-name",
   158                         "integrationRuntimeName": "my-integration-runtime-name",
   159                         "metadataPath": "my-metadata-path",
   160                         "resourceGroupName": "my-resource-group-name"}}]
   161          );
   162      });
   163   });
   164  
   165  
   166  function checkOutput<T>(done: any, output: pulumi.Output<T>, check: (value: T) => void) {
   167      output.apply(value => {
   168          try {
   169              check(value);
   170              done();
   171          } catch (error) {
   172              done(error);
   173          }
   174      });
   175  }