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);
            }
        }
Пример #2
0
        public async Task LimitedCardVisibilityUpdated()
        {
            var db         = DbHelper.GetEmptyTestDB();
            var loggedUser = await UserHelper.CreateInDbAsync(db);

            var cardCreator = await UserHelper.CreateInDbAsync(db);

            var userToDelete = await UserHelper.CreateInDbAsync(db);

            var cardId = await CardHelper.CreateIdAsync(db, cardCreator, userWithViewIds : new[] { loggedUser, cardCreator, userToDelete });

            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))
            {
                var card = await dbContext.Cards.Include(card => card.UsersWithView).SingleAsync(c => c.Id == cardId);

                Assert.AreEqual(2, card.UsersWithView.Count());
                Assert.IsFalse(card.UsersWithView.Any(userWithView => userWithView.UserId == userToDelete));
            }
        }
Пример #3
0
        public async Task TestRun_UsersWithSubscriptions()
        {
            var testDB = DbHelper.GetEmptyTestDB();

            var user1 = await UserHelper.CreateInDbAsync(testDB);

            var user2 = await UserHelper.CreateInDbAsync(testDB);

            await CardSubscriptionHelper.CreateAsync(testDB, user2, await CardHelper.CreateIdAsync(testDB, user1));

            var user3 = await UserHelper.CreateInDbAsync(testDB);

            await CardSubscriptionHelper.CreateAsync(testDB, user3, await CardHelper.CreateIdAsync(testDB, user1));

            await CardSubscriptionHelper.CreateAsync(testDB, user3, await CardHelper.CreateIdAsync(testDB, user2));

            await CardSubscriptionHelper.CreateAsync(testDB, user3, await CardHelper.CreateIdAsync(testDB, user3));

            using var dbContext = new MemCheckDbContext(testDB);
            var counter = new UserCardSubscriptionCounter(dbContext.AsCallContext());

            Assert.AreEqual(0, await counter.RunAsync(user1));
            Assert.AreEqual(1, await counter.RunAsync(user2));
            Assert.AreEqual(3, await counter.RunAsync(user3));
        }
Пример #4
0
        public async Task Rating_TwoCards_TwoUsers()
        {
            var testDB = DbHelper.GetEmptyTestDB();

            var user1Id = await UserHelper.CreateInDbAsync(testDB);

            var user2Id = await UserHelper.CreateInDbAsync(testDB);

            var card1Id = await CardHelper.CreateIdAsync(testDB, user1Id);

            var card2Id = await CardHelper.CreateIdAsync(testDB, user2Id);

            using (var dbContext = new MemCheckDbContext(testDB))
            {
                var result = await new SearchCards(dbContext.AsCallContext()).RunAsync(new SearchCards.Request {
                    UserId = user1Id, RatingFiltering = SearchCards.Request.RatingFilteringMode.AtLeast, RatingFilteringValue = 3
                });
                Assert.AreEqual(0, result.TotalNbCards);
            }

            using (var dbContext = new MemCheckDbContext(testDB))
            {
                await new SetCardRating(dbContext.AsCallContext()).RunAsync(new SetCardRating.Request(user1Id, card1Id, 4));
                await new SetCardRating(dbContext.AsCallContext()).RunAsync(new SetCardRating.Request(user2Id, card1Id, 2));
                await new SetCardRating(dbContext.AsCallContext()).RunAsync(new SetCardRating.Request(user1Id, card2Id, 5));
                await new SetCardRating(dbContext.AsCallContext()).RunAsync(new SetCardRating.Request(user2Id, card2Id, 3));
            }

            using (var dbContext = new MemCheckDbContext(testDB))
            {
                var result = await new SearchCards(dbContext.AsCallContext()).RunAsync(new SearchCards.Request {
                    UserId = user1Id, RatingFiltering = SearchCards.Request.RatingFilteringMode.AtLeast, RatingFilteringValue = 3
                });
                Assert.AreEqual(2, result.TotalNbCards);
                Assert.AreEqual(4, result.Cards.Single(c => c.CardId == card1Id).CurrentUserRating);
                Assert.AreEqual(5, result.Cards.Single(c => c.CardId == card2Id).CurrentUserRating);
                Assert.AreEqual(2, result.Cards.Single(c => c.CardId == card1Id).CountOfUserRatings);
                Assert.AreEqual(2, result.Cards.Single(c => c.CardId == card2Id).CountOfUserRatings);
                Assert.AreEqual(3, result.Cards.Single(c => c.CardId == card1Id).AverageRating);
                Assert.AreEqual(4, result.Cards.Single(c => c.CardId == card2Id).AverageRating);
            }

            using (var dbContext = new MemCheckDbContext(testDB))
            {
                var result = await new SearchCards(dbContext.AsCallContext()).RunAsync(new SearchCards.Request {
                    UserId = user1Id, RatingFiltering = SearchCards.Request.RatingFilteringMode.AtLeast, RatingFilteringValue = 4
                });
                Assert.AreEqual(1, result.TotalNbCards);
                Assert.AreEqual(card2Id, result.Cards.Single().CardId);
                Assert.AreEqual(5, result.Cards.Single().CurrentUserRating);
                Assert.AreEqual(2, result.Cards.Single().CountOfUserRatings);
                Assert.AreEqual(4, result.Cards.Single().AverageRating);
            }
        }
Пример #5
0
        public async Task TooBig()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var languageId = await CardLanguagHelper.CreateAsync(db);

            var card = await CardHelper.CreateIdAsync(db, user, language : languageId);

            using var dbContext = new MemCheckDbContext(db);
            await Assert.ThrowsExceptionAsync <RequestInputException>(async() => await new SetCardRating(dbContext.AsCallContext()).RunAsync(new SetCardRating.Request(user, card, 6)));
        }
Пример #6
0
        public async Task UserDoesNotExist()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var languageId = await CardLanguagHelper.CreateAsync(db);

            var card = await CardHelper.CreateIdAsync(db, user, language : languageId);

            using var dbContext = new MemCheckDbContext(db);
            await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await new SetCardRating(dbContext.AsCallContext()).RunAsync(new SetCardRating.Request(Guid.NewGuid(), card, RandomHelper.Rating())));
        }
Пример #7
0
        public async Task SingleUser_NoPreviousValue()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var languageId = await CardLanguagHelper.CreateAsync(db);

            var card = await CardHelper.CreateIdAsync(db, user, language : languageId);

            var rating = RandomHelper.Rating();

            using (var dbContext = new MemCheckDbContext(db))
                await new SetCardRating(dbContext.AsCallContext()).RunAsync(new SetCardRating.Request(user, card, rating));

            await AssertUserRatingAsync(db, user, card, rating);
            await AssertAverageRatingAsync(db, card, 1, rating);
        }
Пример #8
0
        public async Task Rating_SingleCard_SearchByOtherUser()
        {
            var testDB = DbHelper.GetEmptyTestDB();

            var userId = await UserHelper.CreateInDbAsync(testDB);

            var cardId = await CardHelper.CreateIdAsync(testDB, userId);

            using (var dbContext = new MemCheckDbContext(testDB))
            {
                var result = await new SearchCards(dbContext.AsCallContext()).RunAsync(new SearchCards.Request {
                    RatingFiltering = SearchCards.Request.RatingFilteringMode.NoRating
                });
                Assert.AreEqual(1, result.TotalNbCards);
                Assert.AreEqual(0, result.Cards.Single().CurrentUserRating);
                Assert.AreEqual(0, result.Cards.Single().CountOfUserRatings);
                Assert.AreEqual(0, result.Cards.Single().AverageRating);
            }

            using (var dbContext = new MemCheckDbContext(testDB))
                await new SetCardRating(dbContext.AsCallContext()).RunAsync(new SetCardRating.Request(userId, cardId, 4));

            using (var dbContext = new MemCheckDbContext(testDB))
            {
                var result = await new SearchCards(dbContext.AsCallContext()).RunAsync(new SearchCards.Request {
                    RatingFiltering = SearchCards.Request.RatingFilteringMode.NoRating
                });
                Assert.AreEqual(0, result.TotalNbCards);
            }

            using (var dbContext = new MemCheckDbContext(testDB))
            {
                var result = await new SearchCards(dbContext.AsCallContext()).RunAsync(new SearchCards.Request {
                    RatingFiltering = SearchCards.Request.RatingFilteringMode.AtLeast, RatingFilteringValue = 3
                });
                Assert.AreEqual(1, result.TotalNbCards);
                Assert.AreEqual(0, result.Cards.Single().CurrentUserRating);
                Assert.AreEqual(1, result.Cards.Single().CountOfUserRatings);
                Assert.AreEqual(4, result.Cards.Single().AverageRating);
            }
        }
Пример #9
0
        public async Task Complex()
        {
            var db    = DbHelper.GetEmptyTestDB();
            var user1 = await UserHelper.CreateInDbAsync(db);

            var user2 = await UserHelper.CreateInDbAsync(db);

            var user3 = await UserHelper.CreateInDbAsync(db);

            var languageId = await CardLanguagHelper.CreateAsync(db);

            var card1 = await CardHelper.CreateIdAsync(db, user1, language : languageId);

            var card2 = await CardHelper.CreateIdAsync(db, user2, language : languageId);

            using (var dbContext = new MemCheckDbContext(db))
                await new SetCardRating(dbContext.AsCallContext()).RunAsync(new SetCardRating.Request(user1, card1, 1));
            await AssertAverageRatingAsync(db, card1, 1, 1);
            await AssertAverageRatingAsync(db, card2, 0, 0);
            await AssertUserRatingAsync(db, user1, card1, 1);
            await AssertUserRatingAsync(db, user2, card1, 0);
            await AssertUserRatingAsync(db, user3, card1, 0);
            await AssertUserRatingAsync(db, user1, card2, 0);
            await AssertUserRatingAsync(db, user2, card2, 0);
            await AssertUserRatingAsync(db, user3, card2, 0);

            using (var dbContext = new MemCheckDbContext(db))
                await new SetCardRating(dbContext.AsCallContext()).RunAsync(new SetCardRating.Request(user2, card1, 2));
            await AssertAverageRatingAsync(db, card1, 2, 1.5);
            await AssertAverageRatingAsync(db, card2, 0, 0);
            await AssertUserRatingAsync(db, user1, card1, 1);
            await AssertUserRatingAsync(db, user2, card1, 2);
            await AssertUserRatingAsync(db, user3, card1, 0);
            await AssertUserRatingAsync(db, user1, card2, 0);
            await AssertUserRatingAsync(db, user2, card2, 0);
            await AssertUserRatingAsync(db, user3, card2, 0);

            using (var dbContext = new MemCheckDbContext(db))
                await new SetCardRating(dbContext.AsCallContext()).RunAsync(new SetCardRating.Request(user3, card1, 3));
            await AssertAverageRatingAsync(db, card1, 3, 2);
            await AssertAverageRatingAsync(db, card2, 0, 0);
            await AssertUserRatingAsync(db, user1, card1, 1);
            await AssertUserRatingAsync(db, user2, card1, 2);
            await AssertUserRatingAsync(db, user3, card1, 3);
            await AssertUserRatingAsync(db, user1, card2, 0);
            await AssertUserRatingAsync(db, user2, card2, 0);
            await AssertUserRatingAsync(db, user3, card2, 0);

            using (var dbContext = new MemCheckDbContext(db))
                await new SetCardRating(dbContext.AsCallContext()).RunAsync(new SetCardRating.Request(user1, card2, 1));
            await AssertAverageRatingAsync(db, card1, 3, 2);
            await AssertAverageRatingAsync(db, card2, 1, 1);
            await AssertUserRatingAsync(db, user1, card1, 1);
            await AssertUserRatingAsync(db, user2, card1, 2);
            await AssertUserRatingAsync(db, user3, card1, 3);
            await AssertUserRatingAsync(db, user1, card2, 1);
            await AssertUserRatingAsync(db, user2, card2, 0);
            await AssertUserRatingAsync(db, user3, card2, 0);

            using (var dbContext = new MemCheckDbContext(db))
                await new SetCardRating(dbContext.AsCallContext()).RunAsync(new SetCardRating.Request(user1, card1, 4));
            await AssertAverageRatingAsync(db, card1, 3, 3);
            await AssertAverageRatingAsync(db, card2, 1, 1);
            await AssertUserRatingAsync(db, user1, card1, 4);
            await AssertUserRatingAsync(db, user2, card1, 2);
            await AssertUserRatingAsync(db, user3, card1, 3);
            await AssertUserRatingAsync(db, user1, card2, 1);
            await AssertUserRatingAsync(db, user2, card2, 0);
            await AssertUserRatingAsync(db, user3, card2, 0);
        }