3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
- using System . Linq ;
6
+ using System . Runtime . InteropServices ;
7
7
using System . Threading ;
8
8
using System . Threading . Tasks ;
9
9
using System . Web . Http ;
10
+ using Azure ;
11
+ using Azure . Data . Tables ;
10
12
using Microsoft . AspNetCore . Hosting ;
13
+ using Microsoft . Azure . Storage . Blob ;
14
+ using Microsoft . Azure . Storage . Queue ;
11
15
using Microsoft . Azure . WebJobs . Script . Config ;
12
16
using Microsoft . Azure . WebJobs . Script . Description ;
13
17
using Microsoft . Azure . WebJobs . Script . Eventing ;
14
18
using Microsoft . Azure . WebJobs . Script . Metrics ;
19
+ using Microsoft . Azure . WebJobs . Script . WebHost . Helpers ;
15
20
using Microsoft . Azure . WebJobs . Script . Workers . Rpc ;
21
+ using Microsoft . Azure . WebJobs . Script . Workers . SharedMemoryDataTransfer ;
16
22
using Microsoft . Extensions . Configuration ;
17
23
using Microsoft . Extensions . DependencyInjection ;
18
24
using Microsoft . Extensions . Hosting ;
19
25
using Microsoft . Extensions . Logging ;
20
26
using Microsoft . WebJobs . Script . Tests ;
21
- using Microsoft . Azure . Storage ;
22
- using Microsoft . Azure . Storage . Blob ;
23
- using Microsoft . Azure . Storage . Queue ;
24
- using Microsoft . Azure . Cosmos . Table ;
25
27
using Moq ;
26
28
using Xunit ;
27
29
using CloudStorageAccount = Microsoft . Azure . Storage . CloudStorageAccount ;
28
- using TableStorageAccount = Microsoft . Azure . Cosmos . Table . CloudStorageAccount ;
29
30
using IApplicationLifetime = Microsoft . AspNetCore . Hosting . IApplicationLifetime ;
30
- using Microsoft . Azure . WebJobs . Script . Workers . SharedMemoryDataTransfer ;
31
- using System . Runtime . InteropServices ;
32
31
33
32
namespace Microsoft . Azure . WebJobs . Script . Tests
34
33
{
@@ -73,15 +72,15 @@ protected ScriptHostEndToEndTestFixture(string rootPath, string testId, string f
73
72
74
73
public CloudQueueClient QueueClient { get ; private set ; }
75
74
76
- public CloudTableClient TableClient { get ; private set ; }
75
+ public TableServiceClient TableServiceClient { get ; private set ; }
77
76
78
77
public CloudBlobClient BlobClient { get ; private set ; }
79
78
80
79
public CloudQueue TestQueue { get ; private set ; }
81
80
82
81
public CloudQueue MobileTablesQueue { get ; private set ; }
83
82
84
- public CloudTable TestTable { get ; private set ; }
83
+ public TableClient TestTable { get ; private set ; }
85
84
86
85
public ScriptHost JobHost { get ; private set ; }
87
86
@@ -109,8 +108,7 @@ public async Task InitializeAsync()
109
108
QueueClient = storageAccount . CreateCloudQueueClient ( ) ;
110
109
BlobClient = storageAccount . CreateCloudBlobClient ( ) ;
111
110
112
- TableStorageAccount tableStorageAccount = TableStorageAccount . Parse ( connectionString ) ;
113
- TableClient = tableStorageAccount . CreateCloudTableClient ( ) ;
111
+ TableServiceClient = new TableServiceClient ( connectionString ) ;
114
112
115
113
await CreateTestStorageEntities ( ) ;
116
114
@@ -219,50 +217,56 @@ protected virtual async Task CreateTestStorageEntities()
219
217
await TestOutputContainer . CreateIfNotExistsAsync ( ) ;
220
218
await TestHelpers . ClearContainerAsync ( TestOutputContainer ) ;
221
219
222
- TestTable = TableClient . GetTableReference ( "test" ) ;
220
+ TestTable = TableServiceClient . GetTableClient ( "test" ) ;
223
221
await TestTable . CreateIfNotExistsAsync ( ) ;
224
222
225
- await DeleteEntities ( TestTable , "AAA" ) ;
226
- await DeleteEntities ( TestTable , "BBB" ) ;
227
-
228
- var batch = new TableBatchOperation ( ) ;
229
- batch . Insert ( new TestEntity { PartitionKey = "AAA" , RowKey = "001" , Region = "West" , Name = "Test Entity 1" , Status = 0 } ) ;
230
- batch . Insert ( new TestEntity { PartitionKey = "AAA" , RowKey = "002" , Region = "East" , Name = "Test Entity 2" , Status = 1 } ) ;
231
- batch . Insert ( new TestEntity { PartitionKey = "AAA" , RowKey = "003" , Region = "West" , Name = "Test Entity 3" , Status = 1 } ) ;
232
- batch . Insert ( new TestEntity { PartitionKey = "AAA" , RowKey = "004" , Region = "West" , Name = "Test Entity 4" , Status = 1 } ) ;
233
- batch . Insert ( new TestEntity { PartitionKey = "AAA" , RowKey = "005" , Region = "East" , Name = "Test Entity 5" , Status = 0 } ) ;
234
- await TestTable . ExecuteBatchAsync ( batch ) ;
235
-
236
- batch = new TableBatchOperation ( ) ;
237
- batch . Insert ( new TestEntity { PartitionKey = "BBB" , RowKey = "001" , Region = "South" , Name = "Test Entity 1" , Status = 0 } ) ;
238
- batch . Insert ( new TestEntity { PartitionKey = "BBB" , RowKey = "002" , Region = "West" , Name = "Test Entity 2" , Status = 1 } ) ;
239
- batch . Insert ( new TestEntity { PartitionKey = "BBB" , RowKey = "003" , Region = "West" , Name = "Test Entity 3" , Status = 0 } ) ;
240
- await TestTable . ExecuteBatchAsync ( batch ) ;
223
+ await DeleteEntities ( TestTable , TableServiceClient , "AAA" ) ;
224
+ await DeleteEntities ( TestTable , TableServiceClient , "BBB" ) ;
225
+
226
+ var batch = new List < TableTransactionAction >
227
+ {
228
+ new TableTransactionAction ( TableTransactionActionType . Add , new TestEntity { PartitionKey = "AAA" , RowKey = "001" , Region = "West" , Name = "Test Entity 1" , Status = 0 } ) ,
229
+ new TableTransactionAction ( TableTransactionActionType . Add , new TestEntity { PartitionKey = "AAA" , RowKey = "002" , Region = "East" , Name = "Test Entity 2" , Status = 1 } ) ,
230
+ new TableTransactionAction ( TableTransactionActionType . Add , new TestEntity { PartitionKey = "AAA" , RowKey = "003" , Region = "West" , Name = "Test Entity 3" , Status = 1 } ) ,
231
+ new TableTransactionAction ( TableTransactionActionType . Add , new TestEntity { PartitionKey = "AAA" , RowKey = "004" , Region = "West" , Name = "Test Entity 4" , Status = 1 } ) ,
232
+ new TableTransactionAction ( TableTransactionActionType . Add , new TestEntity { PartitionKey = "AAA" , RowKey = "005" , Region = "East" , Name = "Test Entity 5" , Status = 0 } )
233
+ } ;
234
+ await TestTable . SubmitTransactionAsync ( batch ) ;
235
+
236
+ batch =
237
+ [
238
+ new TableTransactionAction ( TableTransactionActionType . Add , new TestEntity { PartitionKey = "BBB" , RowKey = "001" , Region = "South" , Name = "Test Entity 1" , Status = 0 } ) ,
239
+ new TableTransactionAction ( TableTransactionActionType . Add , new TestEntity { PartitionKey = "BBB" , RowKey = "002" , Region = "West" , Name = "Test Entity 2" , Status = 1 } ) ,
240
+ new TableTransactionAction ( TableTransactionActionType . Add , new TestEntity { PartitionKey = "BBB" , RowKey = "003" , Region = "West" , Name = "Test Entity 3" , Status = 0 } ) ,
241
+ ] ;
242
+ await TestTable . SubmitTransactionAsync ( batch ) ;
241
243
}
242
244
243
- public async Task DeleteEntities ( CloudTable table , string partition = null )
245
+ public async Task DeleteEntities ( TableClient table , TableServiceClient tableServiceClient , string partition = null )
244
246
{
245
- if ( ! await table . ExistsAsync ( ) )
247
+ if ( ! await TableStorageHelpers . TableExistAsync ( table , tableServiceClient ) )
246
248
{
247
249
return ;
248
250
}
249
251
250
- TableQuery query = new TableQuery ( ) ;
252
+ string query = string . Empty ;
251
253
if ( partition != null )
252
254
{
253
- query . FilterString = string . Format ( "PartitionKey eq '{0}'" , partition ) ;
255
+ query = TableClient . CreateQueryFilter ( $ "PartitionKey eq { partition } " ) ;
254
256
}
255
257
256
- var entities = await table . ExecuteQuerySegmentedAsync ( query , null ) ;
258
+ var entities = table . QueryAsync < TableEntity > ( query , null ) ;
257
259
258
- if ( entities . Any ( ) )
260
+
261
+ var batch = new List < TableTransactionAction > ( ) ;
262
+ await foreach ( var entity in entities )
259
263
{
260
- var batch = new TableBatchOperation ( ) ;
261
- foreach ( var entity in entities )
262
- {
263
- batch . Delete ( entity ) ;
264
- }
265
- await table . ExecuteBatchAsync ( batch ) ;
264
+ batch . Add ( new TableTransactionAction ( TableTransactionActionType . Delete , entity ) ) ;
265
+ }
266
+
267
+ if ( batch . Count != 0 )
268
+ {
269
+ await table . SubmitTransactionAsync ( batch ) ;
266
270
}
267
271
}
268
272
@@ -282,13 +286,21 @@ public virtual async Task DisposeAsync()
282
286
Environment . SetEnvironmentVariable ( RpcWorkerConstants . FunctionWorkerRuntimeSettingName , string . Empty ) ;
283
287
}
284
288
285
- private class TestEntity : TableEntity
289
+ private class TestEntity : ITableEntity
286
290
{
287
291
public string Name { get ; set ; }
288
292
289
293
public string Region { get ; set ; }
290
294
291
295
public int Status { get ; set ; }
296
+
297
+ public string PartitionKey { get ; set ; }
298
+
299
+ public string RowKey { get ; set ; }
300
+
301
+ public DateTimeOffset ? Timestamp { get ; set ; }
302
+
303
+ public ETag ETag { get ; set ; }
292
304
}
293
305
}
294
306
}
0 commit comments