github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/testing/test/testdata/output-funcs/dotnet-extras/Tests/CodegenTests.cs (about)

     1  // Copyright 2016-2021, Pulumi Corporation
     2  
     3  using System;
     4  using System.Collections.Generic;
     5  using System.Collections.Immutable;
     6  using System.Linq;
     7  using System.Text;
     8  using System.Text.Json;
     9  using System.Threading.Tasks;
    10  
    11  using FluentAssertions;
    12  using NUnit.Framework;
    13  using Moq;
    14  
    15  using Pulumi;
    16  using Pulumi.Testing;
    17  
    18  using Pulumi.Mypkg.Outputs;
    19  using static Pulumi.Mypkg.TestHelpers;
    20  
    21  namespace Pulumi.Mypkg
    22  {
    23      [TestFixture]
    24      public class UnitTests
    25      {
    26          [Test]
    27          public async Task DependenciesPropagate()
    28          {
    29              await Assert.Output(() =>
    30              {
    31                  var dep = new MockDep("res1");
    32                  var args = new FuncWithAllOptionalInputsInvokeArgs
    33                  {
    34                      A = dep.MockDepOutput,
    35                  };
    36                  return FuncWithAllOptionalInputs.Invoke(args).Apply(x => x.R);
    37              }).DependsOn("mockdep::res1");
    38          }
    39  
    40          [Test]
    41          public async Task FuncWithAllOptionalInputsOutputWorks()
    42          {
    43              Func<string,Func<FuncWithAllOptionalInputsInvokeArgs?>,Task> check = (
    44                  (expected, args) => Assert
    45                  .Output(() => FuncWithAllOptionalInputs.Invoke(args()).Apply(x => x.R))
    46                  .ResolvesTo(expected)
    47              );
    48  
    49              await check("a=null b=null", () => null);
    50  
    51              await check("a=null b=null", () => new FuncWithAllOptionalInputsInvokeArgs());
    52  
    53              await check("a=my-a b=null", () => new FuncWithAllOptionalInputsInvokeArgs
    54              {
    55                  A = Out("my-a"),
    56              });
    57  
    58              await check("a=null b=my-b", () => new FuncWithAllOptionalInputsInvokeArgs
    59              {
    60                  B = Out("my-b"),
    61              });
    62  
    63              await check("a=my-a b=my-b", () => new FuncWithAllOptionalInputsInvokeArgs
    64              {
    65                  A = Out("my-a"),
    66                  B = Out("my-b"),
    67              });
    68          }
    69  
    70          [Test]
    71          public async Task FuncWithDefaultValueOutputWorks()
    72          {
    73              Func<string,Func<FuncWithDefaultValueInvokeArgs>,Task> check = (
    74                  (expected, args) => Assert
    75                  .Output(() => FuncWithDefaultValue.Invoke(args()).Apply(x => x.R))
    76                  .ResolvesTo(expected)
    77              );
    78  
    79              // Since A is required, not passing it is an exception.
    80              Func<Task> act = () => check("", () => new FuncWithDefaultValueInvokeArgs());
    81              await act.Should().ThrowAsync<Exception>();
    82  
    83              // Check that default values from the schema work.
    84              await check("a=my-a b=b-default", () => new FuncWithDefaultValueInvokeArgs()
    85              {
    86                  A = Out("my-a")
    87              });
    88  
    89              await check("a=my-a b=my-b", () => new FuncWithDefaultValueInvokeArgs()
    90              {
    91                  A = Out("my-a"),
    92                  B = Out("my-b")
    93              });
    94          }
    95  
    96          [Test]
    97          public async Task FuncWithDictParamOutputWorks()
    98          {
    99              Func<string,Func<FuncWithDictParamInvokeArgs>,Task> check = (
   100                  (expected, args) => Assert
   101                  .Output(() => FuncWithDictParam.Invoke(args()).Apply(x => x.R))
   102                  .ResolvesTo(expected)
   103              );
   104  
   105              var map = new InputMap<string>();
   106              map.Add("K1", Out("my-k1"));
   107              map.Add("K2", Out("my-k2"));
   108  
   109              // Omitted value defaults to null.
   110              await check("a=null b=null", () => new FuncWithDictParamInvokeArgs());
   111  
   112              await check("a=[K1: my-k1, K2: my-k2] b=null", () => new FuncWithDictParamInvokeArgs()
   113              {
   114                  A = map,
   115              });
   116  
   117              await check("a=[K1: my-k1, K2: my-k2] b=my-b", () => new FuncWithDictParamInvokeArgs()
   118              {
   119                  A = map,
   120                  B = Out("my-b"),
   121              });
   122          }
   123  
   124          [Test]
   125          public async Task FuncWithListParamOutputWorks()
   126          {
   127              Func<string,Func<FuncWithListParamInvokeArgs>,Task> check = (
   128                  (expected, args) => Assert
   129                  .Output(() => FuncWithListParam.Invoke(args()).Apply(x => x.R))
   130                  .ResolvesTo(expected)
   131              );
   132  
   133              var lst = new InputList<string>();
   134              lst.Add("e1");
   135              lst.Add("e2");
   136              lst.Add("e3");
   137  
   138              // Similarly to dicts, omitted value defaults to null, not empty list.
   139              await check("a=null b=null", () => new FuncWithListParamInvokeArgs());
   140  
   141              await check("a=[e1, e2, e3] b=null", () => new FuncWithListParamInvokeArgs()
   142              {
   143                  A = lst,
   144              });
   145  
   146              await check("a=[e1, e2, e3] b=my-b", () => new FuncWithListParamInvokeArgs()
   147              {
   148                  A = lst,
   149                  B = Out("my-b"),
   150              });
   151          }
   152  
   153          [Test]
   154          public async Task GetIntegrationRuntimeObjectMetadatumOuputWorks()
   155          {
   156              Func<string,Func<GetIntegrationRuntimeObjectMetadatumInvokeArgs>,Task> check = (
   157                  (expected, args) => Assert
   158                  .Output(() => GetIntegrationRuntimeObjectMetadatum.Invoke(args()).Apply(x => {
   159                      var nextLink = x.NextLink ?? "null";
   160                      var valueRepr = "null";
   161                      if (x.Value != null)
   162                      {
   163                          valueRepr = string.Join(", ", x.Value.Select(e => $"{e}"));
   164                      }
   165                      return $"nextLink={nextLink} value=[{valueRepr}]";
   166                   }))
   167                  .ResolvesTo(expected)
   168              );
   169  
   170              await check("nextLink=my-next-link value=[factoryName: my-fn, integrationRuntimeName: my-irn, " +
   171                          "metadataPath: my-mp, resourceGroupName: my-rgn]",
   172                          () => new GetIntegrationRuntimeObjectMetadatumInvokeArgs()
   173                          {
   174                              FactoryName = Out("my-fn"),
   175                              IntegrationRuntimeName = Out("my-irn"),
   176                              MetadataPath = Out("my-mp"),
   177                              ResourceGroupName = Out("my-rgn")
   178                          });
   179          }
   180  
   181          [Test]
   182          public async Task TestListStorageAccountsOutputWorks()
   183          {
   184              Func<StorageAccountKeyResponse, string> showSAKR = (r) =>
   185                  $"CreationTime={r.CreationTime} KeyName={r.KeyName} Permissions={r.Permissions} Value={r.Value}";
   186  
   187              Func<string,Func<ListStorageAccountKeysInvokeArgs>,Task> check = (
   188                  (expected, args) => Assert
   189                  .Output(() => ListStorageAccountKeys.Invoke(args()).Apply(x => {
   190                      return "[" + string.Join(", ", x.Keys.Select(k => showSAKR(k))) + "]";
   191                  })).ResolvesTo(expected)
   192              );
   193  
   194              await check("[CreationTime=my-creation-time KeyName=my-key-name Permissions=my-permissions" +
   195                          " Value=[accountName: my-an, expand: my-expand, resourceGroupName: my-rgn]]",
   196                          () => new ListStorageAccountKeysInvokeArgs()
   197                          {
   198                              AccountName = Out("my-an"),
   199                              ResourceGroupName = Out("my-rgn"),
   200                              Expand = Out("my-expand")
   201                          });
   202          }
   203      }
   204  }