Skip to content

Commit

Permalink
Make sure QA resources are cleaned up (#30276)
Browse files Browse the repository at this point in the history
* Make sure QA resources are cleaned up

Fixes #20300

* Fix broken tests

* Update snippets
  • Loading branch information
heaths authored Aug 3, 2022
1 parent 088aac6 commit 78479a0
Show file tree
Hide file tree
Showing 9 changed files with 343 additions and 321 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@ RequestContent importRequestContent = RequestContent.Create(new
{
Metadata = new
{
ProjectName = "NewProjectForExport",
Description = "This is the description for a test project",
Language = "en",
DefaultAnswer = "No answer found for your question.",
MultilingualResource = false,
CreatedDateTime = "2021-11-25T09=35=33Z",
LastModifiedDateTime = "2021-11-25T09=35=33Z",
Settings = new
{
DefaultAnswer = "No answer found for your question."
Expand All @@ -53,7 +50,6 @@ RequestContent importRequestContent = RequestContent.Create(new
});

Operation<BinaryData> importOperation = client.Import(WaitUntil.Completed, importedProjectName, importRequestContent, format: "json");

Console.WriteLine($"Operation status: {importOperation.GetRawResponse().Status}");
```

Expand Down Expand Up @@ -88,13 +84,10 @@ RequestContent importRequestContent = RequestContent.Create(new
{
Metadata = new
{
ProjectName = "NewProjectForExport",
Description = "This is the description for a test project",
Language = "en",
DefaultAnswer = "No answer found for your question.",
MultilingualResource = false,
CreatedDateTime = "2021-11-25T09=35=33Z",
LastModifiedDateTime = "2021-11-25T09=35=33Z",
Settings = new
{
DefaultAnswer = "No answer found for your question."
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Threading;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using Azure.AI.Language.QuestionAnswering.Projects;
using Azure.Core;
using Azure.Core.TestFramework;
using NUnit.Framework;

namespace Azure.AI.Language.QuestionAnswering.Tests
{
public partial class QuestionAnsweringProjectsLiveTestBase : QuestionAnsweringTestBase<QuestionAnsweringProjectsClient>
{
private ConcurrentQueue<string> _projects = new();

public QuestionAnsweringProjectsLiveTestBase(bool isAsync, QuestionAnsweringClientOptions.ServiceVersion serviceVersion)
: base(isAsync, serviceVersion, null /* RecordedTestMode.Record /* to record */)
{
}

[TearDown]
public async Task DeleteProjectsAsync()
{
if (Mode == RecordedTestMode.Playback)
{
return;
}

using TestRecording.DisableRecordingScope scope = Recording.DisableRecording();
while (_projects.TryDequeue(out string projectName))
{
try
{
await Client.DeleteProjectAsync(WaitUntil.Completed, projectName).ConfigureAwait(false);
}
catch (RequestFailedException ex) when (ex.Status == 404)
{
}
}
}

protected string CreateTestProjectName()
{
return "TestProject" + Recording.GenerateId();
Expand All @@ -41,6 +64,7 @@ protected void CreateProject(string projectName = default)
);

Client.CreateProject(projectName, creationRequestContent);
_projects.Enqueue(projectName);
}

protected async Task<Response> CreateProjectAsync(string projectName = default)
Expand All @@ -61,18 +85,12 @@ protected async Task<Response> CreateProjectAsync(string projectName = default)
}
);

return await Client.CreateProjectAsync(projectName, creationRequestContent);
}
Response response = await Client.CreateProjectAsync(projectName, creationRequestContent);
_projects.Enqueue(projectName);

protected async Task DeleteProjectAsync(string projectName)
{
Operation<BinaryData> deletionOperation = await Client.DeleteProjectAsync(WaitUntil.Completed, projectName);
return response;
}

protected void DeleteProject(string projectName)
{
// Insert this back when the delete LRO bug is fixed
Operation<BinaryData> deletionOperation = Client.DeleteProject(WaitUntil.Completed, projectName);
}
protected void EnqueueProjectDeletion(string projectName) => _projects.Enqueue(projectName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public async Task CreateProject()
Assert.AreEqual(200, projectDetailsResponse.Status);
Assert.That((await projects.ToEnumerableAsync()).Any(project => project.ToString().Contains(testProjectName)));
Assert.That(projectDetailsResponse.Content.ToString().Contains(testProjectName));

await DeleteProjectAsync(testProjectName);
}

[RecordedTest]
Expand Down Expand Up @@ -104,8 +102,6 @@ public async Task DeployProject()

Assert.True(deploymentOperation.HasCompleted);
Assert.That((await deployments.ToEnumerableAsync()).Any(deployment => deployment.ToString().Contains(testDeploymentName)));

await DeleteProjectAsync(testProjectName);
}

[RecordedTest]
Expand Down Expand Up @@ -139,8 +135,6 @@ public async Task UpdateQnAs()
Assert.AreEqual(200, updateQnasOperation.GetRawResponse().Status);
Assert.That((await sources.ToEnumerableAsync()).Any(source => source.ToString().Contains(question)));
Assert.That((await sources.ToEnumerableAsync()).Any(source => source.ToString().Contains(answer)));

await DeleteProjectAsync(testProjectName);
}

[RecordedTest]
Expand Down Expand Up @@ -175,8 +169,6 @@ public async Task UpdateSources()
Assert.True(updateSourcesOperation.HasCompleted);
Assert.AreEqual(200, updateSourcesOperation.GetRawResponse().Status);
Assert.That((await sources.ToEnumerableAsync()).Any(source => source.ToString().Contains(sourceUri)));

await DeleteProjectAsync(testProjectName);
}

[RecordedTest]
Expand Down Expand Up @@ -214,8 +206,6 @@ public async Task UpdateSynonyms()
Assert.AreEqual(204, updateSynonymsResponse.Status);
Assert.That((await synonyms.ToEnumerableAsync()).Any(synonym => synonym.ToString().Contains("qnamaker")));
Assert.That((await synonyms.ToEnumerableAsync()).Any(synonym => synonym.ToString().Contains("qna")));

await DeleteProjectAsync(testProjectName);
}

[RecordedTest]
Expand All @@ -233,8 +223,6 @@ public async Task Export()
Assert.True(exportOperation.HasCompleted);
Assert.AreEqual(200, exportOperation.GetRawResponse().Status);
Assert.True(!String.IsNullOrEmpty(exportedFileUrl));

await DeleteProjectAsync(testProjectName);
}

[RecordedTest]
Expand Down Expand Up @@ -268,8 +256,6 @@ public async Task Import()
Assert.AreEqual(200, importOperation.GetRawResponse().Status);
Assert.AreEqual(200, projectDetails.Status);
Assert.That(projectDetails.Content.ToString().Contains(testProjectName));

await DeleteProjectAsync(testProjectName);
}

[RecordedTest]
Expand All @@ -295,7 +281,6 @@ public async Task AddFeedback()
Response addFeedbackResponse = await Client.AddFeedbackAsync(testProjectName, addFeedbackRequestContent);

Assert.AreEqual(204, addFeedbackResponse.Status);
await DeleteProjectAsync(testProjectName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ public async Task CreateAndDeploy()

Assert.True(deploymentOperation.HasCompleted);
Assert.That(deployments.Any(deployment => deployment.ToString().Contains(newDeploymentName)));

DeleteProject(newProjectName);
}

[RecordedTest]
Expand Down Expand Up @@ -236,8 +234,6 @@ public async Task CreateAndDeployAsync()

Assert.True(deploymentOperation.HasCompleted);
Assert.That((await deployments.ToEnumerableAsync()).Any(deployment => deployment.ToString().Contains(newDeploymentName)));

await DeleteProjectAsync(newProjectName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
using NUnit.Framework;
using Azure.AI.Language.QuestionAnswering.Projects;
using Azure.Core;
using System.Linq;
using System.Threading;
using System.Text.Json;

namespace Azure.AI.Language.QuestionAnswering.Tests.Samples
Expand All @@ -32,25 +30,22 @@ public void ExportAndImport()
#endregion

Assert.True(exportOperation.HasCompleted);
Assert.True(!String.IsNullOrEmpty(exportedFileUrl));
Assert.True(!string.IsNullOrEmpty(exportedFileUrl));

#region Snippet:QuestionAnsweringProjectsClient_ImportProject
// Set import project name and request content
string importedProjectName = "{ProjectNameToBeImported}";
#if !SNIPPET
importedProjectName = "importedProject";
importedProjectName = CreateTestProjectName();
#endif
RequestContent importRequestContent = RequestContent.Create(new
{
Metadata = new
{
ProjectName = "NewProjectForExport",
Description = "This is the description for a test project",
Language = "en",
DefaultAnswer = "No answer found for your question.",
MultilingualResource = false,
CreatedDateTime = "2021-11-25T09=35=33Z",
LastModifiedDateTime = "2021-11-25T09=35=33Z",
Settings = new
{
DefaultAnswer = "No answer found for your question."
Expand All @@ -59,7 +54,9 @@ public void ExportAndImport()
});

Operation<BinaryData> importOperation = client.Import(WaitUntil.Completed, importedProjectName, importRequestContent, format: "json");

#if !SNIPPET
EnqueueProjectDeletion(importedProjectName);
#endif
Console.WriteLine($"Operation status: {importOperation.GetRawResponse().Status}");
#endregion

Expand All @@ -73,8 +70,6 @@ public void ExportAndImport()
#endregion

Assert.AreEqual(200, projectDetails.Status);

DeleteProject(importedProjectName);
}

[RecordedTest]
Expand All @@ -94,25 +89,22 @@ public async Task ExportAndImportAsync()
#endregion

Assert.True(exportOperation.HasCompleted);
Assert.True(!String.IsNullOrEmpty(exportedFileUrl));
Assert.True(!string.IsNullOrEmpty(exportedFileUrl));

#region Snippet:QuestionAnsweringProjectsClient_ImportProjectAsync
// Set import project name and request content
string importedProjectName = "{ProjectNameToBeImported}";
#if !SNIPPET
importedProjectName = "importedProject";
importedProjectName = CreateTestProjectName();
#endif
RequestContent importRequestContent = RequestContent.Create(new
{
Metadata = new
{
ProjectName = "NewProjectForExport",
Description = "This is the description for a test project",
Language = "en",
DefaultAnswer = "No answer found for your question.",
MultilingualResource = false,
CreatedDateTime = "2021-11-25T09=35=33Z",
LastModifiedDateTime = "2021-11-25T09=35=33Z",
Settings = new
{
DefaultAnswer = "No answer found for your question."
Expand All @@ -121,6 +113,9 @@ public async Task ExportAndImportAsync()
});

Operation<BinaryData> importOperation = await client.ImportAsync(WaitUntil.Completed, importedProjectName, importRequestContent, format: "json");
#if !SNIPPET
EnqueueProjectDeletion(importedProjectName);
#endif
Console.WriteLine($"Operation status: {importOperation.GetRawResponse().Status}");
#endregion

Expand All @@ -134,8 +129,6 @@ public async Task ExportAndImportAsync()
#endregion

Assert.AreEqual(200, projectDetails.Status);

await DeleteProjectAsync(importedProjectName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ public async Task KnowledgeSources()
#endregion

Assert.AreEqual(204, addFeedbackResponse.Status);

DeleteProject(testProjectName);
}

[RecordedTest]
Expand Down Expand Up @@ -317,8 +315,6 @@ public async Task KnowledgeSourcesAsync()
#endregion

Assert.AreEqual(204, addFeedbackResponse.Status);

await DeleteProjectAsync(testProjectName);
}
}
}
Loading

0 comments on commit 78479a0

Please sign in to comment.