public async void GetDailyChat_InvalidEventType_Test() { //didnt add a test for pagenumber & pagesize, since i opted to handle them with dto attributes //so they are validated at the api level, and here we are directly calling the services and //bypassing the api var dto = new DailyChatInputDto { PageNumber = 1, PageSize = 15, ChatEventTypeId = 5 //invalid ChatEventTypeId }; await Should.ThrowAsync <UserFriendlyException>(async() => await _chatService.GetDailyChat(dto)); }
public async void GetDailyChat_Paging_Test() { var dto = new DailyChatInputDto { PageNumber = 1, PageSize = 8 }; var activeChats = await _chatService.GetDailyChat(dto); activeChats.TotalCount.ShouldBe(13); activeChats.ChatMessages.Count.ShouldBe(8); activeChats.ChatMessages.First().Time.ShouldBe(Clock.GetTimeDate(2020, 12, 24, 17, 0)); //message no.8 is at 6pm activeChats.ChatMessages.Last().Time.ShouldBe(Clock.GetTimeDate(2020, 12, 24, 18, 0)); }
public async Task <DailyChatOutputDto> GetDailyChat(DailyChatInputDto dto) { if (dto.ChatEventTypeId != null && !Enum.IsDefined(typeof(ChatEventTypes), dto.ChatEventTypeId)) { throw new UserFriendlyException("Invalid Event Type"); } var query = from e in _dbContext.ChatEvents join u1 in _dbContext.Users on e.User1Id equals u1.Id join u2 in _dbContext.Users on e.User2Id equals u2.Id into utemp from u2 in utemp.DefaultIfEmpty() where e.IsActive && (dto.ChatEventTypeId == null || e.ChatEventTypeId == dto.ChatEventTypeId) orderby e.CreatedDate select new { ChatEventTypeId = e.ChatEventTypeId, User1 = u1.Username, User2 = u2 != null ? u2.Username : null, Message = e.Message, CreatedDate = e.CreatedDate }; var total = await query.CountAsync(); var messages = await query.Skip(dto.ToSkip).Take(dto.PageSize) .Select(e => new ChatDto { Time = e.CreatedDate, Message = ChatEventsHelper.ParseChatEvent(e.ChatEventTypeId, e.Message, e.User1, e.User2) }).ToListAsync(); var result = new DailyChatOutputDto { TotalCount = total, ChatMessages = messages }; return(result); }
public async void GetDailyChat_Test() { var dto = new DailyChatInputDto { PageNumber = 1, PageSize = 100 }; var activeChats = await _chatService.GetDailyChat(dto); //1 inactive activeChats.TotalCount.ShouldBe(13); //page size should be the same as the total above activeChats.ChatMessages.Count.ShouldBe(13); var allChatsCount = await _dbContext.ChatEvents.CountAsync(); allChatsCount.ShouldBe(14); activeChats.ChatMessages.First().Time.ShouldBe(Clock.GetTimeDate(2020, 12, 24, 17, 0)); activeChats.ChatMessages.Last().Time.ShouldBe(Clock.GetTimeDate(2020, 12, 24, 18, 25)); }
public async Task <DailyChatOutputDto> GetDailyChat(DailyChatInputDto dto) { return(await _chatService.GetDailyChat(dto)); }