Skip to content

Commit

Permalink
Add tests on hierarchical partition key containers
Browse files Browse the repository at this point in the history
  • Loading branch information
miiitch committed Dec 2, 2023
1 parent 629b613 commit 0bc8a08
Show file tree
Hide file tree
Showing 8 changed files with 1,098 additions and 658 deletions.
23 changes: 13 additions & 10 deletions src/TypedItem/TypedItem.Lib/CosmosDbExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public static ItemRequestOptions WithETag<T>(this ItemRequestOptions options, T

public static Task<ItemResponse<T>> UpsertTypedItemAsync<T>(this Container container,
T item,
PartitionKey? Partition = null,
PartitionKey? partitionKey = null,
ItemRequestOptions? requestOptions = null,
CancellationToken cancellationToken = default) where T: TypedItemBase, new()
{
TypedItemHelper<T>.PrepareItem(item, generateId: true);
return container.UpsertItemAsync(item, Partition, requestOptions, cancellationToken);
return container.UpsertItemAsync(item, partitionKey, requestOptions, cancellationToken);
}

public static TransactionalBatch UpsertTypedItem<T>(this TransactionalBatch batch,
Expand All @@ -44,12 +44,12 @@ public static TransactionalBatch UpsertTypedItem<T>(this TransactionalBatch batc
public static Task<ItemResponse<T>> ReplaceTypedItemAsync<T>(this Container container,
T item,
string id,
PartitionKey? Partition = null,
PartitionKey? partitionKey = null,
ItemRequestOptions? requestOptions = null,
CancellationToken cancellationToken = default) where T: TypedItemBase, new()
{
TypedItemHelper<T>.PrepareItem(item);
return container.ReplaceItemAsync(item, id, Partition, requestOptions, cancellationToken);
return container.ReplaceItemAsync(item, id, partitionKey, requestOptions, cancellationToken);
}

public static TransactionalBatch ReplaceTypedItem<T>(this TransactionalBatch batch,
Expand All @@ -65,12 +65,12 @@ public static TransactionalBatch ReplaceTypedItem<T>(this TransactionalBatch bat

public static Task<ItemResponse<T>> CreateTypedItemAsync<T>(this Container container,
T item,
PartitionKey? Partition = null,
PartitionKey? partitionKey = null,
ItemRequestOptions? requestOptions = null,
CancellationToken cancellationToken = default) where T: TypedItemBase, new()
{
TypedItemHelper<T>.PrepareItem(item,generateId:true);
return container.CreateItemAsync(item, Partition, requestOptions, cancellationToken);
return container.CreateItemAsync(item, partitionKey, requestOptions, cancellationToken);
}

public static TransactionalBatch CreateTypedItem<T>(this TransactionalBatch batch,
Expand Down Expand Up @@ -146,12 +146,12 @@ async Task ReadFeedIteratorAsync(bool setContinuationToken)

public static async Task<ItemResponse<T>> ReadTypedItemAsync<T>(this Container container,
string id,
PartitionKey Partition,
PartitionKey partitionKey,
TypedItemRequestOptions? options = null,
CancellationToken cancellationToken = default) where T : TypedItemBase, new()
{
var readDeleted = options is { ReadDeleted: true };
var result = await container.ReadItemAsync<T>(id, Partition, options, cancellationToken);
var result = await container.ReadItemAsync<T>(id, partitionKey, options, cancellationToken);
if (result is null ||
!readDeleted && result.Resource.Deleted ||
!TypedItemHelper<T>.HasSameType(result.Resource))
Expand All @@ -166,7 +166,7 @@ public static async Task<ItemResponse<T>> ReadTypedItemAsync<T>(this Container c

public static Task<ItemResponse<T>> SoftDeleteTypedItemAsync<T>(this Container container,
string id,
PartitionKey Partition,
PartitionKey partitionKey,
ItemRequestOptions? requestOptions = null,
CancellationToken cancellationToken = new()) where T : TypedItemBase, new()
{
Expand All @@ -178,7 +178,7 @@ public static Task<ItemResponse<T>> SoftDeleteTypedItemAsync<T>(this Container c
var patchOptions = new PatchItemRequestOptions();
requestOptions?.Fill(patchOptions);
patchOptions.FilterPredicate = "FROM item WHERE NOT item._deleted";
return container.PatchItemAsync<T>(id, Partition, patchOperations, patchOptions, cancellationToken);
return container.PatchItemAsync<T>(id, partitionKey, patchOperations, patchOptions, cancellationToken);
}

public static TransactionalBatch SoftDeleteTypedItem(this TransactionalBatch batch, string id, TransactionalBatchPatchItemRequestOptions? requestOptions = null)
Expand Down Expand Up @@ -236,9 +236,12 @@ private static void Fill(this ItemRequestOptions itemRequestOptions,
patchItemRequestOptions.IfNoneMatchEtag = itemRequestOptions.IfNoneMatchEtag;
patchItemRequestOptions.EnableContentResponseOnWrite = itemRequestOptions.EnableContentResponseOnWrite;
}

public static bool IsNullOrNone(this PartitionKey partitionKey) => partitionKey == PartitionKey.Null || partitionKey == PartitionKey.None;
}






Expand Down
9 changes: 9 additions & 0 deletions src/TypedItem/TypedItem.Lib/ItemBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,14 @@ public abstract class ItemBase

protected static PartitionKey CreatePartitionKey(string partitionItem) => new PartitionKey(partitionItem);

protected static PartitionKey CreatePartitionKey(params string[] partitionKeyParts)
{
var partitionKeyBuilder = new PartitionKeyBuilder();
foreach (var partitionKeyPart in partitionKeyParts)
{
partitionKeyBuilder.Add(partitionKeyPart);
}
return partitionKeyBuilder.Build();
}
}
}
2 changes: 1 addition & 1 deletion src/TypedItem/TypedItem.Lib/TypedItem.Lib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.32.0" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.34.0" />
</ItemGroup>

<ItemGroup>
Expand Down
46 changes: 46 additions & 0 deletions src/TypedItem/TypedItem.Tests/ItemModels/HCPersonItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using Newtonsoft.Json;
using TypedItem.Lib;

namespace TypedItem.Tests.ItemModels
{
[ItemType("person")]
public sealed class HCPersonItem: HierarchicalContainerItem
{

[JsonProperty("firstName")]
public string FirstName { get; set; }

[JsonProperty("lastName")]
public string LastName { get; set; }

[JsonProperty("birthdate")]
public DateTime BirthDate { get; set; }

[JsonProperty("index")]
public int Index { get; set; }
}

[ItemType("event")]
public class HCEventItem: HierarchicalContainerItem
{
[JsonProperty("date")]
public DateTime Date { get; set; }
}

[ItemType("phonecall")]
public sealed class HCPhonecallItem: HCEventItem
{

[JsonProperty("duration")]
public int Duration { get; set; }
}

[ItemType("teamsmeeting")]
public sealed class HCTeamsMeeting: HCEventItem
{

[JsonProperty("peoples")]
public string[] Peoples { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.Azure.Cosmos;
using Newtonsoft.Json;
using TypedItem.Lib;

namespace TypedItem.Tests.ItemModels;

public class HierarchicalContainerItem : TypedItemBase
{
[JsonProperty("part")]
public string Part { get; set; }

[JsonProperty("subPart")]
public string SubPart { get; set; }

public override PartitionKey GetPartitionKey()
{
return CreatePartitionKey(Part, SubPart);
}
}
8 changes: 4 additions & 4 deletions src/TypedItem/TypedItem.Tests/TypedItem.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.32.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.34.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="NETStandard.Library" Version="2.0.3" />
<PackageReference Include="NFluent" Version="2.8.0" />
<PackageReference Include="NFluent" Version="3.0.0.351" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Loading

0 comments on commit 0bc8a08

Please sign in to comment.