Exemplo n.º 1
0
        public async Task TestGetVolume()
        {
            VolumeReply volumeReply = new VolumeReply()
            {
                Id     = "vol-0000000",
                Name   = "TestVolumeName",
                SizeGb = 3
            };

            volumeReply.MountPoints.Add("c:");

            Mock <IDatabase> databaseMock = new Mock <IDatabase>();

            databaseMock.Setup(d => d.KeyExists(It.IsAny <RedisKey>(), CommandFlags.None)).Returns(true);
            databaseMock.Setup(d => d.HashGetAsync(It.IsAny <RedisKey>(), It.IsAny <RedisValue>(), CommandFlags.None))
            .Returns(async(RedisKey userId, RedisValue key, CommandFlags flags) =>
            {
                if (key == "Id")
                {
                    return(volumeReply.Id);
                }
                if (key == "Name")
                {
                    return(volumeReply.Name);
                }
                else if (key == "Size")
                {
                    return(volumeReply.SizeGb);
                }
                else if (key == "MountPoints")
                {
                    return("c:");
                }
                else
                {
                    throw new Exception();
                }
            });

            Mock <IConnectionMultiplexer> connectionMock = new Mock <IConnectionMultiplexer>();

            connectionMock.Setup(d => d.GetDatabase(-1, null)).Returns(databaseMock.Object);

            Mock <IRedisConnectionService> redisServiceMock = new Mock <IRedisConnectionService>();

            redisServiceMock.Setup(d => d.Connect()).Returns(connectionMock.Object);

            Mock <ILogger <VolumesRepository> > loggerMock = new Mock <ILogger <VolumesRepository> >();
            VolumesRepository volumeDataSource             = new VolumesRepository(redisServiceMock.Object, loggerMock.Object);

            VolumeReply volume = await volumeDataSource.Get("key", "userId");

            Assert.IsTrue(volume.Id.Contains("vol"));
            Assert.IsTrue(volume.SizeGb == volumeReply.SizeGb);
            Assert.IsTrue(volume.Name == volumeReply.Name);
        }
        public void Verify_Get_ByKey_Should_ReturnTheCorrectVolume()
        {
            // Arrange
            Mock <IDbSet <Volume> > mockSetVolumes;
            var mockContext = VolumesMockingSetup.DoMockingSetupForContext(true, out mockSetVolumes);
            var repository  = new VolumesRepository(mockContext.Object);
            // Act
            var volumes = repository.Get("KING-STEPHEN");

            // Assert
            Assert.Equal("/TEST/KING-STEPHEN", volumes.ApiDetailUrl);
        }
        public void Verify_Deactivate_Should_SetTheActivePropertyToFalseOnTheEntity()
        {
            // Arrange
            Mock <IDbSet <Volume> > mockSetVolumes;
            var mockContext = VolumesMockingSetup.DoMockingSetupForContext(true, out mockSetVolumes);
            var repository  = new VolumesRepository(mockContext.Object);
            var volumes     = repository.Get(1);

            // Act
            repository.Deactivate(volumes);
            // Assert
            Assert.Equal(false, volumes.Active);
        }
        public void Verify_Remove_Should_RemoveTheEntityFromTheContext()
        {
            // Arrange
            Mock <IDbSet <Volume> > mockSetVolumes;
            var mockContext = VolumesMockingSetup.DoMockingSetupForContext(true, out mockSetVolumes);
            var repository  = new VolumesRepository(mockContext.Object);
            var volumes     = repository.Get(1);

            // Act
            repository.Remove(volumes);
            // Assert
            mockSetVolumes.Verify(x => x.Remove((Volume)volumes), Times.Once);
        }
        public void Verify_Update_Should_SetTheEntityStateToModified()
        {
            // Arrange
            Mock <IDbSet <Volume> > mockSetVolumes;
            var mockContext = VolumesMockingSetup.DoMockingSetupForContext(true, out mockSetVolumes);
            var repository  = new VolumesRepository(mockContext.Object);
            var volumes     = repository.Get(1);

            volumes.ApiDetailUrl = "/TEST";
            // Act
            repository.Update(volumes);
            // Assert
            mockContext.Verify(x => x.SetModified(It.IsAny <object>()), Times.Once);
        }
        public void Verify_Add_Should_AddTheEntityToTheContext()
        {
            // Arrange
            Mock <IDbSet <Volume> > mockSetVolumes;
            var mockContext = VolumesMockingSetup.DoMockingSetupForContext(false, out mockSetVolumes);
            var repository  = new VolumesRepository(mockContext.Object);
            var volumes     = new Volume {
                Active = true, CustomKey = "SALVATORE-RAA",
            };

            // Act
            repository.Add(volumes);
            // Assert
            mockSetVolumes.Verify(x => x.Add(volumes), Times.Once);
        }
        public void Verify_Search_WithSelectStatement_Should_ReturnAListOfDynamicObjects()
        {
            // Arrange
            Mock <IDbSet <Volume> > mockSetVolumes;
            var mockContext = VolumesMockingSetup.DoMockingSetupForContext(true, out mockSetVolumes, true);
            var repository  = new VolumesRepository(mockContext.Object);
            var searchModel = new Mock <IVolumeSearchModel>();
            // Act
            var volumes = repository.Search(searchModel.Object, i => new { i.Id, MyApiDetailUrl = i.ApiDetailUrl }).ToArray();

            // Assert
            Assert.Equal(1, volumes.Length);
            Assert.Equal(2, volumes[0].Id);
            Assert.Equal("/TEST/NIVEN-LARRY", volumes[0].MyApiDetailUrl);
            // Stephen King was filtered out because he was Inactive
        }
        public void Verify_List_Should_ReturnAListOfVolumesWithData()
        {
            // Arrange
            Mock <IDbSet <Volume> > mockSetVolumes;
            var mockContext = VolumesMockingSetup.DoMockingSetupForContext(true, out mockSetVolumes);
            var repository  = new VolumesRepository(mockContext.Object);
            // Act
            var volumes = repository.List.ToArray();

            // Assert
            Assert.Equal(2, volumes.Length);
            Assert.Equal(1, volumes[0].Id);
            Assert.Equal("/TEST/KING-STEPHEN", volumes[0].ApiDetailUrl);
            Assert.Equal(2, volumes[1].Id);
            Assert.Equal("/TEST/NIVEN-LARRY", volumes[1].ApiDetailUrl);
        }
        public void Verify_Search_WithPaging_Should_ReturnAListOfVolumesWithDataMatchingSearchParameters()
        {
            // Arrange
            Mock <IDbSet <Volume> > mockSetVolumes;
            var mockContext = VolumesMockingSetup.DoMockingSetupForContext(true, out mockSetVolumes);
            var repository  = new VolumesRepository(mockContext.Object);
            var searchModel = new VolumeSearchModel {
                Paging = new Paging {
                    Skip = 1, Take = 1
                }
            };
            // Act
            var volumes = repository.Search(searchModel /*.Object*/).ToArray();

            // Assert
            Assert.Equal(1, volumes.Length);
            Assert.Equal(2, volumes[0].Id);
            Assert.Equal("/TEST/NIVEN-LARRY", volumes[0].ApiDetailUrl);
            // Stephen King was filtered out because he was Skipped
        }
        public void Verify_Search_WithModifiedSince_Should_ReturnAListOfVolumesWithDataMatchingSearchParameters()
        {
            // Arrange
            Mock <IDbSet <Volume> > mockSetVolumes;
            var mockContext = VolumesMockingSetup.DoMockingSetupForContext(true, out mockSetVolumes, true);
            var repository  = new VolumesRepository(mockContext.Object);
            var createDate  = new System.DateTime(2015, 05, 28, 10, 45, 00).AddDays(1).AddMinutes(-1);
            //var searchModel = new Mock<IVolumeSearchModel>();
            //searchModel.Setup(m => m.ModifiedSince).Returns(() => createDate);
            var searchModel = new VolumeSearchModel {
                ModifiedSince = createDate
            };
            // Act
            var volumes = repository.Search(searchModel /*.Object*/).ToArray();

            // Assert
            Assert.Equal(1, volumes.Length);
            Assert.Equal(2, volumes[0].Id);
            Assert.Equal("/TEST/NIVEN-LARRY", volumes[0].ApiDetailUrl);
            // Stephen King was filtered out because he was created before the modified since date
        }