Beispiel #1
0
        public async Task <IHttpActionResult> LogEntries(string date)
        {
            var day = DateTimeOffset.Now;

            var res = DateTimeOffset.TryParseExact(date, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out var d);

            if (res)
            {
                day = d;
            }

            var entries = await _logService.GetLogEntries(startDate : day.Date, endDate : day.Date.AddDays(1).AddSeconds(-1));

            return(Json(entries.Select(e => new LogEntryViewModel
            {
                Date = e.Date,
                Id = e.Id,
                Message = LogFormatter.FormatLogEntry(e),
                User = e.SourceUser
            })));
        }
        private async Task GetLogEntries(DateTimeOffset?date)
        {
            var controller = CreateController();

            var result = await controller.LogEntries(date?.ToString("yyyyMMdd"));

            var actualDate     = date ?? DateTimeOffset.Now.Date;
            var expectedResult = TestLogEntries.Where(l => l.Date == actualDate.Date).ToList();

            Assert.IsType <JsonResult <IEnumerable <LogEntryViewModel> > >(result);

            var res = result as JsonResult <IEnumerable <LogEntryViewModel> >;

            Assert.NotNull(res);

            foreach (var r in res.Content)
            {
                var original = expectedResult.First(l => l.Id == r.Id);

                Assert.Equal(original.Date, r.Date);
                Assert.Equal(LogFormatter.FormatLogEntry(original), r.Message);
                Assert.Equal(original.SourceUser, r.User);
            }
        }
        private static void FormatLogEntry(LogEntry entry, string expectedString)
        {
            var result = LogFormatter.FormatLogEntry(entry);

            Assert.Equal(expectedString, result);
        }