Skip to content

Commit

Permalink
Added InsertOrReplace functionality (#8)
Browse files Browse the repository at this point in the history
* move to .net standard

* Update README.md

* Added InsertOrReplace functionality

As part of a feature request for a library downstream:
giometrix/TableStorage.Abstractions.POCO#2
  • Loading branch information
giometrix authored and Tazmainiandevil committed Apr 28, 2019
1 parent ffcc950 commit 480432e
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/TableStorage.Abstractions/Store/ITableStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public interface ITableStore<T>
/// <param name="records">The records to insert</param>
void Insert(IEnumerable<T> records);

/// <summary>
/// Inserts or replaces the record
/// </summary>
/// <param name="record"></param>
void InsertOrReplace(T record);

/// <summary>
/// Update an record
/// </summary>
Expand Down Expand Up @@ -230,6 +236,12 @@ PagedResult<T> GetByRowKeyPaged(string rowKey, int pageSize = 100,
/// </summary>
/// <param name="records">The records to insert</param>
Task InsertAsync(IEnumerable<T> records);

/// <summary>
/// Inserts or replaces the record
/// </summary>
/// <param name="record"></param>
Task InsertOrReplaceAsync(T record);

/// <summary>
/// Update an record
Expand Down
36 changes: 36 additions & 0 deletions src/TableStorage.Abstractions/Store/TableStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,24 @@ public void Insert(IEnumerable<T> records)
}
}
}
/// <summary>
/// Inserts or replaces the record
/// </summary>
/// <param name="record"></param>
public void InsertOrReplace(T record)
{
if (record == null)
{
throw new ArgumentNullException(nameof(record));
}

var operation = TableOperation.InsertOrReplace(record);
#if NETSTANDARD2_0
_cloudTable.ExecuteAsync(operation).GetAwaiter().GetResult();
#else
_cloudTable.Execute(operation);
#endif
}

/// <summary>
/// Update an record
Expand Down Expand Up @@ -715,6 +733,23 @@ public async Task InsertAsync(IEnumerable<T> records)
}
}

/// <summary>
/// Inserts or replaces the record
/// </summary>
/// <param name="record"></param>
/// <returns></returns>
public async Task InsertOrReplaceAsync(T record)
{
if (record == null)
{
throw new ArgumentNullException(nameof(record));
}

var operation = TableOperation.InsertOrReplace(record);

await _cloudTable.ExecuteAsync(operation).ConfigureAwait(false);
}

/// <summary>
/// Update an record
/// </summary>
Expand Down Expand Up @@ -1155,6 +1190,7 @@ private TableQuerySegment<T> ExecuteQuerySegment(TableQuery<T> query, TableConti
return items;
}


#endregion Helpers
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,37 @@ public void insert_async_with_null_for_multiple_records_throws_exception()
act.Should().Throw<ArgumentNullException>().WithMessage("Value cannot be null.\r\nParameter name: records");
}

[Fact]
public async Task insert_or_replace_async_record_into_the_table_when_record_does_not_exist_and_record_count_should_be_greater_than_zero()
{
// Arrange
var testEntity = new TestTableEntity("John", "Smith") { Age = 21, Email = "john.smith@something.com" };

// Act
await _tableStorage.InsertOrReplaceAsync(testEntity);

var result = (await _tableStorage.GetByRowKeyAsync("John")).ToList();

// Assert
result.Count.Should().BeGreaterThan(0);
}

[Fact]
public async Task insert_or_replace_async_record_into_the_table_when_record_does_exist_and_record_should_have_updated_fields()
{
// Arrange
var testEntity = new TestTableEntity("John", "Smith") { Age = 21, Email = "john.smith@something.com" };
await _tableStorage.InsertAsync(testEntity);
// Act
testEntity = new TestTableEntity("John", "Smith") { Age = 45, Email = "john.smith@something.com" };
await _tableStorage.InsertOrReplaceAsync(testEntity);

var result = (await _tableStorage.GetByRowKeyAsync("John")).ToList();

// Assert
result[0].Age.Should().Be(45);
}

[Fact]
public async Task insert_async_multiple_records_into_the_table_and_record_count_should_be_greater_than_zero()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,37 @@ public void insert_record_into_the_table_and_record_count_should_be_greater_than
result.Count.Should().BeGreaterThan(0);
}

[Fact]
public void insert_or_replace_record_into_the_table_when_record_does_not_exist_and_record_count_should_be_greater_than_zero()
{
// Arrange
var testEntity = new TestTableEntity("John", "Smith") { Age = 21, Email = "john.smith@something.com" };

// Act
_tableStorage.InsertOrReplace(testEntity);

var result = _tableStorage.GetByRowKey("John").ToList();

// Assert
result.Count.Should().BeGreaterThan(0);
}

[Fact]
public void insert_or_replace_record_into_the_table_when_record_does_exist_and_record_should_have_updated_fields()
{
// Arrange
var testEntity = new TestTableEntity("John", "Smith") { Age = 21, Email = "john.smith@something.com" };
_tableStorage.Insert(testEntity);
// Act
testEntity = new TestTableEntity("John", "Smith") { Age = 45, Email = "john.smith@something.com" };
_tableStorage.InsertOrReplace(testEntity);

var result = _tableStorage.GetByRowKey("John").ToList();

// Assert
result[0].Age.Should().Be(45);
}

[Fact]
public void insert_with_null_for_multiple_records_throws_exception()
{
Expand Down

0 comments on commit 480432e

Please sign in to comment.