Esempio n. 1
0
        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));
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        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());
        }
Esempio n. 6
0
        public void TestIsLoadedDoesNotSetIsChanged()
        {
            var storable = new StorableBase();

            storable.IsChanged = false;
            storable.IsLoaded  = true;

            Assert.IsFalse(storable.IsChanged);
        }
Esempio n. 7
0
        public void TestClone_CloneIgnore_StorableBase()
        {
            var storableBase = new StorableBase();

            storableBase.DocumentID = Guid.NewGuid().ToString("N");
            storableBase.ParentID   = Guid.NewGuid();
            var storableBase2 = storableBase.Clone();

            Assert.AreEqual(storableBase.DocumentID, storableBase2.DocumentID);
            Assert.AreEqual(storableBase.ParentID, storableBase2.ParentID);
        }
Esempio n. 8
0
        public void TestAllPropertiesSyncedWhenStorableLoaded()
        {
            DummyStorableVM viewModel = new DummyStorableVM();
            var             model     = new StorableBase();

            model.IsLoaded  = true;
            viewModel.Model = model;

            Assert.IsTrue(viewModel.SyncPreloadedCalled);
            Assert.IsTrue(viewModel.SyncLoadedCalled);
        }
Esempio n. 9
0
        public void TestOnlyPreloadedPropertiesSynced()
        {
            DummyStorableVM viewModel = new DummyStorableVM();
            var             model     = new StorableBase();

            model.IsLoaded  = false;
            viewModel.Model = model;

            Assert.IsTrue(viewModel.SyncPreloadedCalled);
            Assert.IsFalse(viewModel.SyncLoadedCalled);
        }
Esempio n. 10
0
        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 T Put <T>(T item) where T : IUnique
        {
            var type       = item.GetType();
            var properties = new Dictionary <string, object>();

            foreach (var propertyInfo in type.GetProperties())
            {
                properties.Add(propertyInfo.Name, propertyInfo.GetValue(item));
            }

            var storable = new StorableBase {
                Id = item.Id, Properties = properties
            };

            _store.Put(storable);

            return(item);
        }
Esempio n. 12
0
        /// <summary>
        /// Load a model in a background thread disabling emission of events until its loaded to prevent updates
        /// running in a thread different from the main thread
        /// </summary>
        public async Task LoadModel()
        {
            if (Model.IsLoaded)
            {
                return;
            }

            await Task.Run(() => {
                StorableBase storableBase = Model as StorableBase;
                if (storableBase != null)
                {
                    storableBase.IgnoreEvents = true;
                    storableBase.Load();
                    storableBase.IgnoreEvents = false;
                }
            });

            SyncLoadedModel();
        }
Esempio n. 13
0
        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())));
        }
Esempio n. 14
0
        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));
        }
Esempio n. 15
0
        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); });
        }