Rename for clarity: AccountsSettings <=> Accounts

This commit is contained in:
Robert McRackan 2020-08-24 22:57:08 -04:00
parent 743644c4e9
commit 6900a68b9d
11 changed files with 265 additions and 277 deletions

View File

@ -59,7 +59,7 @@ namespace FileLiberator
var proposedOutputFile = Path.Combine(AudibleFileStorage.DecryptInProgress, $"[{libraryBook.Book.AudibleProductId}].m4b");
var account = AudibleApiStorage
.GetAccounts()
.GetAccountsSettings()
.GetAccount(libraryBook.Account, libraryBook.Book.Locale);
var outputAudioFilename = await aaxToM4bConverterDecrypt(proposedOutputFile, aaxFilename, account);

View File

@ -10,49 +10,49 @@ using Newtonsoft.Json;
namespace InternalUtilities
{
public class AccountsPersister : JsonFilePersister<Accounts>
public class AccountsSettingsPersister : JsonFilePersister<AccountsSettings>
{
/// <summary>Alias for Target </summary>
public Accounts Accounts => Target;
public AccountsSettings AccountsSettings => Target;
/// <summary>uses path. create file if doesn't yet exist</summary>
public AccountsPersister(Accounts target, string path, string jsonPath = null)
public AccountsSettingsPersister(AccountsSettings target, string path, string jsonPath = null)
: base(target, path, jsonPath) { }
/// <summary>load from existing file</summary>
public AccountsPersister(string path, string jsonPath = null)
public AccountsSettingsPersister(string path, string jsonPath = null)
: base(path, jsonPath) { }
protected override JsonSerializerSettings GetSerializerSettings()
=> Identity.GetJsonSerializerSettings();
}
// 'Accounts' is intentionally not IEnumerable<> so that properties can be added/extended
// 'AccountsSettings' is intentionally not IEnumerable<> so that properties can be added/extended
// from newtonsoft (https://www.newtonsoft.com/json/help/html/SerializationGuide.htm):
// .NET : IList, IEnumerable, IList<T>, Array
// JSON : Array (properties on the collection will not be serialized)
public class Accounts : IUpdatable
public class AccountsSettings : IUpdatable
{
public event EventHandler Updated;
private void update(object sender = null, EventArgs e = null)
{
foreach (var account in AccountsSettings)
foreach (var account in Accounts)
validate(account);
update_no_validate();
}
private void update_no_validate() => Updated?.Invoke(this, new EventArgs());
public Accounts() { }
public AccountsSettings() { }
// for some reason this will make the json instantiator use _accountsSettings_json.set()
// for some reason this will make the json instantiator use _accounts_json.set()
[JsonConstructor]
protected Accounts(List<Account> accounts) { }
protected AccountsSettings(List<Account> accountsSettings) { }
#region AccountsSettings
private List<Account> _accountsSettings_backing = new List<Account>();
[JsonProperty(PropertyName = "AccountsSettings")]
private List<Account> _accountsSettings_json
#region Accounts
private List<Account> _accounts_backing = new List<Account>();
[JsonProperty(PropertyName = "Accounts")]
private List<Account> _accounts_json
{
get => _accountsSettings_backing;
get => _accounts_backing;
// 'set' is only used by json deser
set
{
@ -66,11 +66,11 @@ namespace InternalUtilities
}
}
[JsonIgnore]
public IReadOnlyList<Account> AccountsSettings => _accountsSettings_json.AsReadOnly();
public IReadOnlyList<Account> Accounts => _accounts_json.AsReadOnly();
#endregion
public static Accounts FromJson(string json)
=> JsonConvert.DeserializeObject<Accounts>(json, Identity.GetJsonSerializerSettings());
public static AccountsSettings FromJson(string json)
=> JsonConvert.DeserializeObject<AccountsSettings>(json, Identity.GetJsonSerializerSettings());
public string ToJson(Formatting formatting = Formatting.Indented)
=> JsonConvert.SerializeObject(this, formatting, Identity.GetJsonSerializerSettings());
@ -85,19 +85,19 @@ namespace InternalUtilities
{
validate(account);
_accountsSettings_backing.Add(account);
_accounts_backing.Add(account);
account.Updated += update;
}
// more common naming convention alias for internal collection
public IReadOnlyList<Account> GetAll() => AccountsSettings;
public IReadOnlyList<Account> GetAll() => Accounts;
public Account GetAccount(string accountId, string locale)
{
if (locale is null)
return null;
return AccountsSettings.SingleOrDefault(a => a.AccountId == accountId && a.IdentityTokens.Locale.Name == locale);
return Accounts.SingleOrDefault(a => a.AccountId == accountId && a.IdentityTokens.Locale.Name == locale);
}
public Account Upsert(string accountId, string locale)
@ -117,11 +117,11 @@ namespace InternalUtilities
public bool Delete(Account account)
{
if (!_accountsSettings_backing.Contains(account))
if (!_accounts_backing.Contains(account))
return false;
account.Updated -= update;
return _accountsSettings_backing.Remove(account);
return _accounts_backing.Remove(account);
}
public bool Delete(string accountId, string locale)

View File

@ -16,7 +16,7 @@ namespace InternalUtilities
{
// saves. BEWARE: this will overwrite an existing file
if (!File.Exists(AccountsSettingsFile))
_ = new AccountsPersister(new Accounts(), AccountsSettingsFile);
_ = new AccountsSettingsPersister(new AccountsSettings(), AccountsSettingsFile);
}
// convenience for for tests and demos. don't use in production Libation
@ -24,10 +24,10 @@ namespace InternalUtilities
=> TEST_GetFirstAccount().GetIdentityTokensJsonPath();
// convenience for for tests and demos. don't use in production Libation
public static Account TEST_GetFirstAccount()
=> GetAccounts().GetAll().FirstOrDefault();
=> GetAccountsSettings().GetAll().FirstOrDefault();
public static Accounts GetAccounts()
=> new AccountsPersister(AccountsSettingsFile).Accounts;
public static AccountsSettings GetAccountsSettings()
=> new AccountsSettingsPersister(AccountsSettingsFile).AccountsSettings;
public static string GetIdentityTokensJsonPath(this Account account)
=> GetIdentityTokensJsonPath(account.AccountId, account.Locale?.Name);
@ -36,7 +36,7 @@ namespace InternalUtilities
var usernameSanitized = trimSurroundingQuotes(JsonConvert.ToString(username));
var localeNameSanitized = trimSurroundingQuotes(JsonConvert.ToString(localeName));
return $"$.AccountsSettings[?(@.AccountId == '{usernameSanitized}' && @.IdentityTokens.LocaleName == '{localeNameSanitized}')].IdentityTokens";
return $"$.Accounts[?(@.AccountId == '{usernameSanitized}' && @.IdentityTokens.LocaleName == '{localeNameSanitized}')].IdentityTokens";
}
// SubString algo is better than .Trim("\"")
// orig string "

View File

@ -13,7 +13,7 @@
<!-- <PublishSingleFile>true</PublishSingleFile> -->
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<Version>3.1.12.249</Version>
<Version>3.1.12.253</Version>
</PropertyGroup>
<ItemGroup>

View File

@ -162,7 +162,7 @@ namespace LibationLauncher
};
// saves to new file
AudibleApiStorage.GetAccounts().Add(account);
AudibleApiStorage.GetAccountsSettings().Add(account);
return account;
}

View File

@ -82,12 +82,8 @@
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(776, 397);
this.dataGridView1.TabIndex = 0;
this.dataGridView1.CellBeginEdit += new System.Windows.Forms.DataGridViewCellCancelEventHandler(this.dataGridView1_CellBeginEdit);
this.dataGridView1.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellEndEdit);
this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.DataGridView1_CellContentClick);
this.dataGridView1.DefaultValuesNeeded += new System.Windows.Forms.DataGridViewRowEventHandler(this.dataGridView1_DefaultValuesNeeded);
this.dataGridView1.RowsAdded += new System.Windows.Forms.DataGridViewRowsAddedEventHandler(this.dataGridView1_RowsAdded);
this.dataGridView1.RowsRemoved += new System.Windows.Forms.DataGridViewRowsRemovedEventHandler(this.dataGridView1_RowsRemoved);
//
// Original
//

View File

@ -2,7 +2,6 @@
using System.Linq;
using System.Windows.Forms;
using AudibleApi;
using FileManager;
using InternalUtilities;
namespace LibationWinForms.Dialogs
@ -37,7 +36,12 @@ namespace LibationWinForms.Dialogs
private void populateGridValues()
{
var accounts = AudibleApiStorage.GetAccounts().AccountsSettings;
// WARNING
// behind the scenes this returns a AccountsSettings
// accounts persister will write ANY EDIT to object immediately to file
// here: copy strings
// only persist in 'save' step
var accounts = AudibleApiStorage.GetAccountsSettings().Accounts;
if (!accounts.Any())
return;
@ -92,42 +96,21 @@ namespace LibationWinForms.Dialogs
private void cancelBtn_Click(object sender, EventArgs e) => this.Close();
#region TEMP
private void saveBtn_Click(object sender, EventArgs e)
{
var accounts = AudibleApiStorage.GetAccountsSettings()
.Accounts;
// find added
// validate
// find deleted
// find edited
// validate
// persist
}
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
//if (e.ColumnIndex == dataGridView1.Columns["ItemID"].Index) //if the ItemID-cell is edited
//{
// dataGridView1.Rows[e.RowIndex].ReadOnly = true; // set all row as read-only
// dataGridView1.Rows[e.RowIndex].Cells["ItemID"].ReadOnly = false; //except ItemID-cell
//}
}
private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
//if (dataGridView1.Rows[e.RowIndex].Cells["ItemID"].Value != null)
//{
// dataGridView1.Rows[e.RowIndex].ReadOnly = true; // set all row as read-only
// dataGridView1.Rows[e.RowIndex].Cells["ItemID"].ReadOnly = false; //except ItemID-cell
//}
}
private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
}
#endregion
}
}

View File

@ -283,6 +283,7 @@
}
#endregion
private System.Windows.Forms.Panel gridPanel;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem importToolStripMenuItem;
@ -309,4 +310,3 @@
private System.Windows.Forms.ToolStripMenuItem accountsToolStripMenuItem;
}
}

View File

@ -44,6 +44,7 @@
this.editQuickFiltersToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.settingsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.accountsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.basicSettingsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.advancedSettingsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
@ -162,7 +163,7 @@
//
this.editQuickFiltersToolStripMenuItem.Name = "editQuickFiltersToolStripMenuItem";
this.editQuickFiltersToolStripMenuItem.Size = new System.Drawing.Size(256, 22);
this.editQuickFiltersToolStripMenuItem.Text = "&Edit quick filters";
this.editQuickFiltersToolStripMenuItem.Text = "&Edit quick filters...";
//
// toolStripSeparator1
//
@ -172,23 +173,30 @@
// settingsToolStripMenuItem
//
this.settingsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.accountsToolStripMenuItem,
this.basicSettingsToolStripMenuItem,
this.advancedSettingsToolStripMenuItem});
this.settingsToolStripMenuItem.Name = "settingsToolStripMenuItem";
this.settingsToolStripMenuItem.Size = new System.Drawing.Size(61, 20);
this.settingsToolStripMenuItem.Text = "&Settings";
//
// accountsToolStripMenuItem
//
this.accountsToolStripMenuItem.Name = "accountsToolStripMenuItem";
this.accountsToolStripMenuItem.Size = new System.Drawing.Size(181, 22);
this.accountsToolStripMenuItem.Text = "&Accounts...";
//
// basicSettingsToolStripMenuItem
//
this.basicSettingsToolStripMenuItem.Name = "basicSettingsToolStripMenuItem";
this.basicSettingsToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.basicSettingsToolStripMenuItem.Text = "&Basic Settings";
this.basicSettingsToolStripMenuItem.Size = new System.Drawing.Size(181, 22);
this.basicSettingsToolStripMenuItem.Text = "&Basic Settings...";
//
// advancedSettingsToolStripMenuItem
//
this.advancedSettingsToolStripMenuItem.Name = "advancedSettingsToolStripMenuItem";
this.advancedSettingsToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.advancedSettingsToolStripMenuItem.Text = "&Advanced Settings";
this.advancedSettingsToolStripMenuItem.Size = new System.Drawing.Size(181, 22);
this.advancedSettingsToolStripMenuItem.Text = "Ad&vanced Settings...";
//
// statusStrip1
//
@ -212,7 +220,7 @@
// springLbl
//
this.springLbl.Name = "springLbl";
this.springLbl.Size = new System.Drawing.Size(232, 17);
this.springLbl.Size = new System.Drawing.Size(233, 17);
this.springLbl.Spring = true;
//
// backupsCountsLbl
@ -224,7 +232,7 @@
// pdfsCountsLbl
//
this.pdfsCountsLbl.Name = "pdfsCountsLbl";
this.pdfsCountsLbl.Size = new System.Drawing.Size(219, 17);
this.pdfsCountsLbl.Size = new System.Drawing.Size(218, 17);
this.pdfsCountsLbl.Text = "| PDFs: NOT d/l\'ed: {0} Downloaded: {1}";
//
// addFilterBtn
@ -262,6 +270,7 @@
}
#endregion
private System.Windows.Forms.Panel gridPanel;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem importToolStripMenuItem;
@ -285,6 +294,6 @@
private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
private System.Windows.Forms.ToolStripMenuItem basicSettingsToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem advancedSettingsToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem accountsToolStripMenuItem;
}
}

View File

@ -34,11 +34,11 @@ namespace AccountsTests
{
var json = @"
{
""AccountsSettings"": []
""Accounts"": []
}
".Trim();
var accounts = Accounts.FromJson(json);
accounts.AccountsSettings.Count.Should().Be(0);
var accountsSettings = AccountsSettings.FromJson(json);
accountsSettings.Accounts.Count.Should().Be(0);
}
[TestMethod]
@ -46,7 +46,7 @@ namespace AccountsTests
{
var json = @"
{
""AccountsSettings"": [
""Accounts"": [
{
""AccountId"": ""cng"",
""AccountName"": ""my main login"",
@ -56,10 +56,10 @@ namespace AccountsTests
]
}
".Trim();
var accounts = Accounts.FromJson(json);
accounts.AccountsSettings.Count.Should().Be(1);
accounts.AccountsSettings[0].AccountId.Should().Be("cng");
accounts.AccountsSettings[0].IdentityTokens.Should().BeNull();
var accountsSettings = AccountsSettings.FromJson(json);
accountsSettings.Accounts.Count.Should().Be(1);
accountsSettings.Accounts[0].AccountId.Should().Be("cng");
accountsSettings.Accounts[0].IdentityTokens.Should().BeNull();
}
[TestMethod]
@ -69,7 +69,7 @@ namespace AccountsTests
var json = $@"
{{
""AccountsSettings"": [
""Accounts"": [
{{
""AccountId"": ""cng"",
""AccountName"": ""my main login"",
@ -79,11 +79,11 @@ namespace AccountsTests
]
}}
".Trim();
var accounts = Accounts.FromJson(json);
accounts.AccountsSettings.Count.Should().Be(1);
accounts.AccountsSettings[0].AccountId.Should().Be("cng");
accounts.AccountsSettings[0].IdentityTokens.Should().NotBeNull();
accounts.AccountsSettings[0].IdentityTokens.ExistingAccessToken.TokenValue.Should().Be(AccessTokenValue);
var accountsSettings = AccountsSettings.FromJson(json);
accountsSettings.Accounts.Count.Should().Be(1);
accountsSettings.Accounts[0].AccountId.Should().Be("cng");
accountsSettings.Accounts[0].IdentityTokens.Should().NotBeNull();
accountsSettings.Accounts[0].IdentityTokens.ExistingAccessToken.TokenValue.Should().Be(AccessTokenValue);
}
}
@ -96,7 +96,7 @@ namespace AccountsTests
var id = JsonConvert.SerializeObject(Identity.Empty, Identity.GetJsonSerializerSettings());
var jsonIn = $@"
{{
""AccountsSettings"": [
""Accounts"": [
{{
""AccountId"": ""cng"",
""AccountName"": ""my main login"",
@ -106,12 +106,12 @@ namespace AccountsTests
]
}}
".Trim();
var accounts = Accounts.FromJson(jsonIn);
var accountsSettings = AccountsSettings.FromJson(jsonIn);
var jsonOut = accounts.ToJson();
var jsonOut = accountsSettings.ToJson();
jsonOut.Should().Be(@"
{
""AccountsSettings"": [
""Accounts"": [
{
""AccountId"": ""cng"",
""AccountName"": ""my main login"",
@ -162,12 +162,12 @@ namespace AccountsTests
public void create_file()
{
File.Exists(TestFile).Should().BeFalse();
var accounts = new Accounts();
_ = new AccountsPersister(accounts, TestFile);
var accountsSettings = new AccountsSettings();
_ = new AccountsSettingsPersister(accountsSettings, TestFile);
File.Exists(TestFile).Should().BeTrue();
File.ReadAllText(TestFile).Should().Be(@"
{
""AccountsSettings"": []
""Accounts"": []
}
".Trim());
}
@ -179,12 +179,12 @@ namespace AccountsTests
WriteToTestFile("foo");
File.Exists(TestFile).Should().BeTrue();
var accounts = new Accounts();
_ = new AccountsPersister(accounts, TestFile);
var accountsSettings = new AccountsSettings();
_ = new AccountsSettingsPersister(accountsSettings, TestFile);
File.Exists(TestFile).Should().BeTrue();
File.ReadAllText(TestFile).Should().Be(@"
{
""AccountsSettings"": []
""Accounts"": []
}
".Trim());
}
@ -192,16 +192,16 @@ namespace AccountsTests
[TestMethod]
public void save_multiple_children()
{
var accounts = new Accounts();
accounts.Add(new Account("a0") { AccountName = "n0" });
accounts.Add(new Account("a1") { AccountName = "n1" });
var accountsSettings = new AccountsSettings();
accountsSettings.Add(new Account("a0") { AccountName = "n0" });
accountsSettings.Add(new Account("a1") { AccountName = "n1" });
// dispose to cease auto-updates
using (var p = new AccountsPersister(accounts, TestFile)) { }
using (var p = new AccountsSettingsPersister(accountsSettings, TestFile)) { }
var persister = new AccountsPersister(TestFile);
persister.Accounts.AccountsSettings.Count.Should().Be(2);
persister.Accounts.AccountsSettings[1].AccountName.Should().Be("n1");
var persister = new AccountsSettingsPersister(TestFile);
persister.AccountsSettings.Accounts.Count.Should().Be(2);
persister.AccountsSettings.Accounts[1].AccountName.Should().Be("n1");
}
[TestMethod]
@ -210,14 +210,14 @@ namespace AccountsTests
var id = new Identity(usLocale);
var idJson = JsonConvert.SerializeObject(id, Identity.GetJsonSerializerSettings());
var accounts = new Accounts();
accounts.Add(new Account("a0") { AccountName = "n0", IdentityTokens = id });
var accountsSettings = new AccountsSettings();
accountsSettings.Add(new Account("a0") { AccountName = "n0", IdentityTokens = id });
// dispose to cease auto-updates
using (var p = new AccountsPersister(accounts, TestFile)) { }
using (var p = new AccountsSettingsPersister(accountsSettings, TestFile)) { }
var persister = new AccountsPersister(TestFile);
var acct = persister.Accounts.AccountsSettings[0];
var persister = new AccountsSettingsPersister(TestFile);
var acct = persister.AccountsSettings.Accounts[0];
acct.AccountName.Should().Be("n0");
acct.Locale.CountryCode.Should().Be("us");
}
@ -231,22 +231,22 @@ namespace AccountsTests
public void save_1_account()
{
// create initial file
using (var p = new AccountsPersister(new Accounts(), TestFile)) { }
using (var p = new AccountsSettingsPersister(new AccountsSettings(), TestFile)) { }
// load file. create account
using (var p = new AccountsPersister(TestFile))
using (var p = new AccountsSettingsPersister(TestFile))
{
var idIn = new Identity(usLocale);
var acctIn = new Account("a0") { AccountName = "n0", IdentityTokens = idIn };
p.Accounts.Add(acctIn);
p.AccountsSettings.Add(acctIn);
}
// re-load file. ensure account still exists
using (var p = new AccountsPersister(TestFile))
using (var p = new AccountsSettingsPersister(TestFile))
{
p.Accounts.AccountsSettings.Count.Should().Be(1);
var acct0 = p.Accounts.AccountsSettings[0];
p.AccountsSettings.Accounts.Count.Should().Be(1);
var acct0 = p.AccountsSettings.Accounts[0];
acct0.AccountName.Should().Be("n0");
acct0.Locale.CountryCode.Should().Be("us");
}
@ -258,46 +258,46 @@ namespace AccountsTests
public void save_2_accounts()
{
// create initial file
using (var p = new AccountsPersister(new Accounts(), TestFile)) { }
using (var p = new AccountsSettingsPersister(new AccountsSettings(), TestFile)) { }
// load file. create account 0
using (var p = new AccountsPersister(TestFile))
using (var p = new AccountsSettingsPersister(TestFile))
{
var idIn = new Identity(usLocale);
var acctIn = new Account("a0") { AccountName = "n0", IdentityTokens = idIn };
p.Accounts.Add(acctIn);
p.AccountsSettings.Add(acctIn);
}
// re-load file. ensure account still exists
using (var p = new AccountsPersister(TestFile))
using (var p = new AccountsSettingsPersister(TestFile))
{
p.Accounts.AccountsSettings.Count.Should().Be(1);
p.AccountsSettings.Accounts.Count.Should().Be(1);
var acct0 = p.Accounts.AccountsSettings[0];
var acct0 = p.AccountsSettings.Accounts[0];
acct0.AccountName.Should().Be("n0");
acct0.Locale.CountryCode.Should().Be("us");
}
// load file. create account 1
using (var p = new AccountsPersister(TestFile))
using (var p = new AccountsSettingsPersister(TestFile))
{
var idIn = new Identity(ukLocale);
var acctIn = new Account("a1") { AccountName = "n1", IdentityTokens = idIn };
p.Accounts.Add(acctIn);
p.AccountsSettings.Add(acctIn);
}
// re-load file. ensure both accounts still exist
using (var p = new AccountsPersister(TestFile))
using (var p = new AccountsSettingsPersister(TestFile))
{
p.Accounts.AccountsSettings.Count.Should().Be(2);
p.AccountsSettings.Accounts.Count.Should().Be(2);
var acct0 = p.Accounts.AccountsSettings[0];
var acct0 = p.AccountsSettings.Accounts[0];
acct0.AccountName.Should().Be("n0");
acct0.Locale.CountryCode.Should().Be("us");
var acct1 = p.Accounts.AccountsSettings[1];
var acct1 = p.AccountsSettings.Accounts[1];
acct1.AccountName.Should().Be("n1");
acct1.Locale.CountryCode.Should().Be("uk");
}
@ -307,23 +307,23 @@ namespace AccountsTests
public void update_Account_field_just_added()
{
// create initial file
using (var p = new AccountsPersister(new Accounts(), TestFile)) { }
using (var p = new AccountsSettingsPersister(new AccountsSettings(), TestFile)) { }
// load file. create 2 accounts
using (var p = new AccountsPersister(TestFile))
using (var p = new AccountsSettingsPersister(TestFile))
{
var id1 = new Identity(usLocale);
var acct1 = new Account("a0") { AccountName = "n0", IdentityTokens = id1 };
p.Accounts.Add(acct1);
p.AccountsSettings.Add(acct1);
// update just-added item. note: this is different than the subscription which happens on initial collection load. ensure this works also
acct1.AccountName = "new";
}
// verify save property
using (var p = new AccountsPersister(TestFile))
using (var p = new AccountsSettingsPersister(TestFile))
{
var acct0 = p.Accounts.AccountsSettings[0];
var acct0 = p.AccountsSettings.Accounts[0];
acct0.AccountName.Should().Be("new");
}
}
@ -333,40 +333,40 @@ namespace AccountsTests
public void update_Account_field()
{
// create initial file
using (var p = new AccountsPersister(new Accounts(), TestFile)) { }
using (var p = new AccountsSettingsPersister(new AccountsSettings(), TestFile)) { }
// load file. create 2 accounts
using (var p = new AccountsPersister(TestFile))
using (var p = new AccountsSettingsPersister(TestFile))
{
var id1 = new Identity(usLocale);
var acct1 = new Account("a0") { AccountName = "n0", IdentityTokens = id1 };
p.Accounts.Add(acct1);
p.AccountsSettings.Add(acct1);
var id2 = new Identity(ukLocale);
var acct2 = new Account("a1") { AccountName = "n1", IdentityTokens = id2 };
p.Accounts.Add(acct2);
p.AccountsSettings.Add(acct2);
}
// update AccountName on existing file
using (var p = new AccountsPersister(TestFile))
using (var p = new AccountsSettingsPersister(TestFile))
{
var acct0 = p.Accounts.AccountsSettings[0];
var acct0 = p.AccountsSettings.Accounts[0];
acct0.AccountName = "new";
}
// re-load file. ensure both accounts still exist
using (var p = new AccountsPersister(TestFile))
using (var p = new AccountsSettingsPersister(TestFile))
{
p.Accounts.AccountsSettings.Count.Should().Be(2);
p.AccountsSettings.Accounts.Count.Should().Be(2);
var acct0 = p.Accounts.AccountsSettings[0];
var acct0 = p.AccountsSettings.Accounts[0];
// new
acct0.AccountName.Should().Be("new");
// still here
acct0.Locale.CountryCode.Should().Be("us");
var acct1 = p.Accounts.AccountsSettings[1];
var acct1 = p.AccountsSettings.Accounts[1];
acct1.AccountName.Should().Be("n1");
acct1.Locale.CountryCode.Should().Be("uk");
}
@ -377,42 +377,42 @@ namespace AccountsTests
public void replace_identity()
{
// create initial file
using (var p = new AccountsPersister(new Accounts(), TestFile)) { }
using (var p = new AccountsSettingsPersister(new AccountsSettings(), TestFile)) { }
// load file. create 2 accounts
using (var p = new AccountsPersister(TestFile))
using (var p = new AccountsSettingsPersister(TestFile))
{
var id1 = new Identity(usLocale);
var acct1 = new Account("a0") { AccountName = "n0", IdentityTokens = id1 };
p.Accounts.Add(acct1);
p.AccountsSettings.Add(acct1);
var id2 = new Identity(ukLocale);
var acct2 = new Account("a1") { AccountName = "n1", IdentityTokens = id2 };
p.Accounts.Add(acct2);
p.AccountsSettings.Add(acct2);
}
// update identity on existing file
using (var p = new AccountsPersister(TestFile))
using (var p = new AccountsSettingsPersister(TestFile))
{
var id = new Identity(ukLocale);
var acct0 = p.Accounts.AccountsSettings[0];
var acct0 = p.AccountsSettings.Accounts[0];
acct0.IdentityTokens = id;
}
// re-load file. ensure both accounts still exist
using (var p = new AccountsPersister(TestFile))
using (var p = new AccountsSettingsPersister(TestFile))
{
p.Accounts.AccountsSettings.Count.Should().Be(2);
p.AccountsSettings.Accounts.Count.Should().Be(2);
var acct0 = p.Accounts.AccountsSettings[0];
var acct0 = p.AccountsSettings.Accounts[0];
// new
acct0.Locale.CountryCode.Should().Be("uk");
// still here
acct0.AccountName.Should().Be("n0");
var acct1 = p.Accounts.AccountsSettings[1];
var acct1 = p.AccountsSettings.Accounts[1];
acct1.AccountName.Should().Be("n1");
acct1.Locale.CountryCode.Should().Be("uk");
}
@ -424,42 +424,42 @@ namespace AccountsTests
public void update_identity_field()
{
// create initial file
using (var p = new AccountsPersister(new Accounts(), TestFile)) { }
using (var p = new AccountsSettingsPersister(new AccountsSettings(), TestFile)) { }
// load file. create 2 accounts
using (var p = new AccountsPersister(TestFile))
using (var p = new AccountsSettingsPersister(TestFile))
{
var id1 = new Identity(usLocale);
var acct1 = new Account("a0") { AccountName = "n0", IdentityTokens = id1 };
p.Accounts.Add(acct1);
p.AccountsSettings.Add(acct1);
var id2 = new Identity(ukLocale);
var acct2 = new Account("a1") { AccountName = "n1", IdentityTokens = id2 };
p.Accounts.Add(acct2);
p.AccountsSettings.Add(acct2);
}
// update identity on existing file
using (var p = new AccountsPersister(TestFile))
using (var p = new AccountsSettingsPersister(TestFile))
{
p.Accounts.AccountsSettings[0]
p.AccountsSettings.Accounts[0]
.IdentityTokens
.Update(new AccessToken("Atna|_NEW_", DateTime.Now.AddDays(1)));
}
// re-load file. ensure both accounts still exist
using (var p = new AccountsPersister(TestFile))
using (var p = new AccountsSettingsPersister(TestFile))
{
p.Accounts.AccountsSettings.Count.Should().Be(2);
p.AccountsSettings.Accounts.Count.Should().Be(2);
var acct0 = p.Accounts.AccountsSettings[0];
var acct0 = p.AccountsSettings.Accounts[0];
// new
acct0.IdentityTokens.ExistingAccessToken.TokenValue.Should().Be("Atna|_NEW_");
// still here
acct0.AccountName.Should().Be("n0");
acct0.Locale.CountryCode.Should().Be("us");
var acct1 = p.Accounts.AccountsSettings[1];
var acct1 = p.AccountsSettings.Accounts[1];
acct1.AccountName.Should().Be("n1");
acct1.Locale.CountryCode.Should().Be("uk");
}
@ -478,11 +478,11 @@ namespace AccountsTests
var idUk = new Identity(ukLocale);
var acct2 = new Account("cng") { IdentityTokens = idUk, AccountName = "bar" };
var accounts = new Accounts();
accounts.Add(acct1);
accounts.Add(acct2);
var accountsSettings = new AccountsSettings();
accountsSettings.Add(acct1);
accountsSettings.Add(acct2);
accounts.GetAccount("cng", "uk").AccountName.Should().Be("bar");
accountsSettings.GetAccount("cng", "uk").AccountName.Should().Be("bar");
}
}
@ -492,23 +492,23 @@ namespace AccountsTests
[TestMethod]
public void upsert_new()
{
var accounts = new Accounts();
accounts.AccountsSettings.Count.Should().Be(0);
var accountsSettings = new AccountsSettings();
accountsSettings.Accounts.Count.Should().Be(0);
accounts.Upsert("cng", "us");
accountsSettings.Upsert("cng", "us");
accounts.AccountsSettings.Count.Should().Be(1);
accounts.GetAccount("cng", "us").AccountId.Should().Be("cng");
accountsSettings.Accounts.Count.Should().Be(1);
accountsSettings.GetAccount("cng", "us").AccountId.Should().Be("cng");
}
[TestMethod]
public void upsert_exists()
{
var accounts = new Accounts();
var orig = accounts.Upsert("cng", "us");
var accountsSettings = new AccountsSettings();
var orig = accountsSettings.Upsert("cng", "us");
orig.AccountName = "foo";
var exists = accounts.Upsert("cng", "us");
var exists = accountsSettings.Upsert("cng", "us");
exists.AccountName.Should().Be("foo");
orig.Should().IsSameOrEqualTo(exists);
@ -521,28 +521,28 @@ namespace AccountsTests
[TestMethod]
public void delete_account()
{
var accounts = new Accounts();
var acct = accounts.Upsert("cng", "us");
accounts.AccountsSettings.Count.Should().Be(1);
var accountsSettings = new AccountsSettings();
var acct = accountsSettings.Upsert("cng", "us");
accountsSettings.Accounts.Count.Should().Be(1);
var removed = accounts.Delete(acct);
var removed = accountsSettings.Delete(acct);
removed.Should().BeTrue();
accounts.AccountsSettings.Count.Should().Be(0);
accountsSettings.Accounts.Count.Should().Be(0);
}
[TestMethod]
public void delete_where()
{
var accounts = new Accounts();
var acct = accounts.Upsert("cng", "us");
accounts.AccountsSettings.Count.Should().Be(1);
var accountsSettings = new AccountsSettings();
_ = accountsSettings.Upsert("cng", "us");
accountsSettings.Accounts.Count.Should().Be(1);
accounts.Delete("baz", "baz").Should().BeFalse();
accounts.AccountsSettings.Count.Should().Be(1);
accountsSettings.Delete("baz", "baz").Should().BeFalse();
accountsSettings.Accounts.Count.Should().Be(1);
accounts.Delete("cng", "us").Should().BeTrue();
accounts.AccountsSettings.Count.Should().Be(0);
accountsSettings.Delete("cng", "us").Should().BeTrue();
accountsSettings.Accounts.Count.Should().Be(0);
}
[TestMethod]
@ -550,26 +550,26 @@ namespace AccountsTests
{
Account acct;
using (var p = new AccountsPersister(new Accounts(), TestFile))
using (var p = new AccountsSettingsPersister(new AccountsSettings(), TestFile))
{
acct = p.Accounts.Upsert("foo", "us");
p.Accounts.AccountsSettings.Count.Should().Be(1);
acct = p.AccountsSettings.Upsert("foo", "us");
p.AccountsSettings.Accounts.Count.Should().Be(1);
acct.AccountName = "old";
}
using (var p = new AccountsPersister(new Accounts(), TestFile))
using (var p = new AccountsSettingsPersister(new AccountsSettings(), TestFile))
{
p.Accounts.Delete(acct);
p.Accounts.AccountsSettings.Count.Should().Be(0);
p.AccountsSettings.Delete(acct);
p.AccountsSettings.Accounts.Count.Should().Be(0);
}
using (var p = new AccountsPersister(new Accounts(), TestFile))
using (var p = new AccountsSettingsPersister(new AccountsSettings(), TestFile))
{
File.ReadAllText(TestFile).Should().Be("{\r\n \"AccountsSettings\": []\r\n}".Trim());
File.ReadAllText(TestFile).Should().Be("{\r\n \"Accounts\": []\r\n}".Trim());
acct.AccountName = "new";
File.ReadAllText(TestFile).Should().Be("{\r\n \"AccountsSettings\": []\r\n}".Trim());
File.ReadAllText(TestFile).Should().Be("{\r\n \"Accounts\": []\r\n}".Trim());
}
}
}
@ -581,31 +581,31 @@ namespace AccountsTests
[TestMethod]
public void violate_validation()
{
var accounts = new Accounts();
var accountsSettings = new AccountsSettings();
var idIn = new Identity(usLocale);
var a1 = new Account("a") { AccountName = "one", IdentityTokens = idIn };
accounts.Add(a1);
accountsSettings.Add(a1);
var a2 = new Account("a") { AccountName = "two", IdentityTokens = idIn };
// violation: validate()
Assert.ThrowsException<InvalidOperationException>(() => accounts.Add(a2));
Assert.ThrowsException<InvalidOperationException>(() => accountsSettings.Add(a2));
}
[TestMethod]
public void identity_violate_validation()
{
var accounts = new Accounts();
var accountsSettings = new AccountsSettings();
var idIn = new Identity(usLocale);
var a1 = new Account("a") { AccountName = "one", IdentityTokens = idIn };
accounts.Add(a1);
accountsSettings.Add(a1);
var a2 = new Account("a") { AccountName = "two" };
accounts.Add(a2);
accountsSettings.Add(a2);
// violation: GetAccount.SingleOrDefault
Assert.ThrowsException<InvalidOperationException>(() => a2.IdentityTokens = idIn);