Skip to content

Commit 9903320

Browse files
committed
Added generic attribute mapper and tested partial update model
1 parent 3eef789 commit 9903320

4 files changed

Lines changed: 95 additions & 0 deletions

File tree

CoreHelpers.WindowsAzure.Storage.Table.Abstractions/IStorageContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public interface IStorageContext : IDisposable
2121

2222
void AddAttributeMapper(Type type);
2323

24+
void AddAttributeMapper<T>(String optionalTablenameOverride = null) where T: class;
25+
2426
void AddAttributeMapper(Type type, String optionalTablenameOverride);
2527

2628
void AddEntityMapper(Type entityType, String partitionKeyFormat, String rowKeyFormat, String tableName);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using CoreHelpers.WindowsAzure.Storage.Table.Backup;
3+
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Contracts;
4+
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Extensions;
5+
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Models;
6+
7+
namespace CoreHelpers.WindowsAzure.Storage.Table.Tests
8+
{
9+
public class ITS22PartialUpates
10+
{
11+
private readonly IStorageContext _rootContext;
12+
13+
public ITS22PartialUpates(IStorageContext context)
14+
{
15+
_rootContext = context;
16+
}
17+
18+
[Fact]
19+
public async Task CreateAndVerifyBackup()
20+
{
21+
using (var scp = _rootContext.CreateChildContext())
22+
{
23+
// set the tablename context
24+
scp.SetTableContext();
25+
26+
// configure the entity mapper
27+
scp.AddAttributeMapper<PartialUpdateModel>();
28+
29+
// verify we don't have any value
30+
Assert.Empty(await scp.EnableAutoCreateTable().Query<PartialUpdateModel>().InAllPartitions().Now());
31+
32+
33+
// insert the value with just on optional integer
34+
await scp.MergeOrInsertAsync<PartialUpdateModel>(new PartialUpdateModel() { CustomerId = "C01", MeterId = "M01", Value01 = 1 });
35+
36+
var result = (await scp.EnableAutoCreateTable().Query<PartialUpdateModel>().Now()).First();
37+
Assert.True(result.Value01.HasValue);
38+
Assert.Equal(result.Value01, 1);
39+
Assert.False(result.Value02.HasValue);
40+
Assert.False(result.Value03.HasValue);
41+
42+
// add the second update with just Value02 and ensure that Value01 stays untouched
43+
await scp.MergeOrInsertAsync<PartialUpdateModel>(new PartialUpdateModel() { CustomerId = "C01", MeterId = "M01", Value02 = 2 });
44+
45+
result = (await scp.EnableAutoCreateTable().Query<PartialUpdateModel>().Now()).First();
46+
Assert.True(result.Value01.HasValue);
47+
Assert.Equal(result.Value01, 1);
48+
Assert.True(result.Value02.HasValue);
49+
Assert.Equal(result.Value02, 2);
50+
Assert.False(result.Value03.HasValue);
51+
52+
// replace the model with Value 03
53+
await scp.InsertOrReplaceAsync<PartialUpdateModel>(new PartialUpdateModel() { CustomerId = "C01", MeterId = "M01", Value03 = 3 });
54+
55+
result = (await scp.EnableAutoCreateTable().Query<PartialUpdateModel>().Now()).First();
56+
Assert.False(result.Value01.HasValue);
57+
Assert.False(result.Value02.HasValue);
58+
Assert.True(result.Value03.HasValue);
59+
Assert.Equal(result.Value03, 3);
60+
61+
// clean up
62+
scp.DropTable<PartialUpdateModel>();
63+
}
64+
}
65+
}
66+
}
67+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using CoreHelpers.WindowsAzure.Storage.Table.Attributes;
3+
4+
namespace CoreHelpers.WindowsAzure.Storage.Table.Tests.Models
5+
{
6+
[Storable(Tablename = "PartialUpdate")]
7+
public class PartialUpdateModel
8+
{
9+
10+
[PartitionKey]
11+
public string CustomerId { get; set; } = String.Empty;
12+
13+
[RowKey]
14+
public string MeterId { get; set; } = String.Empty;
15+
16+
public int? Value01 { get; set; }
17+
public int? Value02 { get; set; }
18+
public int? Value03 { get; set; }
19+
}
20+
}
21+

CoreHelpers.WindowsAzure.Storage.Table/StorageContextAttributeMapping.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ internal void AddAttributeMapper(Assembly assembly)
4444
}
4545
}
4646

47+
public void AddAttributeMapper<T>(String optionalTablenameOverride = null) where T : class
48+
{
49+
AddAttributeMapper(typeof(T), optionalTablenameOverride);
50+
}
51+
4752
public void AddAttributeMapper(Type type)
4853
{
4954
AddAttributeMapper(type, string.Empty);

0 commit comments

Comments
 (0)