Skip to content

Commit

Permalink
#18 Fixed DynamicEntity with null properties
Browse files Browse the repository at this point in the history
(closes #18)
  • Loading branch information
Andrei15193 committed Dec 25, 2020
1 parent a17652e commit 457ab83
Show file tree
Hide file tree
Showing 16 changed files with 445 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Table;
using Xunit;
Expand Down Expand Up @@ -156,6 +157,65 @@ public async Task TableOperation_InsertOrMergeOperation_MergesEntities()
Assert.Equal(default(DateTimeOffset), resultEntity.Timestamp);
}

[Fact]
public async Task ExecuteAsync_WhenDynamicEntityHasNullProperties_TheyAreIgnored()
{
await CloudTable.CreateAsync();

await CloudTable.ExecuteAsync(TableOperation.InsertOrMerge(new DynamicTableEntity
{
PartitionKey = "partition-key",
RowKey = "row-key",
Properties = new Dictionary<string, EntityProperty>(StringComparer.Ordinal)
{
{ nameof(TestEntity.Int32Prop), EntityProperty.CreateEntityPropertyFromObject(null) }
}
}));

var entities = await GetAllEntitiesAsync();
var entity = Assert.Single(entities);
var actualProps = entity.WriteEntity(null);
Assert.False(actualProps.ContainsKey(nameof(TestEntity.PartitionKey)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.RowKey)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.Timestamp)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.ETag)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.Int32Prop)));
}

[Fact]
public async Task ExecuteAsync_WhenDynamicEntityHasNullProperties_TheyAreIgnoredWhenEntityAlreadyExists()
{
await CloudTable.CreateAsync();
await CloudTable.ExecuteAsync(TableOperation.Insert(new DynamicTableEntity
{
PartitionKey = "partition-key",
RowKey = "row-key",
Properties = new Dictionary<string, EntityProperty>(StringComparer.Ordinal)
{
{ nameof(TestEntity.Int32Prop), EntityProperty.GeneratePropertyForInt(1) }
}
}));

await CloudTable.ExecuteAsync(TableOperation.InsertOrMerge(new DynamicTableEntity
{
PartitionKey = "partition-key",
RowKey = "row-key",
Properties = new Dictionary<string, EntityProperty>(StringComparer.Ordinal)
{
{ nameof(TestEntity.Int32Prop), EntityProperty.CreateEntityPropertyFromObject(null) }
}
}));

var entities = await GetAllEntitiesAsync();
var entity = Assert.Single(entities);
var actualProps = entity.WriteEntity(null);
Assert.False(actualProps.ContainsKey(nameof(TestEntity.PartitionKey)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.RowKey)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.Timestamp)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.ETag)));
Assert.Equal(EntityProperty.GeneratePropertyForInt(1), actualProps[nameof(TestEntity.Int32Prop)]);
}

[Fact]
public async Task ExecuteAsync_WhenPartitionKeyIsNull_ThrowsException()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Table;
using Xunit;
Expand Down Expand Up @@ -156,6 +157,65 @@ public async Task ExecuteAsync_InserOperationWhenEntityHasOtherProperties_Insert
Assert.False(actualProps.ContainsKey(nameof(TestEntity.DecimalProp)));
}

[Fact]
public async Task ExecuteAsync_WhenDynamicEntityHasNullProperties_TheyAreIgnored()
{
await CloudTable.CreateAsync();

await CloudTable.ExecuteAsync(TableOperation.InsertOrReplace(new DynamicTableEntity
{
PartitionKey = "partition-key",
RowKey = "row-key",
Properties = new Dictionary<string, EntityProperty>(StringComparer.Ordinal)
{
{ nameof(TestEntity.Int32Prop), EntityProperty.CreateEntityPropertyFromObject(null) }
}
}));

var entities = await GetAllEntitiesAsync();
var entity = Assert.Single(entities);
var actualProps = entity.WriteEntity(null);
Assert.False(actualProps.ContainsKey(nameof(TestEntity.PartitionKey)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.RowKey)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.Timestamp)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.ETag)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.Int32Prop)));
}

[Fact]
public async Task ExecuteAsync_WhenDynamicEntityHasNullProperties_TheyAreRemovedWhenEntityAlreadyExists()
{
await CloudTable.CreateAsync();
await CloudTable.ExecuteAsync(TableOperation.Insert(new DynamicTableEntity
{
PartitionKey = "partition-key",
RowKey = "row-key",
Properties = new Dictionary<string, EntityProperty>(StringComparer.Ordinal)
{
{ nameof(TestEntity.Int32Prop), EntityProperty.GeneratePropertyForInt(1) }
}
}));

await CloudTable.ExecuteAsync(TableOperation.InsertOrReplace(new DynamicTableEntity
{
PartitionKey = "partition-key",
RowKey = "row-key",
Properties = new Dictionary<string, EntityProperty>(StringComparer.Ordinal)
{
{ nameof(TestEntity.Int32Prop), EntityProperty.CreateEntityPropertyFromObject(null) }
}
}));

var entities = await GetAllEntitiesAsync();
var entity = Assert.Single(entities);
var actualProps = entity.WriteEntity(null);
Assert.False(actualProps.ContainsKey(nameof(TestEntity.PartitionKey)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.RowKey)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.Timestamp)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.ETag)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.Int32Prop)));
}

[Fact]
public async Task ExecuteAsync_InsertOrReplaceOperationWhenPartitionKeyIsNull_ThrowsException()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Table;
Expand Down Expand Up @@ -163,6 +164,31 @@ public async Task ExecuteAsync_InserOperationWhenEntityHasOtherProperties_Insert
Assert.False(actualProps.ContainsKey(nameof(TestEntity.DecimalProp)));
}

[Fact]
public async Task ExecuteAsync_WhenDynamicEntityHasNullProperties_TheyAreIgnored()
{
await CloudTable.CreateAsync();

await CloudTable.ExecuteAsync(TableOperation.Insert(new DynamicTableEntity
{
PartitionKey = "partition-key",
RowKey = "row-key",
Properties = new Dictionary<string, EntityProperty>(StringComparer.Ordinal)
{
{ nameof(TestEntity.Int32Prop), EntityProperty.CreateEntityPropertyFromObject(null) }
}
}));

var entities = await GetAllEntitiesAsync();
var entity = Assert.Single(entities);
var actualProps = entity.WriteEntity(null);
Assert.False(actualProps.ContainsKey(nameof(TestEntity.PartitionKey)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.RowKey)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.Timestamp)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.ETag)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.Int32Prop)));
}

[Fact]
public async Task ExecuteAsync_WhenPartitionKeyIsNull_ThrowsException()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Table;
using Xunit;
Expand Down Expand Up @@ -144,6 +145,41 @@ public async Task ExecuteAsync_WhenETagsMatch_MergesEntity()
Assert.Equal(default(DateTimeOffset), resultEntity.Timestamp);
}

[Fact]
public async Task ExecuteAsync_WhenDynamicEntityHasNullProperties_TheyAreIgnored()
{
await CloudTable.CreateAsync();
var tableResult = await CloudTable.ExecuteAsync(TableOperation.Insert(new DynamicTableEntity
{
PartitionKey = "partition-key",
RowKey = "row-key",
Properties = new Dictionary<string, EntityProperty>(StringComparer.Ordinal)
{
{ nameof(TestEntity.Int32Prop), EntityProperty.GeneratePropertyForInt(1) }
}
}));

await CloudTable.ExecuteAsync(TableOperation.Merge(new DynamicTableEntity
{
PartitionKey = "partition-key",
RowKey = "row-key",
ETag = tableResult.Etag,
Properties = new Dictionary<string, EntityProperty>(StringComparer.Ordinal)
{
{ nameof(TestEntity.Int32Prop), EntityProperty.CreateEntityPropertyFromObject(null) }
}
}));

var entities = await GetAllEntitiesAsync();
var entity = Assert.Single(entities);
var actualProps = entity.WriteEntity(null);
Assert.False(actualProps.ContainsKey(nameof(TestEntity.PartitionKey)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.RowKey)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.Timestamp)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.ETag)));
Assert.Equal(EntityProperty.GeneratePropertyForInt(1), actualProps[nameof(TestEntity.Int32Prop)]);
}

[Fact]
public async Task ExecuteAsync_WhenEntityDoesNotExist_ThrowsException()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Table;
using Xunit;
Expand Down Expand Up @@ -138,6 +139,41 @@ public async Task ExecuteAsync_WhenETagsMatch_ReplacesEntity()
Assert.Equal(default(DateTimeOffset), resultEntity.Timestamp);
}

[Fact]
public async Task ExecuteAsync_WhenDynamicEntityHasNullProperties_TheyAreRemoved()
{
await CloudTable.CreateAsync();
var tableResult = await CloudTable.ExecuteAsync(TableOperation.Insert(new DynamicTableEntity
{
PartitionKey = "partition-key",
RowKey = "row-key",
Properties = new Dictionary<string, EntityProperty>(StringComparer.Ordinal)
{
{ nameof(TestEntity.Int32Prop), EntityProperty.GeneratePropertyForInt(1) }
}
}));

await CloudTable.ExecuteAsync(TableOperation.Replace(new DynamicTableEntity
{
PartitionKey = "partition-key",
RowKey = "row-key",
ETag = tableResult.Etag,
Properties = new Dictionary<string, EntityProperty>(StringComparer.Ordinal)
{
{ nameof(TestEntity.Int32Prop), EntityProperty.CreateEntityPropertyFromObject(null) }
}
}));

var entities = await GetAllEntitiesAsync();
var entity = Assert.Single(entities);
var actualProps = entity.WriteEntity(null);
Assert.False(actualProps.ContainsKey(nameof(TestEntity.PartitionKey)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.RowKey)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.Timestamp)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.ETag)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.Int32Prop)));
}

[Fact]
public async Task ExecuteAsync_WhenEntityDoesNotExist_ThrowsException()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Azure.Cosmos.Table;
using Xunit;

Expand Down Expand Up @@ -155,6 +156,65 @@ public void TableOperation_InsertOrMergeOperation_MergesEntities()
Assert.Equal(default(DateTimeOffset), resultEntity.Timestamp);
}

[Fact]
public void Execute_WhenDynamicEntityHasNullProperties_TheyAreIgnored()
{
CloudTable.Create();

CloudTable.Execute(TableOperation.InsertOrMerge(new DynamicTableEntity
{
PartitionKey = "partition-key",
RowKey = "row-key",
Properties = new Dictionary<string, EntityProperty>(StringComparer.Ordinal)
{
{ nameof(TestEntity.Int32Prop), EntityProperty.CreateEntityPropertyFromObject(null) }
}
}));

var entities = GetAllEntities();
var entity = Assert.Single(entities);
var actualProps = entity.WriteEntity(null);
Assert.False(actualProps.ContainsKey(nameof(TestEntity.PartitionKey)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.RowKey)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.Timestamp)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.ETag)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.Int32Prop)));
}

[Fact]
public void Execute_WhenDynamicEntityHasNullProperties_TheyAreIgnoredWhenEntityAlreadyExists()
{
CloudTable.Create();
CloudTable.Execute(TableOperation.Insert(new DynamicTableEntity
{
PartitionKey = "partition-key",
RowKey = "row-key",
Properties = new Dictionary<string, EntityProperty>(StringComparer.Ordinal)
{
{ nameof(TestEntity.Int32Prop), EntityProperty.GeneratePropertyForInt(1) }
}
}));

CloudTable.Execute(TableOperation.InsertOrMerge(new DynamicTableEntity
{
PartitionKey = "partition-key",
RowKey = "row-key",
Properties = new Dictionary<string, EntityProperty>(StringComparer.Ordinal)
{
{ nameof(TestEntity.Int32Prop), EntityProperty.CreateEntityPropertyFromObject(null) }
}
}));

var entities = GetAllEntities();
var entity = Assert.Single(entities);
var actualProps = entity.WriteEntity(null);
Assert.False(actualProps.ContainsKey(nameof(TestEntity.PartitionKey)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.RowKey)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.Timestamp)));
Assert.False(actualProps.ContainsKey(nameof(TestEntity.ETag)));
Assert.Equal(EntityProperty.GeneratePropertyForInt(1), actualProps[nameof(TestEntity.Int32Prop)]);
}

[Fact]
public void Execute_WhenPartitionKeyIsNull_ThrowsException()
{
Expand Down
Loading

0 comments on commit 457ab83

Please sign in to comment.