public async Task Updating_item_updates_corresponding_fileAsync() { //Arrange var id = 42; var bucket1 = new Bucket { Id = id }; var bucket2 = new Bucket { Id = id }; var storagefolder = new Mock <IStorageFolder <FileStoredBucket> >(); var folderprovider = new Mock <IStorageFolderProvider>(); folderprovider .Setup(fake => fake.GetStorageFolder <FileStoredBucket>(It.IsAny <string>())) .Returns(storagefolder.Object); var storyStore = new Mock <IDataStore <Story> >(); var datastore = new InMemoryFileBackedBucketDataStore(folderprovider.Object, storyStore.Object); await datastore.AddOrUpdateAsync(new[] { bucket1 }); //Act await datastore.UpdateAsync(id, bucket2); //Assert storagefolder.Verify(mock => mock.ReplaceFileWithItemAsync(id.ToString(), It.IsAny <FileStoredBucket>())); }
public async Task Updating_Bucket_with_stories_stores_ids_of_those_stories() { //Arrange var stories = new[] { new Story { Id = 31415 }, new Story { Id = 2718 } }; var id = 42; var StoredBucket = new FileStoredBucket { Id = id }; var updatedBucket = new Bucket(stories) { Id = id }; IEnumerable <int> storedStoryIds = null; var storagefolder = new Mock <IStorageFolder <FileStoredBucket> >(); storagefolder .Setup(fake => fake.GetStoredItemsAsync()) .Returns(MakeAsync(new[] { StoredBucket })); storagefolder .Setup(mock => mock.ReplaceFileWithItemAsync(It.IsAny <string>(), It.IsAny <FileStoredBucket>())) .Callback <string, FileStoredBucket>((_, StoredBucket) => storedStoryIds = StoredBucket.StoryIds); var folderprovider = new Mock <IStorageFolderProvider>(); folderprovider .Setup(fake => fake.GetStorageFolder <FileStoredBucket>(It.IsAny <string>())) .Returns(storagefolder.Object); var storyStore = new Mock <IDataStore <Story> >(); var datastore = new InMemoryFileBackedBucketDataStore(folderprovider.Object, storyStore.Object); await datastore.InitializeAsync(); //Act await datastore.UpdateAsync(id, updatedBucket); //Assert Assert.AreEqual(stories.Count(), storedStoryIds.Count()); foreach (var story in stories) { Assert.IsTrue(storedStoryIds.Contains(story.Id), $"Story id ${story.Id} was not stored!"); } }
public async Task Updating_Bucket_with_stories_calls_StoryStore_to_update_stories() { //Arrange var stories = new[] { new Story { Id = 31415 }, new Story { Id = 2718 } }; var id = 42; var StoredBucket = new FileStoredBucket { Id = id }; var updatedBucket = new Bucket(stories) { Id = id }; var storagefolder = new Mock <IStorageFolder <FileStoredBucket> >(); storagefolder .Setup(fake => fake.GetStoredItemsAsync()) .Returns(MakeAsync(new[] { StoredBucket })); var folderprovider = new Mock <IStorageFolderProvider>(); folderprovider .Setup(fake => fake.GetStorageFolder <FileStoredBucket>(It.IsAny <string>())) .Returns(storagefolder.Object); var storyStore = new Mock <IDataStore <Story> >(); var datastore = new InMemoryFileBackedBucketDataStore(folderprovider.Object, storyStore.Object); await datastore.InitializeAsync(); //Act await datastore.UpdateAsync(id, updatedBucket); //Assert storyStore.Verify(mock => mock.AddOrUpdateAsync(It.IsAny <IEnumerable <Story> >())); }
public async Task Update_updates_the_existing_itemAsync() { //Arrange var storagefolder = new Mock <IStorageFolder <FileStoredBucket> >(); var folderprovider = new Mock <IStorageFolderProvider>(); folderprovider .Setup(fake => fake.GetStorageFolder <FileStoredBucket>(It.IsAny <string>())) .Returns(storagefolder.Object); var storyStore = new Mock <IDataStore <Story> >(); var datastore = new InMemoryFileBackedBucketDataStore(folderprovider.Object, storyStore.Object); var id = 42; var bucket1 = new Bucket { Id = id }; var bucket2 = new Bucket { Id = id }; datastore.AddOrUpdateAsync(new[] { bucket1 }).Wait(); //Act await datastore.UpdateAsync(id, bucket2); var retrieved = await datastore.GetAllAsync(); //Assert Assert.AreEqual(bucket2, retrieved.Single(item => item.Id == id)); }
public async Task Trying_to_set_an_item_with_id_that_differs_from_supplied_id_throws_InvalidOperationExceptionAsync() { //Arrange var storagefolder = new Mock <IStorageFolder <FileStoredBucket> >(); var folderprovider = new Mock <IStorageFolderProvider>(); folderprovider .Setup(fake => fake.GetStorageFolder <FileStoredBucket>(It.IsAny <string>())) .Returns(storagefolder.Object); var storyStore = new Mock <IDataStore <Story> >(); var datastore = new InMemoryFileBackedBucketDataStore(folderprovider.Object, storyStore.Object); var id = 42; var bucket1 = new Bucket { Id = id }; await datastore.AddOrUpdateAsync(new[] { bucket1 }); //first, the id must exist var bucket2 = new Bucket { Id = 314 }; //Act && //Assert await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await datastore.UpdateAsync(id, bucket2)); }
public async Task Trying_to_update_non_existing_item_throws_InvalidOperationExceptionAsync() { //Arrange var storagefolder = new Mock <IStorageFolder <FileStoredBucket> >(); var folderprovider = new Mock <IStorageFolderProvider>(); folderprovider .Setup(fake => fake.GetStorageFolder <FileStoredBucket>(It.IsAny <string>())) .Returns(storagefolder.Object); var storyStore = new Mock <IDataStore <Story> >(); var datastore = new InMemoryFileBackedBucketDataStore(folderprovider.Object, storyStore.Object); var id = 42; var bucket1 = new Bucket { Id = id }; //Act && //Assert await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await datastore.UpdateAsync(id, bucket1)); }