Example #1
0
        public void GetTweetTextShouldWorkForGeneralScenario()
        {
            var model = new CommuteInfoModel
                        {
                            ToWorkDaylights = new DaylightInfoModel
                                              {
                                                  IsCurrentlyInDaylight = false,
                                                  NumberOfDaysToTransition = 13,
                                                  CommuteType = Commute.ToWork,
                                                  NextWorkingDayDaylightTransition = new DateTime(2014, 2, 1)
                                              },
                            FromWorkDaylights = new DaylightInfoModel
                                                {
                                                    IsCurrentlyInDaylight = true,
                                                    NumberOfDaysToTransition = 54,
                                                    CommuteType = Commute.FromWork,
                                                    NextWorkingDayDaylightTransition = new DateTime(2014, 2, 1)
                                                }
                        };

            var tweetText = UIHelpers.GetTweetText(model);

            Assert.AreEqual(
                "I have 13 more dark journeys to work and 54 more light journeys back home!",
                tweetText);
        }
        public void IndexGetsLocationfromLocationServiceIfNotSet()
        {
            var toWork = "0715";
            var fromWork = "0830";

            var model = new CommuteInfoModel
                        {
                            h = toWork,
                            w = fromWork,
                            d = WorkingDaysMondayOnly,
                            y = _location.Latitude,
                            x = _location.Longitude,
                        };

            var actionResult = _homeController.Index(model);

            var returnedModel = (CommuteInfoModel)((ViewResult)actionResult).Model;

            _geoServiceMock.Verify(s => s.GetLocationFromIPAddress(null), Times.Never());
            Assert.IsTrue(_homeController.ModelState.IsValid);
            Assert.AreEqual(toWork, returnedModel.h);
            Assert.AreEqual(fromWork, returnedModel.w);
            Assert.AreEqual(WorkingDaysMondayOnly, returnedModel.d);
            Assert.AreEqual(_location.Latitude, returnedModel.y);
            Assert.AreEqual(_location.Longitude, returnedModel.x);
        }
        public void IndexDoesNotReturnJourneysOverlapErrorIfJourneysDoNotOverlap()
        {
            var model = new CommuteInfoModel { h = "0700", w = "1830", j = 30, d = WorkingDaysMondayOnly };

            _homeController.Index(model);

            Assert.IsFalse(_homeController.ModelState.Keys.Contains("JourneysOverlap"));
        }
Example #4
0
        private void SetLocation(CommuteInfoModel model)
        {
            var ipAddress = Request.IsLocal ? "82.44.44.102" : Request.UserHostAddress;

            var location = _geoService.GetLocationFromIPAddress(ipAddress);

            model.y = location.HasValue ? Math.Round(location.Value.Latitude, 2) : default(double?);
            model.x = location.HasValue ? Math.Round(location.Value.Longitude, 2) : default(double?);
        }
Example #5
0
        public ActionResult Index(CommuteInfoModel model)
        {
            if (model == null || model.HasDefaultValues())
            {
                model = new CommuteInfoModel
                {
                    h = "0800",
                    w = "1830",
                    j = 30,
                    d = WorkingDays.Monday | WorkingDays.Tuesday | WorkingDays.Wednesday | WorkingDays.Thursday | WorkingDays.Friday
                };

                SetLocation(model);

                ModelState.Clear();

                return View(model);
            }

            if (!model.y.HasValue || !model.x.HasValue) // shouldn't happen, but could
            {
                SetLocation(model);
            }

            DateTime outboundCommuteStart;
            DateTime returnCommuteStart;
            model.Validate(ModelState, out outboundCommuteStart, out returnCommuteStart);

            if (ModelState.IsValid)
            {
                try
                {
                    var daylightHunter = new DaylightHunter();

                    var outboundCommuteEnd = outboundCommuteStart.AddMinutes(model.j);
                    var returnCommuteEnd = returnCommuteStart.AddMinutes(model.j);

                    var location = new Location { Latitude = model.y.Value, Longitude = model.x.Value };

                    model.TimeZoneId = _geoService.GetTimeZoneId(location);

                    var toWorkDaylightInfo = daylightHunter.GetDaylight(location, model.TimeZoneId, outboundCommuteStart, outboundCommuteEnd);
                    var fromWorkDaylightInfo = daylightHunter.GetDaylight(location, model.TimeZoneId, returnCommuteStart, returnCommuteEnd);

                    model.ToWorkDaylights = Builders.BuildDaylightInfoModel(DateTime.Now.Date, toWorkDaylightInfo, Commute.ToWork, model.d);
                    model.FromWorkDaylights = Builders.BuildDaylightInfoModel(DateTime.Now.Date, fromWorkDaylightInfo, Commute.FromWork, model.d);
                }
                catch (UserDisplayableException ex)
                {
                    ModelState.AddModelError("UserDisplayable", ex.Message);
                }
            }

            return View(model);
        }
        public void IndexDoesNotCallLocationServiceIfLocationIsSet()
        {
            var model = new CommuteInfoModel { h = "0700", w = "1800", d = WorkingDaysMondayOnly };

            var actionResult = _homeController.Index(model);

            var returnedModel = (CommuteInfoModel)((ViewResult)actionResult).Model;

            _geoServiceMock.Verify(s => s.GetLocationFromIPAddress(null), Times.Once());
            Assert.IsTrue(_homeController.ModelState.IsValid);
            Assert.AreEqual(_location.Latitude, returnedModel.y);
            Assert.AreEqual(_location.Longitude, returnedModel.x);
        }
Example #7
0
        public void GetTweetTextShouldWorkWhenJourneyFromWorkAlwaysInLight()
        {
            var model = new CommuteInfoModel
                        {
                            ToWorkDaylights = new DaylightInfoModel
                                              {
                                                  IsCurrentlyInDaylight = false,
                                                  CommuteType = Commute.ToWork,
                                                  NumberOfDaysToTransition = 54,
                                                  NextWorkingDayDaylightTransition = new DateTime(2014, 2, 1)
                                              },
                            FromWorkDaylights = new DaylightInfoModel
                                                {
                                                    IsCurrentlyInDaylight = true,
                                                    CommuteType = Commute.FromWork,
                                                }
                        };

            var tweetText = UIHelpers.GetTweetText(model);

            Assert.AreEqual(
                "I have 54 more dark journeys to work and my journey back home is always in the light!",
                tweetText);
        }
        public void IndexReturnsJourneysOverlapErrorIfJourneysOverlap()
        {
            var model = new CommuteInfoModel { h = "0700", w = "0730", j = 60, d = WorkingDaysMondayOnly };

            _homeController.Index(model);

            Assert.IsTrue(_homeController.ModelState.Keys.Contains("JourneysOverlap"));
        }
        public void IndexReturnsWorkingDaysErrorWhenNoWorkingDaysSet()
        {
            var model = new CommuteInfoModel { h = "0700", w = "1800" };

            _homeController.Index(model);

            Assert.IsTrue(_homeController.ModelState.Keys.Contains("WorkingDays"));
        }
        public void IndexReturnsLocationErrorWhenLocationNotFound()
        {
            var model = new CommuteInfoModel { h = "0700", d = WorkingDaysMondayOnly };

            _homeController.Index(model);

            Assert.IsTrue(_homeController.ModelState.Keys.Contains("Location"));
        }
Example #11
0
        public void GetTweetTextShouldWorkWhenJourneyToWorkAlwaysInDarkAndFromWorkAlwaysInLight()
        {
            var model = new CommuteInfoModel
                        {
                            ToWorkDaylights = new DaylightInfoModel
                                              {
                                                  IsCurrentlyInDaylight = false,
                                                  CommuteType = Commute.ToWork,
                                              },
                            FromWorkDaylights = new DaylightInfoModel
                                                {
                                                    IsCurrentlyInDaylight = true,
                                                    CommuteType = Commute.FromWork,
                                                }
                        };

            var tweetText = UIHelpers.GetTweetText(model);

            Assert.AreEqual(
                "My journey to work is always in the dark and my journey back home is always in the light!",
                tweetText);
        }
Example #12
0
        public static string GetTweetText(CommuteInfoModel model)
        {
            string tweetText;

            if (!model.ToWorkDaylights.NextWorkingDayDaylightTransition.HasValue &&
                !model.FromWorkDaylights.NextWorkingDayDaylightTransition.HasValue &&
                model.ToWorkDaylights.IsCurrentlyInDaylight == model.FromWorkDaylights.IsCurrentlyInDaylight)
            {
                tweetText = string.Format(
                    "My journeys {0} and {1} are always in the {2}!",
                    GetCommuteText(model.ToWorkDaylights.CommuteType),
                    GetCommuteText(model.FromWorkDaylights.CommuteType),
                    GetDaylightText(model.ToWorkDaylights.IsCurrentlyInDaylight));
            }
            else
            {
                var prefixToWorkWithIHave = model.ToWorkDaylights.NextWorkingDayDaylightTransition.HasValue;
                var prefixFromWorkWithIHave = !prefixToWorkWithIHave && model.FromWorkDaylights.NextWorkingDayDaylightTransition.HasValue;

                tweetText = string.Format(
                    "{0} and {1}!",
                    GetDaylightInfoTweetText(model.ToWorkDaylights, prefixToWorkWithIHave),
                    GetDaylightInfoTweetText(model.FromWorkDaylights, prefixFromWorkWithIHave));
            }

            return tweetText;
        }