public async Task UpdateGeocodeWhenAddressChangedAndLatLonNotSetWhenUpdatingExistingRequest()
        {
            var    mockGeocoder   = new Mock <IGeocodeService>();
            string changedAddress = "4444 Changed Address Ln";

            // Because the Geocode method takes a set of strings as arguments, actually verify the arguments are passed in to the mock in the correct order.
            mockGeocoder.Setup(g => g.GetCoordinatesFromAddress(changedAddress, _existingRequest.City, _existingRequest.State, _existingRequest.PostalCode, It.IsAny <string>())).ReturnsAsync(new Coordinates(0, 0));

            var handler = new EditRequestCommandHandler(Context, mockGeocoder.Object);
            await handler.Handle(new EditRequestCommand
            {
                RequestModel = new EditRequestViewModel
                {
                    Id         = _existingRequest.RequestId,
                    Address    = changedAddress,
                    City       = _existingRequest.City,
                    State      = _existingRequest.State,
                    PostalCode = _existingRequest.PostalCode,
                    Latitude   = 0,
                    Longitude  = 0
                }
            });

            mockGeocoder.Verify(x => x.GetCoordinatesFromAddress(changedAddress, _existingRequest.City, _existingRequest.State, _existingRequest.PostalCode, It.IsAny <string>()), Times.Once);
        }
        public async Task ReturnNewRequestIdOnSuccessfulCreation()
        {
            var handler = new EditRequestCommandHandler(Context, new NullObjectGeocoder());
            var requestId = await handler.Handle(new EditRequestCommand { RequestModel = new EditRequestViewModel {  } });

            Assert.NotEqual(Guid.Empty, requestId);
        }
        public async Task ReturnNewRequestIdOnSuccessfulCreation()
        {
            var handler   = new EditRequestCommandHandler(Context, new NullObjectGeocoder());
            var requestId = await handler.Handle(new EditRequestCommand { RequestModel = new EditRequestViewModel {
                                                                          } });

            Assert.NotEqual(Guid.Empty, requestId);
        }
        public async Task SetCorrectOrganizationId()
        {
            var handler   = new EditRequestCommandHandler(Context, new NullObjectGeocoder());
            var requestId = await handler.Handle(new EditRequestCommand { RequestModel = new EditRequestViewModel {
                                                                              EventId = 1
                                                                          } });

            var request = Context.Requests.FirstOrDefault(x => x.RequestId == requestId);

            request.OrganizationId.ShouldBe(1);
        }
        public async Task NotUpdateGeocodeIfAddressDidntChangeWhenUpdatingExistingRequest()
        {
            var mockGeocoder = new Mock<IGeocoder>();

            var handler = new EditRequestCommandHandler(Context, mockGeocoder.Object);
            await handler.Handle(new EditRequestCommand
            {
                RequestModel = new EditRequestViewModel { Id = _existingRequest.RequestId, Address = _existingRequest.Address, City = _existingRequest.City, State = _existingRequest.State, Zip = _existingRequest.Zip }
            });

            mockGeocoder.Verify(x => x.Geocode(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
        }
        public async Task UpdateRequestsThatAlreadyExisted()
        {
            string expectedName = "replaced name";

            var handler = new EditRequestCommandHandler(Context, new NullObjectGeocoder());
            await handler.Handle(new EditRequestCommand
            {
                RequestModel = new EditRequestViewModel { Id = _existingRequest.RequestId, Name = expectedName }
            });

            var request = Context.Requests.First(r => r.RequestId == _existingRequest.RequestId);
            Assert.Equal(expectedName, request.Name );
        }
        public async Task NotUpdateGeocodeIfAddressDidntChangeWhenUpdatingExistingRequest()
        {
            var mockGeocoder = new Mock <IGeocodeService>();

            var handler = new EditRequestCommandHandler(Context, mockGeocoder.Object);
            await handler.Handle(new EditRequestCommand
            {
                RequestModel = new EditRequestViewModel {
                    Id = _existingRequest.RequestId, Address = _existingRequest.Address, City = _existingRequest.City, State = _existingRequest.State, Zip = _existingRequest.Zip
                }
            });

            mockGeocoder.Verify(x => x.GetCoordinatesFromAddress(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()), Times.Never);
        }
        public async Task UpdateRequestsThatAlreadyExisted()
        {
            string expectedName = "replaced name";

            var handler = new EditRequestCommandHandler(Context, new NullObjectGeocoder());
            await handler.Handle(new EditRequestCommand
            {
                RequestModel = new EditRequestViewModel {
                    Id = _existingRequest.RequestId, Name = expectedName
                }
            });

            var request = Context.Requests.First(r => r.RequestId == _existingRequest.RequestId);

            Assert.Equal(expectedName, request.Name);
        }
        public async Task UpdateGeocodeWhenAddressChangedWhenUpdatingExistingRequest()
        {
            var mockGeocoder = new Mock<IGeocoder>();
            string changedAddress = "4444 Changed Address Ln";

            // Because the Geocode method takes a set of strings as arguments, actually verify the arguments are passed in to the mock in the correct order.
            mockGeocoder.Setup(g => g.Geocode(changedAddress, _existingRequest.City, _existingRequest.State, _existingRequest.Zip, It.IsAny<string>())).Returns(new List<Address>());

            var handler = new EditRequestCommandHandler(Context, mockGeocoder.Object);
            await handler.Handle(new EditRequestCommand
            {
                RequestModel = new EditRequestViewModel { Id = _existingRequest.RequestId, Address = changedAddress, City = _existingRequest.City, State = _existingRequest.State, Zip = _existingRequest.Zip }
            });

            mockGeocoder.Verify(x => x.Geocode(changedAddress, _existingRequest.City, _existingRequest.State, _existingRequest.Zip, It.IsAny<string>()), Times.Once);
        }
        public async Task UpdateGeocodeWhenAddressChangedWhenUpdatingExistingRequest()
        {
            var    mockGeocoder   = new Mock <IGeocoder>();
            string changedAddress = "4444 Changed Address Ln";

            // Because the Geocode method takes a set of strings as arguments, actually verify the arguments are passed in to the mock in the correct order.
            mockGeocoder.Setup(g => g.Geocode(changedAddress, _existingRequest.City, _existingRequest.State, _existingRequest.Zip, It.IsAny <string>())).Returns(new List <Address>());

            var handler = new EditRequestCommandHandler(Context, mockGeocoder.Object);
            await handler.Handle(new EditRequestCommand
            {
                RequestModel = new EditRequestViewModel {
                    Id = _existingRequest.RequestId, Address = changedAddress, City = _existingRequest.City, State = _existingRequest.State, Zip = _existingRequest.Zip
                }
            });

            mockGeocoder.Verify(x => x.Geocode(changedAddress, _existingRequest.City, _existingRequest.State, _existingRequest.Zip, It.IsAny <string>()), Times.Once);
        }
        public async Task NotUpdateGeocodeWhenAddressChangedAndLatLonIsSetWhenUpdatingExistingRequest()
        {
            var    mockGeocoder   = new Mock <IGeocodeService>();
            string changedAddress = "4444 Changed Address Ln";

            var handler = new EditRequestCommandHandler(Context, mockGeocoder.Object);
            await handler.Handle(new EditRequestCommand
            {
                RequestModel = new EditRequestViewModel
                {
                    Id         = _existingRequest.RequestId,
                    Address    = changedAddress,
                    City       = _existingRequest.City,
                    State      = _existingRequest.State,
                    PostalCode = _existingRequest.PostalCode,
                    Latitude   = 47.6,
                    Longitude  = -122.3
                }
            });

            mockGeocoder.Verify(x => x.GetCoordinatesFromAddress(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()), Times.Never);
        }