-
Notifications
You must be signed in to change notification settings - Fork 18
/
SimpleTableOperationTests.fs
320 lines (262 loc) · 12.1 KB
/
SimpleTableOperationTests.fs
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
namespace FSharp.AWS.DynamoDB.Tests
open System
open FSharp.AWS.DynamoDB.Tests
open Swensen.Unquote
open Xunit
open FSharp.AWS.DynamoDB
open FSharp.AWS.DynamoDB.Scripting // These tests lean on the Synchronous wrappers
[<AutoOpen>]
module SimpleTableTypes =
type SimpleRecord =
{ [<HashKey>]
HashKey: string
[<RangeKey>]
RangeKey: string
EmptyString: string
Value: int64
Tuple: int64 * int64
Map: Map<string, int64>
Unions: Choice<string, int64, byte[]> list }
[<ConstantHashKey("HashKey", "compatible")>]
type CompatibleRecord =
{ [<RangeKey; CustomName("RangeKey")>]
Id: string
Values: Set<int> }
[<ConstantRangeKey("RangeKey", "compatible")>]
type CompatibleRecord2 =
{ [<HashKey; CustomName("HashKey")>]
Id: string
Values: Set<int> }
type ``Simple Table Operation Tests``(fixture: TableFixture) =
let rand = let r = Random.Shared in fun () -> int64 <| r.Next()
let mkItem () =
{ HashKey = guid ()
RangeKey = guid ()
EmptyString = ""
Value = rand ()
Tuple = rand (), rand ()
Map = seq { for _ in 0L .. rand () % 5L -> "K" + guid (), rand () } |> Map.ofSeq
Unions = [ Choice1Of3(guid ()); Choice2Of3(rand ()); Choice3Of3(Guid.NewGuid().ToByteArray()) ] }
let table = fixture.CreateEmpty<SimpleRecord>()
[<Fact>]
let ``Convert to compatible table`` () =
let table' = table.WithRecordType<CompatibleRecord>()
test <@ table.PrimaryKey = table'.PrimaryKey @>
[<Fact>]
let ``Convert to compatible table 2`` () =
let table' = table.WithRecordType<CompatibleRecord2>()
test <@ table.PrimaryKey = table'.PrimaryKey @>
[<Fact>]
let ``Simple Put Operation`` () =
let value = mkItem ()
let key = table.PutItem value
let value' = table.GetItem key
test <@ value = value' @>
[<Fact>]
let ``ContainsKey Operation`` () =
let value = mkItem ()
let key = table.PutItem value
test <@ table.ContainsKey key @>
let _ = table.DeleteItem key
test <@ not (table.ContainsKey key) @>
[<Fact>]
let ``TryGet Operation`` () =
let value = mkItem ()
let computedKey = table.Template.ExtractKey value
let get k = table.TryGetItemAsync k |> Async.RunSynchronously
let initialLoad = get computedKey
test <@ None = initialLoad @>
let key = table.PutItem value
test <@ computedKey = key @> // "Local key computation should be same as Put result"
let loaded = get computedKey
test <@ Some value = loaded @>
let _ = table.DeleteItem key
test <@ None = get key @>
[<Fact>]
let ``Batch Put Operation`` () =
let values = set [ for _ in 1L .. 20L -> mkItem () ]
let unprocessed = table.BatchPutItems values
let values' =
table.BatchGetItems(values |> Seq.map (fun r -> TableKey.Combined(r.HashKey, r.RangeKey)))
|> Set.ofArray
test <@ Array.isEmpty unprocessed @>
test <@ values = values' @>
[<Fact>]
let ``Batch Delete Operation`` () =
let values = set [ for _ in 1L .. 20L -> mkItem () ]
table.BatchPutItems values |> ignore
let unprocessed = table.BatchDeleteItems(values |> Seq.map (fun r -> TableKey.Combined(r.HashKey, r.RangeKey)))
test <@ Array.isEmpty unprocessed @>
let values' = table.BatchGetItems(values |> Seq.map (fun r -> TableKey.Combined(r.HashKey, r.RangeKey)))
test <@ Array.isEmpty values' @>
[<Fact>]
let ``Simple Delete Operation`` () =
let item = mkItem ()
let key = table.PutItem item
test <@ table.ContainsKey key @>
let item' = table.DeleteItem key
test <@ Some item = item' @>
test <@ not (table.ContainsKey key) @>
[<Fact>]
let ``Idempotent Delete Operation`` () =
let item = mkItem ()
let key = table.PutItem item
test <@ table.ContainsKey key @>
let item' = table.DeleteItem key
test <@ item' = Some item @>
let deletedItem = table.DeleteItem key
test <@ None = deletedItem @>
test <@ not (table.ContainsKey key) @>
[<Theory>]
[<InlineData("",
"rangeKey",
"One or more parameter values are not valid. The AttributeValue for a key attribute cannot contain an empty string value. Key: HashKey")>]
[<InlineData("hashKey",
"",
"One or more parameter values are not valid. The AttributeValue for a key attribute cannot contain an empty string value. Key: RangeKey")>]
[<InlineData("",
"",
"One or more parameter values are not valid. The AttributeValue for a key attribute cannot contain an empty string value. Key: HashKey")>]
let ``Operations with empty key values should fail with a DynamoDB client error`` (hashKey, rangeKey, expectedErrorMsg) =
let value = { mkItem () with HashKey = hashKey; RangeKey = rangeKey }
try
table.PutItem value |> ignore
with :? Amazon.DynamoDBv2.AmazonDynamoDBException as ex ->
test <@ ex.Message = expectedErrorMsg @>
interface IClassFixture<TableFixture>
type ``TransactWriteItems tests``(table1: TableFixture, table2: TableFixture) =
let randInt64 = let r = Random.Shared in fun () -> int64 <| r.Next()
let randInt = let r = Random.Shared in fun () -> int32 <| r.Next()
let mkItem () =
{ HashKey = guid ()
RangeKey = guid ()
EmptyString = ""
Value = randInt64 ()
Tuple = randInt64 (), randInt64 ()
Map = seq { for _ in 0L .. randInt64 () % 5L -> "K" + guid (), randInt64 () } |> Map.ofSeq
Unions = [ Choice1Of3(guid ()); Choice2Of3(randInt64 ()); Choice3Of3(Guid.NewGuid().ToByteArray()) ] }
let mkCompatibleItem () : CompatibleRecord = { Id = guid (); Values = set [ for _ in 0 .. (randInt () % 5) -> randInt () ] }
let table1 = table1.CreateEmpty<SimpleRecord>()
let table2 = table2.CreateEmpty<CompatibleRecord>()
let compileTable1 = table1.Template.PrecomputeConditionalExpr
let compileTable2 = table2.Template.PrecomputeConditionalExpr
let compileUpdateTable1 (e: Quotations.Expr<SimpleRecord -> SimpleRecord>) = table1.Template.PrecomputeUpdateExpr e
let compileUpdateTable2 (e: Quotations.Expr<SimpleRecord -> SimpleRecord>) = table1.Template.PrecomputeUpdateExpr e
let doesntExistConditionTable1 = compileTable1 <@ fun t -> NOT_EXISTS t.Value @>
let doesntExistConditionTable2 = compileTable2 <@ fun t -> NOT_EXISTS t.Values @>
let existsConditionTable1 = compileTable1 <@ fun t -> EXISTS t.Value @>
let existsConditionTable2 = compileTable2 <@ fun t -> EXISTS t.Values @>
[<Fact>]
let ``Minimal happy path`` () = async {
let item = mkItem ()
let transaction = table1.CreateTransaction()
transaction.Put(table1, item, doesntExistConditionTable1)
do! transaction.TransactWriteItems()
let! itemFound = table1.ContainsKeyAsync(table1.Template.ExtractKey item)
true =! itemFound
}
[<Fact>]
let ``Minimal happy path with multiple tables`` () = async {
let item = mkItem ()
let compatibleItem = mkCompatibleItem ()
let transaction = table1.CreateTransaction()
transaction.Put(table1, item, doesntExistConditionTable1)
transaction.Put(table2, compatibleItem, doesntExistConditionTable2)
do! transaction.TransactWriteItems()
let! itemFound = table1.ContainsKeyAsync(table1.Template.ExtractKey item)
true =! itemFound
let! compatibleItemFound = table2.ContainsKeyAsync(table2.Template.ExtractKey compatibleItem)
true =! compatibleItemFound
}
[<Fact>]
let ``Minimal Canceled path`` () = async {
let item = mkItem ()
let transaction = table1.CreateTransaction()
transaction.Put(table1, item, existsConditionTable1)
let mutable failed = false
try
do! transaction.TransactWriteItems()
with Transaction.TransactionCanceledConditionalCheckFailed ->
failed <- true
true =! failed
let! itemFound = table1.ContainsKeyAsync(table1.Template.ExtractKey item)
false =! itemFound
}
[<Theory; InlineData true; InlineData false>]
let ``ConditionCheck outcome should affect sibling TransactWrite`` shouldFail = async {
let item, item2 = mkItem (), mkItem ()
let! key = table1.PutItemAsync item
let transaction = table1.CreateTransaction()
if shouldFail then
transaction.Check(table1, key, doesntExistConditionTable1)
else
transaction.Check(table1, key, existsConditionTable1)
transaction.Put(table1, item2)
let mutable failed = false
try
do! transaction.TransactWriteItems()
with Transaction.TransactionCanceledConditionalCheckFailed ->
failed <- true
failed =! shouldFail
let! item2Found = table1.ContainsKeyAsync(table1.Template.ExtractKey item2)
failed =! not item2Found
}
[<Theory; InlineData true; InlineData false>]
let ``All paths`` shouldFail = async {
let item, item2, item3, item4, item5, item6, item7 = mkItem (), mkItem (), mkItem (), mkItem (), mkItem (), mkItem (), mkItem ()
let! key = table1.PutItemAsync item
let transaction = table1.CreateTransaction()
let requests =
[ transaction.Update(table1, key, compileUpdateTable1 <@ fun t -> { t with Value = 42 } @>, existsConditionTable1)
transaction.Put(table1, item2)
transaction.Put(table1, item3, doesntExistConditionTable1)
transaction.Delete(table1, table1.Template.ExtractKey item4, doesntExistConditionTable1)
transaction.Delete(table1, table1.Template.ExtractKey item5)
transaction.Check(
table1,
table1.Template.ExtractKey item6,
(if shouldFail then
existsConditionTable1
else
doesntExistConditionTable1)
)
transaction.Update(
table1,
TableKey.Combined(item7.HashKey, item7.RangeKey),
compileUpdateTable1 <@ fun t -> { t with Tuple = (42, 42) } @>
) ]
let mutable failed = false
try
do! transaction.TransactWriteItems()
with Transaction.TransactionCanceledConditionalCheckFailed ->
failed <- true
failed =! shouldFail
let! maybeItem = table1.TryGetItemAsync key
test <@ shouldFail <> (maybeItem |> Option.contains { item with Value = 42 }) @>
let! maybeItem2 = table1.TryGetItemAsync(table1.Template.ExtractKey item2)
test <@ shouldFail <> (maybeItem2 |> Option.contains item2) @>
let! maybeItem3 = table1.TryGetItemAsync(table1.Template.ExtractKey item3)
test <@ shouldFail <> (maybeItem3 |> Option.contains item3) @>
let! maybeItem7 = table1.TryGetItemAsync(table1.Template.ExtractKey item7)
test <@ shouldFail <> (maybeItem7 |> Option.map (fun x -> x.Tuple) |> Option.contains (42, 42)) @>
}
let shouldBeRejectedWithArgumentOutOfRangeException (builder: Transaction) = async {
let! e = Async.Catch(builder.TransactWriteItems())
test
<@
match e with
| Choice1Of2() -> false
| Choice2Of2 e -> e :? ArgumentOutOfRangeException
@>
}
[<Fact>]
let ``Empty request list is rejected with AORE`` () =
shouldBeRejectedWithArgumentOutOfRangeException (Transaction(table1.Client))
|> Async.RunSynchronously
[<Fact>]
let ``Over 100 writes are rejected with AORE`` () =
let Transaction = Transaction(table1.Client)
for _x in 1..101 do
Transaction.Put(table1, mkItem ()) |> ignore
shouldBeRejectedWithArgumentOutOfRangeException Transaction
interface IClassFixture<TableFixture>