Skip to content

Commit

Permalink
[Storage] Make Storage clients mockable again. (Azure#18243)
Browse files Browse the repository at this point in the history
* this fixes webjobs.

* queues.

* shares.

* datalake.

* blobs.

* batch.

* fix tests.
  • Loading branch information
kasobol-msft authored Jan 27, 2021
1 parent 3702961 commit 69f25da
Show file tree
Hide file tree
Showing 67 changed files with 537 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Azure.Storage.Blobs.Specialized;
using Azure.Storage.Test;
using Azure.Storage.Test.Shared;
using Moq;
using NUnit.Framework;

namespace Azure.Storage.Blobs.Test
Expand Down Expand Up @@ -773,5 +774,16 @@ public async ValueTask DisposeAsync()
}
}
#endregion Scenario helper

[Test]
public void CanMockClientConstructors()
{
var blobServiceClientMock = new Mock<BlobServiceClient>(TestConfigDefault.ConnectionString)
{
CallBase = true
};
// One has to call .Object to trigger constructor. It's lazy.
var mock = new Mock<BlobBatchClient>(blobServiceClientMock.Object).Object;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions sdk/storage/Azure.Storage.Blobs/tests/AppendBlobClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Azure.Storage.Sas;
using Azure.Storage.Test;
using Azure.Storage.Test.Shared;
using Moq;
using NUnit.Framework;

namespace Azure.Storage.Blobs.Test
Expand Down Expand Up @@ -1835,6 +1836,18 @@ await TestHelper.CatchAsync<Exception>(
}
}

[Test]
public void CanMockClientConstructors()
{
// One has to call .Object to trigger constructor. It's lazy.
var mock = new Mock<AppendBlobClient>(TestConfigDefault.ConnectionString, "name", "name", new BlobClientOptions()).Object;
mock = new Mock<AppendBlobClient>(TestConfigDefault.ConnectionString, "name", "name").Object;
mock = new Mock<AppendBlobClient>(new Uri("https://test/test"), new BlobClientOptions()).Object;
mock = new Mock<AppendBlobClient>(new Uri("https://test/test"), GetNewSharedKeyCredentials(), new BlobClientOptions()).Object;
mock = new Mock<AppendBlobClient>(new Uri("https://test/test"), new AzureSasCredential("foo"), new BlobClientOptions()).Object;
mock = new Mock<AppendBlobClient>(new Uri("https://test/test"), GetOAuthCredential(TestConfigHierarchicalNamespace), new BlobClientOptions()).Object;
}

private AppendBlobRequestConditions BuildDestinationAccessConditions(
AccessConditionParameters parameters,
bool lease = false,
Expand Down
12 changes: 12 additions & 0 deletions sdk/storage/Azure.Storage.Blobs/tests/BlobBaseClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6496,6 +6496,18 @@ public void CanMockParentContainerClientRetrieval()
Assert.AreSame(blobContainerClientMock.Object, blobContainerClient);
}

[Test]
public void CanMockClientConstructors()
{
// One has to call .Object to trigger constructor. It's lazy.
var mock = new Mock<BlobBaseClient>(TestConfigDefault.ConnectionString, "name", "name", new BlobClientOptions()).Object;
mock = new Mock<BlobBaseClient>(TestConfigDefault.ConnectionString, "name", "name").Object;
mock = new Mock<BlobBaseClient>(new Uri("https://test/test"), new BlobClientOptions()).Object;
mock = new Mock<BlobBaseClient>(new Uri("https://test/test"), GetNewSharedKeyCredentials(), new BlobClientOptions()).Object;
mock = new Mock<BlobBaseClient>(new Uri("https://test/test"), new AzureSasCredential("foo"), new BlobClientOptions()).Object;
mock = new Mock<BlobBaseClient>(new Uri("https://test/test"), GetOAuthCredential(TestConfigHierarchicalNamespace), new BlobClientOptions()).Object;
}

public IEnumerable<AccessConditionParameters> AccessConditions_Data
=> new[]
{
Expand Down
13 changes: 13 additions & 0 deletions sdk/storage/Azure.Storage.Blobs/tests/BlobClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Azure.Storage.Test.Shared;
using Azure.Storage.Tests;
using Azure.Storage.Tests.Shared;
using Moq;
using NUnit.Framework;

namespace Azure.Storage.Blobs.Test
Expand Down Expand Up @@ -1206,5 +1207,17 @@ public void WithVersion()
Assert.AreEqual(versionId, blobUriBuilder.VersionId);
Assert.AreEqual(versionUri, blobUriBuilder.ToUri());
}

[Test]
public void CanMockClientConstructors()
{
// One has to call .Object to trigger constructor. It's lazy.
var mock = new Mock<BlobClient>(TestConfigDefault.ConnectionString, "name", "name", new BlobClientOptions()).Object;
mock = new Mock<BlobClient>(TestConfigDefault.ConnectionString, "name", "name").Object;
mock = new Mock<BlobClient>(new Uri("https://test/test"), new BlobClientOptions()).Object;
mock = new Mock<BlobClient>(new Uri("https://test/test"), GetNewSharedKeyCredentials(), new BlobClientOptions()).Object;
mock = new Mock<BlobClient>(new Uri("https://test/test"), new AzureSasCredential("foo"), new BlobClientOptions()).Object;
mock = new Mock<BlobClient>(new Uri("https://test/test"), GetOAuthCredential(TestConfigHierarchicalNamespace), new BlobClientOptions()).Object;
}
}
}
13 changes: 13 additions & 0 deletions sdk/storage/Azure.Storage.Blobs/tests/BlockBlobClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Azure.Storage.Sas;
using Azure.Storage.Test;
using Azure.Storage.Test.Shared;
using Moq;
using NUnit.Framework;
using Metadata = System.Collections.Generic.IDictionary<string, string>;
using Tags = System.Collections.Generic.IDictionary<string, string>;
Expand Down Expand Up @@ -3370,6 +3371,18 @@ await TestHelper.AssertExpectedExceptionAsync<RequestFailedException>(
e => Assert.AreEqual(BlobErrorCode.BlobAlreadyExists.ToString(), e.ErrorCode));
}

[Test]
public void CanMockClientConstructors()
{
// One has to call .Object to trigger constructor. It's lazy.
var mock = new Mock<BlockBlobClient>(TestConfigDefault.ConnectionString, "name", "name", new BlobClientOptions()).Object;
mock = new Mock<BlockBlobClient>(TestConfigDefault.ConnectionString, "name", "name").Object;
mock = new Mock<BlockBlobClient>(new Uri("https://test/test"), new BlobClientOptions()).Object;
mock = new Mock<BlockBlobClient>(new Uri("https://test/test"), GetNewSharedKeyCredentials(), new BlobClientOptions()).Object;
mock = new Mock<BlockBlobClient>(new Uri("https://test/test"), new AzureSasCredential("foo"), new BlobClientOptions()).Object;
mock = new Mock<BlockBlobClient>(new Uri("https://test/test"), GetOAuthCredential(TestConfigHierarchicalNamespace), new BlobClientOptions()).Object;
}

private RequestConditions BuildRequestConditions(AccessConditionParameters parameters)
=> new RequestConditions
{
Expand Down
12 changes: 12 additions & 0 deletions sdk/storage/Azure.Storage.Blobs/tests/ContainerClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2959,6 +2959,18 @@ public async Task CanGetParentBlobServiceClient_WithAccountSAS()
await container.DeleteIfExistsAsync();
}

[Test]
public void CanMockClientConstructors()
{
// One has to call .Object to trigger constructor. It's lazy.
var mock = new Mock<BlobContainerClient>(TestConfigDefault.ConnectionString, "name", new BlobClientOptions()).Object;
mock = new Mock<BlobContainerClient>(TestConfigDefault.ConnectionString, "name").Object;
mock = new Mock<BlobContainerClient>(new Uri("https://test/test"), new BlobClientOptions()).Object;
mock = new Mock<BlobContainerClient>(new Uri("https://test/test"), GetNewSharedKeyCredentials(), new BlobClientOptions()).Object;
mock = new Mock<BlobContainerClient>(new Uri("https://test/test"), new AzureSasCredential("foo"), new BlobClientOptions()).Object;
mock = new Mock<BlobContainerClient>(new Uri("https://test/test"), GetOAuthCredential(TestConfigHierarchicalNamespace), new BlobClientOptions()).Object;
}

#region Secondary Storage
[Test]
public async Task ListContainersSegmentAsync_SecondaryStorageFirstRetrySuccessful()
Expand Down
13 changes: 13 additions & 0 deletions sdk/storage/Azure.Storage.Blobs/tests/PageBlobClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Azure.Storage.Test;
using Azure.Storage.Test.Shared;
using Azure.Storage.Tests;
using Moq;
using NUnit.Framework;

namespace Azure.Storage.Blobs.Test
Expand Down Expand Up @@ -3485,6 +3486,18 @@ public async Task OpenWriteAsync_Position()
TestHelper.AssertSequenceEqual(expectedData, dataResult.ToArray());
}

[Test]
public void CanMockClientConstructors()
{
// One has to call .Object to trigger constructor. It's lazy.
var mock = new Mock<PageBlobClient>(TestConfigDefault.ConnectionString, "name", "name", new BlobClientOptions()).Object;
mock = new Mock<PageBlobClient>(TestConfigDefault.ConnectionString, "name", "name").Object;
mock = new Mock<PageBlobClient>(new Uri("https://test/test"), new BlobClientOptions()).Object;
mock = new Mock<PageBlobClient>(new Uri("https://test/test"), GetNewSharedKeyCredentials(), new BlobClientOptions()).Object;
mock = new Mock<PageBlobClient>(new Uri("https://test/test"), new AzureSasCredential("foo"), new BlobClientOptions()).Object;
mock = new Mock<PageBlobClient>(new Uri("https://test/test"), GetOAuthCredential(TestConfigHierarchicalNamespace), new BlobClientOptions()).Object;
}

private PageBlobRequestConditions BuildAccessConditions(
AccessConditionParameters parameters,
bool lease = false,
Expand Down
13 changes: 13 additions & 0 deletions sdk/storage/Azure.Storage.Blobs/tests/ServiceClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Azure.Storage.Sas;
using Azure.Storage.Test;
using Azure.Storage.Test.Shared;
using Moq;
using NUnit.Framework;

namespace Azure.Storage.Blobs.Test
Expand Down Expand Up @@ -932,5 +933,17 @@ public void GenerateAccountSas_WrongService_Service()
}
}
#endregion

[Test]
public void CanMockClientConstructors()
{
// One has to call .Object to trigger constructor. It's lazy.
var mock = new Mock<BlobServiceClient>(TestConfigDefault.ConnectionString, new BlobClientOptions()).Object;
mock = new Mock<BlobServiceClient>(TestConfigDefault.ConnectionString).Object;
mock = new Mock<BlobServiceClient>(new Uri("https://test/test"), new BlobClientOptions()).Object;
mock = new Mock<BlobServiceClient>(new Uri("https://test/test"), GetNewSharedKeyCredentials(), new BlobClientOptions()).Object;
mock = new Mock<BlobServiceClient>(new Uri("https://test/test"), new AzureSasCredential("foo"), new BlobClientOptions()).Object;
mock = new Mock<BlobServiceClient>(new Uri("https://test/test"), GetOAuthCredential(TestConfigHierarchicalNamespace), new BlobClientOptions()).Object;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 69f25da

Please sign in to comment.