| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | namespace Microsoft.Azure.Search.Tests.Utilities |
| | 6 | | { |
| | 7 | | using System; |
| | 8 | | using System.Runtime.CompilerServices; |
| | 9 | | using Microsoft.Azure.Management.Search; |
| | 10 | | using Microsoft.Rest.ClientRuntime.Azure.TestFramework; |
| | 11 | |
|
| | 12 | | public abstract class SearchTestBase<TTestFixture> : TestBase where TTestFixture : IResourceFixture, new() |
| | 13 | | { |
| | 14 | | private MockContext _currentContext; // Changes as each test runs. |
| | 15 | |
|
| 2666 | 16 | | protected TTestFixture Data { get; private set; } |
| | 17 | |
|
| | 18 | | protected SearchManagementClient GetSearchManagementClient() |
| | 19 | | { |
| 0 | 20 | | if (_currentContext == null) |
| | 21 | | { |
| 0 | 22 | | throw new InvalidOperationException("GetSearchManagementClient() can only be called from a running test. |
| | 23 | | } |
| | 24 | |
|
| 0 | 25 | | return _currentContext.GetServiceClient<SearchManagementClient>(); |
| | 26 | | } |
| | 27 | |
|
| | 28 | | protected void Run( |
| | 29 | | Action testBody, |
| | 30 | | [CallerMemberName] |
| | 31 | | string methodName = "unknown_caller") |
| | 32 | | { |
| 602 | 33 | | using (var mockContext = MockContext.Start(this.GetType(), methodName)) |
| | 34 | | { |
| 602 | 35 | | _currentContext = mockContext; |
| 602 | 36 | | Data = new TTestFixture(); |
| 602 | 37 | | Data.Initialize(mockContext); |
| | 38 | |
|
| | 39 | | try |
| | 40 | | { |
| 602 | 41 | | testBody(); |
| 602 | 42 | | } |
| | 43 | | finally |
| | 44 | | { |
| 602 | 45 | | Data.Cleanup(); |
| 602 | 46 | | } |
| | 47 | | } |
| 602 | 48 | | } |
| | 49 | | } |
| | 50 | | } |