Example #1
0
        public async Task RedisKeyCollectionValuePairsRepositoryOfTweet_AddMultipleValuesInTheSameKeyAndRemoveThem_ShouldWork()
        {
            // Arrange
            const int NumberOfItems  = 1000;
            var       clientProvider = new RedisClientProvider();
            var       repository     = new RedisKeyCollectionValuePairsRepository <ITweet>(clientProvider);
            var       key            = Guid.NewGuid().ToString();

            var values = new List <ITweet>(NumberOfItems);

            // Act: Clean-up database
            await repository.Remove(key);

            await repository.SaveChangesAsync();

            // Act: Add
            for (int i = 0; i < NumberOfItems; ++i)
            {
                var now     = DateTime.Now + TimeSpan.FromHours(i);
                var content = $"{key} {i} {now.ToLongTimeString()}";
                var value   = new Tweet
                {
                    Id       = i,
                    PostedOn = now,
                    Content  = content
                };

                values.Add(value);
                await repository.Add(key, value);
            }

            values.TrimExcess();
            await repository.SaveChangesAsync();

            // Act: Get
            var valuesFromDb = repository.GetAll(key).ToList();

            // Assert: Get
            Assert.AreEqual(NumberOfItems, values.Count);
            Assert.AreEqual(NumberOfItems, valuesFromDb.Count);

            foreach (var value in values)
            {
                var valueFromDb = valuesFromDb.Single(v => v.Id == value.Id);
                Assert.IsNotNull(valueFromDb);

                Assert.AreEqual(value.Id, valueFromDb.Id);
                Assert.AreEqual(value.PostedOn.ToLongTimeString(), valueFromDb.PostedOn.ToLongTimeString());
                Assert.AreEqual(value.Content, valueFromDb.Content);
            }

            // Act: Remove
            await repository.Remove(key);

            Assert.That(async() => await repository.SaveChangesAsync(), Is.EqualTo(0L).After(2000));

            // Assert: Remove
            Assert.AreEqual(0, repository.GetAll(key).ToList().Count, "All values in the list should be removed.");
        }
Example #2
0
        public void RedisKeyCollectionValuePairsRepositoryOfTweet_AddNewValidKeyValuePairAndThenGetAllAndRemoveIt_ShouldWork()
        {
            // Arrange
            var clientProvider = new RedisClientProvider();
            var repository     = new RedisKeyCollectionValuePairsRepository <ITweet>(clientProvider);
            var key            = Guid.NewGuid().ToString();
            var value          = new Tweet
            {
                Id       = 0,
                Content  = Guid.NewGuid().ToString(),
                PostedOn = DateTime.UtcNow
            };

            // Act: Add
            var added = repository.Add(key, value);

            // Assert: Add
            Assert.That(async() => await added, Is.EqualTo(true));

            // Act + Assert: SaveChanges
            Assert.That(async() => await repository.SaveChangesAsync(), Is.EqualTo(0L).After(2000));

            // Act: Get
            var valuesFromDb = repository.GetAll(key);
            var valueFromDb  = valuesFromDb.Single();

            // Assert: Get
            Assert.AreEqual(1, valuesFromDb.Count());

            Assert.IsNotNull(valueFromDb);

            Assert.AreEqual(value.Id, valueFromDb.Id);
            Assert.AreEqual(value.Content, valueFromDb.Content);
            Assert.AreEqual(value.PostedOn.ToLongDateString(), valueFromDb.PostedOn.ToLongDateString());
            Assert.AreEqual(value.PostedOn.ToLongTimeString(), valueFromDb.PostedOn.ToLongTimeString());

            // Act: Remove value
            var removedValue = repository.Remove(key, value);

            // Assert: Remove value
            Assert.That(async() => await removedValue, Is.EqualTo(true));

            // Act: Remove
            var removed = repository.Remove(key);

            // Assert: Remove
            Assert.That(async() => await removed, Is.EqualTo(true));

            // Act + Assert: SaveChanges
            // Expected internal catch of "ServiceStack.Redis.RedisResponseException : Background save already in progress"
            Assert.That(async() => await repository.SaveChangesAsync(), Is.EqualTo(1L));
        }
Example #3
0
        public async Task RedisKeyCollectionValuePairsRepositoryOfTweet_RemoveValidYetPresentKey_ShouldWork()
        {
            // Arrange
            string key = "some key";

            var clientMock = new Mock <IRedisClient>();

            clientMock
            .Setup(c => c.ContainsKey(It.IsAny <string>()))
            .Returns(true);
            clientMock
            .Setup(c => c.Remove(It.IsAny <string>()))
            .Returns(true);

            var clientProviderMock = new Mock <IRedisClientProvider>();

            clientProviderMock
            .Setup(p => p.Create())
            .Returns(clientMock.Object);

            var repository = new RedisKeyCollectionValuePairsRepository <ITweet>(clientProviderMock.Object);

            // Act
            var result = await repository.Remove(key);

            // Asset
            Assert.That(result, Is.EqualTo(true));

            clientProviderMock.Verify(p => p.Create(), Times.Once);
            clientMock.Verify(c => c.ContainsKey(key), Times.Once);
            clientMock.Verify(c => c.Remove(key), Times.Once);
            clientMock.Verify(c => c.Dispose(), Times.Once);
        }
Example #4
0
        public async Task RedisKeyCollectionValuePairsRepositoryOfTweet_AddAndRemoveKeyValuePair_ShouldWork()
        {
            // Arrange
            var clientProvider = new RedisClientProvider();
            var repository     = new RedisKeyCollectionValuePairsRepository <ITweet>(clientProvider);
            var key            = Guid.NewGuid().ToString();

            var now   = DateTime.Now;
            var value = new Tweet
            {
                Id       = 1,
                PostedOn = now,
                Content  = $"{key} {now.ToLongTimeString()}"
            };

            // Act: Clean-up database
            await repository.Remove(key);

            await repository.SaveChangesAsync();

            // Act: Add
            await repository.Add(key, value);

            await repository.SaveChangesAsync();

            // Act + Assert: Retrieve data
            var valuesFromDb = repository.GetAll(key).ToList();

            Assert.AreEqual(1, valuesFromDb.Count);

            var valueFromDb = valuesFromDb.Single();

            Assert.AreEqual(1, valueFromDb.Id);
            Assert.AreEqual(now.ToLongTimeString(), valueFromDb.PostedOn.ToLongTimeString());
            Assert.AreEqual(value.Content, valueFromDb.Content);

            // Act: Remove
            await repository.Remove(key);

            Assert.That(async() => await repository.SaveChangesAsync(), Is.EqualTo(0L).After(2000));

            // Assert: Remove
            Assert.AreEqual(0, repository.GetAll(key).ToList().Count, "All values in the list should be removed.");
        }
Example #5
0
        public void RedisKeyCollectionValuePairsRepositoryOfTweet_RemoveWithInvalidKeyAndNullValue_ShouldThrowArgumentNullException(string key)
        {
            // Arrange
            var clientProviderMock = new Mock <IRedisClientProvider>();

            var repository = new RedisKeyCollectionValuePairsRepository <ITweet>(clientProviderMock.Object);

            // Act + Assert
            Assert.ThrowsAsync <ArgumentNullException>(() =>
            {
                return(repository.Remove(key, null));
            });

            clientProviderMock.Verify(p => p.Create(), Times.Never);
        }
Example #6
0
        public async Task RedisKeyCollectionValuePairsRepositoryOfTweet_GetKeys_ShouldWork()
        {
            // Arrange
            const int NumberOfKeys = 10;
            var       listOfKeys   = Enumerable.Range(0, NumberOfKeys)
                                     .Select(i => Guid.NewGuid().ToString() + i)
                                     .ToList();

            var clientProvider = new RedisClientProvider();
            var repository     = new RedisKeyCollectionValuePairsRepository <ITweet>(clientProvider);

            var value = new Tweet
            {
                Id       = 0,
                Content  = Guid.NewGuid().ToString(),
                PostedOn = DateTime.UtcNow
            };

            // Act: Add
            foreach (var key in listOfKeys)
            {
                await repository.Add(key, value);
            }

            // Act: Get Keys
            var keysAfterAdd = repository.Keys.ToList();

            // Assert: Get Keys
            Assert.AreEqual(NumberOfKeys, keysAfterAdd.Count, $"Number of keys after add should be {NumberOfKeys}.");

            foreach (var key in listOfKeys)
            {
                var keyFromDb = keysAfterAdd.Single(k => k == key);
                Assert.AreEqual(key, keyFromDb);
            }

            // Act: Remove
            foreach (var key in listOfKeys)
            {
                await repository.Remove(key);
            }

            // Act: Get Keys
            var keysAfterRemove = repository.Keys.ToList();

            // Assert: Get Keys
            Assert.AreEqual(0, keysAfterRemove.Count, $"Number of keys after insert should be 0.");
        }
Example #7
0
        public void RedisKeyCollectionValuePairsRepositoryOfTweet_RemoveWithInvalidKey_ShouldThrowArgumentNullExceptionWithCorrectParamName(string key)
        {
            // Arrange
            var clientProviderMock = new Mock <IRedisClientProvider>();

            var repository = new RedisKeyCollectionValuePairsRepository <ITweet>(clientProviderMock.Object);

            // Act + Assert
            var exception = Assert.ThrowsAsync <ArgumentNullException>(() =>
            {
                return(repository.Remove(key));
            });

            Assert.AreEqual(Constants.KeyParamName, exception.ParamName);

            clientProviderMock.Verify(p => p.Create(), Times.Never);
        }
Example #8
0
        public async Task RedisKeyCollectionValuePairsRepositoryOfTweet_RemoveValidKeyAndValidValue_ShouldWork()
        {
            // Arrange
            string key = "some key";

            // JsonStringSerializer does not work with mock? var valueMock = new Mock<ITweet>();
            var value = new Tweet();

            var listMock = new Mock <IRedisList>();

            listMock.Setup(l => l.Remove(It.IsAny <string>()))
            .Returns(true);

            var clientMock = new Mock <IRedisClient>();

            clientMock
            .Setup(c => c.Lists[It.IsAny <string>()])
            .Returns(listMock.Object);

            var clientProviderMock = new Mock <IRedisClientProvider>();

            clientProviderMock
            .Setup(p => p.Create())
            .Returns(clientMock.Object);

            var repository = new RedisKeyCollectionValuePairsRepository <ITweet>(clientProviderMock.Object);

            // Act
            var result = await repository.Remove(key, value);

            // Asset
            Assert.That(result, Is.EqualTo(true));

            clientProviderMock.Verify(p => p.Create(), Times.Once);
            clientMock.Verify(c => c.Lists[key], Times.Once);
            clientMock.Verify(c => c.Dispose(), Times.Once);
            listMock.Verify(l => l.Remove(It.IsAny <string>()), Times.Once);
        }