public async Task PutEntryNotSupportedTest() { using DbContext context = CreateContext(); PracticeHistoryEntriesController controller = CreateController(context); bool wasThrown = false; try { ActionResult <PracticeHistoryEntry> result = await controller.PutAsync(entry1); } catch (NotSupportedException) { wasThrown = true; } Assert.IsTrue(wasThrown); }
public async Task GetEntriesTest() { using DbContext context = CreateContext(); PracticeHistoryEntriesController controller = CreateController(context); Dictionary <string, object> parameters = new Dictionary <string, object>(); //Get all entries of user ActionResult <List <PracticeHistoryEntry> > result = await controller.GetAsync(parameters); Assert.AreEqual(3, result.Value.Count); //Get all entries of a card parameters.Add(nameof(Card.CardId), (long)1); result = await controller.GetAsync(parameters); Assert.AreEqual(2, result.Value.Count); //Get all entries of a deck parameters.Clear(); parameters.Add(nameof(Deck.DeckId), (long)2); result = await controller.GetAsync(parameters); Assert.AreEqual(1, result.Value.Count); //Get all entries of a field parameters.Clear(); parameters.Add(nameof(CardField.FieldId), 3); result = await controller.GetAsync(parameters); Assert.AreEqual(1, result.Value.Count); //Get Problem words parameters.Clear(); parameters.Add(nameof(PracticeHistoryEntriesController.ProblemWords), null); parameters.Add(nameof(Card.CardId), (long)1); result = await controller.GetAsync(parameters); Assert.AreEqual(1, result.Value.Count); Assert.AreEqual(3, result.Value[0].CorrectCount); Assert.AreEqual(6, result.Value[0].HardCount); Assert.AreEqual(9, result.Value[0].WrongCount); }
public async Task GetEntryByIdTest() { using DbContext context = CreateContext(); PracticeHistoryEntriesController controller = CreateController(context); //get entry successfully ActionResult <PracticeHistoryEntry> result = await controller.GetAsync(1); Assert.IsNotNull(result.Value); Assert.AreEqual(field1.FieldId, result.Value.Field.FieldId); //Entry of other user -> unauthorized result = await controller.GetAsync(4); Assert.IsTrue(result.Result is UnauthorizedResult); //Entry does not exist -> not found result = await controller.GetAsync(5); Assert.IsTrue(result.Result is NotFoundResult); }
public async Task PostEntryTest() { using DbContext context = CreateContext(); PracticeHistoryEntriesController controller = CreateController(context); //null as parameter -> bad request ActionResult <PracticeHistoryEntry> result = await controller.PostAsync(null); Assert.IsTrue(result.Result is BadRequestResult); //Same day -> update existing PracticeHistoryEntry newEntry = new PracticeHistoryEntry() { CardId = 1, DeckId = 1, FieldId = 1, PracticeDate = DateTime.Today, PracticeHistoryEntryId = 5, UserId = User.UserId, CorrectCount = 0, HardCount = 0, WrongCount = 1 }; result = await controller.PostAsync(newEntry); Assert.IsNotNull(result.Value); newEntry = context.Find <PracticeHistoryEntry>((long)5); Assert.IsNull(newEntry); newEntry = context.Find <PracticeHistoryEntry>((long)1); Assert.AreEqual(4, newEntry.WrongCount); CardField field1 = context.Find <CardField>((long)1, 1); Assert.AreEqual(1, field1.ProficiencyLevel); Assert.AreEqual(DateTime.Today.AddDays(1).Date, field1.DueDate.Date); //other day -> new entry newEntry = new PracticeHistoryEntry() { CardId = 1, DeckId = 1, FieldId = 1, PracticeDate = DateTime.Today.AddDays(1), PracticeHistoryEntryId = 5, UserId = User.UserId, CorrectCount = 1, HardCount = 0, WrongCount = 0 }; result = await controller.PostAsync(newEntry); Assert.IsNotNull(result.Value); newEntry = context.Find <PracticeHistoryEntry>((long)5); Assert.AreEqual(1, newEntry.CorrectCount); field1 = context.Find <CardField>((long)1, 1); Assert.AreEqual(2, field1.ProficiencyLevel); Assert.AreEqual(DateTime.Today.AddDays(3).Date, field1.DueDate.Date); //other day -> new entry newEntry = new PracticeHistoryEntry() { CardId = 1, DeckId = 1, FieldId = 1, PracticeDate = DateTime.Today.AddDays(1), PracticeHistoryEntryId = 6, UserId = User.UserId, CorrectCount = 0, HardCount = 1, WrongCount = 0 }; result = await controller.PostAsync(newEntry); Assert.IsNotNull(result.Value); field1 = context.Find <CardField>((long)1, 1); Assert.AreEqual((long)1, field1.ProficiencyLevel); Assert.AreEqual(DateTime.Today.AddDays(2).Date, field1.DueDate.Date); }