public void ViewModelOfIndexPageShouldHaveNotEmptySelectListsIfRepositoriesProvidedData() { _stateRepository.GetStates().Returns(_preparedStates); _productRepository.GetProducts().Returns(_preparedProducts); _categoryRepository.GetCategories().Returns(_preparedCategories); var controller = new HomeController(_categoryRepository, _stateRepository, _productRepository); var result = controller.Index() as ViewResult; // Assert Assert.That(result, Is(NotNull())); var viewModel = result?.Model as MainViewModel; Assert.That(viewModel, Is(NotNull())); var productsList = viewModel?.ProductSelectList.Items.Cast <ProductModel>().ToList(); var statesList = viewModel?.StateSelectList.Items.Cast <StateOfAmericaModel>().ToList(); var categoriesList = viewModel?.CategorySelectList.Items.Cast <CategoryModel>().ToList(); Assert.That(productsList, Is <ICollection>(Not(OfLength(0)))); Assert.That(statesList, Is <ICollection>(Not(OfLength(0)))); Assert.That(categoriesList, Is <ICollection>(Not(OfLength(0)))); Assert.That(productsList, Has(Item(EqualTo(_expectedProduct)))); Assert.That(categoriesList, Has(Item(EqualTo(_expectedCategory)))); Assert.That(statesList, Has(Item(EqualTo(_expectedState)))); var state = statesList?.FirstOrDefault(); Assert.That(state?.TaxRates, Has(Item((EqualTo(_expectedTax))))); }
public void ServerShouldStayEmptyIfHadNoVMsDuringBalancing() { var server = A(Server().WithCapacity(1)); Balance(AListOfServersWith(server), AnEmptyListOfVMs()); Assert.That(server, HasLoadPercentageOf(0.0d)); }
BalancingOneServerWithSomeCapacityAndVmWithLesserOrEqualSizeThanServerCapacityFillsTheServerWithTheVm( Server server, Vm vm, double expectedPercentage) { Balance(AListOfServersWith(server), AListOfVms(vm)); Assert.That(server, HasLoadPercentageOf(expectedPercentage)); Assert.That(server.Vms, Has.Item(EqualTo(vm))); }
public void ShouldExistsOnlyThreeStatesThatForCurrentlyAvailableTaxRatesObtainedFromParserHaveTaxExceptions() { var collection = Parser.GetStatesModelsFromWikipedia() .Where(model => model.TaxRates.Any(tax => tax.MinValue > 0.0)).ToList(); Assert.That(collection, Is(OfLength(ExpectedStatesWithExceptions))); }
public void IfIndexWillBeCorrectlyInvokedThenMainViewModelOfActionResultShouldMatchingToExpectedOne() { _stateRepository.GetStates().Returns(_preparedStates); _productRepository.GetProducts().Returns(_preparedProducts); _categoryRepository.GetCategories().Returns(_preparedCategories); var controller = new CategorySelectionController(_categoryRepository, _stateRepository, _productRepository); var result = controller.Index(CategoryName) as ViewResult; Assert.That(result, Is(NotNull())); var viewModel = result?.Model as MainViewModel; Assert.That(viewModel, Is(NotNull())); var productsList = viewModel?.ProductSelectList.Items.Cast <ProductModel>().ToList(); var statesList = viewModel?.StateSelectList.Items.Cast <StateOfAmericaModel>().ToList(); var categoriesList = viewModel?.CategorySelectList.Items.Cast <CategoryModel>().ToList(); Assert.That(productsList, Is <ICollection>(Not(OfLength(0)))); Assert.That(statesList, Is <ICollection>(Not(OfLength(0)))); Assert.That(categoriesList, Is <ICollection>(Not(OfLength(0)))); Assert.That(productsList, Has(Item(EqualTo(_expectedProduct)))); Assert.That(categoriesList, Has(Item(EqualTo(_expectedCategory)))); Assert.That(statesList, Has(Item(EqualTo(_expectedState)))); Assert.That(viewModel?.ProductList, Has(Item(EqualTo(_expectedProduct)))); Assert.That(viewModel?.Category, Is(EqualTo(CategoryName))); }
public void IndexShouldThrowHttpExceptionWithCode403AndSpecifiedMessageIfStateStringWillBeNullOrEmpty() { _productRepository.GetProducts().Returns(_preparedProducts); _productRepository.GetProductByName(Arg.Any <string>()).Returns(_expectedProduct); _stateRepository.GetStateByName(Arg.Any <string>()).Returns(_expectedState); var controller = new StateSelectionController(_stateRepository, _categoryRepository, _productRepository); controller.Index(ProductName, StateName, ExampleCorrectPreferredPriceInput, ExampleCorrectCount); ActionResult Call() => controller.Index(ProductName, Empty, ExampleCorrectPreferredPriceInput, ExampleCorrectCount); ActionResult CallWithNull() => controller.Index(ProductName, null, ExampleCorrectPreferredPriceInput, ExampleCorrectCount); var exception = Xunit.Assert.Throws <HttpException>(Call); var exception2 = Xunit.Assert.Throws <HttpException>(CallWithNull); var code = exception.GetHttpCode(); var message = exception.Message; Assert.That(code, Is(EqualTo(Http403))); Assert.That(message, Is(EqualTo(ExpectedMessageFor403Code))); Assert.That(code, Is(EqualTo(exception2.GetHttpCode()))); Assert.That(message, Is(EqualTo(exception2.Message))); }
public void ServerShouldStayNotFullyLoadedIfVMUsesOnlyThePartOfServerResourcesAfterBalancing() { var server = A(Server().WithCapacity(10)); var vm = A(Vm().WithSize(5)); Balance(AListOfServersWith(server), AListOfVms(vm)); Assert.That(server, HasLoadPercentageOf(50.0)); }
public void ServerShouldStayFullIfHadCapacityForOnlyOneVMDuringBalancing() { var server = A(Server().WithCapacity(1)); var vm = A(Vm().WithSize(1)); Balance(AListOfServersWith(server), AListOfVms(vm)); Assert.That(server, StaysFull()); }
public void IfServerHasNotEnoughPlaceToFitAnotherVmItShouldHaveTheSameLoadRateAfterBalancing() { var serverWithSomeLargeVm = A(Server().WithCapacity(10).Having(A(Vm().WithSize(9)))); var tooLargeVm = A(Vm().WithSize(5)); Balance(AListOfServersWith(serverWithSomeLargeVm), AListOfVms(tooLargeVm)); Assert.That(serverWithSomeLargeVm.Vms, Not(Has.Item(EqualTo(tooLargeVm)))); Assert.That(serverWithSomeLargeVm, HasLoadPercentageOf(90.0)); }
public void BalancingOneServerWithOneSlotCapacityAndOneSlotVmFillsTheServerWithTheVm() { var server = A(Server().WithCapacity(1)); var vm = A(Vm().WithSize(1)); Balance(AListOfServersWith(server), AListOfVms(vm)); Assert.That(server, HasLoadPercentageOf(100.0)); Assert.That(server.Vms, Has.Item(EqualTo(vm))); }
public void IfProductFinalPriceIsLesserThanPurchasePriceCalculateMarginShouldReturnNegativeValue() { var productWithFinalPriceLesserThanPurchasePrice = Product() .WithCategoryId((int)ProductCategoryEnum.Groceries).WithPurchasePrice(2137).WithFinalPrice(1488) .Build(); var margin = Algorithm.CalculateMargin(productWithFinalPriceLesserThanPurchasePrice); Assert.That(margin, Is.LessThan(0.0)); }
public void TestGuamDoesNotHaveSalesTaxRateForIntangibles() { var collection = Parser.GetStatesModelsFromWikipedia(); var guam = collection.First(model => model.Name.Equals("Guam")); Assert.That(guam, Is(NotNull())); var categories = guam.TaxRates.Select(tax => tax.CategoryId).ToList(); Assert.That(categories, Not(Has(Item(EqualTo(Intangibles))))); }
public void IndexShouldBeNotNullWhenProvidedArgumentsAreNotNullOrEmptyOrMalformed() { _productRepository.GetProducts().Returns(_preparedProducts); var controller = new ProductSelectionController(_stateRepository, _categoryRepository, _productRepository); var result = controller.Index(ProductName, ExampleCorrectPreferredPriceInput, ExampleCorrectCount); Assert.That(result, Is(NotNull())); }
public void TestValuesOfAlaskaSalesTaxRates() { var collection = Parser.GetStatesModelsFromWikipedia(); var alaska = collection.First(model => model.Name.Equals("Alaska")); Assert.That(alaska, Is(NotNull())); var rates = alaska.TaxRates.Select(model => model.TaxRate).ToList(); Assert.That(rates, Is(CollectionEqualTo(_alaskaTaxRates))); }
public void IndexShouldBeNotNullWhenProvidedArgumentsAreNotNullOrEmptyOrMalformed() { _productRepository.GetProducts().Returns(_preparedProducts); var controller = new CategorySelectionController(_categoryRepository, _stateRepository, _productRepository); var result = controller.Index(CategoryName); Assert.That(result, Is(NotNull())); }
public void IndexPageShouldNotBeNullIfNoneErrorOccurred() { // Arrange var controller = new HomeController(_categoryRepository, _stateRepository, _productRepository); // Act var result = controller.Index() as ViewResult; // Assert Assert.That(result, Is(NotNull())); }
IfTwoServersAreProvidedAndOnlyOneMachineIsAvailableToAssignTheServerWithTheLesserLoadRateShouldReceiveVm() { var serverWithSomeVm = A(Server().WithCapacity(10).Having(A(Vm().WithSize(2)))); var emptyServer = A(Server().WithCapacity(10)); var vmToBalance = A(Vm().WithSize(5)); Balance(AListOfServersWith(serverWithSomeVm, emptyServer), AListOfVms(vmToBalance)); Assert.That(serverWithSomeVm.Vms, Not(Has.Item(EqualTo(vmToBalance)))); Assert.That(emptyServer.Vms, Has.Item(EqualTo(vmToBalance))); Assert.That(emptyServer, HasLoadPercentageOf(50.0)); Assert.That(serverWithSomeVm, HasLoadPercentageOf(20.0)); }
public void ApplyingTaxThatValueEqualsZeroShouldResultInFinalPriceThatIsEqualToPreferredPriceOfProductAfterThatCalculateFinalPriceIsCalled() { var productWithPreferredPrice = Product() .WithCategoryId((int)ProductCategoryEnum.Clothing).WithPreferredPrice(PreferredPrice) .Build(); void StandardCalculateFinalPriceCall() => Algorithm.CalculateFinalPrice(productWithPreferredPrice, _state, 1); Assert.That(StandardCalculateFinalPriceCall, DoesNotThrow.An <ArgumentOutOfRangeException>()); var finalPrice = productWithPreferredPrice.FinalPrice; Assert.That(finalPrice, Is.EqualTo(PreferredPrice)); }
public void CalculateFinalPriceShouldUpdateProperlyFinalPriceOfProduct() { var state = State().AddTax().OfCategoryId((int)ProductCategoryEnum.Clothing).OfTaxRate(10).ConfirmTax().Build(); var product = Product().WithCategoryId((int)ProductCategoryEnum.Clothing).WithPurchasePrice(20) .WithPreferredPrice(PreferredPriceOf40).Build(); void StandardCalculateFinalPriceCall() => Algorithm.CalculateFinalPrice(product, state, 1); Assert.That(StandardCalculateFinalPriceCall, DoesNotThrow.An <ArgumentOutOfRangeException>()); var finalPrice = product.FinalPrice; Assert.That(finalPrice, Is.EqualTo(FinalPriceOf36)); }
public void AboutPageShouldNotBeNullAndShouldHaveSpecifiedMessageIfNoneErrorOccurred() { // Arrange var controller = new HomeController(_categoryRepository, _stateRepository, _productRepository); // Act var result = controller.About() as ViewResult; // Assert Assert.That(result, Is(NotNull())); Assert.That(result?.ViewBag, Is(NotNull())); var message = result?.ViewBag?.Message; Assert.That(message, Is(EqualTo(ExpectedMessage))); }
public void IndexShouldThrowHttpExceptionWithCode404AndSpecifiedMessageIfNoneProductWillBeAvailable() { var controller = new ProductSelectionController(_stateRepository, _categoryRepository, _productRepository); ActionResult Call() => controller.Index(ProductName, ExampleCorrectPreferredPriceInput, ExampleCorrectCount); var exception = Xunit.Assert.Throws <HttpException>(Call); var code = exception.GetHttpCode(); var message = exception.Message; Assert.That(code, Is(EqualTo(Http404))); Assert.That(message, Is(EqualTo(ExpectedMessageOf404Code))); }
IndexShouldThrowHttpExceptionWithCode500AndSpecifiedMessageForAccessToDbIssuesIfAnyRepositoryThrewAnException() { _productRepository.GetProducts().Throws(new Exception()); var controller = new CategorySelectionController(_categoryRepository, _stateRepository, _productRepository); ActionResult Call() => controller.Index(CategoryName); var exception = Xunit.Assert.Throws <HttpException>(Call); var code = exception.GetHttpCode(); var message = exception.Message; Assert.That(code, Is(EqualTo(Http500))); Assert.That(message, Is(EqualTo(ExpectedMessageOf500CodeForAnyException))); }
IndexShouldThrowHttpExceptionWithCode404AndSpecifiedMessageIfAnyRepositoryThrewAnItemNotFoundException() { _categoryRepository.GetCategories().Throws <ItemNotFoundException>(); var controller = new CategorySelectionController(_categoryRepository, _stateRepository, _productRepository); ActionResult Call() => controller.Index(CategoryName); var exception = Xunit.Assert.Throws <HttpException>(Call); var code = exception.GetHttpCode(); var message = exception.Message; Assert.That(code, Is(EqualTo(Http404))); Assert.That(message, Is(EqualTo(ExpectedMessageOf404Code))); }
public void BalancingEmptyServersAndSomeListOfVmsShouldResultInNearlyEqualVmDistribution( ICollection <Server> servers, ICollection <Vm> vms, List <ICollection <Vm> > expectedVmsList, List <double> expectedLoadPercentages) { Balance(servers, vms); var serversList = servers.ToList(); for (var i = 0; i < serversList.Count; ++i) { var expectedVms = expectedVmsList[i]; var expectedPercentage = expectedLoadPercentages[i]; var server = serversList[i]; Assert.That(server, HasLoadPercentageOf(expectedPercentage)); Assert.That(server, HasVmsEqualTo(expectedVms)); } }
public void ServerWithLargeCapacityShouldContainAllProvidedVMsAfterBalancingAndHaveSpecifiedLoadPercentageRate() { var server = A(Server().WithCapacity(20)); var(vm1, vm2, vm3, vm4, vm5) = (A(Vm().WithSize(2)), A(Vm().WithSize(3)), A(Vm().WithSize(1)), A(Vm().WithSize(5)), A(Vm().WithSize(4))); Balance(AListOfServersWith(server), AListOfVms(vm1, vm2, vm3, vm4, vm5)); var vms = server.Vms; Assert.That(vms, Has.Item(EqualTo(vm1))); Assert.That(vms, Has.Item(EqualTo(vm2))); Assert.That(vms, Has.Item(EqualTo(vm3))); Assert.That(vms, Has.Item(EqualTo(vm4))); Assert.That(vms, Has.Item(EqualTo(vm5))); Assert.That(server, HasLoadPercentageOf(75.0)); }
IndexShouldThrowHttpExceptionWithCode500AndSpecifiedMessageForAccessToDbIssuesIfAnyRepositoryThrewAnAccessToNotConnectedDatabaseException() { _stateRepository.GetStates().Throws(new AccessToNotConnectedDatabaseException()); var controller = new StateSelectionController(_stateRepository, _categoryRepository, _productRepository); ActionResult Call() => controller.Index(ProductName, StateName, ExampleCorrectPreferredPriceInput, ExampleCorrectCount); var exception = Xunit.Assert.Throws <HttpException>(Call); var code = exception.GetHttpCode(); var message = exception.Message; Assert.That(code, Is(EqualTo(Http500))); Assert.That(message, Is(EqualTo(ExpectedMessageOf500CodeForAccessTrouble))); }
public void IndexShouldThrowHttpExceptionWithCode403AndSpecifiedMessageIfCountWillBeNegative() { _productRepository.GetProducts().Returns(_preparedProducts); var controller = new StateSelectionController(_stateRepository, _categoryRepository, _productRepository); ActionResult Call() => controller.Index(ProductName, StateName, ExampleCorrectPreferredPriceInput, ExampleIncorrectCount); var exception = Xunit.Assert.Throws <HttpException>(Call); var code = exception.GetHttpCode(); var message = exception.Message; Assert.That(code, Is(EqualTo(Http403))); Assert.That(message, Is(EqualTo(ExpectedMessageFor403Code))); }
public void BalancingEmptyServersAndSomeListOfVmsShouldResultInNearlyEqualVmDistribution() { var firstServer = A(Server().WithCapacity(8)); var secondServer = A(Server().WithCapacity(12)); var(vm1, vm2, vm3, vm4, vm5) = (A(Vm().WithSize(3)), A(Vm().WithSize(2)), A(Vm().WithSize(1)), A(Vm().WithSize(5)), A(Vm().WithSize(4))); Balance(AListOfServersWith(firstServer, secondServer), AListOfVms(vm1, vm2, vm3, vm4, vm5)); Assert.That(firstServer, HasLoadPercentageOf(87.5)); Assert.That(secondServer, HasLoadPercentageOf(66.6666)); var expectedVmsOfFirstServer = AListOfVms(vm1, vm5); var expectedVmsOfSecondServer = AListOfVms(vm2, vm3, vm4); Assert.That(firstServer, HasVmsEqualTo(expectedVmsOfFirstServer)); Assert.That(secondServer, HasVmsEqualTo(expectedVmsOfSecondServer)); }
public void IndexPageShouldThrowHttpExceptionWithStatusCodeEqualTo500AndSpecifiedMessageIfAnyRepositoryThrowUnspecifiedException() { _stateRepository.GetStates().Throws(new Exception()); var controller = new HomeController(_categoryRepository, _stateRepository, _productRepository); ActionResult Call() => controller.Index(); var exception = Xunit.Assert.Throws <HttpException>(Call); var code = exception.GetHttpCode(); var message = exception.Message; Assert.That(code, Is(EqualTo(Http500))); Assert.That(message, Is(EqualTo(ExpectedMessageOf500CodeForAnyException))); }
public void IfIndexWillBeCorrectlyInvokedThenMainViewModelOfActionResultShouldMatchingToExpectedOne() { _stateRepository.GetStates().Returns(_preparedStates); _productRepository.GetProducts().Returns(_preparedProducts); _categoryRepository.GetCategories().Returns(_preparedCategories); _productRepository.GetProductByName(Arg.Any <string>()).Returns(_expectedProduct); _stateRepository.GetStateByName(Arg.Any <string>()).Returns(_expectedState); var controller = new StateSelectionController(_stateRepository, _categoryRepository, _productRepository); var result = controller.Index(ProductName, StateName, ExampleCorrectPreferredPriceInput, ExampleCorrectCount) as ViewResult; Assert.That(result, Is(NotNull())); var viewModel = result?.Model as MainViewModel; Assert.That(viewModel, Is(NotNull())); var productsList = viewModel?.ProductSelectList.Items.Cast <ProductModel>().ToList(); var statesList = viewModel?.StateSelectList.Items.Cast <StateOfAmericaModel>().ToList(); var categoriesList = viewModel?.CategorySelectList.Items.Cast <CategoryModel>().ToList(); Assert.That(productsList, Is <ICollection>(Not(OfLength(0)))); Assert.That(statesList, Is <ICollection>(Not(OfLength(0)))); Assert.That(categoriesList, Is <ICollection>(Not(OfLength(0)))); Assert.That(productsList, Has(Item(EqualTo(_expectedProduct)))); Assert.That(categoriesList, Has(Item(EqualTo(_expectedCategory)))); Assert.That(statesList, Has(Item(EqualTo(_expectedState)))); var state = statesList?.FirstOrDefault(); Assert.That(state?.TaxRates, Has(Item((EqualTo(_expectedTax))))); var taxes = viewModel?.Tax; Assert.That(taxes, Has(Item(EqualTo(_expectedState.BaseSalesTax)))); var chosenProduct = viewModel?.ChosenProduct; Assert.That(chosenProduct, Is(EqualTo(_expectedProduct))); Assert.That(viewModel?.StateNameList, Has(Item(EqualTo(StateName)))); Assert.That(viewModel?.PreferredPrice ?? 0, Is(EqualTo(PreferredPrice))); Assert.That(viewModel?.FinalPrice, Has(Item(EqualTo(PreferredPrice * ExampleCorrectCount)))); const double diff = (PreferredPrice - PurchasePrice) * ExampleCorrectCount; Assert.That(viewModel?.Margin, Has(Item(EqualTo(diff)))); }