Skip to content

Commit df9c935

Browse files
kinelskiannelo-msft
authored andcommitted
[MetricsAdvisor] Added tests for some Data Feed CRUD operations (Non-Live, Create, Get) (Azure#17721)
1 parent e34e618 commit df9c935

File tree

58 files changed

+10048
-1126
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+10048
-1126
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
using Azure.AI.MetricsAdvisor.Administration;
7+
using Azure.AI.MetricsAdvisor.Models;
8+
using NUnit.Framework;
9+
10+
namespace Azure.AI.MetricsAdvisor.Tests
11+
{
12+
/// <summary>
13+
/// Represents a <see cref="DataFeed"/> that has been created for testing purposes. In order to
14+
/// create a new instance of this class, the <see cref="CreateDataFeedAsync"/> static method must
15+
/// be invoked. The created data feed will be deleted upon disposal.
16+
/// </summary>
17+
public class DisposableDataFeed : IAsyncDisposable
18+
{
19+
/// <summary>
20+
/// The client to use for deleting the data feed upon disposal.
21+
/// </summary>
22+
private readonly MetricsAdvisorAdministrationClient _adminClient;
23+
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="DisposableDataFeed"/> class.
26+
/// </summary>
27+
/// <param name="adminClient">The client to use for deleting the data feed upon disposal.</param>
28+
/// <param name="id">The identifier of the data feed this instance is associated with.</param>
29+
private DisposableDataFeed(MetricsAdvisorAdministrationClient adminClient, string id)
30+
{
31+
_adminClient = adminClient;
32+
Id = id;
33+
}
34+
35+
/// <summary>
36+
/// The identifier of the data feed this instance is associated with.
37+
/// </summary>
38+
public string Id { get; }
39+
40+
/// <summary>
41+
/// Creates a data feed using the specified <see cref="MetricsAdvisorAdministrationClient"/>.
42+
/// A <see cref="DisposableDataFeed"/> instance is returned, from which the ID of the created
43+
/// data feed can be obtained. Upon disposal, the associated data feed will be deleted.
44+
/// </summary>
45+
/// <param name="adminClient">The client to use for creating and for deleting the data feed.</param>
46+
/// <param name="hook">Specifies how the created <see cref="DataFeed"/> should be configured.</param>
47+
/// <returns>A <see cref="DisposableDataFeed"/> instance from which the ID of the created data feed can be obtained.</returns>
48+
public static async Task<DisposableDataFeed> CreateDataFeedAsync(MetricsAdvisorAdministrationClient adminClient, DataFeed dataFeed)
49+
{
50+
string dataFeedId = await adminClient.CreateDataFeedAsync(dataFeed);
51+
52+
Assert.That(dataFeedId, Is.Not.Null.And.Not.Empty);
53+
54+
return new DisposableDataFeed(adminClient, dataFeedId);
55+
}
56+
57+
/// <summary>
58+
/// Deletes the data feed this instance is associated with.
59+
/// </summary>
60+
public async ValueTask DisposeAsync() => await _adminClient.DeleteDataFeedAsync(Id);
61+
}
62+
}

sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/MetricsAdvisorAdministrationClient/DataFeedLiveTests.cs

Lines changed: 897 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Threading;
7+
using Azure.AI.MetricsAdvisor.Administration;
8+
using Azure.AI.MetricsAdvisor.Models;
9+
using Azure.Core.TestFramework;
10+
using NUnit.Framework;
11+
12+
namespace Azure.AI.MetricsAdvisor.Tests
13+
{
14+
public class DataFeedTests : ClientTestBase
15+
{
16+
public DataFeedTests(bool isAsync) : base(isAsync)
17+
{
18+
}
19+
20+
private string FakeGuid => "00000000-0000-0000-0000-000000000000";
21+
22+
[Test]
23+
public void CreateDataFeedValidatesArguments()
24+
{
25+
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();
26+
27+
Assert.That(() => adminClient.CreateDataFeedAsync(null), Throws.InstanceOf<ArgumentNullException>());
28+
29+
Assert.That(() => adminClient.CreateDataFeed(null), Throws.InstanceOf<ArgumentNullException>());
30+
}
31+
32+
[Test]
33+
public void CreateDataFeedRespectsTheCancellationToken()
34+
{
35+
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();
36+
37+
var dataSource = new AzureTableDataFeedSource("connectionString", "table", "query");
38+
var granularity = new DataFeedGranularity(DataFeedGranularityType.Daily);
39+
var schema = new DataFeedSchema(new List<DataFeedMetric>() { new DataFeedMetric("metricName") });
40+
var ingestionSettings = new DataFeedIngestionSettings(DateTimeOffset.UtcNow);
41+
var dataFeed = new DataFeed("dataFeedName", dataSource, granularity, schema, ingestionSettings);
42+
43+
using var cancellationSource = new CancellationTokenSource();
44+
cancellationSource.Cancel();
45+
46+
Assert.That(() => adminClient.CreateDataFeedAsync(dataFeed, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>());
47+
Assert.That(() => adminClient.CreateDataFeed(dataFeed, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>());
48+
}
49+
50+
[Test]
51+
public void UpdateDataFeedValidatesArguments()
52+
{
53+
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();
54+
55+
var dataSource = new AzureTableDataFeedSource("connectionString", "table", "query");
56+
var granularity = new DataFeedGranularity(DataFeedGranularityType.Daily);
57+
var schema = new DataFeedSchema(new List<DataFeedMetric>() { new DataFeedMetric("metricName") });
58+
var ingestionSettings = new DataFeedIngestionSettings(DateTimeOffset.UtcNow);
59+
var dataFeed = new DataFeed("dataFeedName", dataSource, granularity, schema, ingestionSettings);
60+
61+
Assert.That(() => adminClient.UpdateDataFeedAsync(null, dataFeed), Throws.InstanceOf<ArgumentNullException>());
62+
Assert.That(() => adminClient.UpdateDataFeedAsync("", dataFeed), Throws.InstanceOf<ArgumentException>());
63+
Assert.That(() => adminClient.UpdateDataFeedAsync("dataFeedId", dataFeed), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException)));
64+
Assert.That(() => adminClient.UpdateDataFeedAsync(FakeGuid, null), Throws.InstanceOf<ArgumentNullException>());
65+
66+
Assert.That(() => adminClient.UpdateDataFeed(null, dataFeed), Throws.InstanceOf<ArgumentNullException>());
67+
Assert.That(() => adminClient.UpdateDataFeed("", dataFeed), Throws.InstanceOf<ArgumentException>());
68+
Assert.That(() => adminClient.UpdateDataFeed("dataFeedId", dataFeed), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException)));
69+
Assert.That(() => adminClient.UpdateDataFeed(FakeGuid, null), Throws.InstanceOf<ArgumentNullException>());
70+
}
71+
72+
[Test]
73+
public void UpdateDataFeedRespectsTheCancellationToken()
74+
{
75+
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();
76+
77+
var dataSource = new AzureTableDataFeedSource("connectionString", "table", "query");
78+
var granularity = new DataFeedGranularity(DataFeedGranularityType.Daily);
79+
var schema = new DataFeedSchema(new List<DataFeedMetric>() { new DataFeedMetric("metricName") });
80+
var ingestionSettings = new DataFeedIngestionSettings(DateTimeOffset.UtcNow);
81+
var dataFeed = new DataFeed("dataFeedName", dataSource, granularity, schema, ingestionSettings);
82+
83+
using var cancellationSource = new CancellationTokenSource();
84+
cancellationSource.Cancel();
85+
86+
Assert.That(() => adminClient.UpdateDataFeedAsync(FakeGuid, dataFeed, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>());
87+
Assert.That(() => adminClient.UpdateDataFeed(FakeGuid, dataFeed, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>());
88+
}
89+
90+
[Test]
91+
public void GetDataFeedValidatesArguments()
92+
{
93+
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();
94+
95+
Assert.That(() => adminClient.GetDataFeedAsync(null), Throws.InstanceOf<ArgumentNullException>());
96+
Assert.That(() => adminClient.GetDataFeedAsync(""), Throws.InstanceOf<ArgumentException>());
97+
Assert.That(() => adminClient.GetDataFeedAsync("dataFeedId"), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException)));
98+
99+
Assert.That(() => adminClient.GetDataFeed(null), Throws.InstanceOf<ArgumentNullException>());
100+
Assert.That(() => adminClient.GetDataFeed(""), Throws.InstanceOf<ArgumentException>());
101+
Assert.That(() => adminClient.GetDataFeed("dataFeedId"), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException)));
102+
}
103+
104+
[Test]
105+
public void GetDataFeedRespectsTheCancellationToken()
106+
{
107+
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();
108+
109+
using var cancellationSource = new CancellationTokenSource();
110+
cancellationSource.Cancel();
111+
112+
Assert.That(() => adminClient.GetDataFeedAsync(FakeGuid, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>());
113+
Assert.That(() => adminClient.GetDataFeed(FakeGuid, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>());
114+
}
115+
116+
[Test]
117+
public void GetDataFeedsRespectsTheCancellationToken()
118+
{
119+
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();
120+
121+
using var cancellationSource = new CancellationTokenSource();
122+
cancellationSource.Cancel();
123+
124+
IAsyncEnumerator<DataFeed> asyncEnumerator = adminClient.GetDataFeedsAsync(cancellationToken: cancellationSource.Token).GetAsyncEnumerator();
125+
Assert.That(async () => await asyncEnumerator.MoveNextAsync(), Throws.InstanceOf<OperationCanceledException>());
126+
127+
IEnumerator<DataFeed> enumerator = adminClient.GetDataFeeds(cancellationToken: cancellationSource.Token).GetEnumerator();
128+
Assert.That(() => enumerator.MoveNext(), Throws.InstanceOf<OperationCanceledException>());
129+
}
130+
131+
[Test]
132+
public void DeleteDataFeedValidatesArguments()
133+
{
134+
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();
135+
136+
Assert.That(() => adminClient.DeleteDataFeedAsync(null), Throws.InstanceOf<ArgumentNullException>());
137+
Assert.That(() => adminClient.DeleteDataFeedAsync(""), Throws.InstanceOf<ArgumentException>());
138+
Assert.That(() => adminClient.DeleteDataFeedAsync("dataFeedId"), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException)));
139+
140+
Assert.That(() => adminClient.DeleteDataFeed(null), Throws.InstanceOf<ArgumentNullException>());
141+
Assert.That(() => adminClient.DeleteDataFeed(""), Throws.InstanceOf<ArgumentException>());
142+
Assert.That(() => adminClient.DeleteDataFeed("dataFeedId"), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException)));
143+
}
144+
145+
[Test]
146+
public void DeleteDataFeedRespectsTheCancellationToken()
147+
{
148+
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();
149+
150+
using var cancellationSource = new CancellationTokenSource();
151+
cancellationSource.Cancel();
152+
153+
Assert.That(() => adminClient.DeleteDataFeedAsync(FakeGuid, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>());
154+
Assert.That(() => adminClient.DeleteDataFeed(FakeGuid, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>());
155+
}
156+
157+
private MetricsAdvisorAdministrationClient GetMetricsAdvisorAdministrationClient()
158+
{
159+
var fakeEndpoint = new Uri("http://notreal.azure.com");
160+
var fakeCredential = new MetricsAdvisorKeyCredential("fakeSubscriptionKey", "fakeApiKey");
161+
162+
return new MetricsAdvisorAdministrationClient(fakeEndpoint, fakeCredential);
163+
}
164+
}
165+
}

sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/MetricsAdvisorAdministrationClientLiveTests.cs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,6 @@ public async Task GetDataFeeds()
2525
Assert.That(feeds, Is.Not.Empty);
2626
}
2727

28-
[RecordedTest]
29-
public async Task GetDataFeed()
30-
{
31-
var adminClient = GetMetricsAdvisorAdministrationClient();
32-
33-
List<DataFeed> feeds = await adminClient.GetDataFeedsAsync().ToEnumerableAsync().ConfigureAwait(false);
34-
35-
Assert.That(feeds, Is.Not.Empty);
36-
37-
foreach (DataFeed feed in feeds)
38-
{
39-
DataFeed feedResult = await adminClient.GetDataFeedAsync(feed.Id);
40-
41-
Assert.That(feedResult.CreatedTime, Is.EqualTo(feed.CreatedTime));
42-
Assert.That(feedResult.Granularity.GranularityType, Is.EqualTo(feed.Granularity.GranularityType));
43-
Assert.That(feedResult.Granularity.CustomGranularityValue, Is.EqualTo(feed.Granularity.CustomGranularityValue));
44-
Assert.That(feedResult.Id, Is.EqualTo(feed.Id));
45-
Assert.That(feedResult.IngestionSettings.DataSourceRequestConcurrency, Is.EqualTo(feed.IngestionSettings.DataSourceRequestConcurrency));
46-
Assert.That(feedResult.IsAdministrator, Is.EqualTo(feed.IsAdministrator));
47-
Assert.That(feedResult.MetricIds, Is.EqualTo(feed.MetricIds));
48-
Assert.That(feedResult.Name, Is.EqualTo(feed.Name));
49-
Assert.That(feedResult.Administrators, Is.EquivalentTo(feed.Administrators));
50-
Assert.That(feedResult.Schema.DimensionColumns.Count, Is.EqualTo(feed.Schema.DimensionColumns.Count));
51-
Assert.That(feedResult.SourceType, Is.EqualTo(feed.SourceType));
52-
Assert.That(feedResult.Status, Is.EqualTo(feed.Status));
53-
}
54-
}
55-
5628
[RecordedTest]
5729
public async Task CreateAndUpdateBlobDataFeed()
5830
{

0 commit comments

Comments
 (0)