Skip to content

Commit

Permalink
Added retrieve operation to batch operation
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei15193 committed Aug 23, 2021
1 parent c0c0d60 commit 1e2fbe3
Show file tree
Hide file tree
Showing 26 changed files with 141 additions and 27 deletions.
101 changes: 100 additions & 1 deletion CloudStub.Tests/Core/StubTableTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Linq;
using CloudStub.Core;
using CloudStub.Core.Operations;
using CloudStub.Core.OperationResults;
using CloudStub.Core.StorageHandlers;
using Xunit;

Expand Down Expand Up @@ -1598,5 +1598,104 @@ public void BatchOperation_WhenDeletingExistingEntity_TheBatchOperationSucceeds(
Assert.NotNull(deleteOperationResult.Entity.ETag);
Assert.NotNull(deleteOperationResult.Entity.Timestamp);
}

[Fact]
public void BatchOperation_WhenRetrievingEntityThatDoesNotExists_TheBatchOperationFails()
{
var stubTable = new StubTable("table-name", new InMemoryTableStorageHandler());
stubTable.Create();

var batchOperationResult = stubTable
.BatchOperation()
.Retrieve("partition-key", "row-key")
.Execute();

Assert.Equal(StubTableBatchOperationResult.Failed, batchOperationResult.OperationResult);
Assert.False(batchOperationResult.IsSuccessful);
Assert.Empty(stubTable.Query(new StubTableQuery(), default).Entities);

var retrieveOperationResult = Assert.IsType<StubTableRetrieveOperationDataResult>(Assert.Single(batchOperationResult.IndividualOperationResults));
Assert.Equal(StubTableOperationType.Retrieve, retrieveOperationResult.OperationType);
Assert.Equal(StubTableRetrieveOperationResult.EntityDoesNotExists, retrieveOperationResult.OperationResult);
Assert.False(retrieveOperationResult.IsSuccessful);
Assert.Null(retrieveOperationResult.Entity);
}

[Fact]
public void BatchOperation_WhenRetrievingExistingEntity_TheBatchOperationSucceeds()
{
var stubTable = new StubTable("table-name", new InMemoryTableStorageHandler());
stubTable.Create();
stubTable.Insert(new StubEntity("partition-key", "row-key")
{
Properties =
{
{ "property1", new StubEntityProperty("property-1") },
{ "property2", new StubEntityProperty("property-2") }
}
});

var batchOperationResult = stubTable
.BatchOperation()
.Retrieve("partition-key", "row-key")
.Execute();

Assert.Equal(StubTableBatchOperationResult.Success, batchOperationResult.OperationResult);
Assert.True(batchOperationResult.IsSuccessful);
var entity = Assert.Single(stubTable.Query(new StubTableQuery(), default).Entities);

var retrieveOperationResult = Assert.IsType<StubTableRetrieveOperationDataResult>(Assert.Single(batchOperationResult.IndividualOperationResults));
Assert.Equal(StubTableOperationType.Retrieve, retrieveOperationResult.OperationType);
Assert.Equal(StubTableRetrieveOperationResult.Success, retrieveOperationResult.OperationResult);
Assert.True(retrieveOperationResult.IsSuccessful);
Assert.NotNull(retrieveOperationResult.Entity);

Assert.Equal(entity.PartitionKey, retrieveOperationResult.Entity.PartitionKey);
Assert.Equal(entity.RowKey, retrieveOperationResult.Entity.RowKey);
Assert.Equal(entity.ETag, retrieveOperationResult.Entity.ETag);
Assert.Equal(entity.Timestamp, retrieveOperationResult.Entity.Timestamp);

Assert.Equal(entity.Properties.Count, retrieveOperationResult.Entity.Properties.Count);
Assert.Equal(entity.Properties["property1"].Value, retrieveOperationResult.Entity.Properties["property1"].Value);
Assert.Equal(entity.Properties["property2"].Value, retrieveOperationResult.Entity.Properties["property2"].Value);
}

[Fact]
public void BatchOperation_WhenRetrievingExistingEntityWithSelectedProperties_TheBatchOperationSucceeds()
{
var stubTable = new StubTable("table-name", new InMemoryTableStorageHandler());
stubTable.Create();
stubTable.Insert(new StubEntity("partition-key", "row-key")
{
Properties =
{
{ "property1", new StubEntityProperty("property-1") },
{ "property2", new StubEntityProperty("property-2") }
}
});

var batchOperationResult = stubTable
.BatchOperation()
.Retrieve("partition-key", "row-key", new[] { "property2", "property3" })
.Execute();

Assert.Equal(StubTableBatchOperationResult.Success, batchOperationResult.OperationResult);
Assert.True(batchOperationResult.IsSuccessful);
var entity = Assert.Single(stubTable.Query(new StubTableQuery(), default).Entities);

var retrieveOperationResult = Assert.IsType<StubTableRetrieveOperationDataResult>(Assert.Single(batchOperationResult.IndividualOperationResults));
Assert.Equal(StubTableOperationType.Retrieve, retrieveOperationResult.OperationType);
Assert.Equal(StubTableRetrieveOperationResult.Success, retrieveOperationResult.OperationResult);
Assert.True(retrieveOperationResult.IsSuccessful);
Assert.NotNull(retrieveOperationResult.Entity);

Assert.Equal(entity.PartitionKey, retrieveOperationResult.Entity.PartitionKey);
Assert.Equal(entity.RowKey, retrieveOperationResult.Entity.RowKey);
Assert.Equal(entity.ETag, retrieveOperationResult.Entity.ETag);
Assert.Equal(entity.Timestamp, retrieveOperationResult.Entity.Timestamp);

Assert.Equal(1, retrieveOperationResult.Entity.Properties.Count);
Assert.Equal("property-2", retrieveOperationResult.Entity.Properties["property2"].Value);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CloudStub.Core.Operations
namespace CloudStub.Core.OperationResults
{
public interface IStubTableOperationDataResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;

namespace CloudStub.Core.Operations
namespace CloudStub.Core.OperationResults
{
public class StubTableBatchOperationDataResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CloudStub.Core.Operations
namespace CloudStub.Core.OperationResults
{
public enum StubTableBatchOperationResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CloudStub.Core
namespace CloudStub.Core.OperationResults
{
public enum StubTableCreateResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CloudStub.Core.Operations
namespace CloudStub.Core.OperationResults
{
public class StubTableDeleteOperationDataResult : IStubTableOperationDataResult<StubTableDeleteOperationResult>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CloudStub.Core.Operations
namespace CloudStub.Core.OperationResults
{
public enum StubTableDeleteOperationResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CloudStub.Core
namespace CloudStub.Core.OperationResults
{
public enum StubTableDeleteResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CloudStub.Core.Operations
namespace CloudStub.Core.OperationResults
{
public class StubTableInsertOperationDataResult : IStubTableOperationDataResult<StubTableInsertOperationResult>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CloudStub.Core.Operations
namespace CloudStub.Core.OperationResults
{
public enum StubTableInsertOperationResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CloudStub.Core.Operations
namespace CloudStub.Core.OperationResults
{
public class StubTableInsertOrMergeOperationDataResult : IStubTableOperationDataResult<StubTableInsertOrMergeOperationResult>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CloudStub.Core.Operations
namespace CloudStub.Core.OperationResults
{
public enum StubTableInsertOrMergeOperationResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CloudStub.Core.Operations
namespace CloudStub.Core.OperationResults
{
public class StubTableInsertOrReplaceOperationDataResult : IStubTableOperationDataResult<StubTableInsertOrReplaceOperationResult>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CloudStub.Core.Operations
namespace CloudStub.Core.OperationResults
{
public enum StubTableInsertOrReplaceOperationResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CloudStub.Core.Operations
namespace CloudStub.Core.OperationResults
{
public class StubTableMergeOperationDataResult : IStubTableOperationDataResult<StubTableMergeOperationResult>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CloudStub.Core.Operations
namespace CloudStub.Core.OperationResults
{
public enum StubTableMergeOperationResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CloudStub.Core.Operations
namespace CloudStub.Core.OperationResults
{
public enum StubTableOperationType
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;

namespace CloudStub.Core.Operations
namespace CloudStub.Core.OperationResults
{
public class StubTableQueryDataResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CloudStub.Core.Operations
namespace CloudStub.Core.OperationResults
{
public enum StubTableQueryResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CloudStub.Core.Operations
namespace CloudStub.Core.OperationResults
{
public class StubTableReplaceOperationDataResult : IStubTableOperationDataResult<StubTableReplaceOperationResult>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CloudStub.Core.Operations
namespace CloudStub.Core.OperationResults
{
public enum StubTableReplaceOperationResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CloudStub.Core.Operations
namespace CloudStub.Core.OperationResults
{
public class StubTableRetrieveOperationDataResult : IStubTableOperationDataResult<StubTableRetrieveOperationResult>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CloudStub.Core.Operations
namespace CloudStub.Core.OperationResults
{
public enum StubTableRetrieveOperationResult
{
Expand Down
2 changes: 1 addition & 1 deletion CloudStub/Core/StubTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using CloudStub.Core.Operations;
using CloudStub.Core.OperationResults;
using CloudStub.Core.StorageHandlers;

namespace CloudStub.Core
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;
using System.Collections.Generic;
using CloudStub.Core.OperationResults;

namespace CloudStub.Core.Operations
namespace CloudStub.Core
{
public class StubTableBatchOperation
{
Expand Down Expand Up @@ -78,6 +79,20 @@ public StubTableBatchOperation Delete(StubEntity entity)
return this;
}

public StubTableBatchOperation Retrieve(string partitionKey, string rowKey)
=> Retrieve(partitionKey, rowKey, default);

public StubTableBatchOperation Retrieve(string partitionKey, string rowKey, IEnumerable<string> selectedProperties)
{
if (_partitionKey is null)
_partitionKey = partitionKey;
else if (!string.Equals(_partitionKey, partitionKey, StringComparison.OrdinalIgnoreCase))
throw new ArgumentException("Batch operations can be performed only on the same partition.", nameof(partitionKey));

_operationCallbacks.Add(partitionCluster => StubTableOperation.Retrieve(partitionKey, rowKey, selectedProperties, partitionCluster));
return this;
}

public StubTableBatchOperationDataResult Execute()
=> _executeCallback(_partitionKey, _operationCallbacks);
}
Expand Down
4 changes: 2 additions & 2 deletions CloudStub/Core/StubTableOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using CloudStub.Core.Operations;
using CloudStub.Core.OperationResults;

namespace CloudStub.Core
{
Expand Down Expand Up @@ -142,7 +142,7 @@ internal static StubTableRetrieveOperationDataResult Retrieve(string partitionKe
if (selectedProperties is null)
foreach (var property in entity.Properties)
retrieveEntity.Properties.Add(property);
else if (selectedProperties.Any())
else
foreach (var propertyName in selectedProperties)
if (entity.Properties.TryGetValue(propertyName, out var propertyValue))
retrieveEntity.Properties.Add(propertyName, propertyValue);
Expand Down

0 comments on commit 1e2fbe3

Please sign in to comment.