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."); }
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)); }
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."); }
public async Task RedisKeyCollectionValuePairsRepositoryOfTweet_SaveChanges_ShouldWork() { // Arrange var clientMock = new Mock <IRedisClient>(); 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.SaveChangesAsync(); // Asset Assert.That(result, Is.EqualTo(0L)); clientProviderMock.Verify(p => p.Create(), Times.Once); clientMock.Verify(c => c.SaveAsync(), Times.Once); clientMock.Verify(c => c.Dispose(), Times.Once); }