public async Task LocationsControllerDoesNotReturnAddressIfAddressNotFound()
        {
            var badLocation = new LocationModel
            {
                Name          = "Imaginary Spot",
                StreetAddress = "1313 Mockingbird Ln",
                City          = "Keflavik",
                State         = "CZ",
                ZipCode       = "98765"
            };
            var badGeocode = new GoogleGeocodeResponse
            {
                results = Enumerable.Empty <Result>().ToList()
            };
            var mockGeocoder = new Mock <IGeocoder>();

            mockGeocoder.Setup(g => g.GetGeocodeAsync(badLocation)).Returns(Task.FromResult(badGeocode));

            var locationsController = new LocationsController(null, Context, mockGeocoder.Object);
            var result = await locationsController.Create(badLocation) as ViewResult;

            Assert.Equal(
                "This address could not be found. Please check this address and try again!",
                result.ViewData["Error"]);
        }
Пример #2
0
 protected void ReverseGeocode(Action <string, string> callback)
 {
     MapsService.Current.ReverseGeocode(this._latitude, this._longitude, (Action <BackendResult <GoogleGeocodeResponse, ResultCode> >)(response =>
     {
         if (response.ResultCode != ResultCode.Succeeded)
         {
             callback("", "");
         }
         else
         {
             string str1 = "";
             string str2 = "";
             GoogleGeocodeResponse resultData = response.ResultData;
             if (resultData != null)
             {
                 str1    = !string.IsNullOrEmpty(resultData.Route) ? resultData.Route : resultData.AdministrativeArea2;
                 int num = string.Equals(RegionInfo.CurrentRegion.TwoLetterISORegionName, resultData.CountryISO, StringComparison.InvariantCultureIgnoreCase) ? 1 : 0;
                 str2    = resultData.AdministrativeArea1 != str1 ? resultData.AdministrativeArea1 : "";
                 if (num == 0 && !string.IsNullOrEmpty(resultData.Country))
                 {
                     if (!string.IsNullOrEmpty(str2))
                     {
                         str2 += ", ";
                     }
                     str2 += resultData.Country;
                 }
             }
             callback(str1, str2);
         }
     }));
 }
        public async Task LocationsControllerShouldNotPersistTheSameLocationTwice()
        {
            var location = new LocationModel
            {
                Name          = "Prime Spot",
                StreetAddress = "777 E Wisconsin Ave",
                City          = "Milwaukee",
                State         = "WI",
                ZipCode       = "53202"
            };
            var mockGeocoder           = new Mock <IGeocoder>();
            var geocodeWithGoodAddress = new GoogleGeocodeResponse
            {
                results = new List <Result>
                {
                    new Result
                    {
                        formatted_address = "This is a nicely formatted address."
                    }
                }
            };

            mockGeocoder.Setup(g => g.GetGeocodeAsync(location)).Returns(Task.FromResult(geocodeWithGoodAddress));

            var locationsController = new LocationsController(null, Context, mockGeocoder.Object);
            var result = await locationsController.Create(location) as ViewResult;

            result = await locationsController.Create(location) as ViewResult;

            Assert.Equal("The given address already exists. Enter a new address",
                         result.ViewData["Error"]);
        }
Пример #4
0
        public IList <School> CreateSchools(IList <SchoolEnrollment> schoolEnrollments)
        {
            if (schoolEnrollments == null)
            {
                throw new ArgumentNullException("schoolEnrollments");
            }

            List <School> schools = new List <School>();

            //iteratve all schools and retrieve Lat/Long info from google
            foreach (var schoolEnrollment in schoolEnrollments)
            {
                System.Threading.Thread.Sleep(2200);
                GoogleGeocodeResponse geocodeResponse = _googleApiProxy.GetGeocodeAddressResponse(schoolEnrollment.LocationQuery);
                Location schoolLocation   = GetLocationFromGeocodeResponse(geocodeResponse);
                string   formattedAddress = GetFormattedAddressFromGeocodeResponse(geocodeResponse);

                var school = new School()
                {
                    Name            = schoolEnrollment.Name,
                    EnrollmentCount = schoolEnrollment.EnrollmentCount,
                    Category        = schoolEnrollment.Category
                };

                school.FullAddress = formattedAddress;
                school.Latitude    = schoolLocation.Lat;
                school.Longitude   = schoolLocation.Lng;

                schools.Add(school);
            }

            return(schools);
        }
        public void Retrieve_GoogleGeocodeResponse_from_Service()
        {
            var googleApiProxy             = new GoogleApiProxy();
            GoogleGeocodeResponse response = googleApiProxy.GetGeocodeAddressResponse("luverne high school");

            Assert.AreEqual("OK", response.Status);
        }
Пример #6
0
        private string GetFormattedAddressFromGeocodeResponse(GoogleGeocodeResponse response)
        {
            string address = "[unknown address]";

            if (response != null && response.Results != null && response.Results.Count > 0)
            {
                var firstResult = response.Results.First();
                if (firstResult != null && firstResult.FormattedAddress != null)
                {
                    address = firstResult.FormattedAddress;
                }
            }

            return(address);
        }
Пример #7
0
        private Location GetLocationFromGeocodeResponse(GoogleGeocodeResponse response)
        {
            var location = new Location();

            if (response != null && response.Results != null && response.Results.Count > 0)
            {
                var firstResult = response.Results.First();
                if (firstResult != null && firstResult.Geometry != null)
                {
                    location = firstResult.Geometry.Location;
                }
            }

            return(location);
        }
        public async Task CannotMakeExistingLocationsInvalidAsync()
        {
            var goodLocation = new LocationModel
            {
                Name          = "Prime Spot",
                StreetAddress = "777 E Wisconsin Ave",
                City          = "Milwaukee",
                State         = "WI",
                ZipCode       = "53202"
            };
            var mockGeocoder           = new Mock <IGeocoder>();
            var geocodeWithGoodAddress = new GoogleGeocodeResponse
            {
                results = new List <Result>
                {
                    new Result
                    {
                        formatted_address = "This is a nicely formatted address."
                    }
                }
            };

            mockGeocoder.Setup(g => g.GetGeocodeAsync(goodLocation)).Returns(Task.FromResult(geocodeWithGoodAddress));

            var locationsController = new LocationsController(null, Context, mockGeocoder.Object);
            var response            = await locationsController.Create(goodLocation) as ViewResult;

            var badLocation = new LocationModel
            {
                Name          = goodLocation.Name,
                StreetAddress = goodLocation.StreetAddress,
                City          = goodLocation.City,
                State         = goodLocation.State,
                ZipCode       = "99999" // Invalid Zip Code
            };
            var badGeocode = new GoogleGeocodeResponse
            {
                results = Enumerable.Empty <Result>().ToList()
            };

            mockGeocoder.Setup(g => g.GetGeocodeAsync(badLocation)).Returns(Task.FromResult(badGeocode));

            response = await locationsController.Edit(1, badLocation) as ViewResult;

            Assert.Equal(
                "This address could not be found. Please check this address and try again!",
                response.ViewData["Error"]);
        }
Пример #9
0
 private void SyncronizedGeocodeFoundHandler(GoogleGeocodeResponse googleResponse, GoogleGeocodeRequest googleRequest)
 {
     _syncGoogleResponse = googleResponse;
     _autoResetEvent.Set();
 }