forked from CoreHelpers/AzureStorageTable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathITS001StoreWithStaticEntityMapper.cs
More file actions
173 lines (139 loc) · 5.47 KB
/
ITS001StoreWithStaticEntityMapper.cs
File metadata and controls
173 lines (139 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Diagnostics;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Contracts;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Delegates;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Extensions;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Models;
using Xunit.DependencyInjection;
namespace CoreHelpers.WindowsAzure.Storage.Table.Tests
{
[Startup(typeof(Startup))]
[Collection("Sequential")]
public class ITS001StoreWithStaticEntityMapper
{
private readonly IStorageContext _rootContext;
public ITS001StoreWithStaticEntityMapper(IStorageContext context)
{
_rootContext = context;
}
[Fact]
public async Task VerifyTableExists()
{
using (var scp = _rootContext.CreateChildContext())
{
// set the tablename context
scp.SetTableContext();
// configure the entity mapper
scp.AddEntityMapper(typeof(UserModel), "Contact", "Contact", "UserProfiles");
Assert.False(await scp.ExistsTableAsync<UserModel>());
await scp.CreateTableAsync<UserModel>();
Assert.True(await scp.ExistsTableAsync<UserModel>());
}
}
[Fact]
public async Task VerifyStaticEntityMapperOperations()
{
using (var scp = _rootContext.CreateChildContext())
{
// set the tablename context
scp.SetTableContext();
// unification uuid
var runId = Guid.NewGuid().ToString();
// create a new user
var user = new UserModel() { FirstName = "Egon", LastName = "Mueller", Contact = "em@acme.org" };
user.Contact = $"{user.Contact}.{runId}";
// configure the entity mapper
scp.AddEntityMapper(typeof(UserModel), tableName: "UserProfiles", partitionKeyFormat: "Contact", rowKeyFormat: "Contact" );
// execute the store operation
using (var sc = scp.CreateChildContext())
{
// ensure the table exists
await sc.CreateTableAsync<UserModel>();
// ensure we are empty
var resultEmpty = await scp.QueryAsync<UserModel>();
Assert.Empty(resultEmpty);
// inser the model
await sc.MergeOrInsertAsync<UserModel>(user);
}
// verify if the model was created
var result = await scp.QueryAsync<UserModel>();
Assert.NotNull(result);
Assert.Single(result);
Assert.Equal("Egon", result.First().FirstName);
Assert.Equal("Mueller", result.First().LastName);
Assert.Equal($"em@acme.org.{runId}", result.First().Contact);
// Clean up
await scp.DropTableAsync<UserModel>();
Assert.False(await scp.ExistsTableAsync<UserModel>());
}
}
[Fact]
public async Task VerifyVirtualKeysPOCOModel()
{
using (var scp = _rootContext.CreateChildContext())
{
// set the tablename context
scp.SetTableContext();
// create model
var vpmodel = new VirtualPartitionKeyDemoModelPOCO() { Value1 = "abc", Value2 = "def", Value3 = "ghi" };
// configure the entity mapper
scp.AddEntityMapper(typeof(VirtualPartitionKeyDemoModelPOCO), tableName: "VirtualPartitionKeyDemoModelPOCO", partitionKeyFormat: "{{Value1}}-{{Value2}}", rowKeyFormat: "{{Value2}}-{{Value3}}");
// execute the store operation
using (var sc = scp.CreateChildContext())
{
// ensure the table exists
await sc.CreateTableAsync<VirtualPartitionKeyDemoModelPOCO>();
// inser the model
await sc.MergeOrInsertAsync<VirtualPartitionKeyDemoModelPOCO>(vpmodel);
}
// verify if the model was created
var result = await scp.QueryAsync<VirtualPartitionKeyDemoModelPOCO>();
Assert.NotNull(result);
Assert.Single(result);
Assert.Equal("abc", result.First().Value1);
Assert.Equal("def", result.First().Value2);
Assert.Equal("ghi", result.First().Value3);
// Clean up
await scp.DropTableAsync<VirtualPartitionKeyDemoModelPOCO>();
Assert.False(await scp.ExistsTableAsync<VirtualPartitionKeyDemoModelPOCO>());
}
}
[Fact]
public async Task VerifyStatsDelegate()
{
using (var scp = _rootContext.CreateChildContext())
{
// set the tablename context
scp.SetTableContext();
// set the delegate
var stats = new StorageContextStatsDelegate();
scp.SetDelegate(stats);
// unification uuid
var runId = Guid.NewGuid().ToString();
// create a new user
var user = new UserModel() { FirstName = "Egon", LastName = "Mueller", Contact = "em@acme.org" };
user.Contact = $"{user.Contact}.{runId}";
// configure the entity mapper
scp.AddEntityMapper(typeof(UserModel), tableName: "UserProfiles", partitionKeyFormat: "Contact", rowKeyFormat: "Contact");
// execute the store operation
using (var sc = scp.CreateChildContext())
{
// ensure the table exists
await sc.CreateTableAsync<UserModel>();
// inser the model
await sc.MergeOrInsertAsync<UserModel>(user);
}
// verify if the model was created
var result = await scp.QueryAsync<UserModel>();
// Clean up
await scp.DeleteAsync<UserModel>(result);
// verify stats
Assert.Equal(1, stats.QueryOperations);
Assert.Equal(2, stats.StoreOperations.Values.Sum());
// Drop the table
await scp.DropTableAsync<UserModel>();
Assert.False(await scp.ExistsTableAsync<UserModel>());
}
}
}
}