From a1d0a2671c3d723abbab330661d5b667b6be2b46 Mon Sep 17 00:00:00 2001 From: David Eadie Date: Thu, 19 May 2016 15:29:43 +0100 Subject: [PATCH 01/11] Red: You can use Id in a converted objects dictionary --- UnitTests/TestObjectNotIStorable.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/UnitTests/TestObjectNotIStorable.cs b/UnitTests/TestObjectNotIStorable.cs index 4023bb2..a9b21c4 100644 --- a/UnitTests/TestObjectNotIStorable.cs +++ b/UnitTests/TestObjectNotIStorable.cs @@ -28,7 +28,7 @@ public StorableObject GetStorableObject(TestObjectNotIStorable item) { var storableObject = new StorableObject(); - storableObject.Info.Add("Name", item.Name); + storableObject.Info.Add("Id", item.Name); storableObject.Info.Add("StartDate", item.StartDate.Ticks.ToString()); storableObject.Info.Add("EndDate", item.EndDate.Ticks.ToString()); @@ -39,7 +39,7 @@ public TestObjectNotIStorable GetOriginalObject(StorableObject info) { var orginalObject = new TestObjectNotIStorable(); - orginalObject.Name = info.Info["Name"]; + orginalObject.Name = info.Info["Id"]; orginalObject.StartDate = new DateTime(long.Parse(info.Info["StartDate"])); orginalObject.EndDate = new DateTime(long.Parse(info.Info["EndDate"])); @@ -48,7 +48,7 @@ public TestObjectNotIStorable GetOriginalObject(StorableObject info) public bool IsEqual(StorableObject storageObject, TestObjectNotIStorable realObject) { - return realObject.Name == storageObject.Info["Name"]; + return realObject.Name == storageObject.Info["Id"]; } } } \ No newline at end of file From f82755f3a1df5e80b21208d6984f6fe15988d771 Mon Sep 17 00:00:00 2001 From: David Eadie Date: Thu, 19 May 2016 15:37:37 +0100 Subject: [PATCH 02/11] Green: You can use Id in a converted objects dictionary --- .../InMemoryRepositoryWithConverter.cs | 4 ++-- LazyStorage/StorableObject.cs | 4 ++-- LazyStorage/Xml/XmlRepositoryWithConverter.cs | 18 +++++++++--------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/LazyStorage/InMemory/InMemoryRepositoryWithConverter.cs b/LazyStorage/InMemory/InMemoryRepositoryWithConverter.cs index a490cf7..5a9ff22 100644 --- a/LazyStorage/InMemory/InMemoryRepositoryWithConverter.cs +++ b/LazyStorage/InMemory/InMemoryRepositoryWithConverter.cs @@ -38,8 +38,8 @@ public void Set(T item) else { // Insert - var nextId = m_Repository.Any() ? m_Repository.Max(x => x.Id) + 1 : 1; - storableItem.Id = nextId; + var nextId = m_Repository.Any() ? m_Repository.Max(x => x.LazyStorageInternalId) + 1 : 1; + storableItem.LazyStorageInternalId = nextId; m_Repository.Add(storableItem); } } diff --git a/LazyStorage/StorableObject.cs b/LazyStorage/StorableObject.cs index 232607b..bd93f0c 100644 --- a/LazyStorage/StorableObject.cs +++ b/LazyStorage/StorableObject.cs @@ -5,7 +5,7 @@ namespace LazyStorage { public class StorableObject : IEquatable { - public int Id { get; set; } + public int LazyStorageInternalId { get; set; } public Dictionary Info { get; } public StorableObject() @@ -15,7 +15,7 @@ public StorableObject() public bool Equals(StorableObject other) { - return (other.Id == Id); + return (other.LazyStorageInternalId == LazyStorageInternalId); } } } \ No newline at end of file diff --git a/LazyStorage/Xml/XmlRepositoryWithConverter.cs b/LazyStorage/Xml/XmlRepositoryWithConverter.cs index 76dc985..4e0cfc9 100644 --- a/LazyStorage/Xml/XmlRepositoryWithConverter.cs +++ b/LazyStorage/Xml/XmlRepositoryWithConverter.cs @@ -51,8 +51,8 @@ private IEnumerable GetMatchingItemsInStore(T item) foreach (var node in m_File.Element("Root").Elements()) { var storableObject = new StorableObject(); - var idXElements = node.Descendants("Id"); - storableObject.Id = int.Parse(idXElements.Single().Value); + var idXElements = node.Descendants("LazyStorageInternalId"); + storableObject.LazyStorageInternalId = int.Parse(idXElements.Single().Value); foreach (var element in node.Descendants()) { storableObject.Info.Add(element.Name.ToString(), element.Value); @@ -84,8 +84,8 @@ private void Update(StorableObject item, StorableObject oldItem) var info = item.Info; var rootElement = m_File.Element("Root"); - var idXElements = rootElement.Descendants("Id"); - var node = idXElements.Single(x => x.Value == oldItem.Id.ToString()); + var idXElements = rootElement.Descendants("LazyStorageInternalId"); + var node = idXElements.Single(x => x.Value == oldItem.LazyStorageInternalId.ToString()); foreach (var data in info) { @@ -98,14 +98,14 @@ private void Insert(StorableObject item) var typeAsString = typeof (T).ToString(); var rootElement = m_File.Element("Root"); - var idXElements = rootElement.Descendants("Id"); + var idXElements = rootElement.Descendants("LazyStorageInternalId"); - item.Id = idXElements.Any() ? idXElements.Max(x => (int) x) + 1 : 1; + item.LazyStorageInternalId = idXElements.Any() ? idXElements.Max(x => (int) x) + 1 : 1; var info = item.Info; var newElement = new XElement(typeAsString); - newElement.Add(new XElement("Id", item.Id)); + newElement.Add(new XElement("LazyStorageInternalId", item.LazyStorageInternalId)); foreach (var data in info) { @@ -118,8 +118,8 @@ private void Insert(StorableObject item) public void Delete(T item) { var rootElement = m_File.Element("Root"); - var idXElements = rootElement.Descendants("Id"); - var node = idXElements.Single(x => x.Value == GetMatchingItemsInStore(item).First().Id.ToString()); + var idXElements = rootElement.Descendants("LazyStorageInternalId"); + var node = idXElements.Single(x => x.Value == GetMatchingItemsInStore(item).First().LazyStorageInternalId.ToString()); node = node.Parent; node.Remove(); From e3bb3f632cfa2a7fc8e0e92615251b18728c978c Mon Sep 17 00:00:00 2001 From: David Eadie Date: Thu, 19 May 2016 15:38:59 +0100 Subject: [PATCH 03/11] R# extract constant --- LazyStorage/Xml/XmlRepositoryWithConverter.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/LazyStorage/Xml/XmlRepositoryWithConverter.cs b/LazyStorage/Xml/XmlRepositoryWithConverter.cs index 4e0cfc9..348b7e4 100644 --- a/LazyStorage/Xml/XmlRepositoryWithConverter.cs +++ b/LazyStorage/Xml/XmlRepositoryWithConverter.cs @@ -9,6 +9,7 @@ namespace LazyStorage.Xml { internal class XmlRepositoryWithConverter : IRepository { + private const string InternalIdString = "LazyStorageInternalId"; private XDocument m_File; private readonly string m_Uri; private readonly string m_StorageFolder; @@ -51,7 +52,7 @@ private IEnumerable GetMatchingItemsInStore(T item) foreach (var node in m_File.Element("Root").Elements()) { var storableObject = new StorableObject(); - var idXElements = node.Descendants("LazyStorageInternalId"); + var idXElements = node.Descendants(InternalIdString); storableObject.LazyStorageInternalId = int.Parse(idXElements.Single().Value); foreach (var element in node.Descendants()) { @@ -84,7 +85,7 @@ private void Update(StorableObject item, StorableObject oldItem) var info = item.Info; var rootElement = m_File.Element("Root"); - var idXElements = rootElement.Descendants("LazyStorageInternalId"); + var idXElements = rootElement.Descendants(InternalIdString); var node = idXElements.Single(x => x.Value == oldItem.LazyStorageInternalId.ToString()); foreach (var data in info) @@ -98,14 +99,14 @@ private void Insert(StorableObject item) var typeAsString = typeof (T).ToString(); var rootElement = m_File.Element("Root"); - var idXElements = rootElement.Descendants("LazyStorageInternalId"); + var idXElements = rootElement.Descendants(InternalIdString); item.LazyStorageInternalId = idXElements.Any() ? idXElements.Max(x => (int) x) + 1 : 1; var info = item.Info; var newElement = new XElement(typeAsString); - newElement.Add(new XElement("LazyStorageInternalId", item.LazyStorageInternalId)); + newElement.Add(new XElement(InternalIdString, item.LazyStorageInternalId)); foreach (var data in info) { @@ -118,7 +119,7 @@ private void Insert(StorableObject item) public void Delete(T item) { var rootElement = m_File.Element("Root"); - var idXElements = rootElement.Descendants("LazyStorageInternalId"); + var idXElements = rootElement.Descendants(InternalIdString); var node = idXElements.Single(x => x.Value == GetMatchingItemsInStore(item).First().LazyStorageInternalId.ToString()); node = node.Parent; From 6a8aa1128d395a31c147d527d826b22e72ba172d Mon Sep 17 00:00:00 2001 From: David Eadie Date: Thu, 19 May 2016 15:48:20 +0100 Subject: [PATCH 04/11] Make Json storage use converter for update/insert check --- LazyStorage/Json/JsonRepositoryWithConverter.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/LazyStorage/Json/JsonRepositoryWithConverter.cs b/LazyStorage/Json/JsonRepositoryWithConverter.cs index 2ff304a..f7428c5 100644 --- a/LazyStorage/Json/JsonRepositoryWithConverter.cs +++ b/LazyStorage/Json/JsonRepositoryWithConverter.cs @@ -29,12 +29,13 @@ public ICollection Get(Func exp = null) public void Set(T item) { - if (m_Repository.Contains(item)) + var storableObject = m_Converter.GetStorableObject(item); + var matchingItemsInStore = m_Repository.Where(x => m_Converter.IsEqual(storableObject, x)); + + if (matchingItemsInStore.Any()) { // Update - var storableObject = m_Converter.GetStorableObject(item); - var obj = m_Repository.Where(x => m_Converter.IsEqual(storableObject, x)); - m_Repository.Remove(obj.First()); + m_Repository.Remove(matchingItemsInStore.First()); m_Repository.Add(item); } else From fe94d45013da78818eba46acff88dbc800039198 Mon Sep 17 00:00:00 2001 From: David Eadie Date: Thu, 19 May 2016 15:48:34 +0100 Subject: [PATCH 05/11] R# Inline variable --- LazyStorage/InMemory/InMemoryRepositoryWithConverter.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/LazyStorage/InMemory/InMemoryRepositoryWithConverter.cs b/LazyStorage/InMemory/InMemoryRepositoryWithConverter.cs index 5a9ff22..8fcd870 100644 --- a/LazyStorage/InMemory/InMemoryRepositoryWithConverter.cs +++ b/LazyStorage/InMemory/InMemoryRepositoryWithConverter.cs @@ -31,8 +31,7 @@ public void Set(T item) if (matchingItemsInStore.Any()) { // Update - var obj = matchingItemsInStore; - m_Repository.Remove(obj.First()); + m_Repository.Remove(matchingItemsInStore.First()); m_Repository.Add(storableItem); } else From 5c2747bcf399739d73c8fccfa8717d3add90e56d Mon Sep 17 00:00:00 2001 From: David Eadie Date: Thu, 19 May 2016 15:50:24 +0100 Subject: [PATCH 06/11] Remove unused internal Id field --- LazyStorage/InMemory/InMemoryRepositoryWithConverter.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/LazyStorage/InMemory/InMemoryRepositoryWithConverter.cs b/LazyStorage/InMemory/InMemoryRepositoryWithConverter.cs index 8fcd870..1529422 100644 --- a/LazyStorage/InMemory/InMemoryRepositoryWithConverter.cs +++ b/LazyStorage/InMemory/InMemoryRepositoryWithConverter.cs @@ -37,8 +37,6 @@ public void Set(T item) else { // Insert - var nextId = m_Repository.Any() ? m_Repository.Max(x => x.LazyStorageInternalId) + 1 : 1; - storableItem.LazyStorageInternalId = nextId; m_Repository.Add(storableItem); } } From 05b0c9ab59aa42d252def7091116560d9dc4d994 Mon Sep 17 00:00:00 2001 From: David Eadie Date: Thu, 19 May 2016 16:15:22 +0100 Subject: [PATCH 07/11] Make Xml Storage work the same way as Json storage --- LazyStorage/Xml/XmlRepository.cs | 142 ++++++++++++++++--------------- 1 file changed, 73 insertions(+), 69 deletions(-) diff --git a/LazyStorage/Xml/XmlRepository.cs b/LazyStorage/Xml/XmlRepository.cs index 157d3df..b9e80c1 100644 --- a/LazyStorage/Xml/XmlRepository.cs +++ b/LazyStorage/Xml/XmlRepository.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Xml; using System.Xml.Linq; using LazyStorage.Interfaces; @@ -9,125 +10,128 @@ namespace LazyStorage.Xml { internal class XmlRepository : IRepository where T : IStorable, new() { - private readonly string m_StorageFolder; - private XDocument m_File; private readonly string m_Uri; + private List m_Repository = new List(); public XmlRepository(string storageFolder) { - m_StorageFolder = storageFolder; m_Uri = $"{storageFolder}{typeof(T)}.xml"; Load(); } public ICollection Get(Func exp = null) { - ICollection found = new List(); - - foreach (var node in m_File.Element("Root").Elements()) - { - var temp = new T(); - var info = new Dictionary(); - - foreach (var element in node.Descendants()) - { - info.Add(element.Name.ToString(), element.Value); - } - - temp.InitialiseWithStorageInfo(info); - - found.Add(temp); - } - - var query = found.AsQueryable(); - return exp != null ? query.Where(exp).ToList() : found; + return exp != null ? m_Repository.Where(exp).ToList() : m_Repository.ToList(); } public void Set(T item) { - var matchingItem = Get(x => x.Equals(item)); - - if (matchingItem.Any()) + if (m_Repository.Contains(item)) { - Update(item); + // Update + var obj = m_Repository.Where(x => x.Equals(item)); + m_Repository.Remove(obj.First()); + m_Repository.Add(item); } else { - Insert(item); + // Insert + var nextId = m_Repository.Any() ? m_Repository.Max(x => x.Id) + 1 : 1; + item.Id = nextId; + m_Repository.Add(item); } } - private void Update(T item) + public void Delete(T item) { - var info = item.GetStorageInfo(); - - var rootElement = m_File.Element("Root"); - var idXElements = rootElement.Descendants("Id"); - var node = idXElements.SingleOrDefault(x => x.Value == item.Id.ToString()); - - foreach (var data in info) - { - node.Parent.Element(data.Key).Value = data.Value; - } + var obj = m_Repository.SingleOrDefault(x => x.Id == item.Id); + m_Repository.Remove(obj); } - private void Insert(T item) + + public object Clone() { - var typeAsString = typeof (T).ToString(); + var newRepo = new XmlRepository(m_Uri); + + foreach (var item in Get()) + { + var temp = new T(); - var rootElement = m_File.Element("Root"); - var idXElements = rootElement.Descendants("Id"); + var info = item.GetStorageInfo(); - item.Id = idXElements.Any() ? idXElements.Max(x => (int) x) + 1 : 1; + temp.InitialiseWithStorageInfo(info); - var info = item.GetStorageInfo(); + newRepo.Set(temp); + } - var newElement = new XElement(typeAsString); + return newRepo; + } - foreach (var data in info) + public void Load() + { + if (File.Exists(m_Uri)) { - newElement.Add(new XElement(data.Key, data.Value)); + m_Repository = GetObjectsFromXml(m_Uri); + } + else + { + m_Repository = new List(); } - rootElement.Add(newElement); } - public void Delete(T item) + public void Save() { - var rootElement = m_File.Element("Root"); - var idXElements = rootElement.Descendants("Id"); - var node = idXElements.SingleOrDefault(x => x.Value == item.Id.ToString()); - - node = node.Parent; - node.Remove(); + GetXmlOuput(m_Repository).Save(m_Uri); } - public object Clone() + private XDocument GetXmlOuput(List objects) { - var newRepo = new XmlRepository(m_StorageFolder); + var file = new XDocument(new XElement("Root")); - foreach (var item in Get()) - { - var temp = new T(); + var typeAsString = typeof(T).ToString(); + var rootElement = file.Element("Root"); + + foreach (var item in objects) + { var info = item.GetStorageInfo(); - temp.InitialiseWithStorageInfo(info); + var newElement = new XElement(typeAsString); + + foreach (var data in info) + { + newElement.Add(new XElement(data.Key, data.Value)); + } - newRepo.Set(temp); + rootElement.Add(newElement); } - return newRepo; + return file; } - public void Save() + private List GetObjectsFromXml(string uri) { - m_File.Save(m_Uri); - } + var file = XDocument.Load(uri); - public void Load() - { - m_File = !File.Exists(m_Uri) ? new XDocument(new XElement("Root")) : XDocument.Load(m_Uri); + var found = new List(); + + foreach (var node in file.Element("Root").Elements()) + { + var storageInfo = new Dictionary(); + + foreach (var element in node.Descendants()) + { + storageInfo.Add(element.Name.ToString(), element.Value); + } + + var item = new T(); + item.InitialiseWithStorageInfo(storageInfo); + + found.Add(item); + } + + return found; } } } \ No newline at end of file From fc57eb0e7f6d1dbcba548a10f81d84640127b78e Mon Sep 17 00:00:00 2001 From: David Eadie Date: Thu, 19 May 2016 16:18:31 +0100 Subject: [PATCH 08/11] Make Xml storage with converter work the same --- LazyStorage/Xml/XmlRepositoryWithConverter.cs | 156 ++++++++---------- 1 file changed, 69 insertions(+), 87 deletions(-) diff --git a/LazyStorage/Xml/XmlRepositoryWithConverter.cs b/LazyStorage/Xml/XmlRepositoryWithConverter.cs index 348b7e4..acac9ca 100644 --- a/LazyStorage/Xml/XmlRepositoryWithConverter.cs +++ b/LazyStorage/Xml/XmlRepositoryWithConverter.cs @@ -9,9 +9,8 @@ namespace LazyStorage.Xml { internal class XmlRepositoryWithConverter : IRepository { - private const string InternalIdString = "LazyStorageInternalId"; - private XDocument m_File; private readonly string m_Uri; + private List m_Repository = new List(); private readonly string m_StorageFolder; private readonly IConverter m_Converter; @@ -25,132 +24,115 @@ public XmlRepositoryWithConverter(string storageFolder, IConverter converter) public ICollection Get(Func exp = null) { - ICollection found = new List(); - - foreach (var node in m_File.Element("Root").Elements()) - { - var storableObject = new StorableObject(); - - foreach (var element in node.Descendants()) - { - storableObject.Info.Add(element.Name.ToString(), element.Value); - } - - var temp = m_Converter.GetOriginalObject(storableObject); - - found.Add(temp); - } - - var query = found.AsQueryable(); - return exp != null ? query.Where(exp).ToList() : found; - } - - private IEnumerable GetMatchingItemsInStore(T item) - { - var found = new List(); - - foreach (var node in m_File.Element("Root").Elements()) - { - var storableObject = new StorableObject(); - var idXElements = node.Descendants(InternalIdString); - storableObject.LazyStorageInternalId = int.Parse(idXElements.Single().Value); - foreach (var element in node.Descendants()) - { - storableObject.Info.Add(element.Name.ToString(), element.Value); - } - - found.Add(storableObject); - } - return found.Where(x => m_Converter.IsEqual(x, item)); + return exp != null ? m_Repository.Where(exp).ToList() : m_Repository.ToList(); } public void Set(T item) { - var storableItem = m_Converter.GetStorableObject(item); - - var matchingItemsInStore = GetMatchingItemsInStore(item); + var storableObject = m_Converter.GetStorableObject(item); + var matchingItemsInStore = m_Repository.Where(x => m_Converter.IsEqual(storableObject, x)); if (matchingItemsInStore.Any()) { - Update(storableItem, matchingItemsInStore.First()); + // Update + m_Repository.Remove(matchingItemsInStore.First()); + m_Repository.Add(item); } else { - Insert(storableItem); + // Insert + m_Repository.Add(item); } } - private void Update(StorableObject item, StorableObject oldItem) + public void Delete(T item) { - var info = item.Info; - - var rootElement = m_File.Element("Root"); - var idXElements = rootElement.Descendants(InternalIdString); - var node = idXElements.Single(x => x.Value == oldItem.LazyStorageInternalId.ToString()); - - foreach (var data in info) - { - node.Parent.Element(data.Key).Value = data.Value; - } + var storableObject = m_Converter.GetStorableObject(item); + var obj = m_Repository.Where(x => m_Converter.IsEqual(storableObject, x)); + m_Repository.Remove(obj.First()); } - private void Insert(StorableObject item) + + public object Clone() { - var typeAsString = typeof (T).ToString(); + var newRepo = new XmlRepositoryWithConverter(m_StorageFolder, m_Converter); - var rootElement = m_File.Element("Root"); - var idXElements = rootElement.Descendants(InternalIdString); + foreach (var item in Get()) + { + var info = m_Converter.GetStorableObject(item); - item.LazyStorageInternalId = idXElements.Any() ? idXElements.Max(x => (int) x) + 1 : 1; + var temp = m_Converter.GetOriginalObject(info); - var info = item.Info; + newRepo.Set(temp); + } - var newElement = new XElement(typeAsString); - newElement.Add(new XElement(InternalIdString, item.LazyStorageInternalId)); + return newRepo; - foreach (var data in info) + } + public void Load() + { + if (File.Exists(m_Uri)) + { + m_Repository = GetObjectsFromXml(m_Uri); + } + else { - newElement.Add(new XElement(data.Key, data.Value)); + m_Repository = new List(); } - rootElement.Add(newElement); } - public void Delete(T item) + public void Save() { - var rootElement = m_File.Element("Root"); - var idXElements = rootElement.Descendants(InternalIdString); - var node = idXElements.Single(x => x.Value == GetMatchingItemsInStore(item).First().LazyStorageInternalId.ToString()); - - node = node.Parent; - node.Remove(); + GetXmlOuput(m_Repository).Save(m_Uri); } - public object Clone() + private XDocument GetXmlOuput(List objects) { - var newRepo = new XmlRepositoryWithConverter(m_StorageFolder, m_Converter); + var file = new XDocument(new XElement("Root")); - foreach (var item in Get()) + var typeAsString = typeof(T).ToString(); + + var rootElement = file.Element("Root"); + + foreach (var item in objects) { - var info = m_Converter.GetStorableObject(item); + var info = m_Converter.GetStorableObject(item).Info; - var temp = m_Converter.GetOriginalObject(info); + var newElement = new XElement(typeAsString); - newRepo.Set(temp); - } + foreach (var data in info) + { + newElement.Add(new XElement(data.Key, data.Value)); + } - return newRepo; + rootElement.Add(newElement); + } + return file; } - public void Save() + private List GetObjectsFromXml(string uri) { - m_File.Save(m_Uri); - } + var file = XDocument.Load(uri); - public void Load() - { - m_File = !File.Exists(m_Uri) ? new XDocument(new XElement("Root")) : XDocument.Load(m_Uri); + var found = new List(); + + foreach (var node in file.Element("Root").Elements()) + { + var storableObject = new StorableObject(); + + foreach (var element in node.Descendants()) + { + storableObject.Info.Add(element.Name.ToString(), element.Value); + } + + var item = m_Converter.GetOriginalObject(storableObject); + + found.Add(item); + } + + return found; } } } \ No newline at end of file From a0009d0a4dfeefbed34e6dd67f6982883bbd7dc6 Mon Sep 17 00:00:00 2001 From: David Eadie Date: Thu, 19 May 2016 16:24:27 +0100 Subject: [PATCH 09/11] Add tests for persistence when using a converter --- UnitTests/LazyStorageTests.csproj | 1 + UnitTests/PersistenceWithConverterTests .cs | 100 ++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 UnitTests/PersistenceWithConverterTests .cs diff --git a/UnitTests/LazyStorageTests.csproj b/UnitTests/LazyStorageTests.csproj index 86fc68b..c5cb815 100644 --- a/UnitTests/LazyStorageTests.csproj +++ b/UnitTests/LazyStorageTests.csproj @@ -64,6 +64,7 @@ + diff --git a/UnitTests/PersistenceWithConverterTests .cs b/UnitTests/PersistenceWithConverterTests .cs new file mode 100644 index 0000000..03f5c70 --- /dev/null +++ b/UnitTests/PersistenceWithConverterTests .cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using LazyStorage.Tests.StorageTypes; +using Xunit; + +namespace LazyStorage.Tests +{ + public class PersistenceWithConverterTests : IDisposable + { + public static IEnumerable StorageTypes => new[] + { + new object[] {new InMemoryTestStorage(), }, + new object[] {new XmlTestStorage()}, + new object[] {new JsonTestStorage()}, + }; + + private ITestStorage m_CurrentStorage; + + [Theory, MemberData("StorageTypes")] + public void CanSaveToStorage(ITestStorage storage) + { + m_CurrentStorage = storage; + var dal = storage.GetStorage(); + var converter = new TestObjectStorageConverter(); + + var repo = storage.GetStorage().GetRepository(converter); + + var obj = new TestObjectNotIStorable(); + obj.Name = "Test"; + obj.StartDate = DateTime.Now; + obj.EndDate = DateTime.Now; + + repo.Set(obj); + dal.Save(); + + Assert.True(repo.Get().Any(), "The object could not be added to the repository"); + } + + [Theory, MemberData("StorageTypes")] + public void StoragePersistsBetweenSessions(ITestStorage storage) + { + m_CurrentStorage = storage; + var dal = storage.GetStorage(); + var converter = new TestObjectStorageConverter(); + + var repo = storage.GetStorage().GetRepository(converter); + + var obj = new TestObjectNotIStorable(); + obj.Name = "Test"; + obj.StartDate = DateTime.Now; + obj.EndDate = DateTime.Now; + + repo.Set(obj); + dal.Save(); + + var dal2 = storage.GetStorage(); + var repo2 = dal2.GetRepository(converter); + + Assert.True(repo2.Get().Single().ContentEquals(obj), "The object could not be found in the persistent repo"); + } + + [Theory, MemberData("StorageTypes")] + public void StorageDoesNotPersistIfDiscarded(ITestStorage storage) + { + m_CurrentStorage = storage; + + // Insert into the repo + var dal = storage.GetStorage(); + var converter = new TestObjectStorageConverter(); + + var repo = storage.GetStorage().GetRepository(converter); + + var obj1 = new TestObjectNotIStorable(); + obj1.Name = "Test"; + + repo.Set(obj1); + dal.Save(); + + // Make some changes + var obj2 = new TestObjectNotIStorable(); + obj2.Name = "Test"; + + // Update the object in the repo but discard changes + repo.Set(obj2); + dal.Discard(); + + var dal2 = storage.GetStorage(); + var repo2 = dal2.GetRepository(converter); + + var testObject = repo2.Get().Single(); + Assert.True(testObject.ContentEquals(obj1), "The object changes were not reverted in the repo"); + } + + public void Dispose() + { + m_CurrentStorage.CleanUp(); + } + } +} From 7cbb7215e0d905011bce6f8a6d4818258eee8049 Mon Sep 17 00:00:00 2001 From: David Eadie Date: Thu, 19 May 2016 16:26:05 +0100 Subject: [PATCH 10/11] Remove Id from storable object --- LazyStorage/StorableObject.cs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/LazyStorage/StorableObject.cs b/LazyStorage/StorableObject.cs index bd93f0c..aa7d85d 100644 --- a/LazyStorage/StorableObject.cs +++ b/LazyStorage/StorableObject.cs @@ -3,19 +3,13 @@ namespace LazyStorage { - public class StorableObject : IEquatable + public class StorableObject { - public int LazyStorageInternalId { get; set; } public Dictionary Info { get; } public StorableObject() { Info = new Dictionary(); } - - public bool Equals(StorableObject other) - { - return (other.LazyStorageInternalId == LazyStorageInternalId); - } } } \ No newline at end of file From 7b894483684d0c627eae89288d95bdef3d7de9f2 Mon Sep 17 00:00:00 2001 From: David Eadie Date: Thu, 19 May 2016 16:46:51 +0100 Subject: [PATCH 11/11] Try to sort our parallel test runs --- UnitTests/LazyStorageTests.csproj | 1 - UnitTests/PersistenceTests.cs | 75 +++++++++++++++ UnitTests/PersistenceWithConverterTests .cs | 100 -------------------- 3 files changed, 75 insertions(+), 101 deletions(-) delete mode 100644 UnitTests/PersistenceWithConverterTests .cs diff --git a/UnitTests/LazyStorageTests.csproj b/UnitTests/LazyStorageTests.csproj index c5cb815..86fc68b 100644 --- a/UnitTests/LazyStorageTests.csproj +++ b/UnitTests/LazyStorageTests.csproj @@ -64,7 +64,6 @@ - diff --git a/UnitTests/PersistenceTests.cs b/UnitTests/PersistenceTests.cs index 13ccff8..9c9fe58 100644 --- a/UnitTests/PersistenceTests.cs +++ b/UnitTests/PersistenceTests.cs @@ -78,6 +78,81 @@ public void StorageDoesNotPersistIfDiscarded(ITestStorage storage) Assert.True(testObject.ContentEquals(obj1), "The object changes were not reverted in the repo"); } + [Theory, MemberData("StorageTypes")] + public void CanSaveToStorageWithConverter(ITestStorage storage) + { + m_CurrentStorage = storage; + var dal = storage.GetStorage(); + var converter = new TestObjectStorageConverter(); + + var repo = storage.GetStorage().GetRepository(converter); + + var obj = new TestObjectNotIStorable(); + obj.Name = "Test"; + obj.StartDate = DateTime.Now; + obj.EndDate = DateTime.Now; + + repo.Set(obj); + dal.Save(); + + Assert.True(repo.Get().Any(), "The object could not be added to the repository"); + } + + [Theory, MemberData("StorageTypes")] + public void StoragePersistsBetweenSessionsWithConverter(ITestStorage storage) + { + m_CurrentStorage = storage; + var dal = storage.GetStorage(); + var converter = new TestObjectStorageConverter(); + + var repo = storage.GetStorage().GetRepository(converter); + + var obj = new TestObjectNotIStorable(); + obj.Name = "Test"; + obj.StartDate = DateTime.Now; + obj.EndDate = DateTime.Now; + + repo.Set(obj); + dal.Save(); + + var dal2 = storage.GetStorage(); + var repo2 = dal2.GetRepository(converter); + + Assert.True(repo2.Get().Single().ContentEquals(obj), "The object could not be found in the persistent repo"); + } + + [Theory, MemberData("StorageTypes")] + public void StorageDoesNotPersistIfDiscardedWithConverter(ITestStorage storage) + { + m_CurrentStorage = storage; + + // Insert into the repo + var dal = storage.GetStorage(); + var converter = new TestObjectStorageConverter(); + + var repo = storage.GetStorage().GetRepository(converter); + + var obj1 = new TestObjectNotIStorable(); + obj1.Name = "Test"; + + repo.Set(obj1); + dal.Save(); + + // Make some changes + var obj2 = new TestObjectNotIStorable(); + obj2.Name = "Test"; + + // Update the object in the repo but discard changes + repo.Set(obj2); + dal.Discard(); + + var dal2 = storage.GetStorage(); + var repo2 = dal2.GetRepository(converter); + + var testObject = repo2.Get().Single(); + Assert.True(testObject.ContentEquals(obj1), "The object changes were not reverted in the repo"); + } + public void Dispose() { m_CurrentStorage.CleanUp(); diff --git a/UnitTests/PersistenceWithConverterTests .cs b/UnitTests/PersistenceWithConverterTests .cs deleted file mode 100644 index 03f5c70..0000000 --- a/UnitTests/PersistenceWithConverterTests .cs +++ /dev/null @@ -1,100 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using LazyStorage.Tests.StorageTypes; -using Xunit; - -namespace LazyStorage.Tests -{ - public class PersistenceWithConverterTests : IDisposable - { - public static IEnumerable StorageTypes => new[] - { - new object[] {new InMemoryTestStorage(), }, - new object[] {new XmlTestStorage()}, - new object[] {new JsonTestStorage()}, - }; - - private ITestStorage m_CurrentStorage; - - [Theory, MemberData("StorageTypes")] - public void CanSaveToStorage(ITestStorage storage) - { - m_CurrentStorage = storage; - var dal = storage.GetStorage(); - var converter = new TestObjectStorageConverter(); - - var repo = storage.GetStorage().GetRepository(converter); - - var obj = new TestObjectNotIStorable(); - obj.Name = "Test"; - obj.StartDate = DateTime.Now; - obj.EndDate = DateTime.Now; - - repo.Set(obj); - dal.Save(); - - Assert.True(repo.Get().Any(), "The object could not be added to the repository"); - } - - [Theory, MemberData("StorageTypes")] - public void StoragePersistsBetweenSessions(ITestStorage storage) - { - m_CurrentStorage = storage; - var dal = storage.GetStorage(); - var converter = new TestObjectStorageConverter(); - - var repo = storage.GetStorage().GetRepository(converter); - - var obj = new TestObjectNotIStorable(); - obj.Name = "Test"; - obj.StartDate = DateTime.Now; - obj.EndDate = DateTime.Now; - - repo.Set(obj); - dal.Save(); - - var dal2 = storage.GetStorage(); - var repo2 = dal2.GetRepository(converter); - - Assert.True(repo2.Get().Single().ContentEquals(obj), "The object could not be found in the persistent repo"); - } - - [Theory, MemberData("StorageTypes")] - public void StorageDoesNotPersistIfDiscarded(ITestStorage storage) - { - m_CurrentStorage = storage; - - // Insert into the repo - var dal = storage.GetStorage(); - var converter = new TestObjectStorageConverter(); - - var repo = storage.GetStorage().GetRepository(converter); - - var obj1 = new TestObjectNotIStorable(); - obj1.Name = "Test"; - - repo.Set(obj1); - dal.Save(); - - // Make some changes - var obj2 = new TestObjectNotIStorable(); - obj2.Name = "Test"; - - // Update the object in the repo but discard changes - repo.Set(obj2); - dal.Discard(); - - var dal2 = storage.GetStorage(); - var repo2 = dal2.GetRepository(converter); - - var testObject = repo2.Get().Single(); - Assert.True(testObject.ContentEquals(obj1), "The object changes were not reverted in the repo"); - } - - public void Dispose() - { - m_CurrentStorage.CleanUp(); - } - } -}