コード例 #1
0
        public void GetUserWorkspaces_NoUser_ReturnsEmptyEnumerable()
        {
            string     authUserId = Guid.NewGuid().ToString();
            string     cacheKey   = CacheKeys.UserWorkspace(authUserId);
            IDbContext dbContext  = Substitute.For <IDbContext>();
            IEnumerable <WorkspaceViewModel>?cachedViewModel = null;

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

            userRepository.GetByAuthIdAsync(dbContext, authUserId).Returns <UserDbModel>(x => null);

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

            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
            Assert.AreEqual(0, result.Count());
            userRepository.Received(1).GetByAuthIdAsync(dbContext, authUserId);
            workspaceRepository.DidNotReceive().GetManyByIdAsync(Arg.Any <IDbContext>(), Arg.Any <IEnumerable <string> >());
        }
コード例 #2
0
        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>());
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        public void GetUserWorkspaces_WhenUserWorkspacesCached_DoesNotFetchFromRepository()
        {
            string     authUserId = Guid.NewGuid().ToString();
            string     cacheKey   = CacheKeys.UserWorkspace(authUserId);
            IDbContext dbContext  = Substitute.For <IDbContext>();
            IEnumerable <WorkspaceViewModel>?cachedViewModel = new WorkspaceViewModel[] { new WorkspaceViewModel() };

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

            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.DidNotReceive().Set <IEnumerable <WorkspaceViewModel> >(Arg.Any <string>(), Arg.Any <IEnumerable <WorkspaceViewModel> >(), Arg.Any <TimeSpan>());
            workspaceRepository.DidNotReceive().GetManyByIdAsync(Arg.Any <IDbContext>(), Arg.Any <IEnumerable <string> >());
            userRepository.DidNotReceive().GetByAuthIdAsync(Arg.Any <IDbContext>(), Arg.Any <string>());
        }
コード例 #5
0
        public void GetUserWorkspaces_UserWorkspacesEmpty_ReturnsEmptyEnumerable()
        {
            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[0];

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

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

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

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

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

            // assert
            Assert.AreEqual(0, result.Count());
        }
コード例 #6
0
ファイル: CmsTemplate.cs プロジェクト: 921819535/cms
 public object GetCache(string key)
 {
     return(cache.Get(key));
 }