Exemplo n.º 1
0
        public void MapFromData_maps_StoryIds_from_Stories()
        {
            var stories = new[]
            {
                new Story {
                    Id = 31415
                },
                new Story {
                    Id = 2718
                }
            };
            //Arrange
            var data = new Bucket(stories);

            var fileStored = new FileStoredBucket();

            //Act
            fileStored.MapFromData(data);

            //Assert
            foreach (var story in stories)
            {
                var fileStoredContainsId = fileStored.StoryIds.Contains(story.Id);
                Assert.IsTrue(fileStoredContainsId, $"Story id {story.Id} was not mapped");
            }
        }
Exemplo n.º 2
0
        public async Task Stories_are_fetched_from_StoryStoreAsync()
        {
            //Arrange
            var containedStoryIds = new[] { 31415, 2718 };
            var bucket            = new FileStoredBucket
            {
                Id       = 42,
                StoryIds = containedStoryIds.AsEnumerable()
            };

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

            storagefolder
            .Setup(fake => fake.GetStoredItemsAsync())
            .Returns(MakeAsync(new[] { 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);


            //Act
            await datastore.InitializeAsync();

            _ = await datastore.GetAllAsync();

            //Assert
            storyStore.Verify(mock => mock.GetAsync(containedStoryIds), Times.Once);
        }
Exemplo n.º 3
0
        public async Task Initialize_stores_the_retrieved_items_in_the_DataStoreAsync()
        {
            //Arrange
            var bucket = new FileStoredBucket
            {
                Id = 42
            };

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

            storagefolder
            .Setup(fake => fake.GetStoredItemsAsync())
            .Returns(MakeAsync(new[] { 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);

            //Act
            await datastore.InitializeAsync();

            //Assert
            var result = datastore.GetAllAsync().Result;

            Assert.AreEqual(bucket.Id, result.Single().Id);
        }
Exemplo n.º 4
0
        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!");
            }
        }
Exemplo n.º 5
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()));
        }
Exemplo n.º 6
0
        public async Task Contained_stories_from_StoryStore_are_added_to_BucketAsync()
        {
            //Arrange
            var id     = 42;
            var bucket = new FileStoredBucket
            {
                Id = id
            };

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

            storagefolder
            .Setup(fake => fake.GetStoredItemsAsync())
            .Returns(MakeAsync(new[] { bucket }));

            var folderprovider = new Mock <IStorageFolderProvider>();

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

            var stories = new[]
            {
                new Story {
                    Id = 4
                },
                new Story
                {
                    Id = 2
                }
            };
            var storyStore = new Mock <IDataStore <Story> >();

            storyStore
            .Setup(fake => fake.GetAsync(It.IsAny <IEnumerable <int> >()))
            .ReturnsAsync(stories);

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

            //Act
            await datastore.InitializeAsync();

            var buckets = await datastore.GetAllAsync();

            //Assert
            var storiesOfBucket = buckets.Single().Stories;

            Assert.AreEqual(stories.Length, storiesOfBucket.Count);
            foreach (var fakeStory in stories)
            {
                var bucketContainsStory = storiesOfBucket.Any(returnedStory => returnedStory.Id == fakeStory.Id);
                Assert.IsTrue(bucketContainsStory, $"Story with id {fakeStory.Id} not found in bucket!");
            }
        }
Exemplo n.º 7
0
        public void ToData_with_stories_maps_id()
        {
            //Arrange
            var fileStored = new FileStoredBucket
            {
                Id = 42
            };

            //Act
            var data = fileStored.ToData(new[] { new Story() });

            //Assert
            Assert.AreEqual(fileStored.Id, data.Id);
        }
Exemplo n.º 8
0
        public void ToData_maps_Id()
        {
            //Arrange
            var fileStored = new FileStoredBucket
            {
                Id = 42
            };

            //Act
            var data = fileStored.ToData();

            //Assert
            Assert.AreEqual(fileStored.Id, data.Id);
        }
Exemplo n.º 9
0
        public void ToData_maps_bigger_bucket_Id()
        {
            //Arrange
            var fileStored = new FileStoredBucket
            {
                NextBiggerBucketId = 42
            };

            //Act
            var data = fileStored.ToData();

            //Assert
            Assert.AreEqual(fileStored.NextBiggerBucketId, data.NextBiggerBucketId);
        }
Exemplo n.º 10
0
        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> >()));
        }
Exemplo n.º 11
0
        public async Task Get_retrieves_buckets_with_requested_idsAsync()
        {
            //Arrange
            var id1 = 42;
            var id2 = 27;

            var bucket1 = new FileStoredBucket
            {
                Id = id1
            };

            var bucket2 = new FileStoredBucket
            {
                Id = id2
            };

            var bucket3 = new FileStoredBucket
            {
                Id = 123
            };

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

            storagefolder
            .Setup(fake => fake.GetStoredItemsAsync())
            .Returns(MakeAsync(new[] { bucket1, bucket2, bucket3 }));

            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
            var result = datastore.GetAsync(new[] { id1, id2 }).Result;

            //Assert
            var containsBucket1 = result.Any(b => b.Id == id1);
            var containsBucket2 = result.Any(b => b.Id == id2);

            Assert.IsTrue(containsBucket1);
            Assert.IsTrue(containsBucket2);
            Assert.AreEqual(2, result.Count());
        }
Exemplo n.º 12
0
        public void ToData_with_stories_maps_those_stories()
        {
            //Arrange
            var fileStored = new FileStoredBucket();

            var stories = new[] { new Story(), new Story() };

            //Act
            var data = fileStored.ToData(stories);

            //Assert
            Assert.AreEqual(2, data.Stories.Count);
            Assert.AreEqual(stories.First(), data.Stories.First());
            Assert.AreEqual(stories.Last(), data.Stories.Last());
        }
Exemplo n.º 13
0
        public void MapFromData_maps_Id()
        {
            //Arrange
            var data = new Bucket
            {
                Id = 42
            };
            var fileStored = new FileStoredBucket();

            //Act
            fileStored.MapFromData(data);

            //Assert
            Assert.AreEqual(data.Id, fileStored.Id);
        }
Exemplo n.º 14
0
        public void MapFromData_maps_Id_from_NextBiggerBucket()
        {
            //Arrange
            var biggerBucketId = 42;
            var data           = new Bucket
            {
                NextBiggerBucketId = biggerBucketId
            };
            var fileStored = new FileStoredBucket();

            //Act
            fileStored.MapFromData(data);

            //Assert
            Assert.AreEqual(biggerBucketId, fileStored.NextBiggerBucketId);
        }