-
Notifications
You must be signed in to change notification settings - Fork 881
Expand file tree
/
Copy pathDataModelTests.cs
More file actions
4954 lines (4275 loc) · 211 KB
/
DataModelTests.cs
File metadata and controls
4954 lines (4275 loc) · 211 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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DataModel;
using Amazon.DynamoDBv2.DocumentModel;
using Amazon.DynamoDBv2.Model;
using AWSSDK_DotNet.IntegrationTests.Utils;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AWSSDK_DotNet.IntegrationTests.Tests.DynamoDB
{
public partial class DynamoDBTests : TestBase<AmazonDynamoDBClient>
{
[TestMethod]
[TestCategory("DynamoDBv2")]
public async Task TestContextWithEmptyStringEnabled()
{
// It is a known bug that this test currently fails due to an AOT-compilation
// issue, on iOS using mono2x.
foreach (var conversion in new DynamoDBEntryConversion[]
{ DynamoDBEntryConversion.V1, DynamoDBEntryConversion.V2 })
{
TableCache.Clear();
// Cleanup existing data
await CleanupTables();
await TestCompositeHashRangeTable();
// Recreate context
bool isEmptyStringEnabled = true;
CreateContext(conversion, isEmptyStringEnabled);
await TestEmptyStringsWithFeatureEnabled();
await TestEnumHashKeyObjects();
await TestEmptyCollections(conversion);
await TestContextConversions();
await TestUnsupportedTypes();
TestEnums(conversion);
await TestHashObjects();
await TestHashRangeObjects<Employee>();
await TestOtherContextOperations();
await TestBatchOperations();
await TestTransactionOperations();
await TestMultiTableTransactionOperations();
await TestStoreAsEpoch();
}
}
private async Task TestCompositeHashRangeTable()
{
var entity1 = new CompositeHashRangeEntity
{
Id = 1,
Status = "active",
UserName = "alice",
Timestamp = 1000,
OrderId = "order-1",
Region = "us-west-2",
Category = "electronics",
Amount = 100,
Priority = 4
};
var entity11 = new CompositeHashRangeEntity
{
Id = 1,
Status = "pending",
UserName = "alice",
Timestamp = 1001,
OrderId = "order-1",
Region = "us-west-2",
Category = "electronics",
Amount = 100,
Priority = 1
};
var entity21 = new CompositeHashRangeEntity
{
Id = 21,
Status = "active",
UserName = "alice",
Timestamp = 1002,
OrderId = "order-1",
Region = "us-west-1",
Category = "electronics",
Amount = 100,
Priority = 2
};
var entity22 = new CompositeHashRangeEntity
{
Id = 21,
Status = "pending",
UserName = "alice",
Timestamp = 1003,
OrderId = "order-1",
Region = "us-west-2",
Category = "electronics",
Amount = 100,
Priority = 3
};
var entity31 = new CompositeHashRangeEntity
{
Id = 31,
Status = "shipped",
UserName = "bob",
Timestamp = 1004,
OrderId = "order-2",
Region = "us-east-1",
Category = "books",
Amount = 150,
Priority = 1
};
var entity32 = new CompositeHashRangeEntity
{
Id = 31,
Status = "delivered",
UserName = "bob",
Timestamp = 1005,
OrderId = "order-2",
Region = "us-east-1",
Category = "books",
Amount = 150,
Priority = 2
};
var entity41 = new CompositeHashRangeEntity
{
Id = 41,
Status = "active",
UserName = "charlie",
Timestamp = 1006,
OrderId = "order-3",
Region = "us-central",
Category = "clothing",
Amount = 75,
Priority = 3
};
var entity42 = new CompositeHashRangeEntity
{
Id = 41,
Status = "completed",
UserName = "charlie",
Timestamp = 1007,
OrderId = "order-3",
Region = "us-central",
Category = "clothing",
Amount = 75,
Priority = 4
};
await Context.SaveAsync(entity1);
await Context.SaveAsync(entity11);
await Context.SaveAsync(entity21);
await Context.SaveAsync(entity22);
await Context.SaveAsync(entity31);
await Context.SaveAsync(entity32);
await Context.SaveAsync(entity41);
await Context.SaveAsync(entity42);
// Query GSI1 with single hash key
var queryConditional1 = QueryConditional.HashKeyEqualTo("UserName", "bob");
var results1 = await Context.QueryAsync<CompositeHashRangeEntity>(queryConditional1, new QueryConfig
{
IndexName = "GSI1",
QueryFilter = new List<ScanCondition>
{
new ScanCondition("OrderId", ScanOperator.Equal, "order-2")
}
}).GetNextSetAsync();
Assert.AreEqual(2, results1.Count);
Assert.IsTrue(results1.All(r => r.UserName == "bob"));
// Query GSI1: composite-range behavior — Timestamp > 1000 (should return 1001,1002,1003)
var queryGsi1Range = QueryConditional.HashKeyEqualTo("UserName", "alice")
.AndRangeKeyGreaterThan("Timestamp", 1000);
var gsi1RangeResults = await Context.QueryAsync<CompositeHashRangeEntity>(queryGsi1Range, new QueryConfig { IndexName = "GSI1" }).GetNextSetAsync();
Assert.AreEqual(3, gsi1RangeResults.Count);
CollectionAssert.AreEqual(new[] { 1001, 1002, 1003 }, gsi1RangeResults.Select(r => r.Timestamp).ToArray());
// Query GSI1 with descending order
var gsi1DescResults = await Context.QueryAsync<CompositeHashRangeEntity>(queryGsi1Range, new QueryConfig { IndexName = "GSI1", BackwardQuery = true }).GetNextSetAsync();
Assert.AreEqual(3, gsi1DescResults.Count);
Assert.AreEqual(1003, gsi1DescResults.First().Timestamp);
// Query GSI2 with all hash keys (UserName + OrderId)
var queryConditional2 = QueryConditional.HashKeyEqualTo("UserName", "bob").AndHashKeyEqualTo("OrderId", "order-2");
var results2 = await Context.QueryAsync<CompositeHashRangeEntity>(queryConditional2,
new QueryConfig
{
IndexName = "GSI2"
})
.GetNextSetAsync();
Assert.AreEqual(2, results2.Count);
Assert.IsTrue(results2.All(r => r.UserName == "bob" && r.OrderId == "order-2"));
// Query GSI2 with all hash keys (UserName + OrderId) and QueryFilter
var results21 = await Context.QueryAsync<CompositeHashRangeEntity>(queryConditional2,
new QueryConfig
{
IndexName = "GSI2",
QueryFilter = new List<ScanCondition>
{
new ScanCondition("Timestamp", ScanOperator.GreaterThan, 1004)
}
}).GetNextSetAsync();
Assert.AreEqual(1, results21.Count);
Assert.IsTrue(results21.All(r => r.UserName == "bob" && r.OrderId == "order-2"));
// Query GSI2 with all hash keys and first range key
var queryConditional2Range = QueryConditional.HashKeyEqualTo("UserName", "bob").AndHashKeyEqualTo("OrderId", "order-2").AndRangeKeyGreaterThan("Timestamp", 1004);
var results2Range = await Context.QueryAsync<CompositeHashRangeEntity>(queryConditional2Range, new QueryConfig { IndexName = "GSI2" }).GetNextSetAsync();
Assert.AreEqual(1, results2Range.Count);
Assert.IsTrue(results2Range.All(r => r.UserName == "bob" && r.OrderId == "order-2" && r.Timestamp > 1004));
// Query GSI2 with hash keys and Timestamp < 1003 (for alice/order-1 -> should return 1000,1001,1002)
var queryGsi2Alice = QueryConditional.HashKeyEqualTo("UserName", "alice").AndHashKeyEqualTo("OrderId", "order-1").AndRangeKeyLessThan("Timestamp", 1003);
var gsi2AliceResults = await Context.QueryAsync<CompositeHashRangeEntity>(queryGsi2Alice, new QueryConfig { IndexName = "GSI2" }).GetNextSetAsync();
Assert.AreEqual(3, gsi2AliceResults.Count);
CollectionAssert.AreEqual(new[] { 1000, 1001, 1002 }, gsi2AliceResults.Select(r => r.Timestamp).ToArray());
// Query GSI3 with all hash keys
var queryConditional3 = QueryConditional.HashKeyEqualTo("UserName", "bob").AndHashKeyEqualTo("Region", "us-east-1");
var results3 = await Context.QueryAsync<CompositeHashRangeEntity>(queryConditional3, new QueryConfig { IndexName = "GSI3" }).GetNextSetAsync();
Assert.AreEqual(2, results3.Count);
Assert.IsTrue(results3.All(r => r.UserName == "bob" && r.Region == "us-east-1"));
// Query GSI3 with all hash keys and first range key (Status equal)
var queryConditional3Range = QueryConditional.HashKeyEqualTo("UserName", "bob").AndHashKeyEqualTo("Region", "us-east-1").AndRangeKeyEqualTo("Status", "shipped");
var results3Range = await Context.QueryAsync<CompositeHashRangeEntity>(queryConditional3Range, new QueryConfig { IndexName = "GSI3" }).GetNextSetAsync();
Assert.AreEqual(1, results3Range.Count);
Assert.IsTrue(results3Range.All(r => r.UserName == "bob" && r.Region == "us-east-1" && r.Status == "shipped"));
// Query GSI4 with all hash keys
var queryConditional4 = QueryConditional.HashKeyEqualTo("Id", 41).AndHashKeyEqualTo("UserName", "charlie").AndHashKeyEqualTo("OrderId", "order-3").AndHashKeyEqualTo("Region", "us-central");
var results4 = await Context.QueryAsync<CompositeHashRangeEntity>(queryConditional4, new QueryConfig { IndexName = "GSI4" }).GetNextSetAsync();
Assert.AreEqual(2, results4.Count);
Assert.IsTrue(results4.All(r => r.Id == 41 && r.UserName == "charlie" && r.OrderId == "order-3" && r.Region == "us-central"));
// Query GSI4 with all hash keys and first range key (Status equal)
var queryConditional4Range = QueryConditional.HashKeyEqualTo("Id", 41).AndHashKeyEqualTo("UserName", "charlie").AndHashKeyEqualTo("OrderId", "order-3")
.AndHashKeyEqualTo("Region", "us-central").AndRangeKeyEqualTo("Status", "active");
var results4Range = await Context.QueryAsync<CompositeHashRangeEntity>(queryConditional4Range, new QueryConfig { IndexName = "GSI4" }).GetNextSetAsync();
Assert.AreEqual(1, results4Range.Count);
Assert.IsTrue(results4Range.All(r => r.Id == 41 && r.UserName == "charlie" && r.OrderId == "order-3" && r.Region == "us-central" && r.Status == "active"));
// Additional composite-range check on GSI4: Priority > 2 should return both items for Id=41/charlie/order-3/region
var queryGsi4Priority = QueryConditional.HashKeyEqualTo("Id", 41)
.AndHashKeyEqualTo("UserName", "charlie")
.AndHashKeyEqualTo("OrderId", "order-3")
.AndHashKeyEqualTo("Region", "us-central")
.AndRangeKeyEqualTo("Status", "active")
.AndRangeKeyEqualTo("Category", "clothing")
.AndRangeKeyGreaterThan("Amount", 200);
var gsi4PriorityResults = await Context.QueryAsync<CompositeHashRangeEntity>(queryGsi4Priority, new QueryConfig { IndexName = "GSI4" }).GetNextSetAsync();
Assert.AreEqual(0, gsi4PriorityResults.Count);
}
/// <summary>
/// Tests regression reported in https://github.com/aws/aws-sdk-net/issues/4243 is resolved.
/// </summary>
[TestMethod]
[TestCategory("DynamoDBv2")]
public async Task TestTransactWrite_SkipVersionCheck()
{
TableCache.Clear();
await CleanupTables();
TableCache.Clear();
CreateContext(DynamoDBEntryConversion.V2, true, true);
var vp = new VersionedProduct
{
Id = 1000,
Name = "TestProduct",
Price = 100
};
await Context.SaveAsync(vp);
// Load to get initial version
vp = await Context.LoadAsync<VersionedProduct>(vp.Id);
Assert.AreEqual(0, vp.Version);
// Set wrong version - normally would fail with a ConditionalCheck error
vp.Version = 9999;
vp.Price = 200;
// With SkipVersionCheck = true, should succeed
var transactWrite = Context.CreateTransactWrite<VersionedProduct>(new TransactWriteConfig
{
SkipVersionCheck = true
});
transactWrite.AddSaveItem(vp);
await transactWrite.ExecuteAsync();
// Verify update succeeded
var updated = await Context.LoadAsync<VersionedProduct>(vp.Id);
Assert.AreEqual(200, updated.Price);
Assert.AreEqual(9999, updated.Version);
}
[TestMethod]
[TestCategory("DynamoDBv2")]
public async Task TestContextWithEmptyStringDisabled()
{
TableCache.Clear();
// Cleanup existing data
await CleanupTables();
// Recreate context
bool isEmptyStringEnabled = false;
CreateContext(DynamoDBEntryConversion.V2, isEmptyStringEnabled);
await TestEmptyStringsWithFeatureDisabled();
}
/// <summary>
/// Tests that the DynamoDB operations can be invoked successfully based on the table info
/// supplied only via class attributes.
/// </summary>
[TestMethod]
[TestCategory("DynamoDBv2")]
public async Task TestContext_DisableFetchingTableMetadata_WithFullAttributes()
{
TableCache.Clear();
await CleanupTables();
TableCache.Clear();
CreateContext(DynamoDBEntryConversion.V2, true, true);
await TestHashRangeObjects<AnnotatedEmployee>();
}
/// <summary>
/// Tests that the DynamoDB operations can be invoked successfully based on the table info
/// supplied via a combination of class attributes and the app config.
/// </summary>
[TestMethod]
[TestCategory("DynamoDBv2")]
public async Task TestContext_DisableFetchingTableMetadata_WithPartialAttributes()
{
TableCache.Clear();
await CleanupTables();
TableCache.Clear();
CreateContext(DynamoDBEntryConversion.V2, true, true);
await TestHashRangeObjects<PartiallyAnnotatedEmployee>();
}
/// <summary>
/// Tests that the DynamoDB operations can be invoked successfully based on the table info
/// supplied via attributes on the parent class.
/// </summary>
[TestMethod]
[TestCategory("DynamoDBv2")]
public async Task TestContext_DisableFetchingTableMetadata_WithNonAnnotatedChildClass()
{
TableCache.Clear();
await CleanupTables();
TableCache.Clear();
CreateContext(DynamoDBEntryConversion.V2, true, true);
await TestHashRangeObjects<EmployeeChild>();
}
/// <summary>
/// Tests that the DynamoDB operations can be invoked successfully based on a Datetime attribute as the hash key that is stored as epoch.
/// </summary>
[TestMethod]
[TestCategory("DynamoDBv2")]
public async Task TestContext_DisableFetchingTableMetadata_DateTimeAsHashKey()
{
TableCache.Clear();
await CleanupTables();
TableCache.Clear();
CreateContext(DynamoDBEntryConversion.V2, true, true);
var employee = new AnnotatedNumericEpochEmployee
{
Name = "Bob",
Age = 45,
CreationTime = EpochDate,
EpochDate2 = EpochDate,
NonEpochDate1 = EpochDate,
NonEpochDate2 = EpochDate,
NullableEpochDate1 = null,
NullableEpochDate2 = EpochDate,
LongEpochDate1 = LongEpochDate,
LongEpochDate2 = LongEpochDate.AddDays(12),
NullableLongEpochDate1 = null,
NullableLongEpochDate2 = LongEpochDate.AddDays(50)
};
await Context.SaveAsync(employee);
var storedEmployee = await Context.LoadAsync<AnnotatedNumericEpochEmployee>(employee.CreationTime, employee.Name);
Assert.IsNotNull(storedEmployee);
ApproximatelyEqual(EpochDate, storedEmployee.CreationTime);
ApproximatelyEqual(EpochDate, storedEmployee.EpochDate2);
ApproximatelyEqual(EpochDate, storedEmployee.NonEpochDate1);
ApproximatelyEqual(EpochDate, storedEmployee.NonEpochDate2);
Assert.IsNull(storedEmployee.NullableEpochDate1);
ApproximatelyEqual(EpochDate, storedEmployee.NullableEpochDate2.Value);
ApproximatelyEqual(LongEpochDate, storedEmployee.LongEpochDate1);
ApproximatelyEqual(LongEpochDate.AddDays(12), storedEmployee.LongEpochDate2);
Assert.IsNull(storedEmployee.NullableLongEpochDate1);
ApproximatelyEqual(LongEpochDate.AddDays(50), storedEmployee.NullableLongEpochDate2.Value);
Assert.AreEqual(employee.Name, storedEmployee.Name);
Assert.AreEqual(employee.Age, storedEmployee.Age);
}
/// <summary>
/// Tests that disabling fetching table metadata works with a key that has a property converter.
/// </summary>
[TestMethod]
[TestCategory("DynamoDBv2")]
public async Task TestContext_DisableFetchingTableMetadata_KeyWithPropertyConverter()
{
TableCache.Clear();
await CleanupTables();
TableCache.Clear();
CreateContext(DynamoDBEntryConversion.V2, true, true);
var employee = new PropertyConverterEmployee
{
Name = Status.Active,
CreationTime = EpochDate,
};
await Context.SaveAsync(employee);
var storedEmployee = await Context.LoadAsync<PropertyConverterEmployee>(employee.CreationTime, employee.Name);
Assert.IsNotNull(storedEmployee);
ApproximatelyEqual(EpochDate, storedEmployee.CreationTime);
Assert.AreEqual(employee.Name, storedEmployee.Name);
}
/// <summary>
/// Tests that disabling fetching table metadata works with a key that has a property converter.
/// </summary>
[TestMethod]
[TestCategory("DynamoDBv2")]
public async Task TestTransactWrite_AddSaveItem_DocumentTransaction()
{
TableCache.Clear();
await CleanupTables();
TableCache.Clear();
CreateContext(DynamoDBEntryConversion.V2, true, true);
{
var hashRangeOnly = new AnnotatedRangeTable
{
Name = "Bob",
Age = 10
};
var transactWrite = Context.CreateTransactWrite<AnnotatedRangeTable>();
transactWrite.AddSaveItem(hashRangeOnly);
await transactWrite.ExecuteAsync();
var storedHashOnly = await Context.LoadAsync<AnnotatedRangeTable>(hashRangeOnly.Name, hashRangeOnly.Age);
Assert.IsNotNull(storedHashOnly);
Assert.AreEqual(hashRangeOnly.Name, storedHashOnly.Name);
}
{
var hashRangeOnly = new IgnoreAnnotatedRangeTable
{
Name = "Bob",
Age = 10,
IgnoreAttribute = 100
};
var transactWrite = Context.CreateTransactWrite<IgnoreAnnotatedRangeTable>();
transactWrite.AddSaveItem(hashRangeOnly);
await transactWrite.ExecuteAsync();
var storedHashOnly = await Context.LoadAsync<IgnoreAnnotatedRangeTable>(hashRangeOnly.Name, hashRangeOnly.Age);
Assert.IsNotNull(storedHashOnly);
Assert.AreEqual(hashRangeOnly.Name, storedHashOnly.Name);
Assert.AreEqual(hashRangeOnly.Age, storedHashOnly.Age);
}
{
var hashRangeOnly = new AnnotatedRangeTable2
{
Name = "Bob",
Age = 10,
NotAnnotatedAttribute = 100
};
var transactWrite = Context.CreateTransactWrite<AnnotatedRangeTable2>();
transactWrite.AddSaveItem(hashRangeOnly);
await transactWrite.ExecuteAsync();
var storedHashOnly = await Context.LoadAsync<AnnotatedRangeTable2>(hashRangeOnly.Name, hashRangeOnly.Age);
Assert.IsNotNull(storedHashOnly);
Assert.AreEqual(hashRangeOnly.Name, storedHashOnly.Name);
Assert.AreEqual(hashRangeOnly.Age, storedHashOnly.Age);
Assert.AreEqual(hashRangeOnly.NotAnnotatedAttribute, storedHashOnly.NotAnnotatedAttribute);
}
}
/// <summary>
/// Tests that the DynamoDB operations can retrieve <see cref="DateTime"/> attributes in UTC and local timezone.
/// </summary>
[TestMethod]
[TestCategory("DynamoDBv2")]
[DataRow(true)]
[DataRow(false)]
public async Task TestContext_RetrieveDateTimeInUtc(bool retrieveDateTimeInUtc)
{
TableCache.Clear();
await CleanupTables();
TableCache.Clear();
var config = new DynamoDBContextConfig
{
Conversion = DynamoDBEntryConversion.V2,
RetrieveDateTimeInUtc = retrieveDateTimeInUtc
};
Context = new DynamoDBContext(Client, config);
var currTime = DateTime.UtcNow;
var longEpochTime = new DateTime(2039, 2, 5, 17, 49, 55, DateTimeKind.Utc);
var longEpochTimeBefore1970 = new DateTime(1969, 12, 30, 23, 59, 59, DateTimeKind.Utc);
var employee = new AnnotatedNumericEpochEmployee
{
Name = "Bob",
Age = 45,
CreationTime = currTime,
EpochDate2 = currTime,
NonEpochDate1 = currTime,
NonEpochDate2 = currTime,
NullableEpochDate1 = null,
NullableEpochDate2 = currTime,
LongEpochDate1 = longEpochTime,
LongEpochDate2 = longEpochTimeBefore1970,
NullableLongEpochDate1 = longEpochTime,
NullableLongEpochDate2 = null
};
await Context.SaveAsync(employee);
//This is a valid use of .ToLocalTime
var expectedCurrTime = retrieveDateTimeInUtc ? currTime.ToUniversalTime() : currTime.ToLocalTime();
var expectedLongEpochTime =
retrieveDateTimeInUtc ? longEpochTime.ToUniversalTime() : longEpochTime.ToLocalTime();
var expectedLongEpochTimeBefore1970 = retrieveDateTimeInUtc
? longEpochTimeBefore1970.ToUniversalTime()
: longEpochTimeBefore1970.ToLocalTime();
// Load
var storedEmployee = Context.Load<AnnotatedNumericEpochEmployee>(employee.CreationTime, employee.Name);
Assert.IsNotNull(storedEmployee);
ApproximatelyEqual(expectedCurrTime, storedEmployee.CreationTime);
ApproximatelyEqual(expectedCurrTime, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate2);
Assert.IsNull(storedEmployee.NullableEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NullableEpochDate2.Value);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate1);
ApproximatelyEqual(expectedLongEpochTimeBefore1970, storedEmployee.LongEpochDate2);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.NullableLongEpochDate1.Value);
Assert.IsNull(storedEmployee.NullableLongEpochDate2);
Assert.AreEqual(employee.Name, storedEmployee.Name);
Assert.AreEqual(employee.Age, storedEmployee.Age);
// Query
QueryFilter filter = new QueryFilter();
filter.AddCondition("CreationTime", QueryOperator.Equal, currTime);
storedEmployee = (await Context.FromQueryAsync<AnnotatedNumericEpochEmployee>(
new QueryOperationConfig { Filter = filter }
).GetNextSetAsync()).First();
Assert.IsNotNull(storedEmployee);
ApproximatelyEqual(expectedCurrTime, storedEmployee.CreationTime);
ApproximatelyEqual(expectedCurrTime, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate2);
Assert.IsNull(storedEmployee.NullableEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NullableEpochDate2.Value);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate1);
ApproximatelyEqual(expectedLongEpochTimeBefore1970, storedEmployee.LongEpochDate2);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.NullableLongEpochDate1.Value);
Assert.IsNull(storedEmployee.NullableLongEpochDate2);
Assert.AreEqual(employee.Name, storedEmployee.Name);
Assert.AreEqual(employee.Age, storedEmployee.Age);
// Scan
storedEmployee = (await Context.ScanAsync<AnnotatedNumericEpochEmployee>(new List<ScanCondition>()).GetRemainingAsync()).First();
Assert.IsNotNull(storedEmployee);
ApproximatelyEqual(expectedCurrTime, storedEmployee.CreationTime);
ApproximatelyEqual(expectedCurrTime, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate2);
Assert.IsNull(storedEmployee.NullableEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NullableEpochDate2.Value);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate1);
ApproximatelyEqual(expectedLongEpochTimeBefore1970, storedEmployee.LongEpochDate2);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.NullableLongEpochDate1.Value);
Assert.IsNull(storedEmployee.NullableLongEpochDate2);
Assert.AreEqual(employee.Name, storedEmployee.Name);
Assert.AreEqual(employee.Age, storedEmployee.Age);
}
/// <summary>
/// Tests that if a custom <see cref="DateTime"/> converter is used, then the <see cref="DynamoDBContextConfig.RetrieveDateTimeInUtc"/> is ignored.
/// </summary>
/// <param name="retrieveDateTimeInUtc"></param>
[TestMethod]
[TestCategory("DynamoDBv2")]
[DataRow(true)]
[DataRow(false)]
public async Task TestContext_CustomDateTimeConverter(bool retrieveDateTimeInUtc)
{
TableCache.Clear();
await CleanupTables();
TableCache.Clear();
var config = new DynamoDBContextConfig
{
Conversion = DynamoDBEntryConversion.V2,
RetrieveDateTimeInUtc = retrieveDateTimeInUtc
};
Context = new DynamoDBContext(Client, config);
// Add a custom DateTime converter
Context.ConverterCache.Add(typeof(DateTime), new DateTimeUtcConverter());
Context.ConverterCache.Add(typeof(DateTime?), new DateTimeUtcConverter());
var currTime = DateTime.UtcNow;
var longEpochTime = new DateTime(2039, 2, 5, 17, 49, 55, DateTimeKind.Utc);
var longEpochTimeBefore1970 = new DateTime(1950, 12, 30, 19, 43, 30, DateTimeKind.Utc);
var employee = new AnnotatedNumericEpochEmployee
{
Name = "Bob",
Age = 45,
CreationTime = currTime,
EpochDate2 = currTime,
NonEpochDate1 = currTime,
NonEpochDate2 = currTime,
NullableEpochDate1 = null,
NullableEpochDate2 = currTime,
LongEpochDate1 = longEpochTime,
LongEpochDate2 = longEpochTimeBefore1970,
NullableLongEpochDate1 = longEpochTime,
};
await Context.SaveAsync(employee);
// Since we are adding a custom DateTimeUtcConverter, the expected time will be in the UTC time zone regardless of RetrieveDateTimeInUtc value.
// for the properties that does not have property specific attributes like [DynamoDBProperty(StoreAsEpochLong = true)].
var expectedCurrTime = currTime.ToUniversalTime();
var expectedCurrTimeStoreAsEpoch = retrieveDateTimeInUtc ? currTime.ToUniversalTime() : currTime.ToLocalTime();
var expectedLongEpochTime = retrieveDateTimeInUtc ? longEpochTime.ToUniversalTime() : longEpochTime.ToLocalTime();
var expectedLongEpochTimeBefore1970 = retrieveDateTimeInUtc ? longEpochTimeBefore1970.ToUniversalTime() : longEpochTimeBefore1970.ToLocalTime();
// Load
var storedEmployee = Context.Load<AnnotatedNumericEpochEmployee>(employee.CreationTime, employee.Name);
Assert.IsNotNull(storedEmployee);
ApproximatelyEqual(expectedCurrTimeStoreAsEpoch, storedEmployee.CreationTime);
ApproximatelyEqual(expectedCurrTimeStoreAsEpoch, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate2);
Assert.IsNull(storedEmployee.NullableEpochDate1);
ApproximatelyEqual(expectedCurrTimeStoreAsEpoch, storedEmployee.NullableEpochDate2.Value);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate1);
ApproximatelyEqual(expectedLongEpochTimeBefore1970, storedEmployee.LongEpochDate2);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.NullableLongEpochDate1.Value);
Assert.IsNull(storedEmployee.NullableLongEpochDate2);
Assert.AreEqual(employee.Name, storedEmployee.Name);
Assert.AreEqual(employee.Age, storedEmployee.Age);
// Query
QueryFilter filter = new QueryFilter();
filter.AddCondition("CreationTime", QueryOperator.Equal, currTime);
storedEmployee = (await Context.FromQueryAsync<AnnotatedNumericEpochEmployee>(
new QueryOperationConfig { Filter = filter }
).GetNextSetAsync()).First();
Assert.IsNotNull(storedEmployee);
ApproximatelyEqual(expectedCurrTimeStoreAsEpoch, storedEmployee.CreationTime);
ApproximatelyEqual(expectedCurrTimeStoreAsEpoch, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate2);
Assert.IsNull(storedEmployee.NullableEpochDate1);
ApproximatelyEqual(expectedCurrTimeStoreAsEpoch, storedEmployee.NullableEpochDate2.Value);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate1);
ApproximatelyEqual(expectedLongEpochTimeBefore1970, storedEmployee.LongEpochDate2);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.NullableLongEpochDate1.Value);
Assert.IsNull(storedEmployee.NullableLongEpochDate2);
Assert.AreEqual(employee.Name, storedEmployee.Name);
Assert.AreEqual(employee.Age, storedEmployee.Age);
// Scan
storedEmployee = (await Context.ScanAsync<AnnotatedNumericEpochEmployee>(new List<ScanCondition>()).GetRemainingAsync()).First();
Assert.IsNotNull(storedEmployee);
ApproximatelyEqual(expectedCurrTimeStoreAsEpoch, storedEmployee.CreationTime);
ApproximatelyEqual(expectedCurrTimeStoreAsEpoch, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate2);
Assert.IsNull(storedEmployee.NullableEpochDate1);
ApproximatelyEqual(expectedCurrTimeStoreAsEpoch, storedEmployee.NullableEpochDate2.Value);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate1);
ApproximatelyEqual(expectedLongEpochTimeBefore1970, storedEmployee.LongEpochDate2);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.NullableLongEpochDate1.Value);
Assert.IsNull(storedEmployee.NullableLongEpochDate2);
Assert.AreEqual(employee.Name, storedEmployee.Name);
Assert.AreEqual(employee.Age, storedEmployee.Age);
}
/// <summary>
/// Tests that the DynamoDB operations can retrieve <see cref="DateTime"/> attributes in UTC and local timezone using the <see cref="DynamoDBOperationConfig"/>
/// </summary>
[TestMethod]
[TestCategory("DynamoDBv2")]
[DataRow(true)]
[DataRow(false)]
public async Task TestContext_RetrieveDateTimeInUtc_OperationConfig(bool retrieveDateTimeInUtc)
{
TableCache.Clear();
await CleanupTables();
TableCache.Clear();
Context = new DynamoDBContext(Client,
new DynamoDBContextConfig { Conversion = DynamoDBEntryConversion.V2 });
var operationConfig = new DynamoDBOperationConfig { RetrieveDateTimeInUtc = retrieveDateTimeInUtc };
var currTime = DateTime.UtcNow;
var longEpochTime = new DateTime(2039, 7, 23, 2, 3, 4, DateTimeKind.Utc);
var longEpochTimeBefore1970 = new DateTime(1955, 12, 30, 23, 59, 59, DateTimeKind.Utc);
var employee = new AnnotatedNumericEpochEmployee
{
Name = "Bob",
Age = 45,
CreationTime = currTime,
EpochDate2 = currTime,
NonEpochDate1 = currTime,
NonEpochDate2 = currTime,
LongEpochDate1 = longEpochTime,
LongEpochDate2 = longEpochTimeBefore1970
};
await Context.SaveAsync(employee);
//This is a valid use of .ToLocalTime
var expectedCurrTime = retrieveDateTimeInUtc ? currTime.ToUniversalTime() : currTime.ToLocalTime();
var expectedLongEpochTime =
retrieveDateTimeInUtc ? longEpochTime.ToUniversalTime() : longEpochTime.ToLocalTime();
var expectedLongEpochTimeBefore1970 = retrieveDateTimeInUtc
? longEpochTimeBefore1970.ToUniversalTime()
: longEpochTimeBefore1970.ToLocalTime();
// Load
var storedEmployee = Context.Load<AnnotatedNumericEpochEmployee>(employee.CreationTime, employee.Name,
new LoadConfig { RetrieveDateTimeInUtc = retrieveDateTimeInUtc });
Assert.IsNotNull(storedEmployee);
ApproximatelyEqual(expectedCurrTime, storedEmployee.CreationTime);
ApproximatelyEqual(expectedCurrTime, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate2);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate1);
ApproximatelyEqual(expectedLongEpochTimeBefore1970, storedEmployee.LongEpochDate2);
Assert.AreEqual(employee.Name, storedEmployee.Name);
Assert.AreEqual(employee.Age, storedEmployee.Age);
// Query
QueryFilter filter = new QueryFilter();
filter.AddCondition("CreationTime", QueryOperator.Equal, currTime);
storedEmployee = (await Context.FromQueryAsync<AnnotatedNumericEpochEmployee>(
new QueryOperationConfig { Filter = filter },
new FromQueryConfig { RetrieveDateTimeInUtc = retrieveDateTimeInUtc }
)
.GetNextSetAsync())
.First();
Assert.IsNotNull(storedEmployee);
ApproximatelyEqual(expectedCurrTime, storedEmployee.CreationTime);
ApproximatelyEqual(expectedCurrTime, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate2);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate1);
ApproximatelyEqual(expectedLongEpochTimeBefore1970, storedEmployee.LongEpochDate2);
Assert.AreEqual(employee.Name, storedEmployee.Name);
Assert.AreEqual(employee.Age, storedEmployee.Age);
// Scan
storedEmployee = (await Context.ScanAsync<AnnotatedNumericEpochEmployee>(
new List<ScanCondition>(),
new ScanConfig { RetrieveDateTimeInUtc = retrieveDateTimeInUtc })
.GetRemainingAsync())
.First();
Assert.IsNotNull(storedEmployee);
ApproximatelyEqual(expectedCurrTime, storedEmployee.CreationTime);
ApproximatelyEqual(expectedCurrTime, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate2);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate1);
ApproximatelyEqual(expectedLongEpochTimeBefore1970, storedEmployee.LongEpochDate2);
Assert.AreEqual(employee.Name, storedEmployee.Name);
Assert.AreEqual(employee.Age, storedEmployee.Age);
}
[TestMethod]
[TestCategory("DynamoDBv2")]
public async Task TestContext_ScanWithExpression_NestedPaths()
{
TableCache.Clear();
await CleanupTables();
TableCache.Clear();
var product1 = new Product
{
Id = 1,
Name = "Widget",
CompanyInfo = new CompanyInfo
{
Name = "Acme",
Founded = new DateTime(2000, 1, 1),
AllProducts = new List<Product>
{
new Product { Id = 2, Name = "Gadget" }
},
FeaturedBrands = new[] { "Acme", "Contoso" }
},
Price = 100
};
var product2 = new Product
{
Id = 3,
Name = "Thing",
CompanyInfo = new CompanyInfo
{
Name = "Contoso",
Founded = new DateTime(2010, 5, 5),
AllProducts = new List<Product>
{
new Product { Id = 4, Name = "Device" }
},
FeaturedBrands = new[] { "Contoso" }
},
Price = 200
};
var product3 = new Product
{
Id = 5,
Name = "CloudSpotter",
CompanyInfo = new CompanyInfo
{
Name = "Contoso",
Founded = new DateTime(2010, 5, 5),
AllProducts = new List<Product>
{
new Product
{
Id = 6, Name = "Service", Components = new List<string>
{
"Code",
"Storage",
"Network"
}
}
},
CompetitorProducts = new Dictionary<string, List<Product>>()
{
{
"CloudsAreOK", new List<Product>()
{
new Product()
{
Id = 8, Name = "CloudSpotter RipOff"
}
}
}
},
FeaturedBrands = new[] { "Contoso" }
},
Price = 200
};
await Context.SaveAsync(product1);
await Context.SaveAsync(product2);
await Context.SaveAsync(product3);
// 1. Filter on a nested property (CompanyInfo.Name)
var expr1 = new ContextExpression();
expr1.SetFilter<Product>(p => p.CompanyInfo.Name == "Acme");
var byCompanyName = await Context.ScanAsync<Product>(expr1).GetRemainingAsync();
Assert.AreEqual(1, byCompanyName.Count);
Assert.AreEqual("Widget", byCompanyName[0].Name);
// 2. Filter on a nested array property (FeaturedBrands contains "Acme")
var expr2 = new ContextExpression();
expr2.SetFilter<Product>(p => p.CompanyInfo.FeaturedBrands.Contains("Acme"));
var byFeaturedBrand = await Context.ScanAsync<Product>(expr2).GetRemainingAsync();
Assert.AreEqual(1, byFeaturedBrand.Count);
Assert.AreEqual("Widget", byFeaturedBrand[0].Name);
// 3. Filter on a double-nested property
var expr3 = new ContextExpression();
expr3.SetFilter<Product>(p => p.CompanyInfo.AllProducts.First().Name == "Device");
var byDoubleNested = await Context.ScanAsync<Product>(expr3).GetRemainingAsync();
Assert.AreEqual(1, byDoubleNested.Count);
Assert.AreEqual("Thing", byDoubleNested[0].Name);
var expr4 = new ContextExpression();
expr4.SetFilter<Product>(p => p.CompanyInfo.AllProducts[0].Name == "Device");
var byDoubleNested1 = await Context.ScanAsync<Product>(expr4).GetRemainingAsync();
Assert.AreEqual(1, byDoubleNested1.Count);
Assert.AreEqual("Thing", byDoubleNested1[0].Name);
// 4. Filter on a value inside a dictionary of lists
var expr5 = new ContextExpression();
expr5.SetFilter<Product>(p => p.CompanyInfo.CompetitorProducts["CloudsAreOK"][0].Name == "CloudSpotter RipOff");
var byDictionaryNested = await Context.ScanAsync<Product>(expr5).GetRemainingAsync();
Assert.AreEqual(1, byDictionaryNested.Count);
Assert.AreEqual("CloudSpotter", byDictionaryNested[0].Name);
}
[TestMethod]
[TestCategory("DynamoDBv2")]
public async Task TestContext_ScanConfigFilter()
{
TableCache.Clear();
await CleanupTables();
TableCache.Clear();
var employee = new Employee()
{
Name = "Bob",
Age = 45,
CurrentStatus = Status.Active,
CompanyName = "test",
};
var employee3 = new Employee
{
Name = "Cob",
Age = 45,
CurrentStatus = Status.Inactive,
CompanyName = "test1",
};
await Context.SaveAsync(employee);
await Context.SaveAsync(employee3);
var ageEqResultScan = await Context.ScanAsync<Employee>(new List<ScanCondition>(), new ScanConfig()
{
QueryFilter = new List<ScanCondition>()
{
new ScanCondition("Age", ScanOperator.GreaterThan,50)
},
ConditionalOperator = ConditionalOperatorValues.And
}).GetRemainingAsync();
Assert.AreEqual(0, ageEqResultScan.Count);
var ageAndCompanyResultScan = await Context.ScanAsync<Employee>(
new List<ScanCondition>()
{
new ScanCondition("Age", ScanOperator.Equal,45)
},
new ScanConfig()
{
QueryFilter = new List<ScanCondition>()
{
new ScanCondition("CompanyName", ScanOperator.Equal, "test")
},
ConditionalOperator = ConditionalOperatorValues.And
})
.GetRemainingAsync();
Assert.AreEqual(1, ageAndCompanyResultScan.Count);
}
[TestMethod]
[TestCategory("DynamoDBv2")]
public async Task TestContext_Scan_WithExpressionFilter()
{
TableCache.Clear();
await CleanupTables();
TableCache.Clear();
var employee = new Employee()
{
Name = "Bob",
Age = 45,
CurrentStatus = Status.Active,