public void NewTransaction() { var transaction = new Transaction(Guid.NewGuid(), this.book.Securities.First()); using (var tLock = transaction.Lock()) { transaction.SetDate(DateTime.Today.ToUniversalTime(), tLock); var split1 = transaction.AddSplit(tLock); var split2 = transaction.AddSplit(tLock); split1.SetAccount(this.account, tLock); split1.SetSecurity(transaction.BaseSecurity, tLock); split2.SetSecurity(transaction.BaseSecurity, tLock); } this.transactionEditor.SetSplit(transaction.Splits[0]); this.transactionIsNew = true; }
public void GivenATransactionWithBaseSecurityAndTheFollowingSplits(string transactionName, string baseSecurity, Table table) { Assume.That(table.Header, Has.Member("Account")); Assume.That(table.Header, Has.Member("Security")); Assume.That(table.Header, Has.Member("Amount")); var accounts = (from a in table.Rows group a by a["Account"] into g let acctName = g.Key let account = (Account)ScenarioContext.Current[acctName] select new { acctName, account }).ToDictionary(a => a.acctName, a => a.account); var securities = (from a in table.Rows group a by a["Security"] into g let secName = g.Key let security = (Security)ScenarioContext.Current[secName] select new { secName, security }).ToDictionary(a => a.secName, a => a.security); Security transactionSecurity; if (string.IsNullOrEmpty(baseSecurity)) { var firstAccount = accounts[table.Rows[0]["Account"]]; transactionSecurity = firstAccount.Security; } else { transactionSecurity = (Security)ScenarioContext.Current[baseSecurity]; } var transaction = new Transaction( transactionId: Guid.NewGuid(), baseSecurity: transactionSecurity); var securityCount = (from a in accounts group a.Value by a.Value.Security into g select g.Key).Count(); if (securityCount > 1) { Assume.That(table.Header, Has.Member("Transaction Amount")); } using (var @lock = transaction.Lock()) { foreach (var row in table.Rows) { var account = accounts[row["Account"]]; var security = securities[row["Security"]]; var amount = long.Parse(row["Amount"]); var transactionAmount = securityCount > 1 ? long.Parse(row["Transaction Amount"]) : amount; var split = transaction.AddSplit(@lock); split.SetAccount(account, @lock); split.SetSecurity(security, @lock); split.SetAmount(amount, @lock); split.SetTransactionAmount(transactionAmount, @lock); } } ScenarioContext.Current.Add(transactionName, transaction); }
protected override Book Load(Uri uri) { var book = new Book(); var doc = XDocument.Load(uri.ToString()); var securities = new Dictionary<Guid, Security>(); var accounts = new Dictionary<Guid, Account>(); foreach (var s in doc.Element("Book").Element("Securities").Elements("Security")) { var f = s.Element("Format"); var decimalDigits = (int)f.Attribute("decimalDigits"); var decimalSeparator = (string)f.Attribute("decimalSeparator"); var groupSeparator = (string)f.Attribute("groupSeparator"); var groupSizes = ((string)f.Attribute("groupSizes")).Split(',').Select(g => int.Parse(g.Trim())); var positiveFormat = (PositiveFormat)Enum.Parse(typeof(PositiveFormat), (string)f.Attribute("positiveFormat")); var negativeFormat = (NegativeFormat)Enum.Parse(typeof(NegativeFormat), (string)f.Attribute("negativeFormat")); var currencySymbol = (string)f.Attribute("symbol"); var format = new CurrencyFormat(decimalDigits, decimalSeparator, groupSeparator, groupSizes, currencySymbol, positiveFormat, negativeFormat); var securityId = (Guid)s.Attribute("id"); var securityType = (SecurityType)Enum.Parse(typeof(SecurityType), (string)s.Attribute("type")); var name = (string)s.Attribute("name"); var symbol = (string)s.Attribute("symbol"); var fractionTraded = (int)s.Attribute("fractionTraded"); var security = new Security(securityId, securityType, name, symbol, format, fractionTraded); securities.Add(security.SecurityId, security); book.AddSecurity(security); } foreach (var a in doc.Element("Book").Element("Accounts").Elements("Account")) { var accountId = (Guid)a.Attribute("id"); var accountType = (AccountType)Enum.Parse(typeof(AccountType), (string)a.Attribute("type")); Security security = null; var securityAttr = a.Attribute("securityId"); if (securityAttr != null) { security = securities[(Guid)securityAttr]; } Account parentAccount = null; var parentAttr = a.Attribute("parentAccountId"); if (parentAttr != null) { parentAccount = accounts[(Guid)parentAttr]; } var name = (string)a.Attribute("name"); var smallestFraction = (int?)a.Attribute("smallestFraction"); var account = new Account(accountId, accountType, security, parentAccount, name, smallestFraction); accounts.Add(account.AccountId, account); book.AddAccount(account); } foreach (var t in doc.Element("Book").Element("Transactions").Elements("Transaction")) { var transactionId = (Guid)t.Attribute("id"); var securityId = (Guid)t.Attribute("securityId"); var security = securities[securityId]; var date = (DateTime)t.Attribute("date"); var transaction = new Transaction(transactionId, security); using (var tlock = transaction.Lock()) { transaction.SetDate(date, tlock); foreach (var s in t.Elements("Split")) { var split = transaction.AddSplit(tlock); var accountId = (Guid)s.Attribute("accountId"); var account = accounts[accountId]; split.SetAccount(account, tlock); var splitSecurityId = (Guid)s.Attribute("securityId"); var splitSecurity = securities[splitSecurityId]; split.SetSecurity(splitSecurity, tlock); var amount = (long)s.Attribute("amount"); split.SetAmount(amount, tlock); var transactionAmount = (long)s.Attribute("transactionAmount"); split.SetTransactionAmount(transactionAmount, tlock); var dateCleared = (DateTime?)s.Attribute("dateCleared"); split.SetDateCleared(dateCleared, tlock); var reconciled = (bool)s.Attribute("reconciled"); split.SetIsReconciled(reconciled, tlock); } } book.AddTransaction(transaction); } foreach (var p in doc.Element("Book").Element("PriceQuotes").Elements("PriceQuote")) { var priceQuoteId = (Guid)p.Attribute("id"); var dateTime = (DateTime)p.Attribute("date"); var securityId = (Guid)p.Attribute("securityId"); var security = securities[securityId]; var quantity = (long)p.Attribute("quantity"); var currencyId = (Guid)p.Attribute("currencyId"); var currency = securities[currencyId]; var price = (long)p.Attribute("price"); var source = (string)p.Attribute("source"); var priceQuote = new PriceQuote(priceQuoteId, dateTime, security, quantity, currency, price, source); book.AddPriceQuote(priceQuote); } foreach (var s in doc.Element("Book").Element("Settings").Elements("Setting")) { var key = (string)s.Attribute("key"); var value = (string)s.Attribute("value"); book.SetSetting(key, value); } return book; }
public void RemoveSecurity_WhenSecurityIsUsedInSplits_ThrowsException() { // Create a new, valid book. var book = TestUtils.CreateValidBook(); // Create a new account with no security. var account = new Account( Guid.NewGuid(), // OK AccountType.Balance, // OK null, // OK null, // OK "OK_NAME", null); // OK book.AddAccount(account); // Create a transaction that uses this split, but not as the var transaction = new Transaction(Guid.NewGuid(), TestUtils.TestCurrency); using (var tLock = transaction.Lock()) { var split1 = transaction.AddSplit(tLock); split1.SetAccount(account, tLock); split1.SetSecurity(TestUtils.TestCurrency, tLock); split1.SetAmount(0, tLock); } book.AddTransaction(transaction); // Assert that trying to remove the security while the account depends on it throws an InvalidOperationException. Assert.That(() => book.RemoveSecurity(TestUtils.TestCurrency), Throws.InstanceOf<InvalidOperationException>()); }
public void AddTransaction_WhenAnotherTransactionHasTheSameTransactionId_ThrowsException() { // Create a new, valid book. var book = TestUtils.CreateValidBook(); // Create a new, valid account and add it to the book. var account = TestUtils.CreateValidAccount(); book.AddAccount(account); // Create a new, valid transaction and add it to the book. var transaction1 = TestUtils.CreateValidTransaction(account); book.AddTransaction(transaction1); // Construct a new, valid transaction that shares its TransactionId with transaction1. var transaction2 = new Transaction( transaction1.TransactionId, TestUtils.TestCurrency); // OK using (var transactionLock = transaction2.Lock()) { var split = transaction2.AddSplit(transactionLock); split.SetAccount(account, transactionLock); split.SetSecurity(account.Security, transactionLock); } // Assert that trying to add the transaction throws an InvalidOperationException. Assert.That(() => book.AddTransaction(transaction2), Throws.InstanceOf<InvalidOperationException>()); }