Esempio n. 1
0
        public async Task GetCommand_Success(string commandName, LangCode langCode, string expectedName)
        {
            var message = new TextUpdateMessage
            {
                ChatId       = 1,
                LanguageCode = langCode,
            };

            var command = _commandFactory.GetCommand(commandName);
            await command.HandleAsync(message);

            Assert.Equal(command.Name, expectedName);
        }
Esempio n. 2
0
        public void Parse_Fail(LangCode code, DateTime updateDate, string content)
        {
            var message = new TextUpdateMessage()
            {
                LanguageCode   = code,
                Content        = content,
                UpdateDatetime = updateDate,
            };

            (Value value, string error) = _parser.Parse(message);

            Assert.Null(value);
            Assert.NotNull(error);
        }
Esempio n. 3
0
        public void Parse_Success(LangCode code, DateTime updateDate, string content,
                                  double expectedNumber, DateTime expectedDate)
        {
            var message = new TextUpdateMessage()
            {
                LanguageCode   = code,
                Content        = content,
                UpdateDatetime = updateDate,
            };

            (Value value, string error) = _parser.Parse(message);

            Assert.NotNull(value);
            Assert.Null(error);
            Assert.Equal(value.Content, expectedNumber);
            EqualDateTimeToMinutes(value.ValueDate, expectedDate);
        }
Esempio n. 4
0
        public async Task Visit(TextUpdateMessage message)
        {
            var source = GetSource(message);

            source.UpdateLastActionDate();

            // Need to check the user's state to understand how to process text
            if (source.State == StateType.HasMetric)
            {
                // Processing the value for the main metric
                await _valueHandlerService.HandleAsync(message);

                _sourceRepository.Update(source);
                return;
            }
            else if (source.State == StateType.NeedAddMetric)
            {
                // Create new metric with name
                var newMetric = new Metric(message.Content);
                newMetric.UpdateSource(source);
                var mainMetric = _metricRepository.GetMainBySourceId(source.Id);
                newMetric.UpdateMain(mainMetric == null);
                _metricRepository.Add(newMetric);
                source.UpdateState(StateType.NeedAddUnit);
            }
            else if (source.State == StateType.NeedAddUnit)
            {
                // Adding metric unit for existing metric
                var editedMetric = _metricRepository.GetNotCreatedMetric(source.Id)
                                   ?? throw new ArgumentException($"Can't add main unit, there is no edited metric for source {message.ChatId}");

                editedMetric.UpdateUnit(message.Content);
                editedMetric.SetCreated();
                _metricRepository.Update(editedMetric);
                source.UpdateState(StateType.HasMetric);
            }

            _sourceRepository.Update(source);
            await _botClient.SendTextMessageAsync(message, GetMessageCode(source.State));
        }
Esempio n. 5
0
        public async Task Process_TextMessage_SourceHasNoMetric(LangCode langCode, StateType initialState)
        {
            int    externalId = 1;
            string name       = "Koko";
            Source source     = new Source(externalId, name);

            source.UpdateState(initialState);

            var mainMetric = new Metric("Some metric");

            mainMetric.UpdateSource(source);
            mainMetric.UpdateMain(true);

            MetricRepoMock.Setup(r => r.GetMainBySourceId(It.IsAny <long>())).Returns(() => mainMetric);
            MetricRepoMock.Setup(r => r.GetNotCreatedMetric(It.IsAny <long>())).Returns(() => mainMetric);

            SourceRepoMock.Setup(r => r.FindOrCreateNew(externalId, name)).Returns(() => source);
            SourceRepoMock.Setup(r => r.Update(source));

            MetricRepoMock.Setup(r => r.Add(It.IsAny <Metric>()));
            MetricRepoMock.Setup(r => r.Update(It.IsAny <Metric>()));

            var message = new TextUpdateMessage
            {
                ChatId       = externalId,
                Username     = name,
                Content      = "name or unit of metric",
                LanguageCode = langCode,
            };

            await message.Accept(MessageProcessor);

            var localizer = new Localizer();
            var text      = localizer.GetMessage(langCode, ProcessMessageVisitor.GetMessageCode(source.State));

            BotResponseMock.Verify(x => x.Response(text), Times.Once);
        }