Ejemplo n.º 1
0
        public async Task The_same_item_is_not_added_twiceAsync()
        {
            //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 items = new[]
            {
                new Bucket()
            };

            //Act
            await datastore.AddOrUpdateAsync(items);

            var countAfterFirstAdd = (await datastore.GetAllAsync()).Count();

            datastore.AddOrUpdateAsync(items).Wait();
            var countAfterSecondAdd = (await datastore.GetAllAsync()).Count();

            //Assert
            Assert.AreEqual(countAfterFirstAdd, countAfterSecondAdd);
        }
Ejemplo n.º 2
0
        public async Task Adding_item_with_the_same_Id_replaces_current_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);

            //Act
            await datastore.AddOrUpdateAsync(new[] { bucket1 });

            await datastore.AddOrUpdateAsync(new[] { bucket2 });

            //Assert
            storagefolder.Verify(mock => mock.ReplaceFileWithItemAsync(id.ToString(), It.IsAny <FileStoredBucket>()));
        }
Ejemplo n.º 3
0
        public async Task Adding_bucket_with_Stories_calls_StoryStore_to_update_stories()
        {
            //Arrange
            var stories = new[]
            {
                new Story()
            };

            var bucket = new Bucket(stories)
            {
                Id = 42
            };

            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);

            //Act
            await datastore.AddOrUpdateAsync(new[] { bucket });

            //Assert
            storyStore.Verify(mock => mock.AddOrUpdateAsync(It.IsAny <IEnumerable <Story> >()));
        }
Ejemplo n.º 4
0
        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));
        }
Ejemplo n.º 5
0
        public async Task Adding_items_creates_new_files_for_them_using_id_as_FilenameAsync()
        {
            //Arrange
            var bucket1 = new Bucket
            {
                Id = 42
            };

            var bucket2 = new Bucket
            {
                Id = 314
            };

            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);

            //Act
            await datastore.AddOrUpdateAsync(new[] { bucket1, bucket2 });

            //Assert
            storagefolder.Verify(mock => mock.CreateFileForItemAsync(It.IsAny <FileStoredBucket>(), bucket1.Id.ToString()));
            storagefolder.Verify(mock => mock.CreateFileForItemAsync(It.IsAny <FileStoredBucket>(), bucket2.Id.ToString()));
        }
Ejemplo n.º 6
0
        public async Task Added_items_can_be_retrievedAsync()
        {
            //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 items = new[]
            {
                new Bucket()
            };

            //Act
            await datastore.AddOrUpdateAsync(items);

            var retrieved = await datastore.GetAllAsync();

            //Assert
            Assert.AreEqual(items.Single(), retrieved.Single());
        }
Ejemplo n.º 7
0
        public async Task Adding_items_without_Id_gives_them_next_available_Id_and_saves_them_to_that_file()
        {
            //Arrange
            var currentMaxId        = 42;
            var earlierStoredBucket = new FileStoredBucket
            {
                Id = currentMaxId - 7
            };
            var lastStoredBucket = new FileStoredBucket
            {
                Id = currentMaxId
            };

            var newBucket1 = new Bucket
            {
                Id = 0
            };

            var newBucket2 = new Bucket
            {
                Id = 0
            };

            var storedBuckets = new Dictionary <string, FileStoredBucket>();

            var storagefolder = new Mock <IStorageFolder <FileStoredBucket> >();

            storagefolder
            .Setup(fake => fake.GetStoredItemsAsync())
            .Returns(MakeAsync(new[] { lastStoredBucket, earlierStoredBucket }));
            storagefolder
            .Setup(mock => mock.CreateFileForItemAsync(It.IsAny <FileStoredBucket>(), It.IsAny <string>()))
            .Callback <FileStoredBucket, string>((bucket, id) => storedBuckets.Add(id, bucket));
            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.AddOrUpdateAsync(new[] { newBucket1, newBucket2 });

            //Assert
            Assert.AreEqual(currentMaxId + 1, newBucket1.Id, "Bucket 1 got wrong Id");
            Assert.AreEqual(currentMaxId + 2, newBucket2.Id, "Bucket 2 got wrong Id");
            Assert.AreEqual(newBucket1.Id, storedBuckets[newBucket1.Id.ToString()].Id, "Bucket 1 did not get the new Id in storage");
            Assert.AreEqual(newBucket2.Id, storedBuckets[newBucket2.Id.ToString()].Id, "Bucket 2 did not get the new Id in storage");
            storagefolder.Verify(mock => mock.CreateFileForItemAsync(It.IsAny <FileStoredBucket>(), newBucket1.Id.ToString()));
            storagefolder.Verify(mock => mock.CreateFileForItemAsync(It.IsAny <FileStoredBucket>(), newBucket2.Id.ToString()));
        }
Ejemplo n.º 8
0
        public async Task Adding_the_same_Id_again_updates_exising_item_to_the_newAsync()
        {
            //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 item1 = new Bucket
            {
                Id = id
            };

            var item2 = new Bucket
            {
                Id = id
            };

            //Act
            await datastore.AddOrUpdateAsync(new[] { item1 });

            await datastore.AddOrUpdateAsync(new[] { item2 });

            var retrieved = await datastore.GetAllAsync();

            //Assert
            Assert.AreEqual(item2, retrieved.Single(item => item.Id == id));
        }
Ejemplo n.º 9
0
        public async Task Should_not_try_to_write_files_if_another_instance_is_reading()
        {
            //Arrange
            var storagefolder = new Mock <IStorageFolder <FileStoredBucket> >();
            var testHelper    = new AsyncReadTester();

            storagefolder
            .Setup(mock => mock.GetStoredItemsAsync())
            .Returns(testHelper.ReadAsync());
            storagefolder
            .Setup(mock => mock.CreateFileForItemAsync(It.IsAny <FileStoredBucket>(), It.IsAny <string>()))
            .Callback(() => testHelper.SetOtherFunctionCalled());

            var folderprovider = new Mock <IStorageFolderProvider>();

            folderprovider
            .Setup(fake => fake.GetStorageFolder <FileStoredBucket>(It.IsAny <string>()))
            .Returns(storagefolder.Object);

            var storyStore = new Mock <IDataStore <Story> >();

            var datastore1 = new InMemoryFileBackedBucketDataStore(folderprovider.Object, storyStore.Object);
            var datastore2 = new InMemoryFileBackedBucketDataStore(folderprovider.Object, storyStore.Object);

            Task reading, writing;

            try
            {
                reading = datastore1.InitializeAsync();

                //Act
                writing = datastore2.AddOrUpdateAsync(new[] { new Bucket() });
            }
            finally
            {
                testHelper.StopFakeReading();
            }

            await Task.WhenAll(reading, writing);

            //Assert
            Assert.IsFalse(testHelper.CalledWhileInProgress);
        }
Ejemplo n.º 10
0
        public async Task The_same_Id_exists_only_onceAsync()
        {
            //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 items = new[]
            {
                new Bucket
                {
                    Id = id
                },
                new Bucket
                {
                    Id = id
                }
            };

            //Act
            await datastore.AddOrUpdateAsync(items);

            var retrieved = await datastore.GetAllAsync();

            //Assert
            Assert.AreEqual(1, retrieved.Count(item => item.Id == id));
        }
Ejemplo n.º 11
0
        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));
        }