Skip to content

Commit

Permalink
Added generic attribute mapper and tested partial update model
Browse files Browse the repository at this point in the history
  • Loading branch information
dei79 committed Sep 11, 2022
1 parent 3eef789 commit 9903320
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public interface IStorageContext : IDisposable

void AddAttributeMapper(Type type);

void AddAttributeMapper<T>(String optionalTablenameOverride = null) where T: class;

void AddAttributeMapper(Type type, String optionalTablenameOverride);

void AddEntityMapper(Type entityType, String partitionKeyFormat, String rowKeyFormat, String tableName);
Expand Down
67 changes: 67 additions & 0 deletions CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS22PartialUpates.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using CoreHelpers.WindowsAzure.Storage.Table.Backup;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Contracts;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Extensions;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Models;

namespace CoreHelpers.WindowsAzure.Storage.Table.Tests
{
public class ITS22PartialUpates
{
private readonly IStorageContext _rootContext;

public ITS22PartialUpates(IStorageContext context)
{
_rootContext = context;
}

[Fact]
public async Task CreateAndVerifyBackup()
{
using (var scp = _rootContext.CreateChildContext())
{
// set the tablename context
scp.SetTableContext();

// configure the entity mapper
scp.AddAttributeMapper<PartialUpdateModel>();

// verify we don't have any value
Assert.Empty(await scp.EnableAutoCreateTable().Query<PartialUpdateModel>().InAllPartitions().Now());


// insert the value with just on optional integer
await scp.MergeOrInsertAsync<PartialUpdateModel>(new PartialUpdateModel() { CustomerId = "C01", MeterId = "M01", Value01 = 1 });

var result = (await scp.EnableAutoCreateTable().Query<PartialUpdateModel>().Now()).First();
Assert.True(result.Value01.HasValue);
Assert.Equal(result.Value01, 1);
Assert.False(result.Value02.HasValue);
Assert.False(result.Value03.HasValue);

// add the second update with just Value02 and ensure that Value01 stays untouched
await scp.MergeOrInsertAsync<PartialUpdateModel>(new PartialUpdateModel() { CustomerId = "C01", MeterId = "M01", Value02 = 2 });

result = (await scp.EnableAutoCreateTable().Query<PartialUpdateModel>().Now()).First();
Assert.True(result.Value01.HasValue);
Assert.Equal(result.Value01, 1);
Assert.True(result.Value02.HasValue);
Assert.Equal(result.Value02, 2);
Assert.False(result.Value03.HasValue);

// replace the model with Value 03
await scp.InsertOrReplaceAsync<PartialUpdateModel>(new PartialUpdateModel() { CustomerId = "C01", MeterId = "M01", Value03 = 3 });

result = (await scp.EnableAutoCreateTable().Query<PartialUpdateModel>().Now()).First();
Assert.False(result.Value01.HasValue);
Assert.False(result.Value02.HasValue);
Assert.True(result.Value03.HasValue);
Assert.Equal(result.Value03, 3);

// clean up
scp.DropTable<PartialUpdateModel>();
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using CoreHelpers.WindowsAzure.Storage.Table.Attributes;

namespace CoreHelpers.WindowsAzure.Storage.Table.Tests.Models
{
[Storable(Tablename = "PartialUpdate")]
public class PartialUpdateModel
{

[PartitionKey]
public string CustomerId { get; set; } = String.Empty;

[RowKey]
public string MeterId { get; set; } = String.Empty;

public int? Value01 { get; set; }
public int? Value02 { get; set; }
public int? Value03 { get; set; }
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ internal void AddAttributeMapper(Assembly assembly)
}
}

public void AddAttributeMapper<T>(String optionalTablenameOverride = null) where T : class
{
AddAttributeMapper(typeof(T), optionalTablenameOverride);
}

public void AddAttributeMapper(Type type)
{
AddAttributeMapper(type, string.Empty);
Expand Down

0 comments on commit 9903320

Please sign in to comment.