public void ContestantRunViewModelCreate_Judge1Contestant2_Scores()
        {
            var tourney = Factory.CreateStartedTournament(4, 1, 2) // results in 2 heats
                          .WithJudges(2)
                          .WithJudgingCriteria(2);

            // Adding a bunch of judgings. Some which should be returned, some which should be filtered out
            var rc1 = tourney.GetCurrentRound().GetRoundContestantGuarded(1);
            var rc2 = tourney.GetCurrentRound().GetRoundContestantGuarded(2);

            rc1.RunJudgings.Add(new RunJudging {
                JudgeId = 1, RoundContestantId = rc1.Id, RunNo = 1, CriterionId = 1, Score = 641
            });
            rc1.RunJudgings.Add(new RunJudging {
                JudgeId = 1, RoundContestantId = rc1.Id, RunNo = 2, CriterionId = 1, Score = 12
            });
            rc1.RunJudgings.Add(new RunJudging {
                JudgeId = 1, RoundContestantId = rc1.Id, RunNo = 1, CriterionId = 2, Score = 67
            });
            rc1.RunJudgings.Add(new RunJudging {
                JudgeId = 2, RoundContestantId = rc1.Id, RunNo = 1, CriterionId = 2, Score = 765
            });
            rc2.RunJudgings.Add(new RunJudging {
                JudgeId = 1, RoundContestantId = rc2.Id, RunNo = 1, CriterionId = 3, Score = 777
            });

            var target = ContestantRunViewModel.CreateListOfCurrentConestants(tourney, judgeId: 2);

            Assert.AreEqual(2, target.Count, "2 Contestants");
            Assert.AreEqual(2, target[0].Scores.Count, "scorecount");
            Assert.AreEqual(2, target[1].Scores.Count, "0 scores for contestant 1");
            Assert.AreEqual(null, target[0].Scores[0].Score);
            Assert.AreEqual(765, target[0].Scores[1].Score);
        }
        public void ContestantRunViewModelCreate_Judge2BothContestants_Scores()
        {
            var tourney = Factory.CreateStartedTournament(4, 1, 2) // results in 2 heats
                          .WithJudges(2)
                          .WithJudgingCriteria(3);

            // Adding a bunch of judgings. Some which should be returned, some which should be filtered out
            var rc1 = tourney.GetCurrentRound().GetRoundContestantGuarded(1);
            var rc2 = tourney.GetCurrentRound().GetRoundContestantGuarded(2);

            rc1.RunJudgings.Add(new RunJudging {
                JudgeId = 1, RoundContestantId = rc1.Id, RunNo = 1, CriterionId = 1, Score = 641
            });
            rc1.RunJudgings.Add(new RunJudging {
                JudgeId = 1, RoundContestantId = rc1.Id, RunNo = 2, CriterionId = 1, Score = 12
            });
            rc1.RunJudgings.Add(new RunJudging {
                JudgeId = 1, RoundContestantId = rc1.Id, RunNo = 1, CriterionId = 2, Score = 67
            });
            rc1.RunJudgings.Add(new RunJudging {
                JudgeId = 2, RoundContestantId = rc1.Id, RunNo = 1, CriterionId = 2, Score = 765
            });
            rc2.RunJudgings.Add(new RunJudging {
                JudgeId = 1, RoundContestantId = rc2.Id, RunNo = 1, CriterionId = 3, Score = 777
            });

            var target = ContestantRunViewModel.CreateListOfCurrentConestants(tourney, judgeId: 1);

            Assert.AreEqual(2, target.Count, "2 Contestants");
            Assert.AreEqual(3, target[1].Scores.Count, "scoreCount");
            // Expecting all scores objects to be set, but some to have Score=null. cshtml file depends on this
            Assert.AreEqual(null, target[1].Scores[0].Score);
            Assert.AreEqual(null, target[1].Scores[1].Score);
            Assert.AreEqual(777, target[1].Scores[2].Score);
        }
        public void ContestantRunViewModelCreate_Heat1_ContestantNames()
        {
            var tourney = Factory.CreateStartedTournament(4, 1, 2) // results in 2 heats
                          .WithJudges(2)
                          .WithJudgingCriteria(2);

            var target = ContestantRunViewModel.CreateListOfCurrentConestants(tourney, 1);

            Assert.AreEqual(2, target.Count, "2 Contestants");
            Assert.AreEqual("C1", target[0].ContestantName);
            Assert.AreEqual("C2", target[1].ContestantName);
        }
        /// <summary>
        /// Replaces list of runJudging scores as well as calculates total on RoundContestant if done
        /// </summary>
        /// <param name="model"></param>
        public virtual void ReplaceRunJudgings(ContestantRunViewModel model)
        {
            Tournament      tourney    = GetTournamentGuarded(model.TournamentId);
            RoundContestant contestant = GetRoundContestantGuarded(model.RoundContestantId);

            foreach (var score in model.Scores)
            {
                RunJudgings.Replace(score);
            }
            var expectedJudgeEntriesPerRun = tourney.GetExpectedJudgementCountPerRun();

            contestant.CalculateTotalScore(expectedJudgeEntriesPerRun, contestant.Round.RunsPerContestant);
            SaveChanges();
        }
        public void ReplaceRunJudgings_OneEmptyScoringLastOfTwoJudges_NoException()
        {
            var dbMock  = DatabaseFaker.GetFake();
            var db      = dbMock.Object;
            var target  = new TournamentJudgeController(null, db, new Mock <NaHub>().Object);
            var tourney = Factory.CreateStartedTournament(2, 1, 2).WithJudgingCriteria(2).WithJudges(2);

            db.AddTournament(tourney);

            var model = new JudgeViewModel();

            model.Tournament  = tourney;
            model.Contestants = new List <ContestantRunViewModel>();

            // Setup input
            var contestants = tourney.GetRoundNo(1).ContestantEntries.ToList();
            var c1          = new ContestantRunViewModel();

            c1.RoundContestantId = contestants[0].Id;
            c1.TournamentId      = tourney.Id;
            c1.Scores            = new List <RunJudging>()
            {
                new RunJudging()
                {
                    JudgeId = 1, CriterionId = 1, RoundContestantId = c1.RoundContestantId, Score = null
                },
                new RunJudging()
                {
                    JudgeId = 1, CriterionId = 2, RoundContestantId = c1.RoundContestantId, Score = null
                },
                new RunJudging()
                {
                    JudgeId = 2, CriterionId = 1, RoundContestantId = c1.RoundContestantId, Score = null
                },
                new RunJudging()
                {
                    JudgeId = 2, CriterionId = 2, RoundContestantId = c1.RoundContestantId, Score = null
                }
            };
            model.Contestants.Add(c1);
            // Setup GetRoundContestant to return from tourney-bound object
            dbMock.Setup(p => p.GetRoundContestantGuarded(It.IsAny <long>()))
            .Returns <long>(id => contestants.FirstOrDefault(p => p.Id == id));
            foreach (var rj in c1.Scores)
            {
                contestants[0].RunJudgings.Add(rj);                           // This will normally be taken care of by Enitity Framework. Not so in mocked context.
            }
            // Act & Assert
            target.JudgeIndex(model);
        }
        public void ReplaceRunJudgings()
        {
            // Setup input
            var model = new ContestantRunViewModel();

            model.RoundContestantId = 16;
            model.TournamentId      = 136;
            model.Scores            = new System.Collections.Generic.List <RunJudging>()
            {
                new RunJudging()
                {
                    JudgeId = 1, CriterionId = 1, RoundContestantId = model.RoundContestantId, Score = 5.5M
                },
                new RunJudging()
                {
                    JudgeId = 1, CriterionId = 2, RoundContestantId = model.RoundContestantId, Score = 6.5M
                }
            };
            // Setup mocks
            var expectedJudgementCountPerRun = 3;
            var tournamentMock = new Mock <Tournament>();
            var round          = new Round()
            {
                RunsPerContestant = 7
            };
            var rcMock = new Mock <RoundContestant>();

            rcMock.Setup(p => p.Round).Returns(round);
            var contextMock = DatabaseFaker.GetFake();

            contextMock.CallBase = true;
            contextMock.Setup(p => p.GetRoundContestantGuarded(model.RoundContestantId)).Returns(rcMock.Object);
            contextMock.Setup(p => p.GetTournamentGuarded(model.TournamentId)).Returns(tournamentMock.Object);
            tournamentMock.Setup(p => p.GetExpectedJudgementCountPerRun()).Returns(expectedJudgementCountPerRun);

            // Act & Assert
            contextMock.Object.ReplaceRunJudgings(model.TournamentId, model.RoundContestantId, model.Scores);

            rcMock.Verify(p => p.CalculateTotalScore(expectedJudgementCountPerRun, round.RunsPerContestant));
            Assert.AreEqual(2, contextMock.Object.RunJudgings.Count());
        }
        public void IT_PostRunJudgingsFromWeb_FinalizingJudgementOfOneContestant()
        {
            long?id = null;

            try
            {
                var scores        = new ContestantRunViewModel();
                var wrappedScores = new JudgeViewModel();
                // Setup/Act
                using (var db = new NordicArenaDataContext())
                {
                    var tourney = Factory.CreateInitializedTourney();
                    tourney.Contestants.Add(new Contestant("Roger"));
                    tourney.Contestants.Add(new Contestant("Hans"));
                    var judge = new Judge("Dommer");
                    judge.Tournament = tourney;
                    db.Judges.Add(judge);
                    db.Tournaments.Add(tourney);
                    db.SaveChanges();

                    tourney = db.Tournaments.FirstOrDefault(p => p.Id == tourney.Id);
                    tourney.Start();
                    db.SaveChanges();
                    id = tourney.Id;

                    var round1 = tourney.Rounds.FirstOrDefault();
                    round1.RunsPerContestant = 1;
                    var rc1      = round1.ContestantEntries.FirstOrDefault();
                    var critList = tourney.JudgingCriteria.ToList();
                    // Set up scores
                    scores.RoundContestantId = rc1.Id;
                    scores.TournamentId      = tourney.Id;
                    for (int i = 0; i < critList.Count; i++)
                    {
                        var score = new RunJudging();
                        score.CriterionId       = critList[i].Id;
                        score.JudgeId           = judge.Id;
                        score.RoundContestantId = rc1.Id;
                        score.RunNo             = 1;
                        score.Score             = (i + 1);
                        scores.Scores.Add(score);
                    }

                    wrappedScores.Contestants = new List <ContestantRunViewModel>();
                    wrappedScores.Contestants.Add(scores);
                    wrappedScores.Tournament = tourney;
                }
                // Act
                var ctrl = new TournamentJudgeController(null, new EfTournamentService(), ServiceFaker.GetFakeSignalRHub());

                ctrl.JudgeIndex(wrappedScores);
                // Assert
                using (var db = new EfTournamentService())
                {
                    var rc = db.GetRoundContestantGuarded(scores.RoundContestantId);
                    Assert.IsTrue(rc.TotalScore.HasValue);
                }
            }
            catch (DbEntityValidationException exc)
            {
                throw DbValidationExceptionWrapper.Wrap(exc);
            }
            finally
            {
                DatabaseHelper.DeleteTournament(id);
                ServiceFaker.ResetIocContainer();
            }
        }