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