Пример #1
0
        public void The_same_item_is_not_added_twice()
        {
            //Arrange
            var storagefolder = new Mock <IStorageFolder <FileStoredStory> >();

            var folderprovider = new Mock <IStorageFolderProvider>();

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

            var datastore = new InMemoryFileBackedStoryDataStore(folderprovider.Object);

            var items = new[]
            {
                new Story()
            };

            //Act
            datastore.AddOrUpdateAsync(items).Wait();
            var countAfterFirstAdd = datastore.GetAllAsync().Result.Count();

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

            //Assert
            Assert.AreEqual(countAfterFirstAdd, countAfterSecondAdd);
        }
Пример #2
0
        public void Adding_item_with_the_same_Id_replaces_current_file()
        {
            //Arrange
            var id     = 42;
            var story1 = new Story
            {
                Id = id
            };

            var story2 = new Story
            {
                Id = id
            };

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

            var folderprovider = new Mock <IStorageFolderProvider>();

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

            var datastore = new InMemoryFileBackedStoryDataStore(folderprovider.Object);

            //Act
            datastore.AddOrUpdateAsync(new[] { story1 }).Wait();
            datastore.AddOrUpdateAsync(new[] { story2 }).Wait();

            //Assert
            storagefolder.Verify(mock => mock.ReplaceFileWithItemAsync(id.ToString(), It.IsAny <FileStoredStory>()));
        }
Пример #3
0
        public void Adding_the_same_Id_again_updates_exising_item_to_the_new()
        {
            //Arrange
            var storagefolder = new Mock <IStorageFolder <FileStoredStory> >();

            var folderprovider = new Mock <IStorageFolderProvider>();

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

            var datastore = new InMemoryFileBackedStoryDataStore(folderprovider.Object);

            var id    = 42;
            var item1 = new Story
            {
                Id = id
            };

            var item2 = new Story
            {
                Id = id
            };

            //Act
            datastore.AddOrUpdateAsync(new[] { item1 }).Wait();
            datastore.AddOrUpdateAsync(new[] { item2 }).Wait();
            var retrieved = datastore.GetAllAsync().Result;

            //Assert
            Assert.AreEqual(item2, retrieved.Single(item => item.Id == id));
        }
Пример #4
0
        public void The_same_Id_exists_only_once()
        {
            //Arrange
            var storagefolder = new Mock <IStorageFolder <FileStoredStory> >();

            var folderprovider = new Mock <IStorageFolderProvider>();

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

            var datastore = new InMemoryFileBackedStoryDataStore(folderprovider.Object);

            var id = 42;

            var items = new[]
            {
                new Story
                {
                    Id = id
                },
                new Story
                {
                    Id = id
                }
            };

            //Act
            datastore.AddOrUpdateAsync(items).Wait();
            var retrieved = datastore.GetAllAsync().Result;

            //Assert
            Assert.AreEqual(1, retrieved.Count(item => item.Id == id));
        }
Пример #5
0
        public async Task Should_not_try_to_write_files_if_another_instance_is_reading()
        {
            //Arrange
            var storagefolder = new Mock <IStorageFolder <FileStoredStory> >();
            var testHelper    = new AsyncReadTester();

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

            var folderprovider = new Mock <IStorageFolderProvider>();

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


            var datastore1 = new InMemoryFileBackedStoryDataStore(folderprovider.Object);
            var datastore2 = new InMemoryFileBackedStoryDataStore(folderprovider.Object);

            var reading = datastore1.InitializeAsync();

            //Act
            var writing = datastore2.AddOrUpdateAsync(new[] { new Story() });

            testHelper.StopFakeReading();

            await Task.WhenAll(reading, writing);

            //Assert
            Assert.IsFalse(testHelper.CalledWhileInProgress);
        }
Пример #6
0
        public void Trying_to_set_an_item_with_id_that_differs_from_supplied_id_throws_InvalidOperationException()
        {
            //Arrange
            var storagefolder = new Mock <IStorageFolder <FileStoredStory> >();

            var folderprovider = new Mock <IStorageFolderProvider>();

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

            var datastore = new InMemoryFileBackedStoryDataStore(folderprovider.Object);

            var id     = 42;
            var story1 = new Story
            {
                Id = id
            };

            datastore.AddOrUpdateAsync(new[] { story1 }).Wait(); //first, the id must exist

            var story2 = new Story
            {
                Id = 314
            };

            //Act &&
            //Assert
            Assert.ThrowsExceptionAsync <InvalidOperationException>(() => datastore.UpdateAsync(id, story2)).Wait();
        }
Пример #7
0
        public void Adding_items_creates_new_files_for_them_using_id_as_Filename()
        {
            //Arrange
            var story1 = new Story
            {
                Id = 42
            };

            var story2 = new Story
            {
                Id = 314
            };

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

            var folderprovider = new Mock <IStorageFolderProvider>();

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

            var datastore = new InMemoryFileBackedStoryDataStore(folderprovider.Object);

            //Act
            datastore.AddOrUpdateAsync(new[] { story1, story2 }).Wait();

            //Assert
            storagefolder.Verify(mock => mock.CreateFileForItemAsync(It.IsAny <FileStoredStory>(), story1.Id.ToString()));
            storagefolder.Verify(mock => mock.CreateFileForItemAsync(It.IsAny <FileStoredStory>(), story2.Id.ToString()));
        }
Пример #8
0
        public void Added_items_can_be_retrieved()
        {
            //Arrange
            var storagefolder = new Mock <IStorageFolder <FileStoredStory> >();

            var folderprovider = new Mock <IStorageFolderProvider>();

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

            var datastore = new InMemoryFileBackedStoryDataStore(folderprovider.Object);

            var items = new[]
            {
                new Story()
            };

            //Act
            datastore.AddOrUpdateAsync(items).Wait();
            var retrieved = datastore.GetAllAsync().Result;

            //Assert
            Assert.AreEqual(items.Single(), retrieved.Single());
        }