Skip to content

Commit

Permalink
feat: add initial test components for table storage (#193)
Browse files Browse the repository at this point in the history
* pr-fix: correct merge w/ 'main'

* feat: initial commit table storage test components

* feat: add initial test components for table storage

* pr-revert: blob storage logging

* pr-fix: blob fixture changes

* pr-docs: add xml code docs for test context

* pr-fix: merge table w/ blob storage

* pr-fix: correct mdx extension

* pr-fix: add missing tabitem closing tag

* pr-fix: remove unnecessary titles

* pr-fix: use table service client as advanced overload

* pr-fix: expose table name
  • Loading branch information
stijnmoreels authored Oct 28, 2024
1 parent 3d56a87 commit 8d7d14b
Show file tree
Hide file tree
Showing 14 changed files with 1,435 additions and 5 deletions.
106 changes: 106 additions & 0 deletions docs/preview/02-Features/04-Storage/01-storage-account.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,111 @@ await using TemporaryBlobFile file = ...
file.OnTeardown.DeleteExistingBlob();
```

</TabItem>
<TabItem value="table" label="Table storage">

The `Arcus.Testing.Storage.Table` package provides test fixtures related to Azure Table storage. By using the common testing practice 'clean environment', it provides a temporary Table and Table entity.

## Installation
The following functionality is available when installing this package:

```shell
PM> Install-Package -Name Arcus.Testing.Storage.Table
```

## Temporary Table
The `TemporaryTable` provides a solution when the integration test requires a storage system (table) during the test run. An Azure Table is created upon the setup of the test fixture and is deleted again when the test fixture is disposed.

```csharp
using Arcus.Testing;

await using var table = await TemporaryTable.CreateIfNotExistsAsync(
"<account-name", "<table-name>", logger);

// Interact with the table during the lifetime of the fixture.
TableClient client = table.Client;
```

> 🎖️ Overloads are available to fully control the authentication mechanism to Azure Table storage. By default, it uses the [`DefaultAzureCredential`](https://learn.microsoft.com/en-us/dotnet/api/azure.identity.defaultazurecredential).
Adding entities to the table can also be done via the test fixture. It always make sure that any added entities will be removed afterwards upon disposal.

```csharp
using Arcus.Testing;

await using TemporaryTable table = ...

ITableEntity myEntity = ...
await table.AddEntityAsync(myEntity);
```

### Customization
The setup and teardown process of the temporary table is configurable. **By default, it always removes any resources that it was responsible for creating.**

```csharp
using Arcus.Testing;

await TemporaryTable.CreateIfNotExistsAsync(..., options =>
{
// Options related to when the test fixture is set up.
// ---------------------------------------------------
// (Default) Leaves all Azure Table entities untouched that already existed,
// upon the test fixture creation, when there was already an Azure Table available.
options.OnSetup.LeaveAllEntities();

// Delete all Azure Table entities upon the test fixture creation,
// when there was already an Azure Table available.
options.OnSetup.CleanAllEntities();

// Delete Azure Table entities that matches any of the configured filters,
// upon the test fixture creation, when there was already an Azure Table available.
options.OnSetup.CleanMatchingEntities(
TableEntityFilter.RowKeyEqual("<row-key>"),
TableEntityFilter.PartitionKeyEqual("<partition-key>"),
TableEntityFilter.EntityEqual((TableEntity entity) => entity["Key"] == "Value"));

// Options related to when the test fixture is teared down.
// --------------------------------------------------------
// (Default for cleaning entities)
// Delete Azure Table entities upon the test fixture disposal that were added by the fixture.
options.OnTeardown.CleanCreatedEntities();

// Delete all Azure Table entities upon the test fixture disposal,
// even if the test fixture didn't added them.
options.OnTeardown.CleanAllEntities();

// Delete additional Azure Table entities that matches any of the configured filters,
// upon the test fixture disposal.
// ⚠️ Entities added by the test fixture itself will always be deleted.
options.OnTeardown.CleanMatchingEntities(
TableEntityFilter.RowKeyEqual("<row-key>"),
TableEntityFilter.PartitionKeyEqual("<partition-key>"),
TableEntityFilter.EntityEqual((TableEntity entity) => entity["Key"] == "Value"));
});

// `OnTeardown` is also still available after the temporary table is created:
await using TemporaryTable table = ...

table.OnTeardown.CleanAllEntities();
```

> 🎖️ The `TemporaryTable` will always remove any Azure Table entities that were added on the temporary table itself with the `table.AddEntityAsync`. This follows the 'clean environment' testing principal that any test should not leave any state it created behind after the test has run.
## Temporary Table entity
The `TemporaryTableEntity` provides a solution when the integration test requires data (table) during the test run. An Azure Table entity is added upon the setup of the test fixture and is deleted again when the test fixture is disposed.

```csharp
using Arcus.Testing;

ITableEntity entity = ...

await using var entity = await TemporaryTableEntity.AddIfNotExistsAsync(
"<account-name>", "<table-name>", entity, logger);
```

> 🎖️ Overloads are available to fully control the authentication mechanism to Azure Table storage. By default, it uses the [`DefaultAzureCredential`](https://learn.microsoft.com/en-us/dotnet/api/azure.identity.defaultazurecredential).
</TabItem>
</Tabs>
2 changes: 1 addition & 1 deletion src/Arcus.Testing.Storage.Blob/TemporaryBlobContainer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
Expand Down
4 changes: 2 additions & 2 deletions src/Arcus.Testing.Storage.Blob/TemporaryBlobFile.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading.Tasks;
using Azure.Identity;
using Azure.Storage.Blobs;
Expand Down Expand Up @@ -291,4 +291,4 @@ public async ValueTask DisposeAsync()
GC.SuppressFinalize(this);
}
}
}
}
38 changes: 38 additions & 0 deletions src/Arcus.Testing.Storage.Table/Arcus.Testing.Storage.Table.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0;net6.0</TargetFrameworks>
<RootNamespace>Arcus.Testing</RootNamespace>
<Authors>Arcus</Authors>
<Company>Arcus</Company>
<Description>Provides capabilities during testing of Azure Table storage interactions</Description>
<Copyright>Copyright (c) Arcus</Copyright>
<PackageProjectUrl>https://github.com/arcus-azure/arcus.testing</PackageProjectUrl>
<RepositoryUrl>https://github.com/arcus-azure/arcus.testing</RepositoryUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageIcon>icon.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryType>Git</RepositoryType>
<PackageTags>Azure;Testing;Storage</PackageTags>
<PackageId>Arcus.Testing.Storage.Table</PackageId>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
<None Include="..\..\LICENSE" Pack="true" PackagePath="\" />
<None Include="..\..\docs\static\img\icon.png" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="[1.12.1,2.0.0)" />
<PackageReference Include="Azure.Data.Tables" Version="[12.9.1,13.0.0)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Arcus.Testing.Core\Arcus.Testing.Core.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 8d7d14b

Please sign in to comment.