Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
Merge pull request #37 from TheEadie/reduce-duplication
Browse files Browse the repository at this point in the history
Remove IStorable
  • Loading branch information
TheEadie authored Feb 16, 2018
2 parents a757bb0 + 1018da6 commit b786607
Show file tree
Hide file tree
Showing 17 changed files with 123 additions and 603 deletions.
75 changes: 6 additions & 69 deletions src/LazyStorage.Tests/PersistenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,77 +19,14 @@ public sealed class PersistenceTests : IDisposable

[Theory, MemberData(nameof(StorageTypes))]
public void CanSaveToStorage(ITestStorage storage)
{
_currentStorage = storage;
var dal = storage.GetStorage();
var repo = dal.GetRepository<TestObject>();
var obj = new TestObject();

repo.Set(obj);
dal.Save();

Assert.True(repo.Get().Any(), "The object could not be added to the repository");
}

[Theory, MemberData(nameof(StorageTypes))]
public void StoragePersistsBetweenSessions(ITestStorage storage)
{
_currentStorage = storage;
var dal = storage.GetStorage();
var repo = dal.GetRepository<TestObject>();
var obj = new TestObject();

repo.Set(obj);
dal.Save();

var dal2 = storage.GetStorage();
var repo2 = dal2.GetRepository<TestObject>();

Assert.True(repo2.Get().Single().ContentEquals(obj), "The object could not be found in the persistent repo");
}

[Theory, MemberData(nameof(StorageTypes))]
public void StorageDoesNotPersistIfDiscarded(ITestStorage storage)
{
_currentStorage = storage;

// Create an object in memory
var obj1 = new TestObject();

// Insert into the repo
var dal = storage.GetStorage();
var repo = dal.GetRepository<TestObject>();
repo.Set(obj1);
dal.Save();

// Make some changes
var obj2 = new TestObject
{
Id = 1,
Name = "Test"
};

// Update the object in the repo but discard changes
repo.Set(obj2);
dal.Discard();

var dal2 = storage.GetStorage();
var repo2 = dal2.GetRepository<TestObject>();

var testObject = repo2.Get().Single();
Assert.True(testObject.ContentEquals(obj1), "The object changes were not reverted in the repo");
}

[Theory, MemberData(nameof(StorageTypes))]
public void CanSaveToStorageWithConverter(ITestStorage storage)
{
_currentStorage = storage;
var dal = storage.GetStorage();
var converter = new TestObjectStorageConverter();

var repo = storage.GetStorage().GetRepository(converter);

var obj = new TestObjectNotIStorable
var obj = new TestObject
{
Name = "Test",
StartDate = DateTime.Now,
Expand All @@ -103,15 +40,15 @@ public void CanSaveToStorageWithConverter(ITestStorage storage)
}

[Theory, MemberData(nameof(StorageTypes))]
public void StoragePersistsBetweenSessionsWithConverter(ITestStorage storage)
public void StoragePersistsBetweenSessions(ITestStorage storage)
{
_currentStorage = storage;
var dal = storage.GetStorage();
var converter = new TestObjectStorageConverter();

var repo = dal.GetRepository(converter);

var obj = new TestObjectNotIStorable
var obj = new TestObject
{
Name = "Test",
StartDate = DateTime.Now,
Expand All @@ -128,7 +65,7 @@ public void StoragePersistsBetweenSessionsWithConverter(ITestStorage storage)
}

[Theory, MemberData(nameof(StorageTypes))]
public void StorageDoesNotPersistIfDiscardedWithConverter(ITestStorage storage)
public void StorageDoesNotPersistIfDiscarded(ITestStorage storage)
{
_currentStorage = storage;

Expand All @@ -138,13 +75,13 @@ public void StorageDoesNotPersistIfDiscardedWithConverter(ITestStorage storage)

var repo = dal.GetRepository(converter);

var obj1 = new TestObjectNotIStorable {Name = "Test"};
var obj1 = new TestObject {Name = "Test"};

repo.Set(obj1);
dal.Save();

// Make some changes
var obj2 = new TestObjectNotIStorable {Name = "Test"};
var obj2 = new TestObject {Name = "Test"};

// Update the object in the repo but discard changes
repo.Set(obj2);
Expand Down
73 changes: 0 additions & 73 deletions src/LazyStorage.Tests/RepositoryTests.cs

This file was deleted.

26 changes: 13 additions & 13 deletions src/LazyStorage.Tests/RepositoryWithConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@

namespace LazyStorage.Tests
{
public class RepositoryWithConverterTests
public class RepositoryTests
{
public static IEnumerable<object[]> Repos => new[]
{
new object[] {new InMemoryRepositoryWithConverter<TestObjectNotIStorable>(new TestObjectStorageConverter())},
new object[] {new XmlRepositoryWithConverter<TestObjectNotIStorable>("RepositoryWithConverterTests", new TestObjectStorageConverter())},
new object[] {new JsonRepositoryWithConverter<TestObjectNotIStorable>("RepositoryWithConverterTests", new TestObjectStorageConverter())},
new object[] {new InMemoryRepository<TestObject>(new TestObjectStorageConverter())},
new object[] {new XmlRepository<TestObject>("RepositoryWithConverter", new TestObjectStorageConverter())},
new object[] {new JsonRepository<TestObject>("RepositoryWithConverter", new TestObjectStorageConverter())},
};

[Theory, MemberData(nameof(Repos))]
public void CanAddToRepo(IRepository<TestObjectNotIStorable> repo)
public void CanAddToRepo(IRepository<TestObject> repo)
{
var obj = new TestObjectNotIStorable
var obj = new TestObject
{
Name = "Test",
StartDate = DateTime.Now,
Expand All @@ -36,9 +36,9 @@ public void CanAddToRepo(IRepository<TestObjectNotIStorable> repo)
}

[Theory, MemberData(nameof(Repos))]
public void CanUpdateRepo(IRepository<TestObjectNotIStorable> repo)
public void CanUpdateRepo(IRepository<TestObject> repo)
{
var obj = new TestObjectNotIStorable
var obj = new TestObject
{
Name = "Test",
StartDate = new DateTime(2015, 12, 31, 13, 54, 23),
Expand All @@ -56,9 +56,9 @@ public void CanUpdateRepo(IRepository<TestObjectNotIStorable> repo)
}

[Theory, MemberData(nameof(Repos))]
public void CanDeleteFromRepo(IRepository<TestObjectNotIStorable> repo)
public void CanDeleteFromRepo(IRepository<TestObject> repo)
{
var obj = new TestObjectNotIStorable { Name = "Test" };
var obj = new TestObject { Name = "Test" };

repo.Set(obj);
repo.Delete(obj);
Expand All @@ -67,10 +67,10 @@ public void CanDeleteFromRepo(IRepository<TestObjectNotIStorable> repo)
}

[Theory, MemberData(nameof(Repos))]
public void CanGetByLinq(IRepository<TestObjectNotIStorable> repo)
public void CanGetByLinq(IRepository<TestObject> repo)
{
var objOne = new TestObjectNotIStorable { Name = "one" };
var objTwo = new TestObjectNotIStorable { Name = "two" };
var objOne = new TestObject { Name = "one" };
var objTwo = new TestObject { Name = "two" };

repo.Set(objOne);
repo.Set(objTwo);
Expand Down
56 changes: 29 additions & 27 deletions src/LazyStorage.Tests/TestObject.cs
Original file line number Diff line number Diff line change
@@ -1,53 +1,55 @@
using System;
using System.Collections.Generic;
using LazyStorage.Interfaces;

namespace LazyStorage.Tests
{
public sealed class TestObject : IStorable<TestObject>
public class TestObject
{
public int Id { get; set; }
public string Name { get; set; }
private DateTime _startDate;
private DateTime _endDate;
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }

public TestObject()
{
Name = "";
}

public Dictionary<string, string> GetStorageInfo()
public bool ContentEquals(TestObject other)
{
var info = new Dictionary<string, string>
{
{"Id", Id.ToString()},
{"Name", Name},
{"StartDate", _startDate.Ticks.ToString()},
{"EndDate", _endDate.Ticks.ToString()}
};

return info;
return (other.Name == Name)
&& (other.StartDate == StartDate)
&& (other.EndDate == EndDate);
}
}

public void InitialiseWithStorageInfo(Dictionary<string, string> info)
public class TestObjectStorageConverter : IConverter<TestObject>
{
public StorableObject GetStorableObject(TestObject item)
{
Id = int.Parse(info["Id"]);
Name = info["Name"];
_startDate = new DateTime(long.Parse(info["StartDate"]));
_endDate = new DateTime(long.Parse(info["EndDate"]));
var storableObject = new StorableObject();

storableObject.Info.Add("Id", item.Name);
storableObject.Info.Add("StartDate", item.StartDate.Ticks.ToString());
storableObject.Info.Add("EndDate", item.EndDate.Ticks.ToString());

return storableObject;
}

public bool Equals(TestObject other)
public TestObject GetOriginalObject(StorableObject info)
{
return other.Id == Id;
var orginalObject = new TestObject
{
Name = info.Info["Id"],
StartDate = new DateTime(long.Parse(info.Info["StartDate"])),
EndDate = new DateTime(long.Parse(info.Info["EndDate"]))
};

return orginalObject;
}

public bool ContentEquals(TestObject other)
public bool IsEqual(StorableObject storageObject, TestObject realObject)
{
return other.Id == Id
&& other.Name == Name
&& other._startDate == _startDate
&& other._endDate == _endDate;
return realObject.Name == storageObject.Info["Id"];
}
}
}
Loading

0 comments on commit b786607

Please sign in to comment.