Permalink
Browse files

Fixing the code after the big refactoring tsunami.

Rewriting tests
  • Loading branch information...
1 parent 3922909 commit c24a9a55bcaa1ff8733946d6738247f23643767a @bettiolo committed Jul 19, 2012
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace PassFruit.DataStore.Contracts {
+
+ public enum AccountDtoStatus {
+ All,
+ Active,
+ Deleted
+ }
+
+}
@@ -13,7 +13,7 @@ public interface IAccountDto {
IList<IFieldDto> Fields { get; set; }
- IList<string> Tags { get; set; }
+ IList<ITagDto> Tags { get; set; }
string Notes { get; set; }
@@ -9,11 +9,9 @@ public interface IDataStore {
string Description { get; }
- IEnumerable<Guid> GetActiveAccountIds();
+ IEnumerable<Guid> GetAllAccountIds();
- IEnumerable<IAccountDto> GetActiveAccountDtos();
-
- IEnumerable<Guid> GetDeletedAccountIds();
+ IEnumerable<IAccountDto> GetAccountDtos(AccountDtoStatus accountStatus = AccountDtoStatus.Active);
IAccountDto GetAccountDto(Guid accountId);
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace PassFruit.DataStore.Contracts {
+
+ public interface ITagDto {
+
+ string Name { get; set; }
+
+ }
+
+}
@@ -33,11 +33,13 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
+ <Compile Include="AccountDtoStatus.cs" />
<Compile Include="IAccountDto.cs" />
<Compile Include="IFieldDto.cs" />
<Compile Include="IPasswordDto.cs" />
<Compile Include="IDataStore.cs" />
<Compile Include="IDataStoreConfiguration.cs" />
+ <Compile Include="ITagDto.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
@@ -10,17 +10,20 @@ namespace PassFruit.DataStore.InMemoryDataStore.Tests {
public class InMemoryDataStoreTests : DataStoreTestsBase {
private readonly InMemoryDataStoreConfiguration _configuration =
- new InMemoryDataStoreConfiguration(Path.GetTempFileName());
+ new InMemoryDataStoreConfiguration();
protected override IDataStore GetDataStoreWithFakeData() {
- var inMemoryDataStore = new InMemoryDataStore(_configuration);
+ var inMemoryDataStore = _configuration.Instance;
var fakeDataGenerator = new FakeDataGenerator();
+ foreach (var accountId in inMemoryDataStore.GetAllAccountIds()) {
+ inMemoryDataStore.DeleteAccount(accountId);
+ }
fakeDataGenerator.GenerateFakeData(inMemoryDataStore);
return inMemoryDataStore;
}
protected override IDataStore GetDataStore() {
- return new InMemoryDataStore(_configuration);
+ return _configuration.Instance;
}
}
@@ -7,11 +7,11 @@ namespace PassFruit.DataStore.InMemoryDataStore {
public class InMemoryDataStore : DataStoreBase {
- private readonly InMemoryDataStoreConfiguration _configuration;
+ //private readonly InMemoryDataStoreConfiguration _configuration;
- public InMemoryDataStore(InMemoryDataStoreConfiguration configuration) {
- _configuration = configuration;
- }
+ //public InMemoryDataStore(InMemoryDataStoreConfiguration configuration) {
+ // _configuration = configuration;
+ //}
private readonly Dictionary<Guid, IAccountDto> _accountDtos =
new Dictionary<Guid, IAccountDto>();
@@ -55,13 +55,8 @@ public class InMemoryDataStore : DataStoreBase {
_passwordDtos.Remove(accountId);
}
- public override IEnumerable<Guid> GetActiveAccountIds() {
- return _accountDtos.Where(accountDto => !accountDto.Value.IsDeleted)
- .Select(item => item.Key);
- }
-
- public override IEnumerable<Guid> GetDeletedAccountIds() {
- return _accountDtos.Where(item => item.Value.IsDeleted).Select(account => account.Key);
+ public override IEnumerable<Guid> GetAllAccountIds() {
+ return _accountDtos.Select(item => item.Key);
}
public override IAccountDto GetAccountDto(Guid accountId) {
@@ -4,15 +4,17 @@ namespace PassFruit.DataStore.InMemoryDataStore {
public class InMemoryDataStoreConfiguration : IDataStoreConfiguration {
- public InMemoryDataStoreConfiguration(string serializedFilePath) {
- _serializedFilePath = serializedFilePath;
- }
+ public readonly InMemoryDataStore Instance = new InMemoryDataStore();
- private readonly string _serializedFilePath;
+ //public InMemoryDataStoreConfiguration(string serializedFilePath) {
+ // _serializedFilePath = serializedFilePath;
+ //}
- public string SerializedFilePath {
- get { return _serializedFilePath; }
- }
+ //private readonly string _serializedFilePath;
+
+ //public string SerializedFilePath {
+ // get { return _serializedFilePath; }
+ //}
}
@@ -52,8 +52,8 @@ public class FakeDataGenerator {
public static IAccountDto GetFacebookAccount() {
var facebookAccount = new AccountDto { ProviderKey = FacebookProviderKey, Notes = Note1 };
facebookAccount.Fields.Add(new FieldDto { FieldTypeKey = "email", Name = "E-Mail", Value = FacebookEmail });
- facebookAccount.Tags.Add(Tag1);
- facebookAccount.Tags.Add(Tag2);
+ facebookAccount.Tags.Add(new TagDto { Name = Tag1 });
+ facebookAccount.Tags.Add(new TagDto { Name = Tag2 });
return facebookAccount;
}
@@ -65,7 +65,7 @@ public class FakeDataGenerator {
var twitterAccount = new AccountDto { ProviderKey = TwitterProviderKey, Notes = Note2 };
twitterAccount.Fields.Add(new FieldDto { FieldTypeKey = "email", Name = "E-Mail", Value = TwitterEmail });
twitterAccount.Fields.Add(new FieldDto { FieldTypeKey = "username", Name = "User name", Value = TwitterUserName });
- twitterAccount.Tags.Add(Tag1);
+ twitterAccount.Tags.Add(new TagDto { Name = Tag1 });
return twitterAccount;
}
@@ -76,9 +76,9 @@ public class FakeDataGenerator {
private IAccountDto GetGoogle1Account() {
var google1Account = new AccountDto { ProviderKey = GoogleProviderKey };
google1Account.Fields.Add(new FieldDto { FieldTypeKey = "email", Name = "E-Mail", Value = Google1Email });
- google1Account.Tags.Add(Tag1);
- google1Account.Tags.Add(Tag2);
- google1Account.Tags.Add(Tag3);
+ google1Account.Tags.Add(new TagDto { Name = Tag1 });
+ google1Account.Tags.Add(new TagDto { Name = Tag2 });
+ google1Account.Tags.Add(new TagDto { Name = Tag3 });
return google1Account;
}
@@ -89,8 +89,8 @@ public class FakeDataGenerator {
private IAccountDto GetGoogle2Account() {
var google2Account = new AccountDto { ProviderKey = GoogleProviderKey };
google2Account.Fields.Add(new FieldDto { FieldTypeKey = "email", Name = "E-Mail", Value = Google2Email });
- google2Account.Tags.Add(Tag2);
- google2Account.Tags.Add(Tag3);
+ google2Account.Tags.Add(new TagDto { Name = Tag2 });
+ google2Account.Tags.Add(new TagDto { Name = Tag3 });
return google2Account;
}
@@ -32,21 +32,25 @@ public abstract class DataStoreTestsBase {
// Given
var facebookAccount = FakeData.FakeDataGenerator.GetFacebookAccount();
var facebookPassword = FakeData.FakeDataGenerator.GetFacebookPassword();
+ IDataStore originalDataStore = null;
// When
TestWithDataStore(dataStore => {
dataStore.SaveAccountDto(facebookAccount);
dataStore.SavePasswordDto(facebookAccount.Id, facebookPassword);
+ originalDataStore = dataStore;
});
-
+
// Then
- TestWithDataStore(dataStore => {
- dataStore.GetActiveAccountIds().Should().Contain(id => id.Equals(facebookAccount.Id));
- dataStore.GetActiveAccountIds().Should().HaveCount(1);
- dataStore.GetActiveAccountDtos().Should().HaveCount(1);
- dataStore.GetActiveAccountDtos().Should().Contain(accountDto => accountDto.Id == facebookAccount.Id);
+ Action<IDataStore> readDataStore = dataStore => {
+ dataStore.GetAllAccountIds().Should().Contain(id => id.Equals(facebookAccount.Id));
+ dataStore.GetAllAccountIds().Should().HaveCount(1);
+ dataStore.GetAccountDtos().Should().HaveCount(1);
+ dataStore.GetAccountDtos().Should().Contain(accountDto => accountDto.Id == facebookAccount.Id);
dataStore.GetAccountDto(facebookAccount.Id).Equals(facebookAccount).Should().BeTrue();
- });
+ };
+ readDataStore(originalDataStore);
+ TestWithDataStore(readDataStore);
}
/*
@@ -68,14 +68,10 @@ public class XmlDataStore : DataStoreBase {
get { return "XML dataStore, the data is persisted in a XML file"; }
}
- public override IEnumerable<Guid> GetActiveAccountIds() {
+ public override IEnumerable<Guid> GetAllAccountIds() {
return GetAccountIdsWithFilter(accountId => !AccountElementIsDeleted(accountId));
}
- public override IEnumerable<Guid> GetDeletedAccountIds() {
- return GetAccountIdsWithFilter(AccountElementIsDeleted);
- }
-
public override IAccountDto GetAccountDto(Guid accountId) {
return new AccountDto(accountId) {
ProviderKey = GetProviderElement(accountId).Value,
@@ -93,10 +89,12 @@ public class XmlDataStore : DataStoreBase {
GetAccountElement(accountDto.Id).Remove();
GetProviderElement(accountDto.Id).Value = accountDto.ProviderKey;
foreach (var field in accountDto.Fields) {
- GetFieldElement(field.Id, accountDto.Id).Value = field.Value.ToString();
+ GetFieldNameElement(accountDto.Id, field.Id).Value = field.Name;
+ GetFieldTypeKeyElement(accountDto.Id, field.Id).Value = field.FieldTypeKey;
+ GetFieldValueElement(accountDto.Id, field.Id).Value = field.Value.ToString();
}
foreach (var tag in accountDto.Tags) {
- GetTagElement(tag, accountDto.Id);
+ GetTagElement(tag.Name, accountDto.Id);
}
GetNoteElement(accountDto.Id).Value = accountDto.Notes ?? "";
UpdateLastChangedUtc(GetAccountElement(accountDto.Id));
@@ -155,16 +153,16 @@ public class XmlDataStore : DataStoreBase {
return accountIds;
}
- private IList<string> GetTags(Guid accountId) {
- var tags = new List<string>();
+ private IList<ITagDto> GetTags(Guid accountId) {
+ var tags = new List<ITagDto>();
foreach (var tagElement in GetTagsElement(accountId).Elements()) {
var tagElementName = tagElement.Name.LocalName;
if (!tagElementName.StartsWith(TagPrefix)) {
throw new Exception(string.Format("The tag name '{0}' is not starting with the prefix '{1}'",
tagElementName, TagPrefix));
}
var tagKey = tagElement.Name.LocalName.Remove(0, TagPrefix.Length);
- tags.Add(tagKey);
+ tags.Add(new TagDto { Name = tagKey });
}
return tags;
}
@@ -222,7 +220,7 @@ public class XmlDataStore : DataStoreBase {
}
private XAttribute GetLastChangedUtcAttribute(XElement element) {
- return element.Attribute("lastChanged");
+ return GetOrCreateAttribute("lastChanged", element);
}
private XElement GetAccountsElement() {
@@ -283,6 +281,16 @@ public class XmlDataStore : DataStoreBase {
return element;
}
+ private XAttribute GetOrCreateAttribute(string attributeName, XElement element) {
+ attributeName = attributeName.ToLowerInvariant();
+ var attribute = element.Attribute(attributeName);
+ if (attribute == null) {
+ attribute = new XAttribute(attributeName, "");
+ element.Add(attribute);
+ }
+ return attribute;
+ }
+
private void GetElementsWithId(XContainer containingElement, string idPrefix,
Action<Guid, XElement> processChildElement) {
foreach (var childElement in containingElement.Elements()) {
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using PassFruit.DataStore.Contracts;
+using System.Linq;
+using System.Collections;
namespace PassFruit.DataStore {
@@ -11,7 +13,7 @@ public class AccountDto : IAccountDto {
? Guid.NewGuid()
: accuntId.Value;
Fields = new List<IFieldDto>();
- Tags = new List<string>();
+ Tags = new List<ITagDto>();
}
public Guid Id { get; private set; }
@@ -22,7 +24,7 @@ public class AccountDto : IAccountDto {
public IList<IFieldDto> Fields { get; set; }
- public IList<string> Tags { get; set; }
+ public IList<ITagDto> Tags { get; set; }
public DateTime LastChangedUtc { get; set; }
@@ -31,12 +33,12 @@ public class AccountDto : IAccountDto {
public bool Equals(IAccountDto otherAccountDto) {
if (ReferenceEquals(null, otherAccountDto)) return false;
if (ReferenceEquals(this, otherAccountDto)) return true;
- return Equals(otherAccountDto.ProviderKey, ProviderKey)
+ return otherAccountDto.ProviderKey.Equals(ProviderKey)
&& otherAccountDto.IsDeleted.Equals(IsDeleted)
- && otherAccountDto.Id.Equals(Id)
- && Equals(otherAccountDto.Fields, Fields)
- && Equals(otherAccountDto.Tags, Tags)
- && Equals(otherAccountDto.Notes, Notes);
+ && otherAccountDto.Id.Equals(Id)
+ && otherAccountDto.Fields.SequenceEqual(Fields)
+ && otherAccountDto.Tags.SequenceEqual(Tags)
+ && otherAccountDto.Notes.Equals(Notes);
}
public override bool Equals(object obj) {
@@ -12,12 +12,11 @@ public abstract class DataStoreBase : IDataStore {
public abstract string Description { get; }
- public abstract IEnumerable<Guid> GetActiveAccountIds();
+ public abstract IEnumerable<Guid> GetAllAccountIds();
- public abstract IEnumerable<Guid> GetDeletedAccountIds();
-
- public IEnumerable<IAccountDto> GetActiveAccountDtos() {
- return GetActiveAccountIds().Select(GetAccountDto);
+ public IEnumerable<IAccountDto> GetAccountDtos(AccountDtoStatus accountStatus = AccountDtoStatus.Active) {
+ return GetAllAccountIds().Select(GetAccountDto).Where(accountDto =>
+ IsAccountDtoMatchedByStatus(accountStatus, accountDto.IsDeleted));
}
public abstract IAccountDto GetAccountDto(Guid accountId);
@@ -36,6 +35,19 @@ public abstract class DataStoreBase : IDataStore {
public abstract void DeleteAccountPasswords(Guid accountId);
+ private bool IsAccountDtoMatchedByStatus(AccountDtoStatus accountDtoStatus, bool isDeleted) {
+ switch (accountDtoStatus) {
+ case AccountDtoStatus.All:
+ return true;
+ case AccountDtoStatus.Active:
+ return !isDeleted;
+ case AccountDtoStatus.Deleted:
+ return isDeleted;
+ default:
+ throw new NotSupportedException("The account status filter specified is not supported: " + accountDtoStatus);
+ }
+ }
+
}
}
Oops, something went wrong.

0 comments on commit c24a9a5

Please sign in to comment.