public void WhenPuttingThenGettingMultipleTimes_TheOriginalResultShouldBeTheSame() { // Arrange var store = new JsonStore(_rootFileLocation); var id = Guid.NewGuid(); var initialItemToStore = new StorableBase { Id = id, Properties = new Dictionary <string, object> { { "key", "value" } } }; // Act _ = store.Put(initialItemToStore); var retrievedFirstTime = store.Get(id); _ = store.Put(retrievedFirstTime); var retrievedSecondTime = store.Get(id); _ = store.Put(retrievedSecondTime); var retrievedThirdTime = store.Get(id); // Assert (using serialization as a quick way to deep check the values of dictionary) var itemToStorePropertiesAsJson = JsonSerializer.Serialize(initialItemToStore.Properties); var storedPropertiesAsJson = JsonSerializer.Serialize(retrievedThirdTime.Properties); Assert.Equal(initialItemToStore.Id, retrievedThirdTime.Id); Assert.Equal(itemToStorePropertiesAsJson, storedPropertiesAsJson); }
public void AfterOverwritingAnExistingObjectUsingPut_OnlyOneFileExistsOnDisk() { // Arrange var store = new JsonStore(_rootFileLocation); var id = Guid.NewGuid(); var initialItemToStore = new StorableBase { Id = id, Properties = new Dictionary <string, object> { { "key", "value" } } }; _ = store.Put(initialItemToStore); // Act var itemToStore = new StorableBase { Id = id, Properties = new Dictionary <string, object> { { "key", "newValue" } } }; _ = store.Put(itemToStore); // Assert Assert.Single(Directory.GetFiles(_rootFileLocation)); }
public void WhenOverwritingAnExistingObjectUsingPut_TheNewerObjectIsReturned() { // Arrange var store = new JsonStore(_rootFileLocation); var id = Guid.NewGuid(); var initialItemToStore = new StorableBase { Id = id, Properties = new Dictionary <string, object> { { "key", "value" } } }; _ = store.Put(initialItemToStore); // Act var itemToStore = new StorableBase { Id = id, Properties = new Dictionary <string, object> { { "key", "newValue" } } }; _ = store.Put(itemToStore); var storedItem = store.Get(id); // Assert Assert.Equal("newValue", storedItem.Properties["key"].ToString()); }
public void AfterAnObjectIsStored_RetrievingItMatches() { // Arrange var store = new JsonStore(_rootFileLocation); var id = Guid.NewGuid(); var itemToStore = new StorableBase { Id = id, Properties = new Dictionary <string, object> { { "key", "value" } } }; _ = store.Put(itemToStore); // Act var storedItem = store.Get(id); // Assert (using serialization as a quick way to deep check the values of dictionary) var itemToStorePropertiesAsJson = JsonSerializer.Serialize(itemToStore.Properties); var storedPropertiesAsJson = JsonSerializer.Serialize(storedItem.Properties); Assert.Equal(itemToStore.Id, storedItem.Id); Assert.Equal(itemToStorePropertiesAsJson, storedPropertiesAsJson); }
public void WhenPuttingPropertiesWithComplexValueTypes_TheyAreReturnedCorrectly() { // Arrange var store = new JsonStore(_rootFileLocation); var id = Guid.NewGuid(); var complexValue = new ExampleComplexSerializableType { StringValue = "example", IntegerValue = 12345 }; var itemToStore = new StorableBase { Id = id, Properties = new Dictionary <string, object> { { "key", complexValue } } }; // Act _ = store.Put(itemToStore); var retrievedItem = store.Get(id); // Assert var jsonKeyToCheck = retrievedItem.Properties["key"]; var deserializedValue = JsonSerializer.Deserialize <ExampleComplexSerializableType>(((JsonElement)jsonKeyToCheck).GetRawText()); Assert.Equal(complexValue.StringValue, deserializedValue.StringValue); Assert.Equal(complexValue.IntegerValue, deserializedValue.IntegerValue); }
public void WhenAttemptingToUseJsonStoreWithUnauthorizedDirectory_ExceptionIsThrown() { // Arrange var store = new JsonStore("C:\\"); // This test may not run correctly on Linux - could make the path OS dependent var id = Guid.NewGuid(); var itemToStore = new StorableBase { Id = id, Properties = new Dictionary <string, object> { { "key", "value" } } }; // Assert Assert.Throws <UnauthorizedAccessException>(() => { _ = store.Put(itemToStore); }); }
public void WhenStoringAnObject_AFileIsWrittenToDisk() { // Arrange var store = new JsonStore(_rootFileLocation); var id = Guid.NewGuid(); var itemToStore = new StorableBase { Id = id, Properties = new Dictionary <string, object> { { "key", "value" } } }; // Act _ = store.Put(itemToStore); // Assert Assert.True(File.Exists(Path.Combine(_rootFileLocation, id.ToString()))); }
public void AfterDeletingAnObject_TheCorrespondingFileIsDeletedFromDisk() { // Arrange var store = new JsonStore(_rootFileLocation); var id = Guid.NewGuid(); var initialItemToStore = new StorableBase { Id = id, Properties = new Dictionary <string, object> { { "key", "value" } } }; _ = store.Put(initialItemToStore); // Act store.Delete(id); // Assert Assert.Empty(Directory.GetFiles(_rootFileLocation)); }
public void AfterDeletingAnObject_AttemptingToGetItThrowsANotFoundException() { // Arrange var store = new JsonStore(_rootFileLocation); var id = Guid.NewGuid(); var initialItemToStore = new StorableBase { Id = id, Properties = new Dictionary <string, object> { { "key", "value" } } }; _ = store.Put(initialItemToStore); // Act store.Delete(id); // Assert Assert.Throws <KeyNotFoundException>(() => { store.Get(id); }); }