Exemplo n.º 1
0
        public async void RequestContainsExistedWords()
        {
            // arrange
            int userId  = 1;
            int otherId = 2;

            using InWordsDataContext context = InWordsDataContextFactory.Create();
            await context.AddAccount(userId);

            await context.AddAccount(otherId);

            await context.SaveChangesAsync();

            context.Add(CreateUserWordPair(userId, "0", "0-0", 1));
            context.Add(CreateUserWordPair(userId, "1", "1-1", 2));
            context.Add(CreateUserWordPair(otherId, "2", "2-2", 3));
            context.Add(CreateUserWordPair(userId, "3", "3-3", 4));
            await context.SaveChangesAsync();

            // act
            var service     = new GetUserWords(context);
            var requestData = new GetWordsRequest();

            requestData.UserWordpairIds.Add(new int[] { 1, 2, 99, 100 });
            var request = new AuthReq <GetWordsRequest, WordsReply>(requestData)
            {
                UserId = userId
            };
            var reply = await service.HandleRequest(request, default);

            // assert
            Assert.Equal(4, reply.ToAdd[0].UserWordPair);
            Assert.Equal(new int[] { 99, 100 }, reply.ToDelete.ToArray());
        }
Exemplo n.º 2
0
        public async void GetUserWords()
        {
            // arrange
            int userId  = 1;
            int otherId = 2;

            using InWordsDataContext context = InWordsDataContextFactory.Create();
            await context.AddAccount(userId);

            await context.AddAccount(otherId);

            await context.SaveChangesAsync();

            context.Add(CreateUserWordPair(userId, "0", "0-0"));
            context.Add(CreateUserWordPair(userId, "1", "1-1"));
            context.Add(CreateUserWordPair(userId, "3", "3-3"));
            context.Add(CreateUserWordPair(otherId, "2", "2-2"));
            await context.SaveChangesAsync();

            // act
            var service     = new GetUserWords(context);
            var requestData = new GetWordsRequest();
            var request     = new AuthReq <GetWordsRequest, WordsReply>(requestData)
            {
                UserId = userId
            };
            var reply = await service.HandleRequest(request, default);

            // assert
            Assert.Equal(context.UserWordPairs.Where(d => d.UserId == userId).Count(), reply.ToAdd.Count());
            Assert.Empty(reply.ToDelete);
        }
Exemplo n.º 3
0
        public async void DeleteExistedAccount()
        {
            // arrange
            int userId  = 1;
            int otherId = 2;

            await using InWordsDataContext context = InWordsDataContextFactory.Create();
            await context.AddAccount(userId);

            await context.AddAccount(otherId);

            await context.SaveChangesAsync();

            // act
            var requestObject =
                new AuthorizedRequestObject <DeleteAccountRequest, Empty>(
                    new DeleteAccountRequest()
            {
                Text = "Delete me just for test"
            })
            {
                UserId = userId
            };

            var registration = new DeleteAccount(context);
            await registration.HandleRequest(requestObject).ConfigureAwait(false);

            // assert
            Assert.Single(context.Accounts);
            Assert.Equal(otherId, context.Accounts.First().AccountId);
        }
Exemplo n.º 4
0
        public async void DeleteUserWordPairs_ShouldBeOk()
        {
            // arrange
            int userId  = 1;
            int otherId = 2;

            await using InWordsDataContext context = InWordsDataContextFactory.Create();
            await context.AddAccount(userId);

            await context.AddAccount(otherId);

            await context.SaveChangesAsync();

            context.Add(new UserWordPair()
            {
                UserWordPairId = 1,
                ForeignWord    = "Test1",
                NativeWord     = "Тест1",
                UserId         = userId
            });;
            context.Add(new UserWordPair()
            {
                UserWordPairId = 2,
                ForeignWord    = "Test2",
                NativeWord     = "Тест2",
                UserId         = userId
            });
            context.Add(new UserWordPair()
            {
                UserWordPairId = 3,
                ForeignWord    = "Test3",
                NativeWord     = "Тест3",
                UserId         = userId
            });

            await context.SaveChangesAsync();

            DeleteWordsRequest deletewordsRequets = new DeleteWordsRequest();

            deletewordsRequets.Delete.AddRange(new int[] { 1, 3 });

            // act
            var requestObject = new AuthReq <DeleteWordsRequest, Empty>(deletewordsRequets)
            {
                UserId = userId
            };

            var   deleteWords = new DeleteWords(context);
            Empty response    = await deleteWords.HandleRequest(requestObject).ConfigureAwait(false);

            // assert
            Assert.Equal(1, context.UserWordPairs.Count());
            Assert.Equal("Тест2", context.UserWordPairs.First().NativeWord);
        }
Exemplo n.º 5
0
        private static async Task CreateTestContext(InWordsDataContext context)
        {
            await context.AddAccount(1);

            context.Languages.AddLanguages();


            HashSet <GameLevelWord> gameLevelWords = new HashSet <GameLevelWord>
            {
                new GameLevelWord()
                {
                    NativeWord = "тест4", ForeignWord = "test4"
                },
                new GameLevelWord()
                {
                    NativeWord = "тест5", ForeignWord = "test5"
                }
            };

            HashSet <GameLevel> gameLevels = new HashSet <GameLevel>
            {
                new GameLevel()
                {
                    GameLevelWords = gameLevelWords,
                }
            };
            Game game = new Game()
            {
                CreatorId  = 0,
                GameLevels = gameLevels
            };

            context.Games.Add(game);
            await context.SaveChangesAsync();
        }
Exemplo n.º 6
0
        public async void Find_Existed_Account()
        {
            // arrange
            int    userId   = 1;
            string nickname = "nick";

            int other = 2;

            await using InWordsDataContext context = InWordsDataContextFactory.Create();
            await context.AddAccount(userId);

            await context.SaveChangesAsync();

            context.Users.First().NickName = nickname;
            context.SaveChanges();


            // act
            var requestData = new FindUsernameRequest()
            {
                UserName = nickname
            };
            var request = new AuthReq <FindUsernameRequest, PublicProfilesReply>(requestData)
            {
                UserId = other,
            };
            var handler = new FindProfileNickname(context);
            var result  = await handler.Handle(request);

            // assert
            Assert.Equal(userId, result.Users.First().UserId);
        }
Exemplo n.º 7
0
        public async void ConfirmValidCode()
        {
            // arrange
            int    userId    = 1;
            Guid   rightGuid = Guid.NewGuid();
            string newEmail  = "*****@*****.**";

            await using InWordsDataContext context = InWordsDataContextFactory.Create();
            await context.AddAccount(userId);

            context.EmailVerifies.Add(new EmailVerifies()
            {
                UserId = userId,
                Guid   = rightGuid,
                Email  = newEmail
            });
            await context.SaveChangesAsync();

            // act
            var requestObject =
                new RequestObject <ConfirmEmailLinkRequest, ConfirmEmailReply>(
                    new ConfirmEmailLinkRequest()
            {
                ActivationGuid = rightGuid.ToString()
            });

            var registration = new ConfirmEmailLink(context);
            var test         = await registration.HandleRequest(requestObject).ConfigureAwait(false);

            // assert
            Assert.Equal(0, context.EmailVerifies.Count());
            Assert.Equal(newEmail, test.Email);
            Assert.Equal(newEmail, context.Accounts.First().Email);
        }
Exemplo n.º 8
0
        public async void Find_Existed_Account()
        {
            // arrange
            int userId = 1;

            int other = 2;

            await using InWordsDataContext context = InWordsDataContextFactory.Create();
            await context.AddAccount(userId);

            await context.SaveChangesAsync();

            // act
            var requestData = new FindIdRequest()
            {
                UserId = userId
            };
            var request = new AuthReq <FindIdRequest, PublicProfileReply>(requestData)
            {
                UserId = other,
            };
            var handler = new FindProfileId(context);
            var result  = await handler.Handle(request);

            // assert
            Assert.Equal(userId, result.UserId);
        }
Exemplo n.º 9
0
        public async void TrainingIds_Ok()
        {
            // arrange
            int userId  = 1;
            int otherId = 2;

            await using InWordsDataContext context = InWordsDataContextFactory.Create();
            await context.AddAccount(userId);

            await context.AddAccount(otherId);

            await context.SaveChangesAsync();

            var time = DateTime.UtcNow;

            context.UserWordPairs.Add(new UserWordPair()
            {
                UserWordPairId = 1, UserId = userId, NativeWord = "1", ForeignWord = "1", Background = false, TimeGap = time
            });
            context.UserWordPairs.Add(new UserWordPair()
            {
                UserWordPairId = 2, UserId = userId, NativeWord = "2", ForeignWord = "2", Background = false, TimeGap = time.AddDays(1)
            });
            context.UserWordPairs.Add(new UserWordPair()
            {
                UserWordPairId = 3, UserId = userId, NativeWord = "2", ForeignWord = "2", Background = false, TimeGap = time.AddDays(2)
            });
            context.UserWordPairs.Add(new UserWordPair()
            {
                UserWordPairId = 4, UserId = otherId, NativeWord = "3", ForeignWord = "3", Background = false, TimeGap = time
            });
            context.SaveChanges();
            // act
            var requestObject = new AuthReq <Empty, TrainingIdsReply>(new Empty())
            {
                UserId = userId
            };

            var addWords = new GetTrainingIds(context);
            TrainingIdsReply response = await addWords.HandleRequest(requestObject).ConfigureAwait(false);

            // assert
            Assert.Equal(2, response.UserWordPairs.Count);
            Assert.Contains(1, response.UserWordPairs);
            Assert.Contains(2, response.UserWordPairs);
        }
Exemplo n.º 10
0
        public async void GetLevelWords()
        {
            // arrange
            int userId = 1;

            await using InWordsDataContext context = InWordsDataContextFactory.Create();
            Game game = new Game()
            {
            };

            context.Add(game);
            await context.SaveChangesAsync();

            GameLevel gameLevel = new GameLevel()
            {
                GameId = game.GameId
            };

            context.Add(gameLevel);
            await context.SaveChangesAsync();

            List <GameLevelWord> gameLevelWords = new List <GameLevelWord>
            {
                new GameLevelWord()
                {
                    GameLevelId = gameLevel.GameLevelId, ForeignWord = "test1", NativeWord = "тест1"
                },
                new GameLevelWord()
                {
                    GameLevelId = gameLevel.GameLevelId, ForeignWord = "test2", NativeWord = "тест2"
                },
            };

            context.GameLevelWords.AddRange(gameLevelWords);
            await context.SaveChangesAsync();

            await context.AddAccount(userId);

            await context.SaveChangesAsync();

            // act
            var data = new GetLevelWordsRequest()
            {
                LevelId = gameLevel.GameLevelId
            };
            var request = new AuthReq <GetLevelWordsRequest, GetLevelWordsReply>(data)
            {
                UserId = userId
            };
            GetLevelWordsReply result = await new GetLevelWords(context).HandleRequest(request);

            // assert
            Assert.Equal(2, result.Words.Count);
        }
Exemplo n.º 11
0
        public async void AddUserWordPair()
        {
            // arrange
            int userId  = 1;
            int otherId = 2;

            await using InWordsDataContext context = InWordsDataContextFactory.Create();
            await context.AddAccount(userId);

            await context.AddAccount(otherId);

            await context.SaveChangesAsync();

            AddWordsRequest addWordsRequest = new AddWordsRequest();

            addWordsRequest.Words.Add(new AddWordRequest()
            {
                LocalId = 0, WordForeign = "0", WordNative = "0-0"
            });
            addWordsRequest.Words.Add(new AddWordRequest()
            {
                LocalId = 2, WordForeign = "2", WordNative = "2-2"
            });

            // act
            var requestObject = new AuthorizedRequestObject <AddWordsRequest, AddWordsReply>(addWordsRequest)
            {
                UserId = userId
            };

            var           registration = new AddWords(context);
            AddWordsReply response     = await registration.HandleRequest(requestObject).ConfigureAwait(false);

            // assert
            Assert.Equal(4, context.Words.Count());
            Assert.Equal(2, context.WordPairs.Count());
            Assert.Equal(2, context.UserWordPairs.Count());

            CheckPairs(addWordsRequest, response, context, userId);
        }
Exemplo n.º 12
0
        public async void TrainingIds_Empty()
        {
            int userId  = 1;
            int otherId = 2;

            await using InWordsDataContext context = InWordsDataContextFactory.Create();
            await context.AddAccount(userId);

            await context.AddAccount(otherId);

            await context.SaveChangesAsync();

            var requestObject = new AuthReq <Empty, TrainingIdsReply>(new Empty())
            {
                UserId = userId
            };

            var addWords = new GetTrainingIds(context);
            TrainingIdsReply response = await addWords.HandleRequest(requestObject).ConfigureAwait(false);

            Assert.Empty(response.UserWordPairs);
        }
Exemplo n.º 13
0
        public async void EstimateTraining_Returns6Stars()
        {
            // arrange
            int userId = 1;

            using InWordsDataContext context = InWordsDataContextFactory.Create();
            Game game = new Game()
            {
            };

            context.Add(game);
            context.SaveChanges();
            GameLevel gameLevel = new GameLevel()
            {
                GameId = game.GameId
            };

            context.Add(gameLevel);
            context.SaveChanges();
            await context.AddAccount(userId);

            await context.SaveChangesAsync();

            // act
            TrainingDataRequest trainingDataRequest = new TrainingDataRequest();
            Training            training            = new Training {
            };

            training.ClosedCardsMetric = new ClosedCardsMetric();
            training.ClosedCardsMetric.WordIdOpenCount.Add(1, 4);
            training.ClosedCardsMetric.WordIdOpenCount.Add(2, 4);
            training.ClosedCardsMetric.WordIdOpenCount.Add(3, 2);
            training.OpenedCardsMetric = new OpenedCardsMetric();
            training.OpenedCardsMetric.WordIdOpenCount.Add(1, 3);
            training.OpenedCardsMetric.WordIdOpenCount.Add(2, 2);
            training.OpenedCardsMetric.WordIdOpenCount.Add(3, 2);
            trainingDataRequest.Metrics.Add(training);

            var requestData = new AuthReq <TrainingDataRequest, TrainingScoreReply>(trainingDataRequest)
            {
                UserId = userId
            };
            var requestHandler = new EstimateTraining(context);
            var result         = await requestHandler.HandleRequest(requestData);

            // assert
            Assert.Single(result.Scores);
            Assert.Equal(6, result.Scores.Single().ClosedCards.Score);
            Assert.Equal(5, result.Scores.Single().OpenedCards.Score);
            Assert.Equal(6, result.Scores.Single().Score);
        }
Exemplo n.º 14
0
        public async void Find_Existed_Account()
        {
            // arrange
            int userId = 1;

            await using InWordsDataContext context = InWordsDataContextFactory.Create();
            await context.AddAccount(userId);

            await context.SaveChangesAsync();

            var request = new AuthReq <Empty, ProfileReply>(new Empty())
            {
                UserId = userId,
            };

            var handler = new GetCurrentProfile(context);
            var result  = await handler.Handle(request);

            // assert
            Assert.Equal(userId, result.UserId);
        }
Exemplo n.º 15
0
        public async void ConfirmValidCode()
        {
            // arrange
            int    userId    = 1;
            int    rightCode = 111111;
            string newEmail  = "*****@*****.**";

            await using InWordsDataContext context = InWordsDataContextFactory.Create();
            await context.AddAccount(userId);

            context.EmailVerifies.Add(new EmailVerifies()
            {
                UserId = userId,
                Code   = rightCode,
                Email  = newEmail
            });
            await context.SaveChangesAsync();

            Account account = context.Accounts.First();

            // act
            var requestObject =
                new AuthReq <ConfirmEmailRequest, ConfirmEmailReply>(
                    new ConfirmEmailRequest()
            {
                Email = newEmail, Code = rightCode
            })
            {
                UserId = account.AccountId
            };

            var registration = new ConfirmEmail(context);
            var test         = await registration.HandleRequest(requestObject).ConfigureAwait(false);

            // assert
            Assert.Equal(0, context.EmailVerifies.Count());
            Assert.Equal(newEmail, test.Email);
            Assert.Equal(newEmail, context.Accounts.First().Email);
        }
Exemplo n.º 16
0
        public async void CustomGames_SaveOnce()
        {
            int userId = 1;

            using InWordsDataContext context = InWordsDataContextFactory.Create();
            await context.AddAccount(userId);

            await context.SaveChangesAsync();

            EstimateTraining handler = new EstimateTraining(context);

            var payload = new TrainingDataRequest();

            var training = new Training
            {
                AudioMetric             = new AudioMetric(),
                ClosedAudioCards2Metric = new ClosedAudioCardsTwoMetric()
            };

            training.AudioMetric.WordIdOpenCount.Add(1, 1);
            training.AudioMetric.WordIdOpenCount.Add(2, 1);
            training.AudioMetric.WordIdOpenCount.Add(3, 1);
            training.ClosedAudioCards2Metric.WordIdOpenCount.Add(1, 1);
            training.ClosedAudioCards2Metric.WordIdOpenCount.Add(2, 1);
            training.ClosedAudioCards2Metric.WordIdOpenCount.Add(3, 1);
            payload.Metrics.Add(training);

            var request = new AuthReq <TrainingDataRequest, TrainingScoreReply>(payload);

            var result = await handler.HandleRequest(request);

            Assert.Single(context.Games);
            Assert.Single(context.GameTags);
            Assert.Equal(GameTags.CustomLevelsHistory, context.GameTags.First().Tags);
            Assert.Single(context.GameLevels);
            Assert.Single(context.Historylevels);
            Assert.Empty(context.GameLevelWords); // Because words 1,2,3 is not not found
        }
Exemplo n.º 17
0
        private static async Task CreateTestContext(InWordsDataContext context)
        {
            await context.AddAccount(1);

            context.Languages.AddLanguages();

            int word1 = context.AddWordPair("test", "Тест");
            int word2 = context.AddWordPair("tes2", "Тест2");
            int word3 = context.AddWordPair("test3", "Тест3");

            HashSet <GameLevelWord> gameLevelWords = new HashSet <GameLevelWord>
            {
                new GameLevelWord()
                {
                    WordPairId = word1
                },
                new GameLevelWord()
                {
                    WordPairId = word2
                }
            };

            HashSet <GameLevel> gameLevels = new HashSet <GameLevel>
            {
                new GameLevel()
                {
                    GameLevelWords = gameLevelWords,
                }
            };
            Game game = new Game()
            {
                CreatorId  = 0,
                GameLevels = gameLevels
            };

            context.Games.Add(game);
            await context.SaveChangesAsync();
        }
Exemplo n.º 18
0
        private static async Task CreateTestContext(InWordsDataContext context)
        {
            await context.AddAccount(1);

            context.Languages.AddLanguages();

            HashSet <GameLevelWord> gameLevelWords1 = new HashSet <GameLevelWord>
            {
                new GameLevelWord()
                {
                    NativeWord = "тест4", ForeignWord = "test4"
                },
                new GameLevelWord()
                {
                    NativeWord = "тест5", ForeignWord = "test5"
                }
            };

            CreateGame(gameLevelWords1, context);
            await context.SaveChangesAsync();

            HashSet <GameLevelWord> gameLevelWords2 = new HashSet <GameLevelWord>
            {
                new GameLevelWord()
                {
                    NativeWord = "тест6", ForeignWord = "test6"
                },
                new GameLevelWord()
                {
                    NativeWord = "тест7", ForeignWord = "test7"
                }
            };

            CreateGame(gameLevelWords2, context);

            await context.SaveChangesAsync();
        }
Exemplo n.º 19
0
        public async void ConfirmInValidCode()
        {
            // arrange
            int    userId    = 1;
            int    rightCode = 111111;
            int    badCode   = 222222;
            string newEmail  = "*****@*****.**";

            await using InWordsDataContext context = InWordsDataContextFactory.Create();
            await context.AddAccount(userId);

            context.EmailVerifies.Add(new EmailVerifies()
            {
                UserId = userId,
                Code   = rightCode,
                Email  = newEmail
            });
            await context.SaveChangesAsync();

            Account account = context.Accounts.First();

            // act
            var requestObject =
                new AuthReq <ConfirmEmailRequest, ConfirmEmailReply>(
                    new ConfirmEmailRequest()
            {
                Email = newEmail, Code = badCode
            })
            {
                UserId = account.AccountId
            };

            // assert
            var registration = new ConfirmEmail(context);
            await Assert.ThrowsAsync <ArgumentNullException>(() => registration.HandleRequest(requestObject));
        }