From b395dae847efb70cfd627212a9b25eb43035b2c0 Mon Sep 17 00:00:00 2001 From: Malhar Khimsaria <96malhar@gmail.com> Date: Fri, 9 Aug 2024 12:41:54 -0700 Subject: [PATCH] Add unit tests --- .../MockabilityTests/TransactGetTests.cs | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 sdk/test/Services/DynamoDBv2/UnitTests/Custom/MockabilityTests/TransactGetTests.cs diff --git a/sdk/test/Services/DynamoDBv2/UnitTests/Custom/MockabilityTests/TransactGetTests.cs b/sdk/test/Services/DynamoDBv2/UnitTests/Custom/MockabilityTests/TransactGetTests.cs new file mode 100644 index 000000000000..1446e54bfbdf --- /dev/null +++ b/sdk/test/Services/DynamoDBv2/UnitTests/Custom/MockabilityTests/TransactGetTests.cs @@ -0,0 +1,117 @@ +using Amazon.DynamoDBv2.DataModel; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using System.Collections.Generic; + +namespace AWSSDK.UnitTests.DynamoDBv2.NetFramework.Custom.MockabilityTests +{ + [TestClass] + public class TransactGetTests + { + [TestMethod] + public void TestMockability_TransactGet() + { + var mockContext = new Mock(); + mockContext + .Setup(x => x.CreateTransactGet
()) + .Returns(CreateTransactGetMock(new List
() + { + { new Address() { State = "CA", Zip = "12345" }} + })); + + var ddbContext = mockContext.Object; + var addressTransactGet = ddbContext.CreateTransactGet
(); + + + Assert.AreEqual(0, addressTransactGet.Results.Count); + + addressTransactGet.Execute(); + Assert.AreEqual(1, addressTransactGet.Results.Count); + Assert.AreEqual("CA", addressTransactGet.Results[0].State); + Assert.AreEqual("12345", addressTransactGet.Results[0].Zip); + } + + [TestMethod] + public void TestMockability_MultiTableTransactGet() + { + var mockContext = new Mock(); + mockContext + .Setup(x => x.CreateMultiTableTransactGet()) + .Returns(CreateMultiTableTransactGetMock()); + + var ddbContext = mockContext.Object; + var multiTransactGet = ddbContext.CreateMultiTableTransactGet(); + + var addressTransactGet = CreateTransactGetMock(new List
+ { + { new Address() { State = "CA", Zip = "12345" }} + }); + + var personTransactGet = CreateTransactGetMock(new List + { + { new Person() { FirstName = "John", LastName = "Doe" }} + }); + + multiTransactGet.AddTransactionPart(addressTransactGet); + multiTransactGet.AddTransactionPart(personTransactGet); + + Assert.AreEqual(0, addressTransactGet.Results.Count); + Assert.AreEqual(0, personTransactGet.Results.Count); + + + multiTransactGet.Execute(); + Assert.AreEqual(1, addressTransactGet.Results.Count); + Assert.AreEqual("CA", addressTransactGet.Results[0].State); + Assert.AreEqual("12345", addressTransactGet.Results[0].Zip); + Assert.AreEqual(1, personTransactGet.Results.Count); + Assert.AreEqual("John", personTransactGet.Results[0].FirstName); + Assert.AreEqual("Doe", personTransactGet.Results[0].LastName); + } + + public ITransactGet CreateTransactGetMock(List results) + { + var transactGet = new Mock>(); + var dummyResults = new List(); + var keys = new List(); + + transactGet + .Setup(x => x.Execute()) + .Callback(() => + { + dummyResults.Clear(); + dummyResults.AddRange(results); + }); + + transactGet + .Setup(x => x.Results) + .Returns(dummyResults); + + return transactGet.Object; + } + + public IMultiTableTransactGet CreateMultiTableTransactGetMock() + { + var multiTransactGet = new Mock(); + var batches = new List(); + + multiTransactGet + .Setup(x => x.Execute()) + .Callback(() => + { + foreach (var batch in batches) + { + batch.Execute(); + } + }); + + multiTransactGet + .Setup(x => x.AddTransactionPart(It.IsAny())) + .Callback((ITransactGet batch) => + { + batches.Add(batch); + }); + + return multiTransactGet.Object; + } + } +}