Exemple #1
0
        private Tester _save(Tester tester)
        {
            var exTester = this._db.Testers.Where(tt => tt.TesterId == tester.TesterId)
                           .Include(tt => tt.TesterQuestions)
                           .SingleOrDefault();

            if (exTester != null)
            {
                this._db.Entry(exTester).CurrentValues.SetValues(tester);
                foreach (var tq in tester.TesterQuestions)
                {
                    tq.TesterId = tester.TesterId;
                    var exTq = exTester.TesterQuestions
                               .Where(c => (c.TesterId == tq.TesterId && c.QuestionId == tq.QuestionId))
                               .SingleOrDefault();
                    if (exTq != null)
                    {
                        this._db.Entry(exTq).CurrentValues.SetValues(tq);
                        exTq.AnswerId   = tq.AnswerId;
                        exTq.QuestionId = tq.QuestionId;
                        exTq.TesterId   = tq.TesterId;
                    }
                    else
                    {
                        var newTq = new TesterQuestion
                        {
                            TesterId   = tq.TesterId,
                            QuestionId = tq.QuestionId,
                            AnswerId   = tq.Choice.ChoiceId,
                        };
                        exTester.TesterQuestions.Add(newTq);
                    }
                }
                tester = exTester;
            }

            return(tester);
        }
        private void mockTester()
        {
            Tester tester = new Tester
            {
                Name        = "Tester without test save",
                IsCompleted = false
            };

            this._service.GetContext().Testers.Add(tester);

            tester = new Tester
            {
                Name        = "Tester with some test save",
                IsCompleted = false
            };
            var questions = this._service.Quiz();
            int tmpCount  = 0;

            foreach (var qt in questions)
            {
                TesterQuestion tq = new TesterQuestion
                {
                    Question = qt,
                    Choice   = null
                };
                if (tmpCount < 2)
                {
                    tq.Choice = qt.Choices.ElementAt(tmpCount);
                }
                tmpCount++;
                tester.TesterQuestions.Add(tq);
            }
            this._service.GetContext().Testers.Add(tester);

            tester = new Tester
            {
                Name        = "Tester with complete submit but not cal total yet",
                IsCompleted = true
            };
            foreach (var qt in questions)
            {
                TesterQuestion tq = new TesterQuestion
                {
                    Question = qt,
                    Choice   = qt.Choices.ElementAt(3)
                };
                tester.TesterQuestions.Add(tq);
            }
            this._service.GetContext().Testers.Add(tester);

            tester = new Tester
            {
                Name        = "Tester with complete submitted",
                IsCompleted = true
            };
            foreach (var qt in questions)
            {
                TesterQuestion tq = new TesterQuestion
                {
                    Question = qt,
                    Choice   = qt.Choices.ElementAt(3)
                };
                tester.TesterQuestions.Add(tq);
            }
            tester.Score      = 40;
            tester.TotalScore = 50;
            this._service.GetContext().Testers.Add(tester);

            tester = new Tester
            {
                Name        = "Tester with complete Full score",
                IsCompleted = true
            };
            foreach (var qt in questions)
            {
                TesterQuestion tq = new TesterQuestion
                {
                    Question = qt,
                    Choice   = qt.Choices.ElementAt(4)
                };
                tester.TesterQuestions.Add(tq);
            }
            tester.Score      = 50;
            tester.TotalScore = 50;
            this._service.GetContext().Testers.Add(tester);

            this._service.GetContext().SaveChanges();
        }