public void UpdateRecordReturnsBadRequestWhenModelIsInvalid() { var invalidModel = new RecordModel { FuelCardId = 1, CorporationId = 1, CostAllocationId = 1 }; var fuelCard = new FuelCard(); var corporation = new Corporation(); var costAllocation = new CostAllocation(); _fuelCardRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(fuelCard); _corporationRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(corporation); _costAllocationRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(costAllocation); _sut.ModelState.AddModelError("name", "name is required"); var result = _sut.UpdateEntity(invalidModel, 1) as BadRequestResult; Assert.That(result, Is.Not.Null); _recordRepositoryMock.Verify(m => m.Update(It.IsAny <int>(), It.IsAny <Record>()), Times.Never); _fuelCardRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); _corporationRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); _costAllocationRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); _recordRepositoryMock.Verify(m => m.Find(It.IsAny <Expression <Func <Record, bool> > >()), Times.Once); }
public void UpdateRecordReturnsOkWhenEverythingIsCorrect() { var validModel = new RecordModel { Id = 1, FuelCardId = 1, CorporationId = 1, CostAllocationId = 1 }; var fuelCard = new FuelCard(); var corporation = new Corporation(); var costAllocation = new CostAllocation(); _recordRepositoryMock.Setup(m => m.Update(It.IsAny <int>(), It.IsAny <Record>())) .Returns(true); _fuelCardRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(fuelCard); _corporationRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(corporation); _costAllocationRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(costAllocation); var result = _sut.UpdateEntity(validModel, validModel.Id) as OkResult; Assert.That(result, Is.Not.Null); _recordRepositoryMock.Verify(m => m.Update(It.IsAny <int>(), It.IsAny <Record>()), Times.Once); _fuelCardRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); _corporationRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); _costAllocationRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); _recordRepositoryMock.Verify(m => m.Find(It.IsAny <Expression <Func <Record, bool> > >()), Times.Once); }
public void UpdateRecordReturnsBadRequestWhenIdFromModelDoesNotMatchIdFromQueryParameter() { var validModel = new RecordModel { FuelCardId = 1, CorporationId = 1, CostAllocationId = 1 }; var fuelCard = new FuelCard(); var corporation = new Corporation(); var costAllocation = new CostAllocation(); _fuelCardRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(fuelCard); _corporationRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(corporation); _costAllocationRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(costAllocation); var result = _sut.UpdateEntity(validModel, validModel.Id + 1) as BadRequestObjectResult; Assert.That(result, Is.Not.Null); _recordRepositoryMock.Verify(m => m.Update(It.IsAny <int>(), It.IsAny <Record>()), Times.Never); _fuelCardRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); _corporationRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); _costAllocationRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); _recordRepositoryMock.Verify(m => m.Find(It.IsAny <Expression <Func <Record, bool> > >()), Times.Once); }
public void PostRecordReturnsNotFoundWhenCostAllocationIsNotFound() { var validModel = new RecordModel { FuelCardId = 1, CorporationId = 1, CostAllocationId = 1 }; var fuelCard = new FuelCard(); var corporation = new Corporation(); CostAllocation costAllocation = null; _fuelCardRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(fuelCard); _corporationRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(corporation); _costAllocationRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(costAllocation); var result = _sut.PostEntity(validModel) as NotFoundObjectResult; Assert.That(result, Is.Not.Null); _recordRepositoryMock.Verify(m => m.Add(It.IsAny <Record>()), Times.Never); _fuelCardRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); _corporationRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); _costAllocationRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); _recordRepositoryMock.Verify(m => m.Find(It.IsAny <Expression <Func <Record, bool> > >()), Times.Never); }
public void PostRecordReturnsOkWhenModelIsValid() { var validModel = new RecordModel { FuelCardId = 1, CorporationId = 1, CostAllocationId = 1 }; var fuelCard = new FuelCard(); var corporation = new Corporation(); var costAllocation = new CostAllocation(); _fuelCardRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(fuelCard); _corporationRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(corporation); _costAllocationRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(costAllocation); _mapperMock.Setup(m => m.Map <RecordReturnModel>(It.IsAny <Record>())) .Returns(new RecordReturnModel()); _mapperMock.Setup(m => m.Map <Record>(It.IsAny <RecordModel>())) .Returns(new Record()); var result = _sut.PostEntity(validModel) as OkObjectResult; Assert.That(result, Is.Not.Null); Assert.That((RecordReturnModel)result.Value, Is.Not.Null); _recordRepositoryMock.Verify(m => m.Add(It.IsAny <Record>()), Times.Once); _fuelCardRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); _corporationRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); _costAllocationRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); _recordRepositoryMock.Verify(m => m.Find(It.IsAny <Expression <Func <Record, bool> > >()), Times.Once); }
public void GetCostAllocationByIdReturnsNotFoundWhenCostAllocationDoesNotExist() { CostAllocation costAllocation = null; _costAllocationRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(costAllocation); var result = _sut.GetEntityById(0) as NotFoundObjectResult; Assert.That(result, Is.Not.Null); _costAllocationRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); }
public void DeleteCostAllocationReturnsOkWhenEverythingIsCorrect() { var costAllocation = new CostAllocation(); _costAllocationRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(costAllocation); var result = _sut.DeleteEntity(1) as OkResult; Assert.That(result, Is.Not.Null); _costAllocationRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); _costAllocationRepositoryMock.Verify(m => m.Remove(It.IsAny <CostAllocation>()), Times.Once); }
public void DeleteCostAllocationReturnsNotFoundWhenCostAllocationIsNotFound() { CostAllocation costAllocation = null; _costAllocationRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(costAllocation); var result = _sut.DeleteEntity(1) as NotFoundObjectResult; Assert.That(result, Is.Not.Null); _costAllocationRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); _costAllocationRepositoryMock.Verify(m => m.Remove(It.IsAny <CostAllocation>()), Times.Never); }
public void ConstructorSetsCorrectEfenkaContext() //done by checking if getbyid works { EfenkaContextTestFactory.Create(); _sut = new CostAllocationRepository(EfenkaContextTestFactory.EfenkaContext); var costAllocation = new CostAllocation(); _sut.Add(costAllocation); var costAllocationFromDatabase = _sut.GetById(costAllocation.Id); Assert.That(costAllocationFromDatabase, Is.EqualTo(costAllocation)); }
public void GetCostAllocationByIdReturnsOkAndCostAllocationWhenEverythingIsCorrect() { var costAllocation = new CostAllocation(); _costAllocationRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(costAllocation); _mapperMock.Setup(m => m.Map <CostAllocationReturnModel>(It.IsAny <CostAllocation>())) .Returns(new CostAllocationReturnModel()); var result = _sut.GetEntityById(0) as OkObjectResult; Assert.That(result, Is.Not.Null); var value = result.Value as CostAllocationReturnModel; Assert.That(value, Is.Not.Null); _costAllocationRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); }
public void UpdateRecordReturnsBadRequestWhenFuelCardAlreadyExistsAndIsntFromUpdatingRecord() { var validModel = new RecordModel { Id = 1, FuelCardId = 1, CorporationId = 1, CostAllocationId = 1 }; var fuelCard = new FuelCard(); var corporation = new Corporation(); var costAllocation = new CostAllocation(); var records = new List <Record> { new Record { Id = validModel.Id + 1 } }; _recordRepositoryMock.Setup(m => m.Update(It.IsAny <int>(), It.IsAny <Record>())) .Returns(true); _fuelCardRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(fuelCard); _corporationRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(corporation); _costAllocationRepositoryMock.Setup(m => m.GetById(It.IsAny <int>())) .Returns(costAllocation); _recordRepositoryMock.Setup(m => m.Find(It.IsAny <Expression <Func <Record, bool> > >())) .Returns(records); var result = _sut.UpdateEntity(validModel, validModel.Id) as BadRequestObjectResult; Assert.That(result, Is.Not.Null); _recordRepositoryMock.Verify(m => m.Update(It.IsAny <int>(), It.IsAny <Record>()), Times.Never); _fuelCardRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); _corporationRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); _costAllocationRepositoryMock.Verify(m => m.GetById(It.IsAny <int>()), Times.Once); _recordRepositoryMock.Verify(m => m.Find(It.IsAny <Expression <Func <Record, bool> > >()), Times.Once); }
public void GetAllIncludesAllRelationsOfRecord() { var brand = new Brand(); var model = new Model { Brand = brand }; var fuelType = new FuelType(); var engineType = new EngineType { Brand = brand }; var series = new Series { Brand = brand }; var doorType = new DoorType(); var category = new Category(); var country = new Country(); var exteriorColor = new ExteriorColor { Brand = brand }; var interiorColor = new InteriorColor { Brand = brand }; var person = new Person(); var driver = new Driver { Person = person }; var vehicle = new Vehicle { Brand = brand, Model = model, FuelType = fuelType, EngineType = engineType, Series = series, DoorType = doorType, Category = category, Country = country, ExteriorColor = exteriorColor, InteriorColor = interiorColor }; var fuelCard = new FuelCard { Driver = driver, Vehicle = vehicle }; var costAllocation = new CostAllocation(); var company = new Company(); var corporation = new Corporation { Company = company }; var record = new Record { FuelCard = fuelCard, CostAllocation = costAllocation, Corporation = corporation }; _sut.Add(record); var records = _sut.GetAll(); Assert.That(records, Is.Not.Null); var recordFromDatabase = records.FirstOrDefault(r => r.Id == record.Id); Assert.That(recordFromDatabase.CostAllocation, Is.EqualTo(costAllocation)); Assert.That(recordFromDatabase.Corporation, Is.EqualTo(corporation)); Assert.That(recordFromDatabase.Corporation.Company, Is.EqualTo(company)); Assert.That(recordFromDatabase.FuelCard.Vehicle.Brand, Is.EqualTo(brand)); Assert.That(recordFromDatabase.FuelCard.Vehicle.Model, Is.EqualTo(model)); Assert.That(recordFromDatabase.FuelCard.Vehicle.FuelType, Is.EqualTo(fuelType)); Assert.That(recordFromDatabase.FuelCard.Vehicle.EngineType, Is.EqualTo(engineType)); Assert.That(recordFromDatabase.FuelCard.Vehicle.Series, Is.EqualTo(series)); Assert.That(recordFromDatabase.FuelCard.Vehicle.DoorType, Is.EqualTo(doorType)); Assert.That(recordFromDatabase.FuelCard.Vehicle.Category, Is.EqualTo(category)); Assert.That(recordFromDatabase.FuelCard.Vehicle.Country, Is.EqualTo(country)); Assert.That(recordFromDatabase.FuelCard.Vehicle.ExteriorColor, Is.EqualTo(exteriorColor)); Assert.That(recordFromDatabase.FuelCard.Vehicle.InteriorColor, Is.EqualTo(interiorColor)); Assert.That(recordFromDatabase.FuelCard.Vehicle.FuelCard, Is.EqualTo(fuelCard)); Assert.That(recordFromDatabase.FuelCard.Driver, Is.EqualTo(driver)); Assert.That(recordFromDatabase.FuelCard.Driver.Person, Is.EqualTo(person)); }