public override void When() { var quizDate = TestValues.CARD_QUIZ_DATE; this.quizResult = new QuizResult { IsCorrect = false }; this.card = new Card { Level = CARD_LEVEL, QuizDate = quizDate }; this.QuizResultHandler.ApplyQuizResult(this.quizResult, this.card, quizDate.Year, quizDate.Month, quizDate.Day); }
public override void When() { var quizDate = TestValues.CARD_QUIZ_DATE; this.quizResult = new QuizResult { IsCorrect = true }; this.cardLevel = this.UserConfiguration.QuizCalendar.Count; this.card = new Card { Level = this.cardLevel, QuizDate = quizDate }; this.QuizResultHandler.ApplyQuizResult(this.quizResult, this.card, quizDate.Year, quizDate.Month, quizDate.Day); }
public void ReverseQuizResult(QuizResult quizResult, Card card) { card.Level = quizResult.CardLevel; card.QuizDate = quizResult.CardQuizDate; card.IsCorrect = quizResult.CardIsCorrect; card.CompletedQuizYear = quizResult.CardCompletedQuizYear; card.CompletedQuizMonth = quizResult.CardCompletedQuizMonth; card.CompletedQuizDay = quizResult.CardCompletedQuizDay; }
public override void When() { this.TableStorageContext.Setup(x => x.Cards.GetById(TestValues.USER_ID, TestValues.CARD_ID)).Returns((Card)null); this.quizResult = new QuizResult(); this.QuizResultsController.Put( TestValues.USER_ID, TestValues.YEAR, TestValues.MONTH, TestValues.DAY, TestValues.CARD_ID, this.quizResult); }
public override void When() { this.TableStorageContext.Setup(x => x.QuizResults.GetForQuiz(TestValues.YEAR, TestValues.MONTH, TestValues.DAY)) .Returns(new[] {new QuizResult {CardId = TestValues.CARD_ID}}.AsQueryable()); this.quizResult = new QuizResult(); this.QuizResultsController.Post( TestValues.USER_ID, TestValues.YEAR, TestValues.MONTH, TestValues.DAY, TestValues.CARD_ID, this.quizResult); }
public override void When() { this.quizResult = new QuizResult { IsCorrect = true }; this.card = new Card { Level = CARD_LEVEL, QuizDate = TestValues.CARD_QUIZ_DATE, IsCorrect = false, CompletedQuizYear = TestValues.YEAR, CompletedQuizMonth = TestValues.MONTH, CompletedQuizDay = TestValues.DAY }; this.QuizResultHandler.ApplyQuizResult(this.quizResult, this.card, TestValues.CARD_QUIZ_DATE.Year, TestValues.CARD_QUIZ_DATE.Month, TestValues.CARD_QUIZ_DATE.Day); }
public override void When() { this.quizResult = new QuizResult { IsCorrect = true, CardQuizDate = TestValues.CARD_QUIZ_DATE, CardLevel = TestValues.CARD_QUIZ_LEVEL, CardIsCorrect = true, CardCompletedQuizYear = TestValues.YEAR, CardCompletedQuizMonth = TestValues.MONTH, CardCompletedQuizDay = TestValues.DAY }; this.QuizResultHandler.ReverseQuizResult(this.quizResult, this.card); }
public override void When() { var quizResults = Builder<QuizResult>.CreateListOfSize(3) .All().With(x => x.UserId = TestValues.USER_ID) .Random(1).With(x => x.CardId = TestValues.CARD_ID) .Build(); this.TableStorageContext .Setup(x => x.QuizResults.GetForQuiz(TestValues.YEAR, TestValues.MONTH, TestValues.DAY)) .Returns(quizResults.AsQueryable()); this.quizResult = this.QuizResultsController.Get( TestValues.USER_ID, TestValues.YEAR, TestValues.MONTH, TestValues.DAY, TestValues.CARD_ID); }
public void ApplyQuizResult(QuizResult quizResult, Card card, int year, int month, int day) { // Set current card values on QuizResult so it can be reversed quizResult.CardLevel = card.Level >= 0 ? card.Level : 0; quizResult.CardQuizDate = card.QuizDate; quizResult.CardIsCorrect = card.IsCorrect; quizResult.CardCompletedQuizYear = card.CompletedQuizYear; quizResult.CardCompletedQuizMonth = card.CompletedQuizMonth; quizResult.CardCompletedQuizDay = card.CompletedQuizDay; card.Level = quizResult.IsCorrect ? card.Level + 1 : 0; if(card.Level < 0) card.Level = 0; var daysQuizExtended = this.QuizCalendar.GetQuizInterval(card.Level); card.QuizDate = new DateTime(year, month, day).AddDays(daysQuizExtended); card.IsCorrect = quizResult.IsCorrect; card.CompletedQuizYear = year; card.CompletedQuizMonth = month; card.CompletedQuizDay = day; }
public void QuizResultsController_CRUD() { var quizDate = new DateTime(2012, 6, 30); var container = IoC.Initialize(); var config = new HttpConfiguration { DependencyResolver = new StructureMapWebApiResolver(container) }; config.Formatters.JsonFormatter.MediaTypeMappings.Add(new UriPathExtensionMapping("json", "application/json")); config.Formatters.XmlFormatter.MediaTypeMappings.Add(new UriPathExtensionMapping("xml", "application/xml")); WebApiConfig.Configure(config); var server = new HttpServer(config); var client = new HttpClient(server); client.DefaultRequestHeaders.Add(HttpHeaders.X_CLIENT_DATE, DateTime.Now.ToLongDateString()); client.DefaultRequestHeaders.Add(HttpHeaders.X_TEST, "true"); // Create the card var card = new Card { Question = "Created from QuizResultsController_CRUD test.", QuizDate = DateTime.Now }; var postCardResponse = client.PostAsJsonAsync(TestUrls.CARDS, card).Result; var postCard = postCardResponse.Content.ReadAsAsync<Card>().Result; var cardUrl = postCardResponse.Headers.Location; // Create the QuizResult var quizResult = new QuizResult { IsCorrect = true }; // Test POST // ------------------------------------------------------------------------------------- var testUrl = string.Format(TestUrls.QUIZ_RESULTS, postCard.UserId, quizDate.Year, quizDate.Month, quizDate.Day, postCard.EntityId); var postResponse = client.PostAsJsonAsync(testUrl, quizResult).Result; var postQuizResult = postResponse.Content.ReadAsAsync<QuizResult>().Result; // Assert that the POST succeeded postResponse.StatusCode.Should().Be(HttpStatusCode.Created); // Assert that the posted QuizResult was returned in the response postQuizResult.QuizDate.Should().Be(quizDate); // Assert that the relevant fields were set on the QuizResult postQuizResult.PartitionKey.Should().NotBeEmpty(); postQuizResult.RowKey.Should().NotBeEmpty(); postQuizResult.QuizDate.Should().Be(quizDate); postQuizResult.CardId.Should().Be(postCard.EntityId); // Assert that the location of the new QuizResult was returned in the Location header var quizResultUrl = postResponse.Headers.Location; quizResultUrl.AbsoluteUri.Should().BeEquivalentTo(testUrl); // Test DELETE // ------------------------------------------------------------------------------------- var deleteResponse = client.DeleteAsync(quizResultUrl).Result; // Assert that the DELETE succeeded deleteResponse.StatusCode.Should().Be(HttpStatusCode.NoContent); // Assert that the Card is no longer in storage var getResponse = client.GetAsync(quizResultUrl).Result; getResponse.IsSuccessStatusCode.Should().Be(false); // Delete the card var requestMessage = new HttpRequestMessage(HttpMethod.Delete, cardUrl); var cardJson = JsonConvert.SerializeObject(card); requestMessage.Content = new StringContent(cardJson); requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("text/json"); client.SendAsync(requestMessage); // client.DeleteAsync(cardUrl); }
public HttpResponseMessage Post(int userId, int year, int month, int day, int cardId, QuizResult quizResult) { if (this.ModelState.IsValid) { quizResult.QuizDate = new DateTime(year, month, day); quizResult.QuizYear = year; quizResult.QuizMonth = month; quizResult.QuizDay = day; quizResult.UserId = userId; quizResult.CardId = cardId; // Ensure that a QuizResult doesn't already exist for this user for this card for this day var existingQuizResult = this.TableStorageContext.QuizResults .GetForQuiz(year, month, day) .Where(x => x.CardId == cardId) .FirstOrDefault(); if (existingQuizResult != null) { var eTagHeader = this.Request.Headers.IfNoneMatch.FirstOrDefault(); var responseStatusCode = eTagHeader != null && eTagHeader.Tag == "*" ? HttpStatusCode.PreconditionFailed : HttpStatusCode.Conflict; var httpResponseMessage = new HttpResponseMessage(responseStatusCode); var existingRouteValues = new {userId, year, month, day, cardId}; httpResponseMessage.Headers.Location = this.GetQuizResultUri(existingRouteValues); throw new HttpResponseException(httpResponseMessage); } var card = GetCard(userId, cardId, this.TableStorageContext); this.quizResultHandler.ApplyQuizResult(quizResult, card, year, month, day); this.TableStorageContext.QuizResults.Add(quizResult); this.TableStorageContext.Cards.Update(card); this.TableStorageContext.CommitBatch(); // Create the response var response = this.Request.CreateResponse(HttpStatusCode.Created, quizResult); var routeValues = new { userId, year, month, day, cardId }; response.Headers.Location = this.GetQuizResultUri(routeValues); return response; } throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)); }
public HttpResponseMessage Put(int userId, int year, int month, int day, int cardId, QuizResult quizResult) { if (this.ModelState.IsValid) { // Maybe do some sort of valid state check at the beginning of this // method and throw exception if not valid. Check: card exists, quiz result exists, quizResult param // contains valid data (keys not null, etc). var card = GetCard(userId, cardId, this.TableStorageContext); // We are updating the QuizResult for this card so first reverse the previous result then apply the new one var existingQuizResult = this.TableStorageContext.QuizResults.Get(quizResult.PartitionKey, quizResult.RowKey); this.quizResultHandler.ReverseQuizResult(existingQuizResult, card); this.TableStorageContext.Detach(existingQuizResult); this.quizResultHandler.ApplyQuizResult(quizResult, card, year, month, day); this.TableStorageContext.QuizResults.Update(quizResult); this.TableStorageContext.Cards.Update(card); this.TableStorageContext.CommitBatch(); // Create the response var response = this.Request.CreateResponse(HttpStatusCode.OK, quizResult); var routeValues = new { userId, year, month, day, cardId }; response.Headers.Location = this.GetQuizResultUri(routeValues); return response; } throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)); }