public void it_should_build_a_flat_zero_line_if_no_donits()
        {
            var now = DateTime.Now;
            var target = new GoalProcessor(now);
            var goal = new Goal() { IntervalType = IntervalType.Weekly };

            var vm= target.ProcessGoal(goal);
            Assert.AreEqual(vm.Graph.Points.Count, 2);
        }
        public void it_should_increase_hours_left_when_adding_a_new_doneit()
        {
            var now = DateTime.Now;
            var target = new GoalProcessor(now);

            // Week
            var goal = new Goal() { Amount = 7, IntervalType = IntervalType.Weekly };
            goal.DoneIts = new List<DoneIt>() { new DoneIt() { Amount = 3.01M, Date = now.AddDays(-7) } };
            goal.DoneIts.Add(new DoneIt() { Amount = 3, Date = now.AddDays(-4) } );
            goal.DoneIts.Add(new DoneIt() { Amount = 1, Date = now.AddDays(-1) });
            var vm = target.ProcessGoal(goal);
            Assert.AreEqual(GoalStatus.OnTrack, vm.Status);

            goal.DoneIts = new List<DoneIt>() { new DoneIt() { Amount = 2.9M, Date = now.AddDays(-7) } };
            goal.DoneIts.Add(new DoneIt() { Amount = 3, Date = now.AddDays(-4) });
            goal.DoneIts.Add(new DoneIt() { Amount = 1, Date = now.AddDays(-1) });
            vm = target.ProcessGoal(goal);
            Assert.AreEqual(GoalStatus.Behind, vm.Status);
        }
        public void it_should_set_value_of_first_point_to_the_amount_the_first_doneit()
        {
            var now = DateTime.Now;
            var target = new GoalProcessor(now);
            var goal = new Goal() { Amount = 7, IntervalType = IntervalType.Weekly };

            goal.DoneIts = new List<DoneIt>() { new DoneIt() { Amount = 10, Date = now.AddDays(-2) } };
            var vm = target.ProcessGoal(goal);
            Assert.AreEqual(10, vm.Graph.Points.Where(p => p.Time == now.AddDays(-2).AddSeconds(1)).FirstOrDefault().Amount);
        }
        public void it_should_calculate_correct_hours_left_to_zero()
        {
            var now = DateTime.Now;
            var target = new GoalProcessor(now);
            var goal = new Goal() { Amount = 7, IntervalType = IntervalType.Weekly };

            // Week
            goal.DoneIts = new List<DoneIt>() { new DoneIt() { Amount = 7.01M, Date = now.AddDays(-7) } };
            var vm = target.ProcessGoal(goal);
            Assert.AreEqual(GoalStatus.OnTrack, vm.Status);

            goal.DoneIts = new List<DoneIt>(){ new DoneIt() { Amount = 6.99M, Date = now.AddDays(-7) }};
            vm = target.ProcessGoal(goal);
            Assert.AreEqual(GoalStatus.Behind, vm.Status);

            // Hour
            goal = new Goal() { Amount = 24, IntervalType = IntervalType.Dayly };
            goal.DoneIts = new List<DoneIt>() { new DoneIt() { Amount = 24.01M, Date = now.AddDays(-1) } };
            vm = target.ProcessGoal(goal);
            Assert.AreEqual(GoalStatus.OnTrack, vm.Status);

            goal.DoneIts = new List<DoneIt>() { new DoneIt() { Amount = 23.99M, Date = now.AddDays(-1) } };
            vm = target.ProcessGoal(goal);
            Assert.AreEqual(GoalStatus.Behind, vm.Status);

            // Month
            goal = new Goal() { Amount = 3, IntervalType = IntervalType.Monthly };
            goal.DoneIts = new List<DoneIt>() { new DoneIt() { Amount = 3.01M, Date = now.AddDays(-30) } };
            vm = target.ProcessGoal(goal);
            Assert.AreEqual(GoalStatus.OnTrack, vm.Status);

            goal.DoneIts = new List<DoneIt>() { new DoneIt() { Amount = 2.99M, Date = now.AddDays(-30) } };
            vm = target.ProcessGoal(goal);
            Assert.AreEqual(GoalStatus.Behind, vm.Status);
        }
        public ActionResult Index()
        {
            var vm = new HomeViewModel();
            var logic = new GoalProcessor();
            var goals =  db.Goals.Include("DoneIts").Where(x => x.UserName == User.Identity.Name && x.Enabled).ToList();
            foreach (var goal in goals)
            {
                vm.Goals.Add(logic.ProcessGoal(goal));
            }
            vm.OverallStatus = logic.FindOverallStatus(vm.Goals);

            return View(vm);
        }
        public void it_should_show_a_stright_line_when_in_cutoff()
        {
            var now = DateTime.Now;
            var target = new GoalProcessor(now);
            var goal = new Goal() { Amount = 7, IntervalType = IntervalType.Weekly };

            // Simple one entry
            // -28,7
            // -21,0
            // -14,-7
            // -7, -14
            // -6, -14 (cutoff)
            // 0, -14 (cutoff)
            goal.DoneIts = new List<DoneIt>() {
                new DoneIt() { Amount = 7, Date = now.AddDays(-28) } ,
                new DoneIt() { Amount = 0, Date = now.AddDays(-21) } ,
                new DoneIt() { Amount = 0, Date = now.AddDays(-14) } ,
                new DoneIt() { Amount = 0, Date = now.AddDays(-7) } ,
            };
            var vm = target.ProcessGoal(goal);
            // 1: 7
            AssertPointOnGraph(vm, 0, 0, now.AddDays(-28));
            AssertPointOnGraph(vm, 1, 7, now.AddDays(-28));
            // 2: 0
            AssertPointOnGraph(vm, 2, 0, now.AddDays(-21));
            AssertPointOnGraph(vm, 3, 0, now.AddDays(-21));
            // 3: 0
            AssertPointOnGraph(vm, 4, -7, now.AddDays(-14));
            AssertPointOnGraph(vm, 5, -7, now.AddDays(-14));
            // 4: 0
            AssertPointOnGraph(vm, 6, -14, now.AddDays(-7));
            AssertPointOnGraph(vm, 7, -14, now.AddDays(-7));
            // Cutoff
            AssertPointOnGraph(vm, 8, -14, now.AddDays(-7));
            // Cutoff
            AssertPointOnGraph(vm, 9, -14, now.AddDays(-7));
            // Final
            AssertPointOnGraph(vm, 10, -14, now.AddDays(0));
        }
        public void it_should_never_go_below_a_configurable_threshold()
        {
            var now = DateTime.Now;
            var target = new GoalProcessor(now);
            var goal = new Goal() { Amount = 7, IntervalType = IntervalType.Weekly };

            // Hour
            goal = new Goal() { Amount = 24, IntervalType = IntervalType.Dayly };
            goal.DoneIts = new List<DoneIt>() { new DoneIt() { Amount = 24.01M, Date = now.AddDays(-1) } };
            var vm = target.ProcessGoal(goal);
            Assert.AreEqual(GoalStatus.OnTrack, vm.Status);
            Assert.IsTrue(Math.Abs(vm.CurrentLevel - 0) < 0.1M);

            goal.DoneIts = new List<DoneIt>() { new DoneIt() { Amount = 23.99M, Date = now.AddDays(-1) } };
            vm = target.ProcessGoal(goal);
            Assert.AreEqual(GoalStatus.Behind, vm.Status);
            Assert.IsTrue(Math.Abs(vm.CurrentLevel - 0) < 0.1M);

            goal.DoneIts = new List<DoneIt>() { new DoneIt() { Amount = 24.01M, Date = now.AddDays(-2) } };
            vm = target.ProcessGoal(goal);
            Assert.IsTrue(Math.Abs(vm.CurrentLevel - -24) < 0.1M);

            // Hour limited by threshold (2*)
            goal.DoneIts = new List<DoneIt>() { new DoneIt() { Amount = 24.01M, Date = now.AddDays(-4) } };
            vm = target.ProcessGoal(goal);
            Assert.IsTrue(Math.Abs(vm.CurrentLevel - -48) < 0.1M);
        }
        public void when_no_doneits_it_should_show_status_not_started()
        {
            var target = new GoalProcessor();
            var goal = new Goal() { Amount = 7, IntervalType = IntervalType.Weekly };
            var now = DateTime.Now;

            var vm = target.ProcessGoal(goal);
            Assert.AreEqual(GoalStatus.NotStarted, vm.Status);
            //goal.DoneIts.Add(new DoneIt() { Amount = 1, Date = now.a
        }
        public void when_first_doneit_it_should_start_counting_down()
        {
            var now = DateTime.Now;
            var target = new GoalProcessor(now);
            var goal = new Goal() { Amount = 7, IntervalType = IntervalType.Weekly };

            goal.DoneIts.Add(new DoneIt() { Amount = 1, Date = now });

            var vm = target.ProcessGoal(goal);
            Assert.AreEqual(GoalStatus.OnTrack, vm.Status);
        }
        public void it_should_show_a_stright_line_when_in_cutoff2()
        {
            var now = DateTime.Now;
            var target = new GoalProcessor(now);
            var goal = new Goal() { Amount = 1, IntervalType = IntervalType.Dayly };

            // Simple one entry
            // -8,1
            goal.DoneIts = new List<DoneIt>() {
                new DoneIt() { Amount = 1, Date = now.AddDays(-10) } ,
            };
            var vm = target.ProcessGoal(goal);
            // Start 0,1
            AssertPointOnGraph(vm, 0, 0, now.AddDays(-10));
            AssertPointOnGraph(vm, 1, 1, now.AddDays(-10));
            //
            AssertPointOnGraph(vm, 2, -2, now.AddDays(-8));
            AssertPointOnGraph(vm, 3, -2, now);
        }
        public void it_should_start_one_interval_before_and_end_one_interval_after_if_interval()
        {
            var now = DateTime.Now;
            var target = new GoalProcessor(now);
            var goal = new Goal() { EvaluationType = GoalEvaluation.Countinious, IntervalType = IntervalType.Weekly };

            var vm = target.ProcessGoal(goal);
            Assert.AreEqual(vm.Graph.Points[0].Time, now.AddDays(-14));
            Assert.AreEqual(vm.Graph.Points[1].Time, now.AddDays(7));
            Assert.AreEqual(0, vm.Graph.Points[0].Amount);
            Assert.AreEqual(0, vm.Graph.Points[1].Amount);

            goal = new Goal() { EvaluationType = GoalEvaluation.Countinious, IntervalType = IntervalType.Monthly };
            vm = target.ProcessGoal(goal);
            Assert.AreEqual(vm.Graph.Points[0].Time, now.AddMonths(-2));
            Assert.AreEqual(vm.Graph.Points[1].Time, now.AddMonths(1));

            goal = new Goal() { EvaluationType = GoalEvaluation.Countinious, IntervalType = IntervalType.Dayly };
            vm = target.ProcessGoal(goal);
            Assert.AreEqual(vm.Graph.Points[0].Time, now.AddDays(-2));
            Assert.AreEqual(vm.Graph.Points[1].Time, now.AddDays(2));
        }
        public void it_should_start_and_end_on_specified_dates_if_timelimited()
        {
            var now = DateTime.Now;
            var target = new GoalProcessor(now);
            var goal = new Goal() { EvaluationType = GoalEvaluation.Timelimited };

            var vm = target.ProcessGoal(goal);
            Assert.AreEqual(0, vm.Graph.Points[0].Amount);
            Assert.AreEqual(0, vm.Graph.Points[1].Amount);

            goal = new Goal() { EvaluationType = GoalEvaluation.Timelimited, StartDate = now.AddDays(-10), EndDate = now.AddDays(10) };
            vm = target.ProcessGoal(goal);
            Assert.AreEqual(vm.Graph.Points[0].Time, now.AddDays(-10));
            Assert.AreEqual(vm.Graph.Points[1].Time, now.AddDays(10));
        }