/// <summary> /// A simple function that takes a string and does a ToUpper /// </summary> /// <param name="input"></param> /// <param name="context"></param> /// <returns></returns> public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context) { context.Logger.Log("Starting User Post call"); using (AwsFactory factory = new AwsFactory(context.Logger)) { using (PostWeight postWeight = new PostWeight(factory)) { string userId = request.PathParameters["userId"]; string firstName = request.PathParameters["firstName"]; string weightDateString = request.PathParameters["weightDate"]; string weightString = request.QueryStringParameters["weight"]; context.Logger.LogLine($"userId=\"{userId}\""); context.Logger.LogLine($"firstName=\"{firstName}\""); context.Logger.LogLine($"weightDate=\"{weightDateString}\""); context.Logger.LogLine($"weight=\"{weightString}\""); DateTime weightDate = Convert.ToDateTime(weightDateString); decimal weight = Convert.ToDecimal(weightString); string jsonResponse = postWeight.Save(userId, firstName, weightDate, weight); context.Logger.LogLine($"Response: {jsonResponse}"); APIGatewayProxyResponse response = new APIGatewayProxyResponse() { Body = jsonResponse, StatusCode = 200 }; return(response); } } }
public void TestSave() { WeighInWeight actualWeight = null; Mock <IDynamoDBContext> context = new Mock <IDynamoDBContext>(); context.Setup(C => C.SaveAsync <WeighInWeight>(It.IsAny <WeighInWeight>(), It.IsAny <CancellationToken>())).Returns((WeighInWeight weight, object t) => { actualWeight = weight; return(Task.CompletedTask); }); context.Setup(D => D.LoadAsync <WeighInUser>(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <CancellationToken>())).Returns((string a, string b, object c) => { WeighInUser user = new WeighInUser() { UserId = a, FirstName = b, UserKey = "UnitTestKey", StartingWeight = 88.8m, StartingWeightDate = new DateTime(2018, 7, 16) }; return(Task.FromResult <WeighInUser>(user)); }); TestAwsFactory factory = new TestAwsFactory() { DynamoDBContext = context.Object }; using (PostWeight postWeight = new PostWeight(factory)) { string weightOutputJson = postWeight.Save("UnitTestId", "Unit", DateTime.Today, 78.8m); Assert.That(actualWeight, Is.Not.Null, "actualUser"); Assert.That(actualWeight.UserKey, Is.Not.Null.And.Not.Empty, "actualUserKey"); Assert.That(actualWeight.Weight, Is.EqualTo(78.8m), "actualWeight"); Assert.That(actualWeight.WeightDate, Is.EqualTo(DateTime.Today), "actualWeightDate"); dynamic weight = JObject.Parse(weightOutputJson); Assert.That((string)weight.UserKey, Is.Not.Null.And.Not.Empty, "UserKey"); Assert.That((decimal)weight.Weight, Is.EqualTo(78.8m), "StartingWeight"); Assert.That((DateTime)weight.WeightDate, Is.EqualTo(DateTime.Today), "StartingWeightDate"); } }