forked from CoreHelpers/AzureStorageTable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathITS026RelatedTable.cs
More file actions
121 lines (97 loc) · 4.68 KB
/
ITS026RelatedTable.cs
File metadata and controls
121 lines (97 loc) · 4.68 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
using System;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Contracts;
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 ITS026RelatedTable
{
private readonly IStorageContext _rootContext;
public ITS026RelatedTable(IStorageContext context)
{
_rootContext = context;
}
[Fact]
public async Task ReadRelatedTable()
{
using (var storageContext = _rootContext.CreateChildContext())
{
// set the tablename context
storageContext.SetTableContext();
//
// create a new user
var user = new UserModel2() { FirstName = "Egon", LastName = "Mueller", Contact = "em@acme.org" };
var demo = new DemoModel3() { P = "P2", R = "R2", UserContact = "em@acme.org" };
// ensure we are using the attributes
storageContext.AddAttributeMapper();
// ensure the tables exists
await storageContext.CreateTableAsync<UserModel2>();
await storageContext.CreateTableAsync<DemoModel3>();
// inser the models
await storageContext.MergeOrInsertAsync<UserModel2>(user);
await storageContext.MergeOrInsertAsync<DemoModel3>(demo);
// query all
var result = await storageContext.QueryAsync<DemoModel3>();
Assert.Single(result);
Assert.Equal("Egon", result.First().User?.FirstName);
Assert.Equal("Mueller", result.First().User?.LastName);
Assert.Equal("em@acme.org", result.First().User?.Contact);
// Clean up
user = result.First().User;
if (user != null)
await storageContext.DeleteAsync<UserModel2>(user);
var userResult = await storageContext.QueryAsync<UserModel2>();
Assert.NotNull(userResult);
Assert.Empty(userResult);
await storageContext.DeleteAsync<DemoModel3>(result);
result = await storageContext.QueryAsync<DemoModel3>();
Assert.NotNull(result);
Assert.Empty(result);
await storageContext.DropTableAsync<UserModel2>();
await storageContext.DropTableAsync<DemoModel3>();
}
}
[Fact]
public async Task WriteRelatedTable()
{
using (var storageContext = _rootContext.CreateChildContext())
{
// set the tablename context
storageContext.SetTableContext();
//
// create a new user
var user = new UserModel2() { FirstName = "Egon", LastName = "Mueller", Contact = "em@acme.org" };
var demo = new DemoModel4() { P = "P2", R = "R2", UserContact = "em@acme.org", User = user };
// ensure we are using the attributes
storageContext.AddAttributeMapper();
// ensure the tables exists
await storageContext.CreateTableAsync<UserModel2>();
await storageContext.CreateTableAsync<DemoModel4>();
// inser the model
await storageContext.MergeOrInsertAsync<DemoModel4>(demo);
// query all
var result = await storageContext.QueryAsync<DemoModel4>();
Assert.Single(result);
Assert.Equal("Egon", result.First().User?.FirstName);
Assert.Equal("Mueller", result.First().User?.LastName);
Assert.Equal("em@acme.org", result.First().User?.Contact);
// Clean up
user = result.First().User;
if (user != null)
await storageContext.DeleteAsync<UserModel2>(user);
var userResult = await storageContext.QueryAsync<UserModel2>();
Assert.NotNull(userResult);
Assert.Empty(userResult);
await storageContext.DeleteAsync<DemoModel4>(result);
result = await storageContext.QueryAsync<DemoModel4>();
Assert.NotNull(result);
Assert.Empty(result);
await storageContext.DropTableAsync<UserModel2>();
await storageContext.DropTableAsync<DemoModel4>();
}
}
}
}