[longomatch] Add a new IStorage method to retrieve by ID



commit 23471069a6b80ce3bcc103fc2919afb89ab9c41c
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Fri Mar 13 19:10:22 2015 +0100

    Add a new IStorage method to retrieve by ID

 LongoMatch.Core/Interfaces/IStorage.cs      |    7 +++
 LongoMatch.Services/Services/FileStorage.cs |   13 +++++
 Tests/Services/TestFileStorage.cs           |   70 +++++++++++++++++++--------
 3 files changed, 70 insertions(+), 20 deletions(-)
---
diff --git a/LongoMatch.Core/Interfaces/IStorage.cs b/LongoMatch.Core/Interfaces/IStorage.cs
index bf12859..0a60c12 100644
--- a/LongoMatch.Core/Interfaces/IStorage.cs
+++ b/LongoMatch.Core/Interfaces/IStorage.cs
@@ -29,6 +29,13 @@ namespace LongoMatch.Core.Interfaces
                List<T> RetrieveAll<T>() where T : IStorable;
 
                /// <summary>
+               /// Retrieve on object with the specified id.
+               /// </summary>
+               /// <param name="id">The object unique identifier.</param>
+               /// <typeparam name="T">The 1st type parameter.</typeparam>
+               T Retrieve<T> (Guid id) where T : IStorable;
+
+               /// <summary>
                /// Retrieve every object of type T, where T must implement IStorable using on the dictionary 
as a filter on its properties
                /// </summary>
                /// <typeparam name="T">The type of IStorable you want to retrieve.</typeparam>
diff --git a/LongoMatch.Services/Services/FileStorage.cs b/LongoMatch.Services/Services/FileStorage.cs
index 8f57227..59bdf51 100644
--- a/LongoMatch.Services/Services/FileStorage.cs
+++ b/LongoMatch.Services/Services/FileStorage.cs
@@ -65,6 +65,19 @@ namespace LongoMatch.Services.Services
 
                #region IStorage implementation
 
+               public T Retrieve<T> (Guid id) where T : IStorable
+               {
+                       string typePath = ResolvePath<T>();
+                       string path = Path.Combine (typePath, id.ToString () + GetExtension(typeof(T)));
+
+                       if (File.Exists (path)) {
+                               T t = Serializer.LoadSafe<T>(path);
+                               Log.Information (string.Format ("Retrieving {0} at {1}", t.ID, typePath));
+                               return t;
+                       }
+                       return default (T);
+               }
+
                public List<T> RetrieveAll<T> () where T : IStorable
                {
                        List<T> l = new List<T> ();
diff --git a/Tests/Services/TestFileStorage.cs b/Tests/Services/TestFileStorage.cs
index b17c7fd..8121911 100644
--- a/Tests/Services/TestFileStorage.cs
+++ b/Tests/Services/TestFileStorage.cs
@@ -24,14 +24,17 @@ using LongoMatch.Core.Interfaces;
 
 namespace Tests.Services
 {
-       [TestFixture()]
+       [TestFixture ()]
        public class TestFileStorage
        {
+
+               FileStorage fs;
+
                private class TestStorable : IStorable
                {
                        public string memberString;
 
-                       public TestStorable(string memberString)
+                       public TestStorable (string memberString)
                        {
                                this.memberString = memberString;
                                ID = Guid.NewGuid ();
@@ -43,39 +46,66 @@ namespace Tests.Services
                        }
                }
 
-               [Test()]
+               [SetUp]
+               public void CreateStorage ()
+               {
+                       fs = new FileStorage (Path.Combine (Path.GetTempPath (), "TestFileStorage"), true);
+               }
+
+               [TearDown]
+               public void RemoveStorage ()
+               {
+                       try {
+                               Directory.Delete (Path.Combine (Path.GetTempPath (), "TestFileStorage"), 
true);
+                       } catch {
+                       }
+               }
+
+               [Test ()]
                public void TestCase ()
                {
-                       FileStorage fs = new FileStorage(Path.Combine(Path.GetTempPath(), "TestFileStorage"), 
true);
-                       TestStorable ts1 = new TestStorable("first");
+                       TestStorable ts1 = new TestStorable ("first");
 
-                       fs.Store<TestStorable>(ts1);
-                       List<TestStorable> lts = fs.RetrieveAll<TestStorable>();
+                       fs.Store<TestStorable> (ts1);
+                       List<TestStorable> lts = fs.RetrieveAll<TestStorable> ();
 
                        // Check that we have stored one object
-                       Assert.AreEqual(lts.Count, 1);
-                       TestStorable ts2 = lts[0];
-                       Assert.AreNotSame(ts2, null);
+                       Assert.AreEqual (lts.Count, 1);
+                       TestStorable ts2 = lts [0];
+                       Assert.AreNotSame (ts2, null);
 
                        // Check that the object is the same
-                       Assert.AreEqual(ts2.memberString, ts1.memberString);
+                       Assert.AreEqual (ts2.memberString, ts1.memberString);
 
                        // Get based on memberString
-                       Dictionary<string, object> dict = new Dictionary<string, object>();
-                       dict.Add("memberString", "first");
-                       lts = fs.Retrieve<TestStorable>(dict);
+                       Dictionary<string, object> dict = new Dictionary<string, object> ();
+                       dict.Add ("memberString", "first");
+                       lts = fs.Retrieve<TestStorable> (dict);
 
                        // Check that we have stored one object
-                       Assert.AreEqual(lts.Count, 1);
+                       Assert.AreEqual (lts.Count, 1);
 
                        // Check that the returned object is the one we are looking for
-                       ts2 = lts[0];
-                       Assert.AreNotSame(ts2, null);
+                       ts2 = lts [0];
+                       Assert.AreNotSame (ts2, null);
 
                        // Check that the storage is empty
-                       fs.Delete<TestStorable>(ts2);
-                       lts = fs.RetrieveAll<TestStorable>();
-                       Assert.AreEqual(lts.Count, 0);
+                       fs.Delete<TestStorable> (ts2);
+                       lts = fs.RetrieveAll<TestStorable> ();
+                       Assert.AreEqual (lts.Count, 0);
+               }
+
+               [Test ()]
+               public void TestRetrieveByID ()
+               {
+                       TestStorable ts1 = new TestStorable ("first");
+                       fs.Store<TestStorable> (ts1);
+
+                       TestStorable ts2 = fs.Retrieve<TestStorable> (ts1.ID);
+                       Assert.IsNotNull (ts2);
+                       Assert.AreEqual (ts1.ID, ts2.ID);
+
+                       Assert.IsNull (fs.Retrieve<TestStorable> (Guid.NewGuid()));
                }
        }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]