public void GetUserWorkspaces_WorkspacesExist_CachesResult()
        {
            string     authUserId = Guid.NewGuid().ToString();
            string     cacheKey   = CacheKeys.UserWorkspace(authUserId);
            IDbContext dbContext  = Substitute.For <IDbContext>();
            IEnumerable <WorkspaceViewModel>?cachedViewModel = null;
            UserDbModel userDbModel = new UserDbModel();

            userDbModel.Workspaces = new string[1] {
                "workspace1"
            };

            WorkspaceDbModel[] workspaceDbModels = new WorkspaceDbModel[1] {
                new WorkspaceDbModel()
            };

            UserRepository userRepository = Substitute.For <UserRepository>();

            userRepository.GetByAuthIdAsync(dbContext, authUserId).Returns <UserDbModel>(userDbModel);

            WorkspaceRepository workspaceRepository = Substitute.For <WorkspaceRepository>();

            workspaceRepository.GetManyByIdAsync(dbContext, userDbModel.Workspaces).Returns(workspaceDbModels);

            IMemoryCacheWrapper memoryCache = Substitute.For <IMemoryCacheWrapper>();

            memoryCache.Get <IEnumerable <WorkspaceViewModel> >(cacheKey).Returns(cachedViewModel);

            UserWorkspaceViewService         userWorkspaceViewService = new UserWorkspaceViewService(dbContext, memoryCache, Substitute.For <IMapper>(), userRepository, workspaceRepository);
            IEnumerable <WorkspaceViewModel> result = userWorkspaceViewService.GetUserWorkspaces(authUserId).Result;

            // assert
            memoryCache.Received(1).Set <IEnumerable <WorkspaceViewModel> >(cacheKey, Arg.Any <IEnumerable <WorkspaceViewModel> >(), Arg.Any <TimeSpan>());
        }
        public async Task <IEnumerable <WorkspaceViewModel> > GetUserWorkspaces(string authUserId)
        {
            string cacheKey = CacheKeys.UserWorkspace(authUserId);
            IEnumerable <WorkspaceViewModel> workspaceViewModels = _memoryCache.Get <IEnumerable <WorkspaceViewModel> >(cacheKey);

            if (workspaceViewModels == null)
            {
                var userModel = await _userRepository.GetByAuthIdAsync(_dbContext, authUserId);

                // if the user has not added any workspaces yet, they may have no user record....so just return an empty enum
                if (userModel == null)
                {
                    return(Enumerable.Empty <WorkspaceViewModel>());
                }
                if (userModel.Workspaces == null || !userModel.Workspaces.Any())
                {
                    workspaceViewModels = Enumerable.Empty <WorkspaceViewModel>();
                }
                else
                {
                    var workspaceDbModels = await _workspaceRepository.GetManyByIdAsync(_dbContext, userModel.Workspaces);

                    workspaceViewModels = _mapper.Map <IEnumerable <WorkspaceDbModel>, IEnumerable <WorkspaceViewModel> >(workspaceDbModels);
                }

                _memoryCache.Set(cacheKey, workspaceViewModels, TimeSpan.FromMinutes(15));
            }

            return(workspaceViewModels);
        }
예제 #3
0
        public void GetManyByIdAsync_OnExecution_InvokesDbContextCall()
        {
            IDbContext dbContext = Substitute.For <IDbContext>();

            string[] ids = new string[2] {
                "1", "2"
            };

            WorkspaceRepository workspaceRepository = new WorkspaceRepository();

            workspaceRepository.GetManyByIdAsync(dbContext, ids).Wait();

            dbContext.Received(1).GetManyAsync <WorkspaceDbModel>(DbSchema.WorkspaceContainer, Arg.Any <FilterDefinition <WorkspaceDbModel> >());
        }
예제 #4
0
        public void GetManyByIdAsync_ItemsDoNotExist_ReturnsEmptyCollection()
        {
            MongoDbContext dbContext = RepositoryTestUtils.CreateDbContext();

            dbContext.InsertAsync(DbSchema.WorkspaceContainer, DbModelUtils.GetWorkspaceDbModel()).Wait();
            dbContext.InsertAsync(DbSchema.WorkspaceContainer, DbModelUtils.GetWorkspaceDbModel()).Wait();
            dbContext.InsertAsync(DbSchema.WorkspaceContainer, DbModelUtils.GetWorkspaceDbModel()).Wait();
            IEnumerable <string> ids = new string[2] {
                "1", "2"
            };

            WorkspaceRepository            workspaceRepository = new WorkspaceRepository();
            IEnumerable <WorkspaceDbModel> result = workspaceRepository.GetManyByIdAsync(dbContext, ids).Result;

            Assert.AreEqual(0, result.Count());
        }
예제 #5
0
        public void GetManyByIdAsync_ItemsExist_ReturnsCollection()
        {
            MongoDbContext   dbContext         = RepositoryTestUtils.CreateDbContext();
            WorkspaceDbModel workspaceDbModel1 = DbModelUtils.GetWorkspaceDbModel();

            dbContext.InsertAsync(DbSchema.WorkspaceContainer, workspaceDbModel1).Wait();
            WorkspaceDbModel workspaceDbModel2 = DbModelUtils.GetWorkspaceDbModel();

            dbContext.InsertAsync(DbSchema.WorkspaceContainer, workspaceDbModel2).Wait();

            dbContext.InsertAsync(DbSchema.WorkspaceContainer, DbModelUtils.GetWorkspaceDbModel()).Wait();
            dbContext.InsertAsync(DbSchema.WorkspaceContainer, DbModelUtils.GetWorkspaceDbModel()).Wait();

            IEnumerable <string> ids = new string[2] {
                workspaceDbModel1.Id, workspaceDbModel2.Id
            };

            WorkspaceRepository     workspaceRepository = new WorkspaceRepository();
            List <WorkspaceDbModel> result = workspaceRepository.GetManyByIdAsync(dbContext, ids).Result.ToList();

            Assert.AreEqual(2, result.Count());
            Assert.IsNotNull(result.SingleOrDefault(x => x.Id == workspaceDbModel1.Id));
            Assert.IsNotNull(result.SingleOrDefault(x => x.Id == workspaceDbModel2.Id));
        }