Skip to content

Commit 2653b8f

Browse files
committed
Improve test tables management
1 parent 2c3b72b commit 2653b8f

File tree

3 files changed

+567
-658
lines changed

3 files changed

+567
-658
lines changed

src/Tests/DocumentRepositoryTests.cs

Lines changed: 119 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Devlooped
1010
{
11-
public class DocumentRepositoryTests
11+
public class DocumentRepositoryTests : IDisposable
1212
{
1313
public static IEnumerable<object[]> Serializers => new object[][]
1414
{
@@ -19,260 +19,190 @@ public class DocumentRepositoryTests
1919
new object[] { ProtobufDocumentSerializer.Default },
2020
};
2121

22+
TableConnection table = new TableConnection(CloudStorageAccount.DevelopmentStorageAccount, "a" + Guid.NewGuid().ToString("n"));
23+
void IDisposable.Dispose() => this.table.GetTableAsync().Result.Delete();
24+
2225
[Theory]
2326
[MemberData(nameof(Serializers))]
2427
public async Task DocumentEndToEnd(IDocumentSerializer serializer)
2528
{
26-
var table = CloudStorageAccount.DevelopmentStorageAccount
27-
.CreateTableServiceClient()
28-
.GetTableClient(serializer.GetType().Name);
29+
var repo = DocumentRepository.Create<DocumentEntity>(table, serializer: serializer);
2930

30-
await table.DeleteAsync();
31-
await table.CreateAsync();
31+
var partitionKey = "P" + Guid.NewGuid().ToString("N");
32+
var rowKey = "R" + Guid.NewGuid().ToString("N");
3233

33-
try
34+
var entity = await repo.PutAsync(new DocumentEntity
3435
{
35-
var repo = DocumentRepository.Create<DocumentEntity>(CloudStorageAccount.DevelopmentStorageAccount,
36-
table.Name, serializer: serializer);
37-
38-
var partitionKey = "P" + Guid.NewGuid().ToString("N");
39-
var rowKey = "R" + Guid.NewGuid().ToString("N");
36+
PartitionKey = partitionKey,
37+
RowKey = rowKey,
38+
Title = "Foo",
39+
});
4040

41-
var entity = await repo.PutAsync(new DocumentEntity
42-
{
43-
PartitionKey = partitionKey,
44-
RowKey = rowKey,
45-
Title = "Foo",
46-
});
41+
entity.Title = "Bar";
4742

48-
entity.Title = "Bar";
43+
await repo.PutAsync(entity);
4944

50-
await repo.PutAsync(entity);
45+
var saved = await repo.GetAsync(partitionKey, rowKey);
5146

52-
var saved = await repo.GetAsync(partitionKey, rowKey);
47+
Assert.NotNull(saved);
48+
Assert.Equal("Bar", saved!.Title);
49+
Assert.Equal(DateOnly.FromDateTime(DateTime.Today), saved.Date);
5350

54-
Assert.NotNull(saved);
55-
Assert.Equal("Bar", saved!.Title);
56-
Assert.Equal(DateOnly.FromDateTime(DateTime.Today), saved.Date);
51+
var entities = new List<DocumentEntity>();
5752

58-
var entities = new List<DocumentEntity>();
53+
await foreach (var e in repo.EnumerateAsync(partitionKey))
54+
entities.Add(e);
5955

60-
await foreach (var e in repo.EnumerateAsync(partitionKey))
61-
entities.Add(e);
56+
Assert.Single(entities);
6257

63-
Assert.Single(entities);
64-
65-
// Verify that the entity is not serialized as a string for non-string serializer
66-
if (serializer is not IStringDocumentSerializer)
67-
{
68-
var generic = TableRepository.Create(CloudStorageAccount.DevelopmentStorageAccount, table.Name);
69-
var row = await generic.GetAsync(partitionKey, rowKey);
70-
Assert.NotNull(row);
71-
Assert.IsType<byte[]>(row["Document"]);
72-
}
58+
// Verify that the entity is not serialized as a string for non-string serializer
59+
if (serializer is not IStringDocumentSerializer)
60+
{
61+
var generic = TableRepository.Create(table);
62+
var row = await generic.GetAsync(partitionKey, rowKey);
63+
Assert.NotNull(row);
64+
Assert.IsType<byte[]>(row["Document"]);
65+
}
7366

74-
await repo.DeleteAsync(saved);
67+
await repo.DeleteAsync(saved);
7568

76-
Assert.Null(await repo.GetAsync(partitionKey, rowKey));
69+
Assert.Null(await repo.GetAsync(partitionKey, rowKey));
7770

78-
await foreach (var _ in repo.EnumerateAsync(partitionKey))
79-
Assert.Fail("Did not expect to find any entities");
80-
}
81-
finally
82-
{
83-
await table.DeleteAsync();
84-
}
71+
await foreach (var _ in repo.EnumerateAsync(partitionKey))
72+
Assert.Fail("Did not expect to find any entities");
8573
}
8674

8775
[Theory]
8876
[MemberData(nameof(Serializers))]
8977
public async Task DocumentPartitionEndToEnd(IDocumentSerializer serializer)
9078
{
91-
var table = CloudStorageAccount.DevelopmentStorageAccount
92-
.CreateTableServiceClient()
93-
.GetTableClient(serializer.GetType().Name);
79+
var repo = DocumentPartition.Create<DocumentEntity>(table, serializer: serializer);
9480

95-
await table.DeleteAsync();
96-
await table.CreateAsync();
81+
var partitionKey = "P" + Guid.NewGuid().ToString("N");
82+
var rowKey = "R" + Guid.NewGuid().ToString("N");
9783

98-
try
84+
var entity = await repo.PutAsync(new DocumentEntity
9985
{
100-
var repo = DocumentPartition.Create<DocumentEntity>(CloudStorageAccount.DevelopmentStorageAccount,
101-
table.Name, serializer: serializer);
86+
PartitionKey = partitionKey,
87+
RowKey = rowKey,
88+
Title = "Foo",
89+
});
10290

103-
var partitionKey = "P" + Guid.NewGuid().ToString("N");
104-
var rowKey = "R" + Guid.NewGuid().ToString("N");
91+
entity.Title = "Bar";
10592

106-
var entity = await repo.PutAsync(new DocumentEntity
107-
{
108-
PartitionKey = partitionKey,
109-
RowKey = rowKey,
110-
Title = "Foo",
111-
});
93+
await repo.PutAsync(entity);
11294

113-
entity.Title = "Bar";
95+
var saved = await repo.GetAsync(rowKey);
11496

115-
await repo.PutAsync(entity);
97+
Assert.NotNull(saved);
98+
Assert.Equal("Bar", saved!.Title);
11699

117-
var saved = await repo.GetAsync(rowKey);
100+
var entities = new List<DocumentEntity>();
118101

119-
Assert.NotNull(saved);
120-
Assert.Equal("Bar", saved!.Title);
102+
await foreach (var e in repo.EnumerateAsync())
103+
entities.Add(e);
121104

122-
var entities = new List<DocumentEntity>();
105+
Assert.Single(entities);
123106

124-
await foreach (var e in repo.EnumerateAsync())
125-
entities.Add(e);
107+
await repo.DeleteAsync(saved);
126108

127-
Assert.Single(entities);
109+
Assert.Null(await repo.GetAsync(rowKey));
128110

129-
await repo.DeleteAsync(saved);
130-
131-
Assert.Null(await repo.GetAsync(rowKey));
132-
133-
await foreach (var _ in repo.EnumerateAsync())
134-
Assert.Fail("Did not expect to find any entities");
135-
}
136-
finally
137-
{
138-
await table.DeleteAsync();
139-
}
111+
await foreach (var _ in repo.EnumerateAsync())
112+
Assert.Fail("Did not expect to find any entities");
140113
}
141114

142115
[Theory]
143116
[MemberData(nameof(Serializers))]
144117
public async Task CanQueryDocument(IDocumentSerializer serializer)
145118
{
146-
var table = CloudStorageAccount.DevelopmentStorageAccount
147-
.CreateTableServiceClient()
148-
.GetTableClient(nameof(CanQueryDocument) + serializer.GetType().Name);
119+
var repo = DocumentRepository.Create<DocumentEntity>(table, serializer: serializer);
149120

150-
await table.DeleteAsync();
151-
await table.CreateAsync();
121+
var partitionKey = "P5943C610208D4008BEC052272ED07214";
152122

153-
try
123+
await repo.PutAsync(new DocumentEntity
154124
{
155-
var repo = DocumentRepository.Create<DocumentEntity>(CloudStorageAccount.DevelopmentStorageAccount,
156-
table.Name, serializer: serializer);
157-
158-
var partitionKey = "P5943C610208D4008BEC052272ED07214";
159-
160-
await repo.PutAsync(new DocumentEntity
161-
{
162-
PartitionKey = partitionKey,
163-
RowKey = "Bar",
164-
Title = "Bar",
165-
});
166-
167-
await repo.PutAsync(new DocumentEntity
168-
{
169-
PartitionKey = partitionKey,
170-
RowKey = "Foo",
171-
Title = "Foo",
172-
});
173-
174-
var typeName = typeof(DocumentEntity).FullName!.Replace('+', '.');
175-
176-
var entities = await repo.EnumerateAsync(e =>
177-
e.PartitionKey == partitionKey &&
178-
e.RowKey.CompareTo("Foo") >= 0 && e.RowKey.CompareTo("Fop") < 0 &&
179-
e.Version != "1.0" &&
180-
e.Type == typeName)
181-
.ToListAsync();
182-
183-
Assert.Single(entities);
184-
}
185-
finally
125+
PartitionKey = partitionKey,
126+
RowKey = "Bar",
127+
Title = "Bar",
128+
});
129+
130+
await repo.PutAsync(new DocumentEntity
186131
{
187-
await table.DeleteAsync();
188-
}
132+
PartitionKey = partitionKey,
133+
RowKey = "Foo",
134+
Title = "Foo",
135+
});
136+
137+
var typeName = typeof(DocumentEntity).FullName!.Replace('+', '.');
138+
139+
var entities = await repo.EnumerateAsync(e =>
140+
e.PartitionKey == partitionKey &&
141+
e.RowKey.CompareTo("Foo") >= 0 && e.RowKey.CompareTo("Fop") < 0 &&
142+
e.Version != "1.0" &&
143+
e.Type == typeName)
144+
.ToListAsync();
145+
146+
Assert.Single(entities);
189147
}
190148

191149
[Theory]
192150
[MemberData(nameof(Serializers))]
193151
public async Task CanIncludeProperties(IDocumentSerializer serializer)
194152
{
195-
var table = CloudStorageAccount.DevelopmentStorageAccount
196-
.CreateTableServiceClient()
197-
.GetTableClient(nameof(CanIncludeProperties) + serializer.GetType().Name);
153+
var repo = DocumentRepository.Create<DocumentEntity>(table, serializer: serializer, includeProperties: true);
198154

199-
await table.DeleteAsync();
200-
await table.CreateAsync();
155+
var partitionKey = "P5943C610208D4008BEC052272ED07214";
201156

202-
try
157+
await repo.PutAsync(new DocumentEntity
203158
{
204-
var repo = DocumentRepository.Create<DocumentEntity>(CloudStorageAccount.DevelopmentStorageAccount,
205-
table.Name, serializer: serializer, includeProperties: true);
206-
207-
var partitionKey = "P5943C610208D4008BEC052272ED07214";
208-
209-
await repo.PutAsync(new DocumentEntity
210-
{
211-
PartitionKey = partitionKey,
212-
RowKey = "Bar",
213-
Title = "Bar",
214-
});
215-
216-
await repo.PutAsync(new DocumentEntity
217-
{
218-
PartitionKey = partitionKey,
219-
RowKey = "Foo",
220-
Title = "Foo",
221-
});
222-
223-
var entity = table.GetEntity<TableEntity>(partitionKey, "Bar");
224-
Assert.Equal("Bar", entity.Value["Title"]);
225-
226-
entity = table.GetEntity<TableEntity>(partitionKey, "Foo");
227-
Assert.Equal("Foo", entity.Value["Title"]);
228-
}
229-
finally
159+
PartitionKey = partitionKey,
160+
RowKey = "Bar",
161+
Title = "Bar",
162+
});
163+
164+
await repo.PutAsync(new DocumentEntity
230165
{
231-
await table.DeleteAsync();
232-
}
166+
PartitionKey = partitionKey,
167+
RowKey = "Foo",
168+
Title = "Foo",
169+
});
170+
171+
var client = await table.GetTableAsync();
172+
var entity = client.GetEntity<TableEntity>(partitionKey, "Bar");
173+
Assert.Equal("Bar", entity.Value["Title"]);
174+
175+
entity = client.GetEntity<TableEntity>(partitionKey, "Foo");
176+
Assert.Equal("Foo", entity.Value["Title"]);
233177
}
234178

235179
[Theory]
236180
[MemberData(nameof(Serializers))]
237181
public async Task CanIncludePropertiesInParition(IDocumentSerializer serializer)
238182
{
239-
var table = CloudStorageAccount.DevelopmentStorageAccount
240-
.CreateTableServiceClient()
241-
.GetTableClient(nameof(CanIncludeProperties) + serializer.GetType().Name);
242-
243-
await table.DeleteAsync();
244-
await table.CreateAsync();
183+
var partitionKey = "P5943C610208D4008BEC052272ED07214";
184+
var repo = DocumentPartition.Create<DocumentEntity>(table, partitionKey, serializer: serializer, includeProperties: true);
245185

246-
try
186+
await repo.PutAsync(new DocumentEntity
247187
{
248-
var partitionKey = "P5943C610208D4008BEC052272ED07214";
249-
var repo = DocumentPartition.Create<DocumentEntity>(CloudStorageAccount.DevelopmentStorageAccount,
250-
table.Name, partitionKey, serializer: serializer, includeProperties: true);
251-
252-
await repo.PutAsync(new DocumentEntity
253-
{
254-
PartitionKey = partitionKey,
255-
RowKey = "Bar",
256-
Title = "Bar",
257-
});
258-
259-
await repo.PutAsync(new DocumentEntity
260-
{
261-
PartitionKey = partitionKey,
262-
RowKey = "Foo",
263-
Title = "Foo",
264-
});
265-
266-
var entity = table.GetEntity<TableEntity>(partitionKey, "Bar");
267-
Assert.Equal("Bar", entity.Value["Title"]);
268-
269-
entity = table.GetEntity<TableEntity>(partitionKey, "Foo");
270-
Assert.Equal("Foo", entity.Value["Title"]);
271-
}
272-
finally
188+
PartitionKey = partitionKey,
189+
RowKey = "Bar",
190+
Title = "Bar",
191+
});
192+
193+
await repo.PutAsync(new DocumentEntity
273194
{
274-
await table.DeleteAsync();
275-
}
195+
PartitionKey = partitionKey,
196+
RowKey = "Foo",
197+
Title = "Foo",
198+
});
199+
200+
var client = await table.GetTableAsync();
201+
var entity = client.GetEntity<TableEntity>(partitionKey, "Bar");
202+
Assert.Equal("Bar", entity.Value["Title"]);
203+
204+
entity = client.GetEntity<TableEntity>(partitionKey, "Foo");
205+
Assert.Equal("Foo", entity.Value["Title"]);
276206
}
277207

278208
[ProtoContract]

0 commit comments

Comments
 (0)