1. チュートリアルのテスト

Web クライアント SDK を介した Stylelabs.M.SDK.Fluent の使用

Version:
日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

このセクションでは、次のアクションについて説明します。

  1. [プロジェクトを作成] (# プロジェクトを作成)
  2. [基本クラスを作成する] (# 基本クラスを作成する)
  3. [エンティティの作成] (# エンティティの作成)
  4. [クエリ エンティティ] (# クエリ エンティティ)

プロジェクトの作成

注意

次の例では、.NET Core CLI を使用していますが、お好みの IDE で同等の機能を使用できます。

新しいプロジェクトを作成するには、次のコマンドを実行します。

> mkdir Walkthrough
> cd Walkthrough
> dotnet new integration-tests --framework net6.0

プロジェクトが作成されると、Content Hub SDK Fluent ライブラリの参照が必要です。

dotnet add package Stylelabs.M.Sdk.Fluent.Testing --source https://slpartners.myget.org/F/m-public/api/v3/index.json

新しい基底クラスを作成する

注意

これらの例では、テスト フレームワークとして xUnit を使用しています。

SDK クライアントの作成など、一般的な機能のすべてを処理する基底クラスを作成します。

using System;
using Stylelabs.M.Sdk.Fluent;
using Stylelabs.M.Sdk.WebClient.Authentication;

namespace Integration.Tests.Walkthrough
{
public abstract class TestBase
{
protected readonly IWebMClient _mClient;
protected readonly FluentClient _fluentClient;

protected TestBase()
{
_mClient = CreateMClient();
_fluentClient = new FluentClient(_mClient);
}

private static IWebMClient CreateMClient()
{
var endpoint = new Uri(Configuration.Authentication.Url);

var oath = new OAuthPasswordGrant
{
ClientId = Configuration.Authentication.ClientId,
ClientSecret = Configuration.Authentication.ClientSecret,
Password = Configuration.Authentication.Password,
UserName = Configuration.Authentication.Username
};

return Stylelabs.M.Sdk.WebClient.MClientFactory.CreateMClient(endpoint, oath);
}
}
}

エンティティの作成

新しいテスト クラスを作成します。このクラスのテストでは、次のことを行います。

  • 新しい Entity Definition を作成し、メンバーを検証する
  • 新しい Entity を作成し、メンバーを検証する
using System.Collections.Generic;
using System.Globalization;
using Stylelabs.M.Sdk.Fluent.Testing.Extensions;
using Stylelabs.M.Sdk.Models.Base.PropertyDefinitions;
using Stylelabs.M.Sdk.WebClient.IntegrationTests.Fluent.Wrappers;
using Xunit;

namespace Sdk.Fluent.Testing.Walkthrough
{
public class NewEntities : FluentTestBase
{
[Fact]
public void Create_New_EntityDefinition()
{
var definition = _fluentClient.EntityDefinitions
.New("My.Definition")
.NewMemberGroup("MyGroup", overviewGroup => overviewGroup
.AddPropertyDefinition<StringPropertyDefinition>(
"myStringProperty",
stringPropertyDefinition =>
{
stringPropertyDefinition.IncludedInContent = true;
stringPropertyDefinition.IsMultiValue = false;
stringPropertyDefinition.Labels = new Dictionary<CultureInfo, string>
{ { CultureInfo.InvariantCulture, "myStringProperty" } };
})
.AddPropertyDefinition<BooleanPropertyDefinition>("myBooleanProperty")
.AddPropertyDefinition<IntegerPropertyDefinition>("myIntProperty"))
.Save();

definition
.AssertMemberGroup("MyGroup")
.MemberGroup("MyGroup", group => group
.AssertPropertyExists("myStringProperty")
.AssertPropertyExists("myBooleanProperty")
.AssertPropertyExists("myIntProperty"));
}

[Fact]
public void Create_New_Entity()
{
var entity = _fluentClient.EntityFactory
.Create("My.Definition")
.SetProperty("myStringProperty", "this is an string value")
.SetProperty("myBooleanProperty", true)
.SetProperty("myIntProperty", 100)
.Save();

entity
.AssertPropertyEqual("myBooleanProperty", true)
.AssertPropertyNotEmpty("myStringProperty")
.AssertPropertyGreaterOrEqual("myIntProperty", 100);
}
}
}

クエリ エンティティ

新しいテスト クラスを作成します。このクラスのテストでは、次のことを行います。

  • エンティティ定義によって Entities をクエリする
  • ID によって Entity をクエリする
  • 多くの Entities をクエリする
using Stylelabs.M.Sdk.Fluent.Models;
using Stylelabs.M.Sdk.Fluent.Testing.Extensions;
using Stylelabs.M.Sdk.WebClient.IntegrationTests.Fluent.Wrappers;
using Xunit;

namespace Sdk.Fluent.Testing.Walkthrough
{
public class QueryEntities : FluentTestBase
{
[Fact]
public void Query_Entity_ByEntityDefinition()
{
var entities = _fluentClient.Entities
.GetByDefinition("My.Definition");

foreach (FluentEntity entity in entities)
{
entity
.AssertPropertyEqual("myBooleanProperty", true)
.AssertPropertyNotEmpty("myStringProperty")
.AssertPropertyEqual("myIntProperty", 100);
}
}

[Fact]
public void Query_Many_Entities()
{
var entities = _fluentClient.Entities
.GetMany(new long[] { 12345, 67890 }); // this should be a list of valid ids

foreach (FluentEntity entity in entities)
{
entity
.AssertPropertyEqual("myBooleanProperty", true)
.AssertPropertyNotEmpty("myStringProperty")
.AssertPropertyEqual("myIntProperty", 100);
}
}

[Fact]
public void Query_Entity_ById()
{
_fluentClient.Entities
.Get(12345) // this should be a valid id
.AssertPropertyEqual("myBooleanProperty", true)
.AssertPropertyNotEmpty("myStringProperty")
.AssertPropertyNotEqual("myIntProperty", 10);
}
}
}
この記事を改善するための提案がある場合は、 お知らせください!