Exemple #1
0
        public async void TestWithException()
        {
            var quoteProvider = Substitute.For <IQuoteProvider>();

            quoteProvider
            .WhenForAnyArgs(x => x.GetCurrencyValues("", null))
            .Do(x =>
            {
                throw new QuoteException(201, "You have supplied an invalid Source Currency. [Example: source=BRL]");
            });

            var currencySource = "HUEHUE";
            var currencyTarget = "USD";

            var currencyLayerService = new QuoteCurrencyService(quoteProvider);
            var controller           = new QuoteController(currencyLayerService);
            var controllerResult     = await controller.GetQuotes(currencySource, currencyTarget);

            Assert.IsType <ObjectResult>(controllerResult);

            var formattedControllerResult = ((ObjectResult)controllerResult);

            Assert.Equal(500, formattedControllerResult.StatusCode);

            var quoteException = (QuoteException)formattedControllerResult.Value;

            Assert.Equal(201, quoteException.ErrorCode);
            Assert.Equal("You have supplied an invalid Source Currency. [Example: source=BRL]", quoteException.Message);
        }
        public async Task GetQuotes_OkStatusCode_WithNOTEmptyContext()
        {
            // Arrange
            var controller = new QuoteController(_context, _logger);

            // Act
            var result = await controller.GetQuotes();

            var okResult = result as OkObjectResult;
            var quotes   = okResult.Value as ICollection <QuoteDTO>;

            //Assert
            Assert.NotNull(okResult);
            Assert.Equal(StatusCodes.Status200OK, okResult.StatusCode);
            Assert.Equal(11, quotes.Count);
        }
        public async Task GetQuotes_OkStatusCode_WithEmptyContext()
        {
            // Arrange
            var optionBuilder = new DbContextOptionsBuilder <AldoGiovanniGiacomoAPIContext>()
                                .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var exmptyContext = new AldoGiovanniGiacomoAPIContext(optionBuilder.Options);
            var controller    = new QuoteController(exmptyContext, _logger);

            // Act
            var result = await controller.GetQuotes();

            var okResult = result as OkObjectResult;
            var quotes   = okResult.Value as ICollection <QuoteDTO>;

            //Assert
            Assert.NotNull(okResult);
            Assert.Equal(StatusCodes.Status200OK, okResult.StatusCode);
            Assert.Equal(0, quotes.Count);
        }
Exemple #4
0
        public async void TestWithSuccess()
        {
            var quoteProvider = Substitute.For <IQuoteProvider>();

            var currencySource = "BRL";
            var currencyQuotes = new List <string>()
            {
                "USD", "AUD", "CAD", "PLN", "MXN"
            };

            var fakeResult = new CurrencyLayerResult()
            {
                Success   = true,
                Terms     = "https://currencylayer.com/terms",
                Privacy   = "https://currencylayer.com/privacy",
                Timestamp = 1533403523,
                Source    = currencySource,
                Quotes    = new Dictionary <string, double> {
                    { "BRLUSD", 0.269757 }
                },
                Error = null
            };

            quoteProvider
            .GetCurrencyValues(currencySource, currencyQuotes)
            .ReturnsForAnyArgs(fakeResult);

            var currencyLayerService = new QuoteCurrencyService(quoteProvider);
            var controller           = new QuoteController(currencyLayerService);
            var controllerResult     = await controller.GetQuotes(currencySource, "USD");

            Assert.IsType <OkObjectResult>(controllerResult);
            var quoteCurrencyResult = (QuoteCurrencyResult)((OkObjectResult)controllerResult).Value;

            Assert.Equal(currencySource, quoteCurrencyResult.Source);
            Assert.Single(quoteCurrencyResult.Quotes);
            Assert.Equal("USD", quoteCurrencyResult.Quotes[0].CurrencyName);
            Assert.Equal(0.269757, quoteCurrencyResult.Quotes[0].CurrencyValue);
        }