public void GetAssignmentWithId()
 {
     _contextMock.Setup(m => m.Assignments).Returns(CreateSampleData(2));
     SetupControllerForTests(_controller);
     var assignment = new Assignment{Id = 3, Title = "title 3"};
     var result = _controller.Post(assignment);
     var getresult = _controller.Get(assignment.Id);
     Assert.AreEqual(assignment.Title, getresult.Title);
 }
        public ActionResult Create(Assignment assignment)
        {
            if (ModelState.IsValid){
                _db.Assignments.Add(assignment);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(assignment);
        }
        public static Score CreateScore(Assignment assignment, Participant participant, bool correctOutput, double timeDifference)
        {
            var score = new Score{
                Assignment = assignment,
                IsCorrectOutput = correctOutput,
                Participant = participant,
                TimeSpent = timeDifference
            };

            return score;
        }
 public void SetUp()
 {
     _contextMock = new Mock<IDbContext>();
     _controller = new ScoresController(_contextMock.Object);
     ICollection<Assignment> col = new Collection<Assignment>();
     var ass = new Assignment { Id = 1, Title = "Test", Scores = new Collection<Score>() };
     var participant = new Participant { Id = 1, Email = "vin" };
     ass.Scores.Add(new Score { Assignment = ass, Id = 1, IsCorrectOutput = true, Participant = participant, TimeSpent = 199 });
     col.Add(ass);
     Contest cont = new Contest{ Assignments = col, Id = 1, IsActive = true, Name = "Contest A" } ;
     _scoreCalc = new ParticipantScore(cont, participant);
     FakeDbSet<Contest> condb = new FakeDbSet<Contest>();
     condb.Add(cont);
     FakeDbSet<Participant> pardb = new FakeDbSet<Participant>();
     pardb.Add(participant);
     _leaderboard = new Leaderboard(condb, pardb);
 }
        public void PostAssignmentCallsAddOnContextWithProvidedAssignment()
        {
            _contextMock.Setup(m => m.Assignments.Add(It.IsAny<Assignment>()));
            SetupControllerForTests(_controller);

            var assignment = new Assignment{Id = 1, Title = "title 1"};
            _controller.Post(assignment);

            _contextMock.Verify(m => m.Assignments.Add(It.IsAny<Assignment>()), Times.Exactly(1));
        }
        public ActionResult Edit(int id, Assignment assignment)
        {
            if (ModelState.IsValid){
                var assignmentOld = _db.Assignments.Find(id);

                if (TryUpdateModel(assignmentOld)){
                    _db.SaveChanges();
                    return RedirectToAction("Index");
                }
                ModelState.AddModelError("Internal Error", "Unexpected error while saving to the database.");
            }
            return View(assignment);
        }
        public void PutProgressWhenAlreadyDoingAssignmentIsForbidden()
        {
            SetupControllerForTests(_controller);
            Assignment assignment = new Assignment { Id = 2, MaxSolveTime = 1000 };

            Progress progress = new Progress {Assignment = assignment , Id = 2, Participant = _participant };
            var result = _controller.Put(2, progress);
            Assert.AreEqual(HttpStatusCode.Forbidden, result.StatusCode);
        }
        /// <summary>
        ///     Add an assignment to the database (REDUNDANT)
        /// </summary>
        /// <param name="assignment"></param>
        /// <returns></returns>
        public HttpResponseMessage Post(Assignment assignment)
        {
            if (!ModelState.IsValid || assignment == null)
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            if (_context.Assignments == null)
                throw new HttpResponseException(HttpStatusCode.InternalServerError);

            try{
                _context.Assignments.Add(assignment);
                _context.SaveChanges();
            } catch (Exception){
                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }

            return Request.CreateResponse(HttpStatusCode.Created);
        }
        public void Assignment_PostEditAssignment()
        {
            _contextMock.Setup(m => m.Assignments).Returns(CreateSampleData(5));

            Assignment ass1 = new Assignment { Id = 3, Title = "test" };
            SetupControllerForTests(_controller);

            var result = _controller.Edit(3, ass1) as ViewResult;
            Assert.IsNotNull(_contextMock.Object.Assignments.Find(3));
        }
        public void Assignment_PostCreateAssignment()
        {
            Assignment ass1 = new Assignment { Id = 3, Title = "title 3" };
            SetupControllerForTests(_controller);
            _contextMock.Setup(m => m.Assignments).Returns(CreateSampleData(2));

            Assert.IsNull(_contextMock.Object.Assignments.Find(3));
            var result = _controller.Create(ass1);
            Assert.IsNotNull(_contextMock.Object.Assignments.Find(3));
        }