Atc.Cosmos.Functions.Testing
2.1.0
dotnet add package Atc.Cosmos.Functions.Testing --version 2.1.0
NuGet\Install-Package Atc.Cosmos.Functions.Testing -Version 2.1.0
<PackageReference Include="Atc.Cosmos.Functions.Testing" Version="2.1.0" />
<PackageVersion Include="Atc.Cosmos.Functions.Testing" Version="2.1.0" />
<PackageReference Include="Atc.Cosmos.Functions.Testing" />
paket add Atc.Cosmos.Functions.Testing --version 2.1.0
#r "nuget: Atc.Cosmos.Functions.Testing, 2.1.0"
#:package Atc.Cosmos.Functions.Testing@2.1.0
#addin nuget:?package=Atc.Cosmos.Functions.Testing&version=2.1.0
#tool nuget:?package=Atc.Cosmos.Functions.Testing&version=2.1.0
Atc.Cosmos
A .NET library for configuring containers in Azure Cosmos DB and providing an easy way to read and write document resources using System.Text.Json.
Table of Contents
Features
- Container configuration — declarative setup of Cosmos DB containers with automatic provisioning
- Read/Write abstractions —
ICosmosReader<T>andICosmosWriter<T>for simple CRUD operations - Bulk operations —
ICosmosBulkReader<T>andICosmosBulkWriter<T>for high-throughput batch operations - Change feed processing — built-in change feed processor support with partitioned data handling
- Paged queries — efficient pagination with continuation token support
- LINQ queries — query builder support using
IQueryable<T> - Cross-partition queries — read and query across partition boundaries
- Optimistic concurrency — ETag-based conflict detection on write operations
- Update pattern — read-modify-write with automatic retry on conflicts
- Delete by partition key — bulk delete all documents in a partition
- Multi-database support — connect to multiple Cosmos DB databases from a single application
- Unit testing fakes —
FakeCosmos<T>for testing without a real Cosmos DB instance - Azure Functions trigger verification —
Atc.Cosmos.Functions.Testingchecks that Cosmos DB triggers can read the documents the library writes - System.Text.Json — serialization using
System.Text.Jsonwith configurableJsonSerializerOptions
Installation
dotnet add package Atc.Cosmos
Getting Started
Once the library is added to your project, you will have access to the following interfaces for reading and writing Cosmos document resources:
| Interface | Description |
|---|---|
ICosmosReader<T> |
Read Cosmos resources |
ICosmosWriter<T> |
Write Cosmos resources |
ICosmosBulkReader<T> |
Bulk read operations |
ICosmosBulkWriter<T> |
Bulk write operations |
A document resource is represented by a class deriving from the CosmosResource base class, or by implementing the ICosmosResource interface directly.
To configure where each resource will be stored in Cosmos, the ConfigureCosmos(builder) extension method is used on the IServiceCollection when setting up dependency injection.
Configure Cosmos Connection
The library uses the CosmosOptions class for configuring the connection to Cosmos:
| Name | Description |
|---|---|
AccountEndpoint |
URL to the Cosmos Account |
AccountKey |
Key for the Cosmos Account |
DatabaseName |
Name of the Cosmos database (will be provisioned by the library) |
DatabaseThroughput |
Throughput provisioned for the database in Request Units per second |
SerializerOptions |
JsonSerializerOptions used for System.Text.Json.JsonSerializer |
Credential |
TokenCredential for Azure AD authentication. When set, AccountKey is ignored |
There are 3 ways to provide the CosmosOptions to the library:
As an argument to the
ConfigureCosmos()extension method.As a
Func<IServiceProvider, CosmosOptions>factory method argument on theConfigureCosmos()extension method.As an
IOptions<CosmosOptions>instance configured using the Options framework and registered in dependency injection.This could be done by reading the
CosmosOptionsfrom configuration:services.Configure<CosmosOptions>( Configuration.GetSection(configurationSectionName));Or by using a factory class implementing the
IConfigureOptions<CosmosOptions>interface:services.ConfigureOptions<ConfigureCosmosOptions>();The latter is the recommended approach.
Configure Containers
For each Cosmos resource you want to access using the readers and writers you will need to:
Create a class representing the Cosmos document resource.
The class should implement the abstract
CosmosResourcebase class, which requiresGetDocumentId()andGetPartitionKey()methods to be implemented.The class will be serialized using
System.Text.Json.JsonSerializer, soJsonPropertyNameAttributecan be used to control property names in the JSON document.Configure the container used for the Cosmos document resource.
This is done on the
ICosmosBuildermade available using theConfigureCosmos()extension onIServiceCollection:builder.Services.ConfigureCosmos(b => b.AddContainer<MyResource>(containerName));Connect to multiple databases by scoping your container to a new
CosmosOptionsinstance:builder.Services.ConfigureCosmos( b => b.AddContainer<MyResource>(containerName) .ForDatabase(secondDbOptions) .AddContainer<MySecondResource>(containerName));The first call to
AddContaineris scoped to the default options. The call toForDatabasereturns a new builder scoped to the provided options.
Initialize Containers
The library supports adding initializers for each container that can create the container and configure it with the correct keys and indexes.
Create an initializer by implementing the
ICosmosContainerInitializerinterface.Usually the implementation will call
CreateContainerIfNotExistsAsync()on the providedDatabaseobject with the desiredContainerProperties.Register the initializer on the
ICosmosBuilder:builder.Services.ConfigureCosmos(b => b.AddContainer<MyInitializer>(containerName));Run the initialization using a hosted service:
builder.Services.ConfigureCosmos(b => b.UseHostedService());
Using the Readers and Writers
Once the setup is in place, the readers and writers are registered with the Microsoft.Extensions.DependencyInjection container and can be obtained via constructor injection.
The bulk reader and writer optimize performance when executing many operations towards Cosmos. They work by creating all the tasks and then using Task.WhenAll() to await them, grouping operations by partition key and sending them in batches of 100.
When not operating with bulks, the normal readers are faster as there is no delay waiting for more work.
Change Feeds
The library supports adding change feed processors for a container.
Create a processor by implementing the
IChangeFeedProcessorinterface.Register the change feed processor during initialization:
builder.Services.ConfigureCosmos(b => b .AddContainer<MyInitializer, MyResource>(containerName) .WithChangeFeedProcessor<MyChangeFeedProcessor>());Or using the
ICosmosContainerBuilder<T>:builder.Services.ConfigureCosmos(b => b .AddContainer<MyInitializer>( containerName, c => c .AddResource<MyResource>() .WithChangeFeedProcessor<MyChangeFeedProcessor>()));
Note: The change feed processor relies on a HostedService, which means this feature is only available in hosted applications.
Delete by Partition Key
The ICosmosWriter<T>.DeletePartitionAsync() method allows you to delete all documents within a partition by partition key. This uses the Cosmos DB delete all items by partition key feature.
Priority Based Execution
The library exposes low priority readers and writers:
| Interface | Description |
|---|---|
ILowPriorityCosmosReader<T> |
Read Cosmos resources with low priority |
ILowPriorityCosmosWriter<T> |
Write Cosmos resources with low priority |
ILowPriorityCosmosBulkReader<T> |
Bulk read with low priority |
ILowPriorityCosmosBulkWriter<T> |
Bulk write with low priority |
The "Priority Based Execution" feature needs to be enabled on the CosmosDB account, either in the Azure Portal under Settings > Features, or via Azure CLI:
az cosmosdb update --resource-group $ResourceGroup --name $AccountName --enable-priority-based-execution true
See Microsoft Learn for more details.
Unit Testing
The reader and writer interfaces can easily be mocked, but in some cases it is useful to have a fake implementation that mimics the behavior of read and write operations. The Atc.Cosmos.Testing namespace provides:
| Class | Description |
|---|---|
FakeCosmosReader<T> |
Fake ICosmosReader<T> / ICosmosBulkReader<T> |
FakeCosmosWriter<T> |
Fake ICosmosWriter<T> / ICosmosBulkWriter<T> |
FakeCosmos<T> |
Combined fake reader and writer with shared state |
Using Atc.Test, a test using the fakes could look like this:
[Theory, AutoNSubstituteData]
public async Task Should_Update_Cosmos_With_NewData(
[Frozen(Matching.ImplementedInterfaces)]
FakeCosmos<MyCosmosResource> cosmos,
MyCosmosService sut,
MyCosmosResource resource,
string newData)
{
cosmos.Documents.Add(resource);
await sut.UpdateAsync(resource.Id, newData);
resource
.Data
.Should()
.Be(newData);
}
Testing Azure Functions Cosmos DB Triggers
Atc.Cosmos writes documents with CosmosOptions.SerializerOptions, but an Azure Functions [CosmosDBTrigger] reads the change feed with the Functions worker's serializer (WorkerOptions.Serializer), unless the parameter has an [InputConverter]. When the two differ, the trigger fails before the function body runs. For example, an enum stored as text cannot be read by the worker's default serializer:
FunctionInputConverterException: ... The JSON value could not be converted to MyEnum. Path: $[0].type
The Atc.Cosmos.Functions.Testing package finds every Cosmos DB trigger in a Functions assembly and verifies it in a unit test:
dotnet add package Atc.Cosmos.Functions.Testing
| Class | Description |
|---|---|
CosmosTriggerDiscovery |
Finds the functions with a [CosmosDBTrigger] parameter of type IReadOnlyList<T>, IEnumerable<T>, T[] or another collection of T |
CosmosTriggerBindingVerifier |
Writes a sample document like Atc.Cosmos does, reads it like the Functions worker does, and compares the two |
The verifier builds its services on top of the Functions worker defaults (AddFunctionsWorkerDefaults), so a trigger without configuration fails the same way it does at runtime. Register the application's own services in configureServices: the CosmosOptions, the WorkerOptions and the dependencies of any input converters.
public sealed class CosmosTriggerBindingTests
{
private static readonly Assembly FunctionsAssembly = typeof(MyChangeFeedFunction).Assembly;
public static TheoryData<string> FunctionNames
=> new(CosmosTriggerDiscovery.Find(FunctionsAssembly).Select(x => x.FunctionName));
[Fact]
public void Find_Should_Discover_Cosmos_Triggers()
=> CosmosTriggerDiscovery
.Find(FunctionsAssembly)
.Should()
.NotBeEmpty();
[Theory]
[MemberData(nameof(FunctionNames))]
public async Task Cosmos_Trigger_Should_Read_Documents_Written_By_Atc_Cosmos(string functionName)
{
// Arrange
var fixture = new Fixture();
var sut = new CosmosTriggerBindingVerifier(
services => services
.ConfigureMyCosmos()
.ConfigureMyWorkerSerializer(),
type => new SpecimenContext(fixture).Resolve(type));
// Act
var actual = await sut.VerifyAsync(FunctionsAssembly, functionName, TestContext.Current.CancellationToken);
// Assert
actual.IsSuccess
.Should()
.BeTrue(actual.Message);
}
}
When the verification fails, Message names the function, the document type and the cause, and WrittenJson holds the change feed batch as Atc.Cosmos writes it. To fix a failing trigger, use the Cosmos serializer options for the worker:
builder.Services.Configure<WorkerOptions>(
options => options.Serializer = new JsonObjectSerializer(jsonSerializerOptions));
or add an [InputConverter] that deserializes the batch with the same options.
Note: There is no function invocation during verification, so the
ConverterContextpassed to an input converter has noFunctionContext. Converters that only readSourceandTargetTypeare supported.
Sample
See the sample API for an example of how to configure the library with a minimal API, including container initialization, reading, and writing resources.
Requirements
- .NET 8 SDK (or later)
How to contribute
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Atc.Cosmos (>= 2.1.0)
- Microsoft.Azure.Functions.Worker (>= 2.52.0)
- Microsoft.Azure.Functions.Worker.Extensions.CosmosDB (>= 4.16.1)
-
net8.0
- Atc.Cosmos (>= 2.1.0)
- Microsoft.Azure.Functions.Worker (>= 2.52.0)
- Microsoft.Azure.Functions.Worker.Extensions.CosmosDB (>= 4.16.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.1.0 | 74 | 11/20/2026 |