Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generated code and SDK API tests for Personalizer v1.0 preview #5944

Merged
merged 10 commits into from
Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!--This file and it's contents are updated at build time moving or editing might result in build failure. Take due deligence while editing this file-->
<PropertyGroup>
<AzureApiTag />
<PackageTags>$(PackageTags);$(CommonTags);$(AzureApiTag);</PackageTags>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Net.Http;

namespace Microsoft.Azure.CognitiveServices.Personalizer.Tests
{
public abstract class BaseTests
{
public static bool IsTestTenant = false;
// BaseEndpoint only contains protocol and hostname
private static string BaseEndpoint = "http://localhost:5000";
private static string ApiKey = "000";

protected IPersonalizerClient GetClient(DelegatingHandler handler)
{
return new PersonalizerClient(new ApiKeyServiceClientCredentials(ApiKey), handlers: handler)
{
Endpoint = BaseEndpoint
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$([MSBuild]::GetPathOfFileAbove('AzSdk.test.reference.props'))" />
<PropertyGroup>
<Description>Microsoft.Azure.CognitiveServices.Personalizer.Tests Class Library</Description>
<AssemblyName>Microsoft.Azure.CognitiveServices.Personalizer.Tests</AssemblyName>
<Version>1.0.0</Version>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" Version="1.6.0-preview" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Personalizer\Personalizer.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="SessionRecords\**\*.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>

Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using Microsoft.Azure.CognitiveServices.Personalizer.Models;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using Xunit;
using System;
using System.Threading.Tasks;

namespace Microsoft.Azure.CognitiveServices.Personalizer.Tests
{
public class ConfigurationsTests : BaseTests
{
[Fact]
public async Task GetServiceConfiguration()
{
using (MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "GetServiceConfiguration");

IPersonalizerClient client = GetClient(HttpMockServer.CreateInstance());

ServiceConfiguration defaultConfig = await client.GetServiceConfigurationAsync();
Assert.Equal("00:01:00", defaultConfig.RewardWaitTime);
Assert.Equal("01:00:00", defaultConfig.ModelExportFrequency);
Assert.Equal(0D, defaultConfig.DefaultReward);
Assert.Equal(0.2, defaultConfig.ExplorationPercentage);
Assert.Equal(0, defaultConfig.LogRetentionDays);
}
}

[Fact]
public async Task UpdateServiceConfiguration()
{
using (MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "UpdateServiceConfiguration");

IPersonalizerClient client = GetClient(HttpMockServer.CreateInstance());

TimeSpan newExperimentalUnitDuration = TimeSpan.FromMinutes(1);
TimeSpan modelExportFrequency = TimeSpan.FromHours(1);
double newDefaultReward = 1.0;
string newRewardFuntion = "average";
double newExplorationPercentage = 0.2f;


var config = new ServiceConfiguration
{
RewardAggregation = newRewardFuntion,
ModelExportFrequency = modelExportFrequency.ToString(),
DefaultReward = newDefaultReward,
RewardWaitTime = newExperimentalUnitDuration.ToString(),
ExplorationPercentage = newExplorationPercentage
};


ServiceConfiguration result = await client.UpdateServiceConfigurationAsync(config);

Assert.Equal(config.DefaultReward, result.DefaultReward);
Assert.True(Math.Abs(config.ExplorationPercentage - result.ExplorationPercentage) < 1e-3);
Assert.Equal(config.ModelExportFrequency, result.ModelExportFrequency);
Assert.Equal(config.RewardAggregation, result.RewardAggregation);
Assert.Equal(config.RewardWaitTime, result.RewardWaitTime);
}
}

[Fact]
public async Task GetPolicy()
{
using (MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "GetPolicy");

IPersonalizerClient client = GetClient(HttpMockServer.CreateInstance());

PolicyContract policy = await client.GetPolicyAsync();

Assert.Equal("app1", policy.Name);
Assert.Equal("--cb_explore_adf --quadratic GT --quadratic MR --quadratic GR --quadratic ME --quadratic OT --quadratic OE --quadratic OR --quadratic MS --quadratic GX --ignore A --cb_type ips --epsilon 0.2",
policy.Arguments);
}
}

[Fact]
public async Task UpdatePolicy()
{
using (MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "UpdatePolicy");

IPersonalizerClient client = GetClient(HttpMockServer.CreateInstance());

var policy = new PolicyContract
{
Name = "app1",
Arguments = "--cb_explore_adf --quadratic GT --quadratic MR --quadratic GR --quadratic ME --quadratic OT --quadratic OE --quadratic OR --quadratic MS --quadratic GX --ignore A --cb_type ips --epsilon 0.2"
};

var updatedPolicy = await client.UpdatePolicyAsync(policy);

Assert.NotNull(updatedPolicy);
Assert.Equal(policy.Arguments, updatedPolicy.Arguments);

}
}

[Fact]
public async Task DeletePolicy()
{
using (MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "DeletePolicy");

IPersonalizerClient client = GetClient(HttpMockServer.CreateInstance());

PolicyContract policy = await client.DeletePolicyAsync();

Assert.Equal("--cb_explore_adf --quadratic GT --quadratic MR --quadratic GR --quadratic ME --quadratic OT --quadratic OE --quadratic OR --quadratic MS --quadratic GX --ignore A --cb_type ips --epsilon 0.2",
policy.Arguments);

}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using Microsoft.Azure.CognitiveServices.Personalizer.Models;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using Xunit;
using System.Collections.Generic;
using System;

namespace Microsoft.Azure.CognitiveServices.Personalizer.Tests
{
public class EvaluationsTests : BaseTests
{
[Fact]
public void GetEvaluations()
{
using (MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "GetEvaluations");

IPersonalizerClient client = GetClient(HttpMockServer.CreateInstance());

IList<Evaluation> evaluations = client.GetEvaluations();

Assert.Equal(2, evaluations.Count);
Assert.Equal("myFirstEvaluation", evaluations[0].Name);
}
}

[Fact]
public void CreateEvaluation()
{
using (MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "CreateEvaluation");

IPersonalizerClient client = GetClient(HttpMockServer.CreateInstance());

var evaluation = new EvaluationContract
{
Name = "myFirstEvaluation",
StartTime = new DateTime(2018, 12, 19),
EndTime = new DateTime(2019, 1, 19),
EnableOfflineExperimentation = true,
Policies = new PolicyContract[]
{
new PolicyContract
{
Name = "Custom Policy 1",
Arguments = "--cb_explore_adf --epsilon 0.2 --dsjson --cb_type ips -l 0.5 --l1 1E-07 --power_t 0.5"
}
}
};

Evaluation createdEvaluation = client.CreateEvaluation(evaluation);

Assert.Equal(evaluation.Name, createdEvaluation.Name);
}
}

[Fact]
public void GetEvaluation()
{
using (MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "GetEvaluation");

IPersonalizerClient client = GetClient(HttpMockServer.CreateInstance());

string evaluationId = "b58c6d92-b727-48c1-9487-4be2782c9e0a";
Evaluation evaluation = client.GetEvaluation(evaluationId);

Assert.Equal(evaluationId, evaluation.Id);
}
}

[Fact]
public void DeleteEvaluation()
{
using (MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "DeleteEvaluation");

IPersonalizerClient client = GetClient(HttpMockServer.CreateInstance());

string evaluationId = "b58c6d92-b727-48c1-9487-4be2782c9e0a";
client.DeleteEvaluation(evaluationId);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.Azure.CognitiveServices.Personalizer.Models;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using Xunit;
using System;
using System.Threading.Tasks;

namespace Microsoft.Azure.CognitiveServices.Personalizer.Tests
{
public class LogsTests : BaseTests
{
[Fact]
public async Task GetLogsProperties()
{
using (MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "GetLogsProperties");

IPersonalizerClient client = GetClient(HttpMockServer.CreateInstance());

LogsProperties properties = await client.GetLogsPropertiesAsync();
Assert.Equal(new DateTime(2019, 04, 13), new DateTime(properties.DateRange.FromProperty.Value.Year, properties.DateRange.FromProperty.Value.Month, properties.DateRange.FromProperty.Value.Day));
Assert.Equal(new DateTime(2019, 04, 23), new DateTime(properties.DateRange.To.Value.Year, properties.DateRange.To.Value.Month, properties.DateRange.To.Value.Day));
}
}

[Fact]
public async Task DeleteLogs()
{
using (MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "DeleteLogs");

IPersonalizerClient client = GetClient(HttpMockServer.CreateInstance());

await client.DeleteLogsAsync();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Microsoft.Azure.CognitiveServices.Personalizer.Models;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using Xunit;
using System.IO;
using System.Threading.Tasks;

namespace Microsoft.Azure.CognitiveServices.Personalizer.Tests
{
public class ModelTests : BaseTests
{
[Fact]
public async Task GetModel()
{
using (MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "GetModel");

IPersonalizerClient client = GetClient(HttpMockServer.CreateInstance());

Stream stream = await client.GetModelAsync();
Assert.NotEqual(stream.ReadByte(), -1);
}
}

[Fact]
public async Task DeleteModel()
{
using (MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "DeleteModel");

IPersonalizerClient client = GetClient(HttpMockServer.CreateInstance());

await client.DeleteModelAsync();
}
}

[Fact]
public async Task GetModelProperties()
{
using (MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "GetModelProperties");

IPersonalizerClient client = GetClient(HttpMockServer.CreateInstance());

ModelProperties modelProperties = await client.GetModelPropertiesAsync();
Assert.True(modelProperties.CreationTime != null);
Assert.True(modelProperties.LastModifiedTime != null);
}
}
}
}
Loading