public void SaveTournament_DeletesRound_ReordersIndicies()
        {
            var updatedTournament = new Tournament();

            updatedTournament.InitializeDefaults();

            var dbMock = DatabaseFaker.GetFake();
            var db     = dbMock.Object;

            updatedTournament.Rounds.First().RoundNo = 2;
            int firstRoundNo = 2;

            dbMock.Setup(p => p.GetTournament(It.IsAny <long>())).Returns(updatedTournament);
            dbMock.Setup(p => p.UpdateTournamentAndRounds(It.IsAny <Tournament>())).Callback <Tournament>(
                (tournament) =>
            {
                firstRoundNo = tournament.Rounds.First().RoundNo;
            });

            var target = new TournamentAdminController(db, null, new Mock <NaHub>().Object);
            var updatedTouranmentModel = new EditTournamentViewModel(updatedTournament);

            target.SaveTournament(updatedTouranmentModel);

            Assert.AreEqual(1, firstRoundNo);
        }
        public void DatabaseFaking_AddAndReadWorks()
        {
            var db = DatabaseFaker.GetFake().Object;

            db.Tournaments.Add(new Tournament());
            var tourneys = db.Tournaments.ToList();

            Assert.AreEqual(1, tourneys.Count);
        }
        public void DeleteJudge_Existing_Ok()
        {
            var context = DatabaseFaker.GetFake().Object;

            context.Judges.Add(new Judge()
            {
                Id = 1
            });
            context.DeleteJudge(1);
        }
        public void JudgementListViewModelCreate_NoJudgements_AllStillListed()
        {
            var db      = DatabaseFaker.GetFake().Object;
            var tourney = Factory.CreateStartedTournament(2, 2, 1)
                          .WithJudges(2)
                          .WithJudgingCriteria(2);

            db.FeedWith(tourney);

            var model = new JudgementListViewModel(tourney, 1);

            Assert.AreEqual(8, model.Judgements.Count);
        }
        public void JudgementListViewModelCreate_16judgementsWith2Criterias_8Lines()
        {
            var dbMock  = DatabaseFaker.GetFake();
            var tourney = Factory.CreateStartedTournament(2, 2, 1)
                          .WithJudges(2)
                          .WithJudgingCriteria(2)
                          .WithJudgementsForRound(1);

            dbMock.Object.FeedWith(tourney);

            var model = new JudgementListViewModel(tourney, 1);

            Assert.AreEqual(8, model.Judgements.Count);
        }
        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 JudgementListViewModelCreate_16judgementsWith2Criterias_2ScoresPerLine()
        {
            var db      = DatabaseFaker.GetFake().Object;
            var tourney = Factory.CreateStartedTournament(2, 2, 1)
                          .WithJudges(2)
                          .WithJudgingCriteria(2)
                          .WithJudgementsForRound(1);

            db.FeedWith(tourney);

            var model = new JudgementListViewModel(tourney, 1);

            var scoreList = model.Judgements.Select(p => p.Scores);

            Assert.IsTrue(scoreList.All(p => p.Count == 2));
        }
        public void StartTournament_SavesAndStarts()
        {
            var dbMock     = DatabaseFaker.GetFake();
            var db         = dbMock.Object;
            var controller = new TournamentAdminController(db, ServiceFaker.GetFakeFormFeedbackHandler(), new Mock <NaHub>().Object);
            var tournament = Factory.CreateInitializedTourney()
                             .WithJudges(1)
                             .WithContestants(4);

            db.AddTournament(tournament);

            var model = new EditTournamentViewModel(tournament);

            model.RoundList[0].ContestantsPerHeat = 3;
            controller.StartTournament(model);

            Assert.AreEqual(3, tournament.GetRoundNo(1).ContestantsPerHeat);
            Assert.AreEqual(TournamentStatus.Running, tournament.Status);
        }
        public void JudgementListViewModelCreate_16judgementsWith2Criterias_GroupByJudgeBeforeContestant()
        {
            var db      = DatabaseFaker.GetFake().Object;
            var tourney = Factory.CreateStartedTournament(2, 2, 1)
                          .WithJudges(2)
                          .WithJudgingCriteria(2)
                          .WithJudgementsForRound(1);

            db.FeedWith(tourney);

            var model = new JudgementListViewModel(tourney, 1);

            Assert.AreEqual("J1", model.Judgements[0].JudgeName);
            Assert.AreEqual("J1", model.Judgements[1].JudgeName);
            Assert.AreEqual(model.Judgements[0].ContestantName, model.Judgements[1].ContestantName);
            Assert.AreEqual(1, model.Judgements[0].RunNo);
            Assert.AreEqual(2, model.Judgements[1].RunNo);
            Assert.AreEqual("J1", model.Judgements[2].JudgeName);
        }
        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 EditTournamentPost_AddDeleteAndReplaceOk()
        {
            var dbMock        = DatabaseFaker.GetFake();
            var db            = dbMock.Object;
            var oldTournament = new Tournament();

            oldTournament.Rounds = new HashSet <Round>();
            oldTournament.Rounds.Add(new Round()
            {
                Id = 1
            });
            oldTournament.Rounds.Add(new Round()
            {
                Id = 2
            });
            db.AddTournament(oldTournament);

            var newTournament = new Tournament();

            newTournament.Rounds = new HashSet <Round>();
            var newTournamentModel = new EditTournamentViewModel(newTournament);

            newTournamentModel.RoundList.Add(new Round()
            {
                Id = 1, Title = "Hi"
            });
            newTournamentModel.RoundList.Add(new Round()
            {
                Id = 3
            });

            var target = new TournamentAdminController(db, ServiceFaker.GetFakeFormFeedbackHandler(), new Mock <NaHub>().Object);

            target.SaveTournament(newTournamentModel);

            Assert.IsTrue(oldTournament.Rounds.Any(p => p.Id == 1), "1");
            Assert.IsFalse(oldTournament.Rounds.Any(p => p.Id == 2), "2");
            Assert.IsTrue(oldTournament.Rounds.Any(p => p.Id == 3), "3");
            Assert.AreEqual("Hi", oldTournament.Rounds.FirstOrDefault(p => p.Id == 1).Title);
        }
        public void JudgementListViewModelCreate_16judgementsWith2Criterias_ScoresOrderedAsCriterias()
        {
            var db      = DatabaseFaker.GetFake().Object;
            var tourney = Factory.CreateStartedTournament(1, 1, 1)
                          .WithJudges(1)
                          .WithJudgingCriteria(5); // Precondition: Judging criterias are generated with ID's in rising order

            if (!tourney.JudgingCriteria.AreInRisingOrder(p => p.Id))
            {
                throw new InvalidOperationException("Expected elements to be in rising order");
            }
            var cont  = tourney.Rounds.First().ContestantEntries.First();
            var crits = tourney.JudgingCriteria.ToList();
            var judge = tourney.Judges.First();

            // Add the scores in random order
            cont.RunJudgings.Add(new RunJudging(cont, judge, crits[2], 1, 5.0M));
            cont.RunJudgings.Add(new RunJudging(cont, judge, crits[1], 1, 6.0M));
            cont.RunJudgings.Add(new RunJudging(cont, judge, crits[4], 1, 7.0M));
            cont.RunJudgings.Add(new RunJudging(cont, judge, crits[3], 1, 8.0M));
            cont.RunJudgings.Add(new RunJudging(cont, judge, crits[0], 1, 9.0M));
            db.FeedWith(tourney);

            var result = new JudgementListViewModel(tourney, 1);

            // Assert that criterias are ordered by CriteriaId
            Assert.AreEqual(crits[0], result.Criteria[0]);
            Assert.AreEqual(crits[1], result.Criteria[1]);
            Assert.AreEqual(crits[2], result.Criteria[2]);
            Assert.AreEqual(crits[3], result.Criteria[3]);
            Assert.AreEqual(crits[4], result.Criteria[4]);
            // Assert that scores are ordered by CriteriaId
            var scores = result.Judgements[0].Scores;

            Assert.AreEqual(9.0M, scores[0]);
            Assert.AreEqual(6.0M, scores[1]);
            Assert.AreEqual(5.0M, scores[2]);
            Assert.AreEqual(8.0M, scores[3]);
            Assert.AreEqual(7.0M, scores[4]);
        }
        public void SaveTournament_StartedTournament_DoesntOverwriteStatus()
        {
            var updatedTournament = new Tournament();

            updatedTournament.InitializeDefaults();
            updatedTournament.Status = TournamentStatus.Prestart;
            var savedTournament = new Tournament();

            savedTournament.InitializeDefaults();
            savedTournament.Status = TournamentStatus.Running;

            var dbMock = DatabaseFaker.GetFake();
            var db     = dbMock.Object;

            dbMock.Setup(p => p.GetTournament(It.IsAny <long>())).Returns(savedTournament);

            var target = new TournamentAdminController(db, ServiceFaker.GetFakeFormFeedbackHandler(), new Mock <NaHub>().Object);

            target.SaveTournament(new EditTournamentViewModel(updatedTournament));

            Assert.AreEqual(TournamentStatus.Running, savedTournament.Status);
        }
        public void SaveTournament_StartedTournament_SavesNewName()
        {
            var updatedTournament = new Tournament();

            updatedTournament.InitializeDefaults();
            updatedTournament.Name = "After";

            var savedTournament = new Tournament();

            savedTournament.InitializeDefaults();
            savedTournament.Name = "Before";

            var dbMock = DatabaseFaker.GetFake();
            var db     = dbMock.Object;

            dbMock.Setup(p => p.GetTournament(It.IsAny <long>())).Returns(savedTournament);

            var target = new TournamentAdminController(db, ServiceFaker.GetFakeFormFeedbackHandler(), new Mock <NaHub>().Object);
            var updatedTouranmentModel = new EditTournamentViewModel(updatedTournament);

            target.SaveTournament(updatedTouranmentModel);

            Assert.AreEqual("After", savedTournament.Name);
        }
        public void DeleteJudge_NonExistant_Exception()
        {
            var context = DatabaseFaker.GetFake().Object;

            context.DeleteJudge(1);
        }