public async Task TestPredictionAsync(string customTextKey, string endpointUrl, string appId, IHttpHandler httpHandler, string inputText, CliException expectedException) { /* TEST NOTES * ************* * we only care about prediction result * i.e. prediction results maps to our object correctly * * we don't care about the actual values in the object * because the service provider (in this case CustomText team) * may optimize their engine * rendering the values in our "ExpectedResult" object in correct * */ // act if (expectedException == null) { var predictionService = new CustomTextPredictionService(httpHandler, customTextKey, endpointUrl, appId); var actualResult = await predictionService.GetPredictionAsync(inputText); // validate object values aren't null Assert.NotNull(actualResult.Prediction.PositiveClassifiers); Assert.NotNull(actualResult.Prediction.Classifiers); Assert.NotNull(actualResult.Prediction.Extractors); } else { await Assert.ThrowsAsync(expectedException.GetType(), async() => { var predictionService = new CustomTextPredictionService(httpHandler, customTextKey, endpointUrl, appId); await predictionService.GetPredictionAsync(inputText); }); } }
public async Task TestPredictionAsync(string customTextKey, string endpointUrl, string appId, string inputText, CliException expectedException) { // arrange var mockHttpHandler = new Mock <IHttpHandler>(); // mock post submit prediction request mockHttpHandler.Setup(handler => handler.SendJsonPostRequestAsync( It.IsAny <string>(), It.IsAny <Object>(), It.IsAny <Dictionary <string, string> >(), It.IsAny <Dictionary <string, string> >() ) ).Returns(Task.FromResult(GetPredictionRequestHttpResponseMessage(expectedException))); // mock get operation status mockHttpHandler.Setup(handler => handler.SendGetRequestAsync( It.Is <string>(s => s.Contains("status")), It.IsAny <Dictionary <string, string> >(), It.IsAny <Dictionary <string, string> >() ) ).Returns(Task.FromResult(GetStatusHttpResponseMessage(expectedException))); // mock get prediction result mockHttpHandler.Setup(handler => handler.SendGetRequestAsync( It.Is <string>(s => !s.Contains("status")), It.IsAny <Dictionary <string, string> >(), It.IsAny <Dictionary <string, string> >() ) ).Returns(Task.FromResult(GetResultHttpResponseMessage(expectedException))); // act if (expectedException == null) { var predictionService = new CustomTextPredictionService(mockHttpHandler.Object, customTextKey, endpointUrl, appId); var actualResult = await predictionService.GetPredictionAsync(inputText); // validate object values aren't null Assert.NotNull(actualResult.Prediction.PositiveClassifiers); Assert.NotNull(actualResult.Prediction.Classifiers); Assert.NotNull(actualResult.Prediction.Extractors); } else { await Assert.ThrowsAsync(expectedException.GetType(), async() => { var predictionService = new CustomTextPredictionService(mockHttpHandler.Object, customTextKey, endpointUrl, appId); await predictionService.GetPredictionAsync(inputText); }); } }