public async Task Should_update_an_existing_lot() { using (var store = NewDocumentStore()) { using (var session = store.OpenAsyncSession()) { var supplier = new Supplier {Id = "Suppliers-1"}; await SaveEntity(supplier, session); var lot = new Lot {Id = "lots/2015/3", Arrived = new DateTime(2015, 6, 5)}; await SaveEntity(lot, session); } using (var session = store.OpenAsyncSession()) { var lotDto = new LotDto {Id = "lots/2015/3", Arrived = new DateTime(2015, 6, 5), SupplierId = "Suppliers-1"}; var service = GetLotItemsService(session); await service.Save(lotDto); } using (var session = store.OpenAsyncSession()) { var service = GetLotItemsService(session); var actual = await service.GetLotById("lots/2015/3"); actual.SupplierId.Should().Be("Suppliers-1"); } } }
public async Task Should_save_a_lot_to_the_db() { using (var store = NewDocumentStore()) { using (var session = store.OpenAsyncSession()) { // Arrange var service = GetLotItemsService(session); var arrived = new DateTime(2013, 6, 3, 15, 30, 0); var dto = new LotDto { Id = null, SupplierId = "Suppliers-1", Arrived = arrived }; var model = await service.Save(dto); // Assert model.Success.Should().BeTrue(); model.Message.Should().Be("Criou lote 2013/1"); model.Entity.Id.Should().Be("lots/2013/1"); var actual = await session.LoadAsync<Lot>("lots/2013/1"); actual.Id.Should().Be("lots/2013/1"); actual.SupplierId.Should().Be("Suppliers-1"); actual.Arrived.Should().Be(arrived); actual.AuditInfo.Username.Should().NotBeNullOrEmpty(); } } }
public async Task Should_retrieve_a_lot_dto_with_supplier_details_from_the_db() { using (var store = NewDocumentStore()) { // Arrange using (var session = store.OpenAsyncSession()) { var fixture = new Fixture(); var supplier = fixture.Build<Supplier>() .Without(c => c.Id) .With(c => c.Name, "Natalia") .With(c => c.Cnpj, "CNPJ") .With(c => c.Cpf, "CPF") .With(c => c.Municipio, "Municipio") .Create(); await SaveEntity(supplier, session); } using (var session = store.OpenAsyncSession()) { var dto = new LotDto { Id = "lots/2015/2", SupplierId = "Suppliers-1", Arrived = new DateTime(2015, 6, 4) }; var service = GetLotItemsService(session); await service.Save(dto); } // Act using (var session = store.OpenAsyncSession()) { var service = GetLotItemsService(session); var actual = await service.GetLotById("lots/2015/2"); // Assert actual.SupplierId.Should().Be("Suppliers-1"); actual.Supplier.Name.Should().Be("Natalia"); actual.Supplier.Cnpj.Should().Be("CNPJ"); actual.Supplier.Cpf.Should().Be("CPF"); actual.Supplier.Municipio.Should().Be("Municipio"); } } }
public async Task<BaseUpdateModel<LotDto>> Save(LotDto dto) { if (dto.Arrived == DateTime.MinValue) { throw new ArgumentException("Missing arrived date"); } SetUpdateText(dto); var entity = dto.Id.IsNullOrEmpty() ? new Lot() : await Session.LoadAsync<Lot>(dto.Id) ?? new Lot(); if (dto.Id.IsNullOrEmpty()) { dto.Id = await _lotsIdentityService.GetNextId(dto.Arrived.Year); } Mapper.Map(dto, entity); UpdateAuditInfo(entity); await Session.StoreAsync(entity); await Session.SaveChangesAsync(); return new BaseUpdateModel<LotDto>(dto, $"{UpdateText} lote {entity.Id.ToLotNumber()}", true); }
public async Task<LotDto> GetLotById(string id) { if (id == null) throw new ArgumentNullException(nameof(id)); var entity = await Session.Include<Lot>(c => c.SupplierId).LoadAsync<Lot>(id); if (entity == null) throw new NotFoundException($"Não existe lot {id.ToLotNumber()}"); var supplier = await Session.LoadAsync<Supplier>(entity.SupplierId); if (supplier == null) { throw new NotFoundException($"{entity.SupplierId} não existe!"); } var dto = new LotDto { Id = id, Arrived = entity.Arrived, ArrivalQuantity = entity.ArrivalQuantity, DeliveryPoint = entity.DeliveryPoint, NotaFiscal = entity.NotaFiscal, NumberPlate = entity.NumberPlate, Region = entity.Region, Transportation = entity.Transportation, SupplierId = entity.SupplierId, Supplier = supplier }; return dto; }
public void Should_throw_exception_if_lot_dto_arrived_is_minvalue() { using (var store = NewDocumentStore()) { using (var session = store.OpenAsyncSession()) { // Arrange var dto = new LotDto(); var service = GetLotItemsService(session); // Act Func<Task> act = async () => await service.Save(dto); // Assert act.ShouldThrow<ArgumentException>("Missing arrival date"); } } }