public void Copy_Route()
        {
            // Arange
            int routeId = 1;
            string routeCode = "RouteCode1";
            string routeName = "RouteName1";
            string newRouteName = "00:10 Pier 1 to Pier 5";
            DateTime startDate = new DateTime(2013, 06, 01);
            DateTime endDate = new DateTime(2013, 07, 01);

            int timeTableId = 1;
            string timeTableCode = "TimeTableCode1";
            string timeTableName = "TimeTableName1";
            TimeTable TimeTable = new TimeTable { Id = timeTableId, Code = timeTableCode, Name = timeTableName };

            List<Schedule> schedules = new List<Schedule>();

            Route oldRoute = new Route() 
                { 
                    Id = routeId ,
                    Code = routeCode,
                    Name = routeName,
                    TimeTableId = timeTableId,
                    TimeTable = TimeTable
                };

            string[] pierNames = { "Pier 1", "Pier 2", "Pier 3", "Pier 4", "Pier 5" };
            int[] pierIds = { 1, 2, 3, 4, 5 };

            for (int i = 0; i < 5; i++)
            {
                schedules.Add(new Schedule
                {
                    Id = i,
                    Name = "Schedule " + i.ToString(),
                    Pier = new Pier { Id = pierIds[i], Name = pierNames[i] },
                    Route = oldRoute,
                    Time = new TimeSpan(i, 0, 0)
                });
            }

            oldRoute.Schedules = schedules;

            TimeSpan increment = new TimeSpan(0, 10, 0);

            // Act
            Route newRoute = oldRoute.Copy(increment);
            var timeTableIsCorrect = oldRoute.Schedules.Zip(newRoute.Schedules, (s1, s2) => s1.Time == (s2.Time - increment));

            // Assert
            Assert.AreNotSame(newRoute, oldRoute); // Not the same object
            Assert.IsTrue(timeTableIsCorrect.All(timeTable => timeTable)); // Increment has been applied

            // Check properties
            Assert.AreEqual(newRoute.Id, routeId);
            Assert.AreEqual(newRoute.Code, routeCode);
            Assert.AreEqual(newRoute.Name, newRouteName);
            Assert.AreEqual(newRoute.TimeTableId, timeTableId);
        }
        public ActionResult Create(TimeTable timeTable)
        {
            if (ModelState.IsValid)
            {
                timeTableRepository.UpdateWithGraph(timeTable);
                timeTableRepository.SaveChanges();
                return RedirectToAction("Index");
            }

            return View("Create", timeTable);
        }
        public void Create_Returns_To_Index_When_Valid()
        {
            // Arange
            TimeTable validTimeTableToAdd = new TimeTable();
            string expectedAction = "Index";

            TimeTableController timeTableController = unityContainer.Resolve<TimeTableController>();

            // Act
            var result = timeTableController.Create(validTimeTableToAdd) as RedirectToRouteResult;

            // Asert
            Assert.IsNotNull(result, "Expected RedirectToRouteResult to be returned");
            Assert.AreEqual(expectedAction, result.RouteValues["Action"]);
        }
        public void Create_Returns_To_Create_When_InValid()
        {
            // Arange
            TimeTable validTimeTableToAdd = new TimeTable();

            string expectedViewName = "Create";
            TimeTableController timeTableController = unityContainer.Resolve<TimeTableController>();
            timeTableController.ModelState.AddModelError("Test Model Error", "An Error to test an invalid Model");

            // Act
            var result = timeTableController.Create(validTimeTableToAdd) as ViewResult;

            // Asert
            Assert.IsNotNull(result, "Expected ViewResult to be returned");
            Assert.AreEqual(expectedViewName, result.ViewName);
        }
        public void Add_Schedule()
        {
            Pier pier = new Pier { Name = "Pier Test" };
            TimeTable timeTable = new TimeTable { Name = "Timetable Test" };
            Route route = new Route { Name = "Route Test", TimeTable = timeTable };

            // Add to database
            using (ThamesClipperContext ctx = new ThamesClipperContext())
            {
                ScheduleRepository scheduleRepository = new ScheduleRepository(ctx);
                scheduleRepository.Add(new Schedule { Pier = pier, Route = route, Time = new TimeSpan(1, 0, 0) });
                scheduleRepository.SaveChanges();
            }

            // Check that it is there
            using (ThamesClipperContext ctx = new ThamesClipperContext())
            {
                List<Schedule> schedules = ctx.Schedules.Where(schedule => schedule.Pier.Id == pier.Id && schedule.Route.Id == route.Id).ToList();
                Assert.AreEqual(1, schedules.Count());
            }
        }
        public void Add_Or_Update_Schedule()
        {
            Pier pier = new Pier { Name = "Pier" };
            TimeTable timeTable = new TimeTable { Name = "TimeTable" };
            Route routeExisting = new Route { Name = "Route1", TimeTable = timeTable };
            Route routeAdd = new Route { Name = "Route2", TimeTable = timeTable };

            TimeSpan timeExisting = new TimeSpan(13, 0, 0);
            TimeSpan timeAdd = new TimeSpan(14, 0, 0);
            TimeSpan timeUpdated = new TimeSpan(15, 0, 0);

            Schedule existing = new Schedule { Pier = pier, Route = routeExisting, Time = timeExisting };
            Schedule add = new Schedule { Pier = pier, Route = routeAdd, Time = timeAdd };

            // Add the existing one to database
            using (ThamesClipperContext ctx = new ThamesClipperContext())
            {
                ScheduleRepository scheduleRepository = new ScheduleRepository(ctx);
                scheduleRepository.Add(existing);
                scheduleRepository.SaveChanges();
            }

            // Find and update it
            using (ThamesClipperContext ctx = new ThamesClipperContext())
            {
                ScheduleRepository scheduleRepository = new ScheduleRepository(ctx);
                existing.Time = timeUpdated;
                scheduleRepository.AddOrUpate(existing);
                scheduleRepository.AddOrUpate(add);
                scheduleRepository.SaveChanges();
            }

            // Check that the new name is there
            using (ThamesClipperContext ctx = new ThamesClipperContext())
            {
                var schedulesExisting = ctx.Schedules.Where(schedule => schedule.Pier.Id == pier.Id && schedule.Route.Id == routeExisting.Id).ToList();
                Assert.AreEqual(1, schedulesExisting.Count());
                Assert.AreEqual(timeUpdated, schedulesExisting[0].Time);

                var schedulesAdd = ctx.Schedules.Where(schedule => schedule.Pier.Id == pier.Id && schedule.Route.Id == routeAdd.Id).ToList();
                Assert.AreEqual(1, schedulesAdd.Count());
            }
        }
        public ActionResult Edit(TimeTable timeTable)
        {
            if (ModelState.IsValid && timeTable != null)
            {
                timeTableRepository.UpdateWithGraph(timeTable);
                timeTableRepository.SaveChanges();

                return RedirectToAction("Index");
            }
            return View("Edit", timeTable);
        }
 public ActionResult Create()
 {
     TimeTable timeTable = new TimeTable();
     timeTable.PopulateMissingStops(pierRepository.List());
     return View("Create", timeTable);
 }
 public void TimeTable_Is_Not_Active_On_Day()
 {
     TimeTable timeTable = new TimeTable { Tuesday = true };
     Assert.IsFalse(timeTable.IsActiveOn(DayOfWeek.Monday));
 }
 public void TimeTable_Is_Active_On_Day()
 {
     TimeTable timeTable = new TimeTable { Monday = true };
     Assert.IsTrue(timeTable.IsActiveOn(DayOfWeek.Monday));
 }
        public void Create_Calls_Save_When_Valid()
        {
            // Arange
            TimeTable validTimeTableAdd = new TimeTable();

            TimeTableController timeTableController = unityContainer.Resolve<TimeTableController>();

            // Act
            var result = timeTableController.Create(validTimeTableAdd) as RedirectToRouteResult;

            // Asert
            mockTimeTableRepository.Verify(x => x.UpdateWithGraph(validTimeTableAdd), Times.Once());
            mockTimeTableRepository.Verify(x => x.SaveChanges());
        }
        public void Edit_Does_Not_Calls_Save_When_InValid()
        {
            // Arange
            TimeTable validTimeTableToEdit = new TimeTable { Id = 1, Name = "Route 1" };

            TimeTableController timeTableController = unityContainer.Resolve<TimeTableController>();
            timeTableController.ModelState.AddModelError("Test Model Error", "An Error to test an invalid Model");

            // Act
            var result = timeTableController.Edit(validTimeTableToEdit) as ViewResult;

            // Asert
            mockTimeTableRepository.Verify(x => x.Update(validTimeTableToEdit), Times.Never());
            mockTimeTableRepository.Verify(x => x.SaveChanges(), Times.Never());
        }
        public void Edit_Calls_Edit_When_No_Stops()
        {
            // Arange
            TimeTable validTimeTableToEdit = new TimeTable { Id = 1, Name = "Route 1" };

            TimeTableController timeTableController = unityContainer.Resolve<TimeTableController>();

            // Act
            var result = timeTableController.Edit(validTimeTableToEdit) as RedirectToRouteResult;

            // Asert
            mockTimeTableRepository.Verify(x => x.UpdateWithGraph(validTimeTableToEdit), Times.Once());
            mockTimeTableRepository.Verify(x => x.SaveChanges(), Times.Once());
        }
        public void Create_Does_Not_Calls_Save_When_InValid()
        {
            // Arange
            TimeTable validTimeTableAdd = new TimeTable();

            TimeTableController timeTableController = unityContainer.Resolve<TimeTableController>();
            timeTableController.ModelState.AddModelError("Test Model Error", "An Error to test an invalid Model");

            // Act
            var result = timeTableController.Create(validTimeTableAdd) as ViewResult;

            // Asert
            mockTimeTableRepository.Verify(x => x.Add(validTimeTableAdd), Times.Never());
            mockTimeTableRepository.Verify(x => x.SaveChanges(), Times.Never());
        }