示例#1
0
        public async Task TwoCardsWithLanguages()
        {
            var db     = DbHelper.GetEmptyTestDB();
            var french = await CardLanguagHelper.CreateAsync(db, "Français");

            var otherLanguage = await CardLanguagHelper.CreateAsync(db, RandomHelper.String());

            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user);

            var frenchCreatedCard = await CardHelper.CreateAsync(db, user, language : french);

            var otherLanguageCard = await CardHelper.CreateAsync(db, user, language : otherLanguage);

            var addDate = RandomHelper.Date();
            await DeckHelper.AddCardAsync(db, deck, frenchCreatedCard.Id, 1, addDate);

            await DeckHelper.AddCardAsync(db, deck, otherLanguageCard.Id, 1, addDate);

            using var dbContext = new MemCheckDbContext(db);
            var request = new GetCardsToRepeat.Request(user, deck, Array.Empty <Guid>(), Array.Empty <Guid>(), 10);
            var cards   = await new GetCardsToRepeat(dbContext.AsCallContext(), addDate.AddDays(1)).RunAsync(request);

            Assert.IsTrue(cards.Cards.Single(card => card.CardId == frenchCreatedCard.Id).IsInFrench);
            Assert.IsFalse(cards.Cards.Single(card => card.CardId == otherLanguageCard.Id).IsInFrench);
        }
示例#2
0
        public async Task OneCardWithImages()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user);

            var image1 = await ImageHelper.CreateAsync(db, user);

            var image2 = await ImageHelper.CreateAsync(db, user);

            var createdCard = await CardHelper.CreateAsync(db, user, frontSideImages : new[] { image1, image2 });

            var addDate = RandomHelper.Date();
            await DeckHelper.AddCardAsync(db, deck, createdCard.Id, 1, addDate);

            using var dbContext = new MemCheckDbContext(db);
            var request      = new GetCardsToRepeat.Request(user, deck, Array.Empty <Guid>(), Array.Empty <Guid>(), 10);
            var cards        = await new GetCardsToRepeat(dbContext.AsCallContext(), addDate.AddDays(1)).RunAsync(request);
            var resultImages = cards.Cards.Single().Images;

            Assert.AreEqual(2, resultImages.Count());
            Assert.AreEqual(image1, resultImages.First().ImageId);
            Assert.AreEqual(image2, resultImages.Last().ImageId);
        }
示例#3
0
        public async Task MoveToSameHeap()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user);

            var card = await CardHelper.CreateAsync(db, user);

            var lastLearnTime = RandomHelper.Date();
            await DeckHelper.AddCardAsync(db, deck, card.Id, heap : 1, lastLearnUtcTime : lastLearnTime);

            using (var dbContext = new MemCheckDbContext(db))
                await new MoveCardsToHeap(dbContext.AsCallContext()).RunAsync(new MoveCardsToHeap.Request(user, deck, 1, card.Id.AsArray()));

            using (var dbContext = new MemCheckDbContext(db))
            {
                var cardInDeck = dbContext.CardsInDecks.Single();
                Assert.AreEqual(1, cardInDeck.CurrentHeap);
                Assert.AreEqual(lastLearnTime, cardInDeck.LastLearnUtcTime);
                Assert.AreEqual(lastLearnTime.AddDays(1), cardInDeck.ExpiryUtcTime);
                Assert.AreEqual(1, cardInDeck.NbTimesInNotLearnedHeap);
                Assert.AreEqual(1, cardInDeck.BiggestHeapReached);
            }
        }
        public async Task ComplexCaseWithLessCardsThanRequested()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user, algorithmId : DeveloperHeapingAlgorithm.ID);

            const int cardCount = 10;

            for (int i = 0; i < cardCount / 2; i++)
            {
                await DeckHelper.AddNeverLearntCardAsync(db, deck, await CardHelper.CreateIdAsync(db, user));

                await DeckHelper.AddCardAsync(db, deck, await CardHelper.CreateIdAsync(db, user), 0);
            }
            using var dbContext = new MemCheckDbContext(db);
            var request = new GetUnknownCardsToLearn.Request(user, deck, Array.Empty <Guid>(), Array.Empty <Guid>(), cardCount * 2);
            var cards   = (await new GetUnknownCardsToLearn(dbContext.AsCallContext()).RunAsync(request)).Cards.ToImmutableArray();

            Assert.AreEqual(cardCount, cards.Length);
            for (int i = 1; i < cardCount / 2; i++)
            {
                Assert.AreEqual(CardInDeck.NeverLearntLastLearnTime, cards[i].LastLearnUtcTime);
            }
            for (int i = cardCount / 2; i < cards.Length; i++)
            {
                Assert.IsTrue(cards[i].LastLearnUtcTime >= cards[i - 1].LastLearnUtcTime);
            }
        }
示例#5
0
        public async Task RequestedCount()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user, algorithmId : DeveloperHeapingAlgorithm.ID);

            var       loadTime  = RandomHelper.Date();
            const int cardCount = 50;

            for (int i = 0; i < cardCount; i++)
            {
                var card = await CardHelper.CreateAsync(db, user);

                await DeckHelper.AddCardAsync(db, deck, card.Id, RandomHelper.Heap(true), RandomHelper.DateBefore(loadTime));
            }
            using var dbContext = new MemCheckDbContext(db);
            var request = new GetCardsToRepeat.Request(user, deck, Array.Empty <Guid>(), Array.Empty <Guid>(), cardCount / 2);
            var cards   = await new GetCardsToRepeat(dbContext.AsCallContext(), loadTime).RunAsync(request);

            Assert.AreEqual(request.CardsToDownload, cards.Cards.Count());

            request = new GetCardsToRepeat.Request(user, deck, Array.Empty <Guid>(), Array.Empty <Guid>(), cardCount);
            cards   = await new GetCardsToRepeat(dbContext.AsCallContext(), loadTime).RunAsync(request);
            Assert.AreEqual(request.CardsToDownload, cards.Cards.Count());

            request = new GetCardsToRepeat.Request(user, deck, Array.Empty <Guid>(), Array.Empty <Guid>(), cardCount * 2);
            cards   = await new GetCardsToRepeat(dbContext.AsCallContext(), loadTime).RunAsync(request);
            Assert.AreEqual(cardCount, cards.Cards.Count());
        }
示例#6
0
        public async Task LearnMoveToSameHeap()
        {
            //This could happen due to multiple sessions by the user

            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user);

            var card = await CardHelper.CreateAsync(db, user);

            var initialTime = RandomHelper.Date();
            await DeckHelper.AddCardAsync(db, deck, card.Id, heap : 1, lastLearnUtcTime : initialTime);

            using (var dbContext = new MemCheckDbContext(db))
                await new MoveCardToHeap(dbContext.AsCallContext(), RandomHelper.Date(initialTime)).RunAsync(new MoveCardToHeap.Request(user, deck, card.Id, 1));

            using (var dbContext = new MemCheckDbContext(db))
            {
                var cardInDeck = dbContext.CardsInDecks.Single();
                Assert.AreEqual(1, cardInDeck.CurrentHeap);
                Assert.AreEqual(initialTime, cardInDeck.LastLearnUtcTime);
                Assert.AreEqual(1, cardInDeck.NbTimesInNotLearnedHeap);
                Assert.AreEqual(1, cardInDeck.BiggestHeapReached);
                Assert.AreEqual(initialTime.AddDays(1), cardInDeck.ExpiryUtcTime);
            }
        }
        public async Task Complex()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user);

            var card1 = await CardHelper.CreateAsync(db, user);

            await DeckHelper.AddCardAsync(db, deck, card1.Id);

            var card2 = await CardHelper.CreateAsync(db, user);

            await DeckHelper.AddCardAsync(db, deck, card2.Id);

            var card3 = await CardHelper.CreateAsync(db, user);

            await DeckHelper.AddCardAsync(db, deck, card3.Id);

            using (var dbContext = new MemCheckDbContext(db))
                await new RemoveCardFromDeck(dbContext.AsCallContext()).RunAsync(new RemoveCardFromDeck.Request(user, deck, card2.Id));

            await DeckHelper.CheckDeckDoesNotContainCard(db, deck, card2.Id);

            await DeckHelper.CheckDeckContainsCards(db, deck, card1.Id, card3.Id);
        }
示例#8
0
        public async Task LearnMoveToUnknown()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user);

            var card = await CardHelper.CreateAsync(db, user);

            await DeckHelper.AddCardAsync(db, deck, card.Id, heap : 4);

            var runTime = RandomHelper.Date();

            using (var dbContext = new MemCheckDbContext(db))
                await new MoveCardToHeap(dbContext.AsCallContext(), runTime).RunAsync(new MoveCardToHeap.Request(user, deck, card.Id, CardInDeck.UnknownHeap));

            using (var dbContext = new MemCheckDbContext(db))
            {
                var cardInDeck = dbContext.CardsInDecks.Single();
                Assert.AreEqual(CardInDeck.UnknownHeap, cardInDeck.CurrentHeap);
                Assert.AreEqual(runTime, cardInDeck.LastLearnUtcTime);
                Assert.AreEqual(2, cardInDeck.NbTimesInNotLearnedHeap);
                Assert.AreEqual(4, cardInDeck.BiggestHeapReached);
                Assert.AreEqual(DateTime.MinValue, cardInDeck.ExpiryUtcTime);
            }
        }
示例#9
0
        public async Task MoveToZeroTwice()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user);

            var card = await CardHelper.CreateAsync(db, user);

            var addTime = RandomHelper.Date();
            await DeckHelper.AddCardAsync(db, deck, card.Id, heap : 8, lastLearnUtcTime : addTime);

            using (var dbContext = new MemCheckDbContext(db))
                await new MoveCardsToHeap(dbContext.AsCallContext()).RunAsync(new MoveCardsToHeap.Request(user, deck, CardInDeck.UnknownHeap, card.Id.AsArray()));

            for (int i = 0; i < 2; i++)
            {
                using (var dbContext = new MemCheckDbContext(db))
                {
                    var loadedCard = dbContext.CardsInDecks.Single();
                    Assert.AreEqual(CardInDeck.UnknownHeap, loadedCard.CurrentHeap);
                    Assert.AreEqual(addTime, loadedCard.LastLearnUtcTime);
                    Assert.AreEqual(DateTime.MinValue, loadedCard.ExpiryUtcTime);
                    Assert.AreEqual(2, loadedCard.NbTimesInNotLearnedHeap);
                    Assert.AreEqual(8, loadedCard.BiggestHeapReached);
                }
            }
        }
示例#10
0
        public async Task Complex()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user);

            var tag1 = await TagHelper.CreateAsync(db);

            var card1 = await CardHelper.CreateAsync(db, user, tagIds : tag1.AsArray());

            await DeckHelper.AddCardAsync(db, deck, card1.Id);

            var tag2 = await TagHelper.CreateAsync(db);

            var card2 = await CardHelper.CreateAsync(db, user, tagIds : tag2.AsArray());

            await DeckHelper.AddCardAsync(db, deck, card2.Id);

            var card3 = await CardHelper.CreateAsync(db, user, tagIds : new[] { tag1, tag2 });

            await DeckHelper.AddCardAsync(db, deck, card3.Id);

            var card4 = await CardHelper.CreateAsync(db, user);

            await DeckHelper.AddCardAsync(db, deck, card4.Id);

            using var dbContext = new MemCheckDbContext(db);
            var request = new GetTagsOfDeck.Request(user, deck);
            var result  = await new GetTagsOfDeck(dbContext).RunAsync(request);

            Assert.AreEqual(2, result.Count());
            Assert.IsTrue(result.Any(t => t.TagId == tag1));
            Assert.IsTrue(result.Any(t => t.TagId == tag2));
        }
示例#11
0
        public async Task DoesNotDeleteOtherUserDeck()
        {
            var db         = DbHelper.GetEmptyTestDB();
            var loggedUser = await UserHelper.CreateInDbAsync(db);

            var deckNotToDelete = await DeckHelper.CreateAsync(db, loggedUser);

            var userToDelete = await UserHelper.CreateInDbAsync(db);

            var deckToDelete = await DeckHelper.CreateAsync(db, userToDelete);

            var card = await CardHelper.CreateAsync(db, userToDelete);

            await DeckHelper.AddCardAsync(db, deckToDelete, card.Id);

            await DeckHelper.AddCardAsync(db, deckNotToDelete, card.Id);

            using (var dbContext = new MemCheckDbContext(db))
            {
                using var userManager = UserHelper.GetUserManager(dbContext);
                await new DeleteUserAccount(dbContext.AsCallContext(new TestRoleChecker(loggedUser)), userManager).RunAsync(new DeleteUserAccount.Request(loggedUser, userToDelete));
            }

            using (var dbContext = new MemCheckDbContext(db))
            {
                Assert.IsFalse(await dbContext.Decks.AnyAsync(deck => deck.Id == deckToDelete));
                Assert.IsFalse(await dbContext.CardsInDecks.AnyAsync(cardInDeck => cardInDeck.Deck.Id == deckToDelete));

                Assert.IsTrue(await dbContext.Decks.AnyAsync(deck => deck.Id == deckNotToDelete));
                Assert.IsTrue(await dbContext.CardsInDecks.AnyAsync(cardInDeck => cardInDeck.Deck.Id == deckNotToDelete));
            }
        }
        public async Task UnknownCardsLearnt_CheckOrder()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user, algorithmId : DeveloperHeapingAlgorithm.ID);

            const int cardCount = 100;

            for (int i = 0; i < cardCount; i++)
            {
                var card = await CardHelper.CreateAsync(db, user);

                await DeckHelper.AddCardAsync(db, deck, card.Id, 0);
            }
            using var dbContext = new MemCheckDbContext(db);
            var request = new GetUnknownCardsToLearn.Request(user, deck, Array.Empty <Guid>(), Array.Empty <Guid>(), cardCount);
            var cards   = (await new GetUnknownCardsToLearn(dbContext.AsCallContext()).RunAsync(request)).Cards.ToImmutableArray();

            Assert.AreEqual(cardCount, cards.Length);
            for (int i = 1; i < cards.Length; i++)
            {
                Assert.IsTrue(cards[i].LastLearnUtcTime >= cards[i - 1].LastLearnUtcTime);
            }
        }
示例#13
0
        public async Task ReduceVisibility_OnlyUserWithView_NoOtherUserHasInDeck_OnlyAuthor()
        {
            var db          = DbHelper.GetEmptyTestDB();
            var cardCreator = await UserHelper.CreateInDbAsync(db);

            var languageId = await CardLanguagHelper.CreateAsync(db);

            var card = await CardHelper.CreateAsync(db, cardCreator, language : languageId, userWithViewIds : Array.Empty <Guid>());

            var deck = await DeckHelper.CreateAsync(db, cardCreator);

            await DeckHelper.AddCardAsync(db, deck, card.Id, 0);

            var otherUser = await UserHelper.CreateInDbAsync(db);

            using (var dbContext = new MemCheckDbContext(db))
            {
                CardVisibilityHelper.CheckUserIsAllowedToViewCards(dbContext, cardCreator, card.Id);
                CardVisibilityHelper.CheckUserIsAllowedToViewCards(dbContext, otherUser, card.Id);
            }

            using (var dbContext = new MemCheckDbContext(db))
            {
                var r = UpdateCardHelper.RequestForVisibilityChange(card, cardCreator.AsArray());
                await new UpdateCard(dbContext.AsCallContext()).RunAsync(r);
            }

            using (var dbContext = new MemCheckDbContext(db))
            {
                CardVisibilityHelper.CheckUserIsAllowedToViewCards(dbContext, cardCreator, card.Id);
                Assert.ThrowsException <InvalidOperationException>(() => CardVisibilityHelper.CheckUserIsAllowedToViewCards(dbContext, otherUser, card.Id));
            }
        }
示例#14
0
        public async Task LearnMoveUp()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user, algorithmId : UnitTestsHeapingAlgorithm.ID);

            var card = await CardHelper.CreateAsync(db, user);

            await DeckHelper.AddCardAsync(db, deck, card.Id, heap : 1);

            var runTime = RandomHelper.Date();

            using (var dbContext = new MemCheckDbContext(db))
                await new MoveCardToHeap(dbContext.AsCallContext(), runTime).RunAsync(new MoveCardToHeap.Request(user, deck, card.Id, 2));

            using (var dbContext = new MemCheckDbContext(db))
            {
                var cardInDeck = dbContext.CardsInDecks.Single();
                Assert.AreEqual(2, cardInDeck.CurrentHeap);
                Assert.AreEqual(runTime, cardInDeck.LastLearnUtcTime);
                Assert.AreEqual(1, cardInDeck.NbTimesInNotLearnedHeap);
                Assert.AreEqual(2, cardInDeck.BiggestHeapReached);
                Assert.AreEqual(runTime.AddDays(2), cardInDeck.ExpiryUtcTime);
            }
        }
示例#15
0
        public async Task OneDeck()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user, algorithmId : HeapingAlgorithms.DefaultAlgoId);

            var tag1 = await TagHelper.CreateAsync(db);

            var tag2 = await TagHelper.CreateAsync(db);

            await DeckHelper.AddCardAsync(db, deck, (await CardHelper.CreateAsync(db, user, tagIds: tag1.AsArray())).Id, 0);

            await DeckHelper.AddCardAsync(db, deck, (await CardHelper.CreateAsync(db, user)).Id, 0);

            await DeckHelper.AddCardAsync(db, deck, (await CardHelper.CreateAsync(db, user)).Id, 1);

            await DeckHelper.AddCardAsync(db, deck, (await CardHelper.CreateAsync(db, user, tagIds: new[] { tag1, tag2 })).Id, 1);

            await DeckHelper.AddCardAsync(db, deck, (await CardHelper.CreateAsync(db, user)).Id, 2);

            await DeckHelper.AddCardAsync(db, deck, (await CardHelper.CreateAsync(db, user)).Id, 4);

            await DeckHelper.AddCardAsync(db, deck, (await CardHelper.CreateAsync(db, user)).Id, 4);

            using var dbContext = new MemCheckDbContext(db);
            var result     = await new GetUserDecksWithHeapsAndTags(dbContext.AsCallContext()).RunAsync(new GetUserDecksWithHeapsAndTags.Request(user));
            var resultDeck = result.Single();

            Assert.AreEqual(deck, resultDeck.DeckId);
            Assert.IsTrue(resultDeck.Heaps.SequenceEqual(new[] { 0, 1, 2, 4 }));
            Assert.AreEqual(2, resultDeck.Tags.Count());
            Assert.IsTrue(resultDeck.Tags.Any(tag => tag.TagId == tag1));
            Assert.IsTrue(resultDeck.Tags.Any(tag => tag.TagId == tag2));
        }
        public async Task OneDeck_WithCards()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user, algorithmId : HeapingAlgorithms.DefaultAlgoId);

            var runDate = RandomHelper.Date();

            await DeckHelper.AddCardAsync(db, deck, (await CardHelper.CreateAsync(db, user)).Id, 0);

            await DeckHelper.AddCardAsync(db, deck, (await CardHelper.CreateAsync(db, user)).Id, 0);

            await DeckHelper.AddCardAsync(db, deck, (await CardHelper.CreateAsync(db, user)).Id, 1, RandomHelper.DateBefore(runDate.AddDays(-3)));  //expired

            await DeckHelper.AddCardAsync(db, deck, (await CardHelper.CreateAsync(db, user)).Id, 1, runDate.AddDays(-1));

            await DeckHelper.AddCardAsync(db, deck, (await CardHelper.CreateAsync(db, user)).Id, 2, runDate.AddDays(-10));  //expired

            await DeckHelper.AddCardAsync(db, deck, (await CardHelper.CreateAsync(db, user)).Id, 4, runDate.AddDays(-10));

            await DeckHelper.AddCardAsync(db, deck, (await CardHelper.CreateAsync(db, user)).Id, 4, runDate.AddDays(-13));

            using var dbContext = new MemCheckDbContext(db);
            var result     = await new GetUserDecksWithHeaps(dbContext.AsCallContext(), runDate).RunAsync(new GetUserDecksWithHeaps.Request(user));
            var resultDeck = result.Single();

            Assert.AreEqual(deck, resultDeck.DeckId);
            Assert.AreEqual(7, resultDeck.CardCount);
            Assert.AreEqual(4, resultDeck.Heaps.Count());

            var unknownHeap = resultDeck.Heaps.Single(heap => heap.HeapId == 0);

            Assert.AreEqual(DateTime.MaxValue, unknownHeap.NextExpiryUtcDate);
            Assert.AreEqual(2, unknownHeap.TotalCardCount);

            var heap1 = resultDeck.Heaps.Single(heap => heap.HeapId == 1);

            Assert.AreEqual(2, heap1.TotalCardCount);
            Assert.AreEqual(1, heap1.ExpiredCardCount);
            DateAssert.IsInRange(runDate.AddDays(1), TimeSpan.FromMinutes(20), heap1.NextExpiryUtcDate);

            var heap2 = resultDeck.Heaps.Single(heap => heap.HeapId == 2);

            Assert.AreEqual(1, heap2.TotalCardCount);
            Assert.AreEqual(1, heap2.ExpiredCardCount);
            Assert.AreEqual(DateTime.MaxValue, heap2.NextExpiryUtcDate);

            var heap4 = resultDeck.Heaps.Single(heap => heap.HeapId == 4);

            Assert.AreEqual(2, heap4.TotalCardCount);
            Assert.AreEqual(0, heap4.ExpiredCardCount);
            DateAssert.IsInRange(runDate.AddDays(3), TimeSpan.FromMinutes(160), heap4.NextExpiryUtcDate);
        }
示例#17
0
        public async Task ReduceVisibility_OtherUserHasInDeck_OtherAuthor()
        {
            var db          = DbHelper.GetEmptyTestDB();
            var cardCreator = await UserHelper.CreateInDbAsync(db);

            var languageId = await CardLanguagHelper.CreateAsync(db);

            var card = await CardHelper.CreateAsync(db, cardCreator, language : languageId, userWithViewIds : Array.Empty <Guid>());

            var userWithCardInDeck = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, userWithCardInDeck);

            await DeckHelper.AddCardAsync(db, deck, card.Id, 0);

            var otherUser = await UserHelper.CreateInDbAsync(db);

            using (var dbContext = new MemCheckDbContext(db))
            {
                var r = UpdateCardHelper.RequestForFrontSideChange(card, RandomHelper.String(), otherUser);
                await new UpdateCard(dbContext.AsCallContext()).RunAsync(r);
            }

            using (var dbContext = new MemCheckDbContext(db))
            {
                var r = UpdateCardHelper.RequestForVisibilityChange(card, cardCreator.AsArray());
                await Assert.ThrowsExceptionAsync <RequestInputException>(async() => await new UpdateCard(dbContext.AsCallContext()).RunAsync(r));
            }

            using (var dbContext = new MemCheckDbContext(db))
            {
                var r = UpdateCardHelper.RequestForVisibilityChange(card, otherUser.AsArray(), otherUser);
                await Assert.ThrowsExceptionAsync <RequestInputException>(async() => await new UpdateCard(dbContext.AsCallContext()).RunAsync(r));
            }

            using (var dbContext = new MemCheckDbContext(db))
            {
                var r = UpdateCardHelper.RequestForVisibilityChange(card, new[] { userWithCardInDeck }, userWithCardInDeck);
                await Assert.ThrowsExceptionAsync <RequestInputException>(async() => await new UpdateCard(dbContext.AsCallContext()).RunAsync(r));
            }

            using (var dbContext = new MemCheckDbContext(db))
            {
                var r = UpdateCardHelper.RequestForVisibilityChange(card, new[] { cardCreator, otherUser, userWithCardInDeck });
                await new UpdateCard(dbContext.AsCallContext()).RunAsync(r);
            }

            using (var dbContext = new MemCheckDbContext(db))
            {
                CardVisibilityHelper.CheckUserIsAllowedToViewCards(dbContext, cardCreator, card.Id);
                CardVisibilityHelper.CheckUserIsAllowedToViewCards(dbContext, otherUser, card.Id);
                CardVisibilityHelper.CheckUserIsAllowedToViewCards(dbContext, userWithCardInDeck, card.Id);
            }
        }
示例#18
0
        public async Task TwoDecks_WithTags()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck1Name = RandomHelper.String();
            var deck1Algo = RandomHelper.HeapingAlgorithm();
            var deck1     = await DeckHelper.CreateAsync(db, user, deck1Name, deck1Algo);

            var deck2Name = RandomHelper.String();
            var deck2Algo = RandomHelper.HeapingAlgorithm();
            var deck2     = await DeckHelper.CreateAsync(db, user, deck2Name, deck2Algo);

            var tag1 = await TagHelper.CreateAsync(db);

            var card1 = await CardHelper.CreateAsync(db, user, tagIds : tag1.AsArray());

            await DeckHelper.AddCardAsync(db, deck1, card1.Id);

            await DeckHelper.AddCardAsync(db, deck2, card1.Id);

            var tag2 = await TagHelper.CreateAsync(db);

            var card2 = await CardHelper.CreateAsync(db, user, tagIds : tag2.AsArray());

            await DeckHelper.AddCardAsync(db, deck1, card2.Id);

            var card3 = await CardHelper.CreateAsync(db, user, tagIds : new[] { tag1, tag2 });

            await DeckHelper.AddCardAsync(db, deck1, card3.Id);

            var card4 = await CardHelper.CreateAsync(db, user);

            await DeckHelper.AddCardAsync(db, deck1, card4.Id);

            await DeckHelper.AddCardAsync(db, deck2, card4.Id);

            using var dbContext = new MemCheckDbContext(db);
            var result = await new GetUserDecksWithTags(dbContext.AsCallContext()).RunAsync(new GetUserDecksWithTags.Request(user));

            var resultDeck1 = result.Single(d => d.DeckId == deck1);

            Assert.AreEqual(deck1, resultDeck1.DeckId);
            Assert.AreEqual(deck1Name, resultDeck1.Description);
            Assert.AreEqual(2, resultDeck1.Tags.Count());
            Assert.IsTrue(resultDeck1.Tags.Any(t => t.TagId == tag1));
            Assert.IsTrue(resultDeck1.Tags.Any(t => t.TagId == tag2));

            var resultDeck2 = result.Single(d => d.DeckId == deck2);

            Assert.AreEqual(deck2, resultDeck2.DeckId);
            Assert.AreEqual(deck2Name, resultDeck2.Description);
            Assert.AreEqual(tag1, resultDeck2.Tags.Single().TagId);
        }
示例#19
0
        public async Task CheckAllFields()
        {
            var db           = DbHelper.GetEmptyTestDB();
            var languageName = RandomHelper.String();
            var language     = await CardLanguagHelper.CreateAsync(db, languageName);

            var creatorName = RandomHelper.String();
            var creatorId   = await UserHelper.CreateInDbAsync(db, userName : creatorName);

            var creationDate   = RandomHelper.Date();
            var frontSide      = RandomHelper.String();
            var backSide       = RandomHelper.String();
            var additionalInfo = RandomHelper.String();
            var tagName        = RandomHelper.String();
            var tag            = await TagHelper.CreateAsync(db, tagName);

            var otherUserName = RandomHelper.String();
            var otherUserId   = await UserHelper.CreateInDbAsync(db, userName : otherUserName);

            var versionDescription = RandomHelper.String();
            var card = await CardHelper.CreateAsync(db, creatorId, language : language, versionDate : creationDate, frontSide : frontSide, backSide : backSide, additionalInfo : additionalInfo, tagIds : tag.AsArray(), userWithViewIds : new[] { creatorId, otherUserId }, versionDescription : versionDescription);

            var deck = await DeckHelper.CreateAsync(db, otherUserId);

            await DeckHelper.AddCardAsync(db, deck, card.Id);

            using var dbContext = new MemCheckDbContext(db);
            var loaded = await new GetCardForEdit(dbContext.AsCallContext()).RunAsync(new GetCardForEdit.Request(creatorId, card.Id));

            Assert.AreEqual(frontSide, loaded.FrontSide);
            Assert.AreEqual(backSide, loaded.BackSide);
            Assert.AreEqual(additionalInfo, loaded.AdditionalInfo);
            Assert.AreEqual(language, loaded.LanguageId);
            Assert.AreEqual(languageName, loaded.LanguageName);
            Assert.AreEqual(tag, loaded.Tags.Single().TagId);
            Assert.AreEqual(tagName, loaded.Tags.Single().TagName);
            Assert.AreEqual(2, loaded.UsersWithVisibility.Count());
            Assert.IsTrue(loaded.UsersWithVisibility.Count(u => u.UserId == creatorId) == 1);
            Assert.AreEqual(creatorName, loaded.UsersWithVisibility.Single(u => u.UserId == creatorId).UserName);
            Assert.IsTrue(loaded.UsersWithVisibility.Count(u => u.UserId == otherUserId) == 1);
            Assert.AreEqual(otherUserName, loaded.UsersWithVisibility.Single(u => u.UserId == otherUserId).UserName);
            Assert.AreEqual(creationDate, loaded.FirstVersionUtcDate);
            Assert.AreEqual(creationDate, loaded.LastVersionUtcDate);
            Assert.AreEqual(creatorName, loaded.LastVersionCreatorName);
            Assert.AreEqual(versionDescription, loaded.LastVersionDescription);
            Assert.AreEqual(1, loaded.UsersOwningDeckIncluding.Count());
            Assert.IsTrue(loaded.UsersOwningDeckIncluding.Single() == otherUserName);
            Assert.AreEqual(0, loaded.Images.Count());
            Assert.AreEqual(0, loaded.UserRating);
            Assert.AreEqual(0, loaded.AverageRating);
            Assert.AreEqual(0, loaded.CountOfUserRatings);
        }
示例#20
0
        public async Task CardDoesNotExist()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user);

            var card = await CardHelper.CreateAsync(db, user);

            await DeckHelper.AddCardAsync(db, deck, card.Id);

            using var dbContext = new MemCheckDbContext(db);
            await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await new MoveCardsToHeap(dbContext.AsCallContext()).RunAsync(new MoveCardsToHeap.Request(user, deck, RandomHelper.Heap(), new[] { card.Id, Guid.NewGuid() })));
        }
示例#21
0
        public async Task HeapTooBig()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user);

            var card = await CardHelper.CreateAsync(db, user);

            await DeckHelper.AddCardAsync(db, deck, card.Id);

            using var dbContext = new MemCheckDbContext(db);
            await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await new MoveCardsToHeap(dbContext.AsCallContext()).RunAsync(new MoveCardsToHeap.Request(user, deck, CardInDeck.MaxHeapValue + 1, card.Id.AsArray())));
        }
示例#22
0
        public async Task UserNotLoggedIn()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user);

            var card = await CardHelper.CreateAsync(db, user);

            await DeckHelper.AddCardAsync(db, deck, card.Id);

            using var dbContext = new MemCheckDbContext(db);
            await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await new MoveCardsToHeap(dbContext.AsCallContext()).RunAsync(new MoveCardsToHeap.Request(Guid.Empty, deck, RandomHelper.Heap(), card.Id.AsArray())));
        }
示例#23
0
        public async Task LearnMoveDown()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user);

            var card = await CardHelper.CreateAsync(db, user);

            await DeckHelper.AddCardAsync(db, deck, card.Id, heap : 4);

            using var dbContext = new MemCheckDbContext(db);
            await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await new MoveCardToHeap(dbContext.AsCallContext()).RunAsync(new MoveCardToHeap.Request(user, deck, card.Id, 3)));
        }
示例#24
0
        public async Task MultipleImagesOnAdditionalSide()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var language = await CardLanguagHelper.CreateAsync(db);

            var image1 = await ImageHelper.CreateAsync(db, user);

            var image2 = await ImageHelper.CreateAsync(db, user);

            Guid cardGuid;
            var  createRequest = new CreateCard.Request(
                user,
                RandomHelper.String(),
                Array.Empty <Guid>(),
                RandomHelper.String(),
                Array.Empty <Guid>(),
                RandomHelper.String(),
                new Guid[] { image1, image2 },
                language,
                Array.Empty <Guid>(),
                Array.Empty <Guid>(),
                RandomHelper.String());

            using (var dbContext = new MemCheckDbContext(db))
                cardGuid = (await new CreateCard(dbContext.AsCallContext()).RunAsync(createRequest)).CardId;

            var deck = await DeckHelper.CreateAsync(db, user);

            var addToDeckDate = RandomHelper.Date();
            await DeckHelper.AddCardAsync(db, deck, cardGuid, 1, addToDeckDate);

            using (var dbContext = new MemCheckDbContext(db))
            {
                var request = new GetCardsToRepeat.Request(user, deck, Array.Empty <Guid>(), Array.Empty <Guid>(), 10);
                var card    = (await new GetCardsToRepeat(dbContext.AsCallContext(), addToDeckDate.AddDays(1)).RunAsync(request)).Cards.Single();

                var images = card.Images;
                Assert.AreEqual(2, images.Count());

                var first = card.Images.First();
                Assert.AreEqual(ImageInCard.AdditionalInfo, first.CardSide);
                Assert.AreEqual(image1, first.ImageId);

                var last = card.Images.Last();
                Assert.AreEqual(ImageInCard.AdditionalInfo, last.CardSide);
                Assert.AreEqual(image2, last.ImageId);
            }
        }
示例#25
0
        public async Task FullTestWithOneDeck()
        {
            var testDB = DbHelper.GetEmptyTestDB();
            var userId = await UserHelper.CreateInDbAsync(testDB);

            var deckDescription = RandomHelper.String();
            var deck            = await DeckHelper.CreateAsync(testDB, userId, deckDescription, UnitTestsHeapingAlgorithm.ID);

            var jan01 = new DateTime(2030, 01, 01).ToUniversalTime();
            var jan30 = new DateTime(2030, 01, 30, 0, 0, 0).ToUniversalTime();
            var jan31 = new DateTime(2030, 01, 31).ToUniversalTime();
            var jan28 = new DateTime(2030, 01, 28).ToUniversalTime();

            await DeckHelper.AddCardAsync(testDB, deck, (await CardHelper.CreateAsync(testDB, userId)).Id, 0, jan31);

            await DeckHelper.AddCardAsync(testDB, deck, (await CardHelper.CreateAsync(testDB, userId)).Id, 0, jan30);

            await DeckHelper.AddCardAsync(testDB, deck, (await CardHelper.CreateAsync(testDB, userId)).Id, 1, jan31);

            await DeckHelper.AddCardAsync(testDB, deck, (await CardHelper.CreateAsync(testDB, userId)).Id, 2, jan31);

            await DeckHelper.AddCardAsync(testDB, deck, (await CardHelper.CreateAsync(testDB, userId)).Id, 1, jan30);

            await DeckHelper.AddCardAsync(testDB, deck, (await CardHelper.CreateAsync(testDB, userId)).Id, 1, jan30);

            await DeckHelper.AddCardAsync(testDB, deck, (await CardHelper.CreateAsync(testDB, userId)).Id, 3, jan30);

            await DeckHelper.AddCardAsync(testDB, deck, (await CardHelper.CreateAsync(testDB, userId)).Id, 1, jan01);

            await DeckHelper.AddCardAsync(testDB, deck, (await CardHelper.CreateAsync(testDB, userId)).Id, 3, jan28);

            await DeckHelper.AddCardAsync(testDB, deck, (await CardHelper.CreateAsync(testDB, userId)).Id, 6, jan28);

            await DeckHelper.AddCardAsync(testDB, deck, (await CardHelper.CreateAsync(testDB, userId)).Id, 4, jan01);

            await DeckHelper.AddCardAsync(testDB, deck, (await CardHelper.CreateAsync(testDB, userId)).Id, 6, jan01);

            using var dbContext = new MemCheckDbContext(testDB);
            var request = new GetDecksWithLearnCounts.Request(userId);
            var result  = await new GetDecksWithLearnCounts(dbContext.AsCallContext(), new DateTime(2030, 02, 01, 0, 30, 0)).RunAsync(request);
            var loaded  = result.Single();

            Assert.AreEqual(deckDescription, loaded.Description);
            Assert.AreEqual(2, loaded.UnknownCardCount);
            Assert.AreEqual(7, loaded.ExpiredCardCount);
            Assert.AreEqual(0, loaded.ExpiringNextHourCount);
            Assert.AreEqual(2, loaded.ExpiringFollowing24hCount);
            Assert.AreEqual(1, loaded.ExpiringFollowing3DaysCount);
            Assert.AreEqual(12, loaded.CardCount);
        }
        public async Task UserDoesNotExist()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user);

            var card = await CardHelper.CreateAsync(db, user);

            await DeckHelper.AddCardAsync(db, deck, card.Id);

            using var dbContext = new MemCheckDbContext(db);
            await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await new RemoveCardFromDeck(dbContext.AsCallContext()).RunAsync(new RemoveCardFromDeck.Request(Guid.NewGuid(), deck, card.Id)));
        }
示例#27
0
        public async Task MoveMultiple()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user);

            var card1 = await CardHelper.CreateAsync(db, user);

            var card2 = await CardHelper.CreateAsync(db, user);

            var card3 = await CardHelper.CreateAsync(db, user);

            var card1LastLearnTime = RandomHelper.Date();
            var card2LastLearnTime = RandomHelper.Date();
            var card3LastLearnTime = RandomHelper.Date();
            await DeckHelper.AddCardAsync(db, deck, card1.Id, heap : 1, lastLearnUtcTime : card1LastLearnTime);

            await DeckHelper.AddCardAsync(db, deck, card2.Id, heap : 8, lastLearnUtcTime : card2LastLearnTime);

            await DeckHelper.AddCardAsync(db, deck, card3.Id, heap : CardInDeck.UnknownHeap, lastLearnUtcTime : card3LastLearnTime);

            using (var dbContext = new MemCheckDbContext(db))
                await new MoveCardsToHeap(dbContext.AsCallContext()).RunAsync(new MoveCardsToHeap.Request(user, deck, 3, new[] { card1.Id, card2.Id, card3.Id }));

            using (var dbContext = new MemCheckDbContext(db))
            {
                var loadedCard1 = dbContext.CardsInDecks.Single(c => c.CardId == card1.Id);
                Assert.AreEqual(3, loadedCard1.CurrentHeap);
                Assert.AreEqual(card1LastLearnTime, loadedCard1.LastLearnUtcTime);
                Assert.AreEqual(card1LastLearnTime.AddDays(3), loadedCard1.ExpiryUtcTime);
                Assert.AreEqual(1, loadedCard1.NbTimesInNotLearnedHeap);
                Assert.AreEqual(3, loadedCard1.BiggestHeapReached);

                var loadedCard2 = dbContext.CardsInDecks.Single(c => c.CardId == card2.Id);
                Assert.AreEqual(3, loadedCard2.CurrentHeap);
                Assert.AreEqual(card2LastLearnTime, loadedCard2.LastLearnUtcTime);
                Assert.AreEqual(card2LastLearnTime.AddDays(3), loadedCard2.ExpiryUtcTime);
                Assert.AreEqual(1, loadedCard2.NbTimesInNotLearnedHeap);
                Assert.AreEqual(8, loadedCard2.BiggestHeapReached);

                var loadedCard3 = dbContext.CardsInDecks.Single(c => c.CardId == card3.Id);
                Assert.AreEqual(3, loadedCard3.CurrentHeap);
                Assert.AreEqual(card3LastLearnTime, loadedCard3.LastLearnUtcTime);
                Assert.AreEqual(card3LastLearnTime.AddDays(3), loadedCard3.ExpiryUtcTime);
                Assert.AreEqual(1, loadedCard3.NbTimesInNotLearnedHeap);
                Assert.AreEqual(3, loadedCard3.BiggestHeapReached);
            }
        }
        public async Task OnlyCardInTheDeck()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user);

            var card = await CardHelper.CreateAsync(db, user);

            await DeckHelper.AddCardAsync(db, deck, card.Id);

            using (var dbContext = new MemCheckDbContext(db))
                await new RemoveCardsFromDeck(dbContext.AsCallContext()).RunAsync(new RemoveCardsFromDeck.Request(user, deck, card.Id.AsArray()));

            await DeckHelper.CheckDeckDoesNotContainCard(db, deck, card.Id);
        }
        public async Task OneCardToRepeat()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user, algorithmId : DefaultHeapingAlgorithm.ID);

            var card = await CardHelper.CreateAsync(db, user);

            await DeckHelper.AddCardAsync(db, deck, card.Id, 1, RandomHelper.Date());

            using var dbContext = new MemCheckDbContext(db);
            var request = new GetUnknownCardsToLearn.Request(user, deck, Array.Empty <Guid>(), Array.Empty <Guid>(), 10);
            var cards   = await new GetUnknownCardsToLearn(dbContext.AsCallContext()).RunAsync(request);

            Assert.IsFalse(cards.Cards.Any());
        }
        public async Task OneLearnt()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var deck = await DeckHelper.CreateAsync(db, user, algorithmId : DefaultHeapingAlgorithm.ID);

            var card = await CardHelper.CreateAsync(db, user);

            await DeckHelper.AddCardAsync(db, deck, card.Id, 0);

            using var dbContext = new MemCheckDbContext(db);
            var request = new GetUnknownCardsToLearn.Request(user, deck, Array.Empty <Guid>(), Array.Empty <Guid>(), 10);
            var cards   = (await new GetUnknownCardsToLearn(dbContext.AsCallContext()).RunAsync(request)).Cards;

            Assert.AreEqual(1, cards.Count());
            Assert.AreNotEqual(CardInDeck.NeverLearntLastLearnTime, cards.First().LastLearnUtcTime);
        }