예제 #1
0
        public void AddAccount_WhenAccountsSecurityHasNotBeenAdded_ThrowsException()
        {
            // Create a new, empty book.
            var book = new Book();

            // Build a new, valid account.
            var account = TestUtils.CreateValidAccount();

            // Assert that trying to add the account without first adding the security throws an InvalidOperationException.
            Assert.That(() => book.AddAccount(account), Throws.InstanceOf <InvalidOperationException>());
        }
예제 #2
0
        public void Replay_WhenSavePointIsNull_ReplaysFromTheBeginning()
        {
            // Create a new, valid book.
            var book = new Book();

            // Create a new, valid account.
            var account = TestUtils.CreateValidAccount();

            // Create a new, valid transaction.
            var transaction = TestUtils.CreateValidTransaction(account);

            // Get two valid securities.
            var security1 = TestUtils.TestCurrency;
            var security2 = TestUtils.TestStock;

            // Create a new, valid price quote based on the above securities.
            var priceQuote = TestUtils.CreateValidPriceQuote();

            // Add and immediately remove the security, account, and transaction from the book.
            book.AddSecurity(security1);
            book.AddSecurity(security2);
            book.AddAccount(account);
            book.AddPriceQuote(priceQuote);
            book.RemovePriceQuote(priceQuote);
            book.AddTransaction(transaction);
            book.RemoveTransaction(transaction);
            book.RemoveAccount(account);
            book.RemoveSecurity(security2);
            book.RemoveSecurity(security1);

            // Create a new mock data adapter and replay the book changes into the mock.
            var destination = new MockSaver();

            book.Replay(destination, null);

            // Assert that the mock received the proper additional and removals of each component.
            Assert.That(destination.SecurityAdditions.Count, Is.EqualTo(2));
            Assert.That(destination.SecurityRemovals.Count, Is.EqualTo(2));
            Assert.That(destination.PriceQuoteAdditions.Count, Is.EqualTo(1));
            Assert.That(destination.PriceQuoteRemovals.Count, Is.EqualTo(1));
            Assert.That(destination.AccountAdditions.Count, Is.EqualTo(1));
            Assert.That(destination.AccountRemovals.Count, Is.EqualTo(1));
            Assert.That(destination.TransactionAdditions.Count, Is.EqualTo(1));
            Assert.That(destination.TransactionRemovals.Count, Is.EqualTo(1));
        }
예제 #3
0
        public void Replay_WhenSavePointIsNull_ReplaysFromTheBeginning()
        {
            // Create a new, valid book.
            var book = new Book();

            // Create a new, valid account.
            var account = TestUtils.CreateValidAccount();

            // Create a new, valid transaction.
            var transaction = TestUtils.CreateValidTransaction(account);

            // Get two valid securities.
            var security1 = TestUtils.TestCurrency;
            var security2 = TestUtils.TestStock;

            // Create a new, valid price quote based on the above securities.
            var priceQuote = TestUtils.CreateValidPriceQuote();

            // Add and immediately remove the security, account, and transaction from the book.
            book.AddSecurity(security1);
            book.AddSecurity(security2);
            book.AddAccount(account);
            book.AddPriceQuote(priceQuote);
            book.RemovePriceQuote(priceQuote);
            book.AddTransaction(transaction);
            book.RemoveTransaction(transaction);
            book.RemoveAccount(account);
            book.RemoveSecurity(security2);
            book.RemoveSecurity(security1);

            // Create a new mock data adapter and replay the book changes into the mock.
            var destination = new MockSaver();
            book.Replay(destination, null);

            // Assert that the mock received the proper additional and removals of each component.
            Assert.That(destination.SecurityAdditions.Count, Is.EqualTo(2));
            Assert.That(destination.SecurityRemovals.Count, Is.EqualTo(2));
            Assert.That(destination.PriceQuoteAdditions.Count, Is.EqualTo(1));
            Assert.That(destination.PriceQuoteRemovals.Count, Is.EqualTo(1));
            Assert.That(destination.AccountAdditions.Count, Is.EqualTo(1));
            Assert.That(destination.AccountRemovals.Count, Is.EqualTo(1));
            Assert.That(destination.TransactionAdditions.Count, Is.EqualTo(1));
            Assert.That(destination.TransactionRemovals.Count, Is.EqualTo(1));
        }
예제 #4
0
        /// <inheritdoc/>
        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)
                {
                    Date = date,
                };

                foreach (var s in t.Elements("Split"))
                {
                    var split = transaction.AddSplit();

                    var accountId = (Guid)s.Attribute("accountId");
                    var account   = accounts[accountId];
                    split.Account = account;

                    var splitSecurityId = (Guid?)s.Attribute("securityId");
                    var splitSecurity   = securities[splitSecurityId ?? securityId];
                    split.Security = splitSecurity;

                    var amount = (long)s.Attribute("amount");
                    split.Amount = amount;

                    var transactionAmount = (long?)s.Attribute("transactionAmount");
                    split.TransactionAmount = splitSecurity != security ? transactionAmount.Value : transactionAmount ?? amount;

                    var dateCleared = (DateTime?)s.Attribute("dateCleared");
                    split.DateCleared = dateCleared;

                    var reconciled = (bool)s.Attribute("reconciled");
                    split.IsReconciled = reconciled;
                }

                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);
        }
예제 #5
0
        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;
        }
예제 #6
0
        public void AddAccount_WhenAccountsSecurityHasNotBeenAdded_ThrowsException()
        {
            // Create a new, empty book.
            var book = new Book();

            // Build a new, valid account.
            var account = TestUtils.CreateValidAccount();

            // Assert that trying to add the account without first adding the security throws an InvalidOperationException.
            Assert.That(() => book.AddAccount(account), Throws.InstanceOf<InvalidOperationException>());
        }