Example #1
0
        public async void ClearIfRequired_With_Lower_Version_Should_Not_Invoke_Client()
        {
            var service = MoqHelper.CreateWithMocks <RedisGatewayService>();

            var versionInRedis  = 9;
            var versionInClient = 10;

            var ISettingsService = Mock.Get(service.ISettingsService);

            ISettingsService
            .Setup(x => x.Get <CacheServiceSettings>())
            .Returns(new CacheServiceSettings {
                Redis = new CacheServiceSettings.RedisSettings {
                    DataVersion = versionInRedis
                }
            });

            var IRedisClient = new Mock <IRedisClient>();

            IRedisClient.Setup(x => x.Get <int>(It.IsAny <string>())).Returns(versionInClient);

            var IRedisClientFactory = Mock.Get(service.IRedisClientFactory);

            IRedisClientFactory.Setup(x => x.GetClient()).Returns(IRedisClient.Object);

            await service.ClearIfRequiredAsync();

            IRedisClient.Verify(x => x.FlushDb(), Times.Never);
            IRedisClient.Verify(x => x.Set(It.IsAny <string>(), versionInRedis), Times.Never);
        }
Example #2
0
        public async void GetHashCounters_Should_Invoke_Client()
        {
            var service = MoqHelper.CreateWithMocks <RedisGatewayService>();

            var dict = new Dictionary <string, string> {
                { "key1", "1" }, { "key2", "2" }
            };

            var IRedisClient = new Mock <IRedisClient>();

            IRedisClient.Setup(x => x.GetAllEntriesFromHash("hash")).Returns(dict);

            var IRedisClientFactory = Mock.Get(service.IRedisClientFactory);

            IRedisClientFactory.Setup(x => x.GetClient()).Returns(IRedisClient.Object);

            var result = await service.GetHashCountersAsync("hash");

            foreach (var p in result)
            {
                Assert.Equal(dict[p.Key], p.Value.ToString());
            }

            IRedisClient.Verify(x => x.GetAllEntriesFromHash("hash"), Times.Once);
        }
Example #3
0
        public void GetQueryable()
        {
            var service = MoqHelper.CreateWithMocks <MemoryDatabaseDelegate>();

            var store = new List <DummyEntity>
            {
                new DummyEntity
                {
                    Id   = "1",
                    Name = "Foo"
                },
                new DummyEntity
                {
                    Id   = "2",
                    Name = "Bar"
                }
            };

            var IMemoryCollectionFactory = Mock.Get(service.IMemoryCollectionFactory);

            IMemoryCollectionFactory.Setup(x => x.GetOrCreateCollection <DummyEntity>()).Returns(store);

            var query = service.GetQueryable <DummyEntity>();

            Assert.Equal(2, query.Count());
        }
Example #4
0
        public async void GetSet_Should_Invoke_Client()
        {
            var service = MoqHelper.CreateWithMocks <RedisGatewayService>();

            var values = new HashSet <string> {
                "v1", "v2"
            };

            var IRedisClient = new Mock <IRedisClient>();

            IRedisClient
            .Setup(x => x.GetAllItemsFromSet("key"))
            .Returns(values);

            var IRedisClientFactory = Mock.Get(service.IRedisClientFactory);

            IRedisClientFactory.Setup(x => x.GetClient()).Returns(IRedisClient.Object);

            var result = await service.GetSetAsync <string>("key");

            foreach (var value in result)
            {
                Assert.True(values.ContainsIgnoreCase(value));
            }
        }
Example #5
0
        private RedisCacheBus CreateService()
        {
            var service = MoqHelper.CreateWithMocks <RedisCacheBus>();

            var IThreadService = Mock.Get(service.IThreadService);

            IThreadService
            .Setup(x => x.QueueUserWorkItem(It.IsAny <WaitCallback>()))
            .Callback <WaitCallback>(x => x.Invoke(null));

            var ISettingsService = Mock.Get(service.ISettingsService);

            ISettingsService
            .Setup(x => x.Get <CacheServiceSettings>())
            .Returns(new CacheServiceSettings());

            var IRedisSubscription = new Mock <IRedisSubscription>();

            var IRedisClient = new Mock <IRedisClient>();

            IRedisClient
            .Setup(x => x.CreateSubscription())
            .Returns(IRedisSubscription.Object);

            var IRedisClientFactory = Mock.Get(service.IRedisClientFactory);

            IRedisClientFactory
            .Setup(x => x.GetClient())
            .Returns(IRedisClient.Object);

            return(service);
        }
Example #6
0
        public async void GetIdByAutoId_Should_AutoId()
        {
            var service          = MoqHelper.CreateWithMocks <IdTranslationService>();
            var ICacheService    = Mock.Get(service.ICacheService);
            var IDatabaseService = Mock.Get(service.IDatabaseService);

            var store = new List <DummyEntity>
            {
                new DummyEntity
                {
                    Id     = "id",
                    AutoId = 123
                }
            };

            ICacheService
            .Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <Func <string> >(), It.IsAny <CacheOptions <string> >()))
            .Returns <string, Func <string>, CacheOptions <string> >((key, factory, options) => Task.FromResult(factory()));

            IDatabaseService
            .Setup(x => x.GetQueryable <DummyEntity>())
            .Returns(store.AsQueryable());

            var verify = await service.GetIdByAutoIdAsync <DummyEntity>(123);

            Assert.Equal("id", verify);
        }
Example #7
0
        public async void Remove()
        {
            var service = MoqHelper.CreateWithMocks <MemoryDatabaseDelegate>();

            var store = new List <DummyEntity>
            {
                new DummyEntity
                {
                    Id   = "123",
                    Name = "Foo"
                }
            };

            var IMemoryCollectionFactory = Mock.Get(service.IMemoryCollectionFactory);

            IMemoryCollectionFactory.Setup(x => x.GetOrCreateCollection <DummyEntity>()).Returns(store);

            var entity = new DummyEntity
            {
                Id = "123"
            };

            await service.RemoveAsync <DummyEntity>(entity.Id);

            Assert.DoesNotContain(store, x => x.Id == "123");
        }
Example #8
0
        public async void GetOrCreate_Should_Create_Uncached()
        {
            var service = MoqHelper.CreateWithMocks <DynamicCollectionService>();

            var store = new List <DummyEntity> {
                new DummyEntity {
                }
            };

            var IRepository = new Mock <IRepository <DummyEntity> >();

            IRepository.Setup(x => x.GetQueryable()).Returns(store.AsQueryable());

            var IContainerService = Mock.Get(service.IContainerService);

            IContainerService.Setup(x => x.Get <IRepository <DummyEntity> >()).Returns(IRepository.Object);

            var IDatabaseService = Mock.Get(service.IDatabaseService);

            IDatabaseService.SetupIgnoreArgs(x => x.QueryAsync <DummyEntity>(null)).Returns(Task.FromResult(store));

            var IDynamicCollectionFactory = Mock.Get(service.IDynamicCollectionFactory);

            IDynamicCollectionFactory
            .SetupIgnoreArgs(x => x.Create <DummyEntity>(null, null))
            .Returns(new DynamicCollection <DummyEntity>(IRepository.Object, new[] { "1" }, null));

            var verify =
                await service.GetOrCreateAsync <DummyEntity>(
                    "key",
                    c => c
                    );

            Assert.NotNull(verify);
        }
Example #9
0
        public void CreateCampaign_Command_Should_Run_Correctly()
        {
            var command = _createCampaignCommandFaker.Generate();

            _productRepositoryMock.Setup(x => x.AnyAsync(It.IsAny <Expression <Func <ProductEntity, bool> > >()))
            .ReturnsAsync(true);

            _campaignRepositoryMock.Setup(x => x.FindAsync(It.IsAny <Expression <Func <CampaignEntity, bool> > >()))
            .ReturnsAsync(new List <CampaignEntity>());

            _campaignRepositoryMock.Setup(x => x.AnyAsync(MoqHelper.IsExpression <CampaignEntity>(campaign => campaign.Name == command.CampaignName)))
            .ReturnsAsync(false);

            _campaignRepositoryMock.Setup(x => x.AddAsync(It.IsAny <CampaignEntity>()))
            .ReturnsAsync(FakeObjects.GenerateCampaign());

            var result = _handler.Handle(command, CancellationToken.None).Result;

            result.Campaign.Should().NotBeNull();

            _productRepositoryMock.Verify(x => x.AnyAsync(It.IsAny <Expression <Func <ProductEntity, bool> > >()), Times.Once);
            _campaignRepositoryMock.Verify(x => x.FindAsync(It.IsAny <Expression <Func <CampaignEntity, bool> > >()), Times.Once);
            _campaignRepositoryMock.Verify(x => x.AnyAsync(It.IsAny <Expression <Func <CampaignEntity, bool> > >()), Times.Once);
            _campaignRepositoryMock.Verify(x => x.AddAsync(It.IsAny <CampaignEntity>()), Times.Once);
        }
Example #10
0
        public async void GetMany_Should_Invoke_Client()
        {
            var service = MoqHelper.CreateWithMocks <RedisGatewayService>();

            var dict = new Dictionary <string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" }
            };

            var IRedisClient = new Mock <IRedisClient>();

            IRedisClient
            .Setup(x => x.GetAll <string>(dict.Keys))
            .Returns(dict.ToDictionary(x => x.Key, x => x.Value.ToJsonWithTypeInformation()));

            var IRedisClientFactory = Mock.Get(service.IRedisClientFactory);

            IRedisClientFactory.Setup(x => x.GetClient()).Returns(IRedisClient.Object);

            var result = await service.GetManyAsync <string>(dict.Keys);

            foreach (var value in result)
            {
                Assert.True(dict.Values.ContainsIgnoreCase(value));
            }
        }
Example #11
0
        private MemoryCacheDelegate CreateService()
        {
            var result = MoqHelper.CreateWithMocks <MemoryCacheDelegate>();

            result.IMemoryCacheService = new MemoryCacheService();
            return(result);
        }
Example #12
0
        public void Insert_Adds_Entity_To_DbSet()
        {
            var product = TestDataProvider.Products.First();

            repository.Insert(product);

            MoqHelper.GetMockFromObject(dbContext.Object.Products).Verify(p => p.Add(product), Times.Once);
        }
Example #13
0
        public void GetById_Should_Invoke_Delegate()
        {
            var service           = MoqHelper.CreateWithMocks <DummyDatabaseServnice>();
            var IDatabaseDelegate = Mock.Get(service.IMemoryDatabaseDelegate);

            service.GetByIdAsync <DummyEntity>("id");
            IDatabaseDelegate.Verify(x => x.GetByIdAsync <DummyEntity>(It.IsAny <string>()), Times.Once);
        }
Example #14
0
        public void Create_Should_Return_List()
        {
            var service = MoqHelper.CreateWithMocks <DynamicCollectionFactory>();

            var verify = service.Create <DummyEntity>(new[] { "id" });

            Assert.NotNull(verify);
        }
Example #15
0
        public void GetQueryable_Should_Invoke_Delegate()
        {
            var service           = MoqHelper.CreateWithMocks <DummyDatabaseServnice>();
            var IDatabaseDelegate = Mock.Get(service.IMemoryDatabaseDelegate);

            service.GetQueryable <DummyEntity>();
            IDatabaseDelegate.Verify(x => x.GetQueryable <DummyEntity>(), Times.Once);
        }
Example #16
0
        public void GetQueryable_Should_Invoke_DatabaseService()
        {
            var service          = MoqHelper.CreateWithMocks <DummyRepository>();
            var IDatabaseService = Mock.Get(service.IDatabaseService);

            service.GetQueryable();
            IDatabaseService.Verify(x => x.GetQueryable <DummyEntity>(), Times.Once);
        }
 public TestSetup()
 {
     CacheMock         = new TestCacheManager().CacheMock;
     TestLoggerFactory = new TestLoggerFactory();
     UserManager       = MoqHelper.GetTestUserManager();
     //UserService = new UserService(UserManager, TestLoggerFactory, CacheMock.Object, null);
     UserService = new UserService(UserManager, TestLoggerFactory, CacheMock.Object);
 }
Example #18
0
 public async void Command_Without_Start_Should_Throw_Exception()
 {
     await Assert.ThrowsAsync <Exception>(async() =>
     {
         var service = MoqHelper.CreateWithMocks <TestCacheBus>();
         await service.RemoveAsync("1");
     });
 }
Example #19
0
        public void Save_Should_Invoke_Delegate()
        {
            var service           = MoqHelper.CreateWithMocks <DummyDatabaseServnice>();
            var IDatabaseDelegate = Mock.Get(service.IMemoryDatabaseDelegate);

            service.SaveAsync(new DummyEntity());
            IDatabaseDelegate.Verify(x => x.SaveAsync(It.IsAny <DummyEntity>()), Times.Once);
        }
        public async void GetOrganisationsForUser()
        {
            Mock <HttpRequest> mockRequest = MoqHelper.CreateMockRequest(_renamedOrganisationJWT);

            var          organisationFunctions = new OrganisationFunctions();
            ObjectResult response = (ObjectResult)await organisationFunctions.GetOrganisationsForUser(mockRequest.Object, logger);

            Assert.True(response.StatusCode == 200);
        }
Example #21
0
        public async void GetById_Should_Invoke_Cache()
        {
            var service       = MoqHelper.CreateWithMocks <EntityCacheService>();
            var ICacheService = Mock.Get(service.ICacheService);

            await service.GetByIdAsync <DummyEntity>("123");

            ICacheService.VerifyIgnoreArgs(x => x.GetAsync <DummyEntity>(null, null, null), Times.Once);
        }
Example #22
0
        public async void GetByName_Should_Invoke_Database()
        {
            var service = MoqHelper.CreateWithMocks <DummyRepository>();
            var IIdTranslationService = Mock.Get(service.IIdTranslationService);

            await service.GetByNameAsync("id");

            IIdTranslationService.Verify(x => x.GetIdByNameAsync <DummyEntity>(It.IsAny <string>()), Times.Once);
        }
Example #23
0
        public async void GetByAutoId_Should_Invoke_IdTranslation()
        {
            var service = MoqHelper.CreateWithMocks <DummyRepository>();
            var IIdTranslationService = Mock.Get(service.IIdTranslationService);

            await service.GetByAutoIdAsync(1ul);

            IIdTranslationService.Verify(x => x.GetIdByAutoIdAsync <DummyEntity>(It.IsAny <ulong>()), Times.Once);
        }
Example #24
0
        public async void GetNext_Should_Create_AutoId()
        {
            var service           = MoqHelper.CreateWithMocks <AutoIdService>();
            var IAutoIdRepository = Mock.Get(service.IAutoIdRepository);

            await service.GetNext <DummyEntity>();

            IAutoIdRepository.Verify(x => x.SaveAsync(It.IsAny <AutoId>()), Times.Exactly(2));
        }
Example #25
0
        public async void PresetName_Should_Invoke_Cache()
        {
            var service       = MoqHelper.CreateWithMocks <IdTranslationService>();
            var ICacheService = Mock.Get(service.ICacheService);

            await service.PresetNameAsync(new DummyEntity { Id = "id", Name = "Foo" });

            ICacheService.VerifyIgnoreArgs(x => x.SetAsync <string>(null, null, null), Times.Once);
        }
Example #26
0
        public async void ClearAsyncc_Should_Add_Command()
        {
            var service = MoqHelper.CreateWithMocks <TestCacheBus>();

            service.Start();
            await service.ClearAsync();

            Assert.Contains("ClearAsync", service.Commands);
        }
Example #27
0
        public async void RemoveFromSetAsyncc_Should_Add_Command()
        {
            var service = MoqHelper.CreateWithMocks <TestCacheBus>();

            service.Start();
            await service.RemoveFromSetAsync("key", "value");

            Assert.Contains("RemoveFromSetAsync", service.Commands);
        }
Example #28
0
        public async void AddToSetAsyncc_Should_Add_Command()
        {
            var service = MoqHelper.CreateWithMocks <TestCacheBus>();

            service.Start();
            await service.AddToSetAsync("key", new[] { "value " });

            Assert.Contains("AddToSetAsync", service.Commands);
        }
Example #29
0
        public void Start_Should_Create_Random_Instance_Id()
        {
            var service        = MoqHelper.CreateWithMocks <TestCacheBus>();
            var IRandomService = Mock.Get(service.IRandomService);

            service.Start();
            IRandomService
            .Verify(x => x.RandomBase24(It.IsAny <int>(), It.IsAny <bool>()), Times.Once);
        }
Example #30
0
        public void Is_Mocked_Works_With_Auto_Mocked()
        {
            var mock = new Mock <IToMock>()
            {
                DefaultValue = DefaultValue.Mock
            };

            Assert.That(MoqHelper.IsMocked(mock.Object.DefaultMock()));
        }