public void AddsAllSymbolsWhenNoneExist()
            {
                //// SETUP

                // Test Data
                var stock1 = new Stock { Ticker = "FLWS", CompanyName = "1-800 FLOWERS.COM" };
                var stock2 = new Stock { Ticker = "FCTY", CompanyName = "1st Century Bancshares" };
                var stock3 = new Stock { Ticker = "FCCY", CompanyName = "1st Constitution Bancorp (NJ)" };
                var expectedResults = new List<Stock> { stock1, stock2, stock3 };

                // Create a mock generic repository.
                var mockGenericRepository = new Mock<IRepository<Stock>>();
                mockGenericRepository.Setup(mock => mock.FindBy(It.IsAny<Expression<Func<Stock, bool>>>())).Returns((Stock)null);

                // Setup target
                var target = new StockRepository(mockGenericRepository.Object);

                // EXECUTE
                var actualCountAdded = target.TryAddNewStocks(expectedResults);

                // VERIFY
                Assert.AreEqual(expectedResults.Count, actualCountAdded);
                mockGenericRepository.Verify(mock => mock.Add(expectedResults), Times.Once());
                mockGenericRepository.Verify(mock => mock.Add(It.IsAny<Stock>()), Times.Never());
            }
 /// <summary>
 /// Safely returns a stock ID that can be used in comparison-related logic.
 /// </summary>
 /// <param name="stock">The stock.</param>
 /// <returns>
 /// Returns the stock's ID when the stock is not null.  Otherwise, returns zero.
 /// </returns>
 private static int GetStockIdForComparison(Stock stock)
 {
     return stock != null ? stock.Id : 0;
 }
            public void AddsNoSymbolsWhenAllExist()
            {
                //// SETUP

                // Create two record sets that are identical
                var stock1 = new Stock { Ticker = "FLWS", CompanyName = "1-800 FLOWERS.COM" };
                var stock2 = new Stock { Ticker = "FCTY", CompanyName = "1st Century Bancshares" };
                var stock3 = new Stock { Ticker = "FCCY", CompanyName = "1st Constitution Bancorp (NJ)" };
                var preexistingRecords = new List<Stock> { stock1, stock2, stock3 };

                // Create a mock generic repository.
                var mockGenericRepository = new Mock<IRepository<Stock>>();

                var numFindByCalls = 0;
                mockGenericRepository.Setup(mock => mock.FindBy(It.IsAny<Expression<Func<Stock, bool>>>())).Returns(() =>
                {
                    numFindByCalls++;

                    switch (numFindByCalls)
                    {
                        case 1:
                            // Stock 1 is preexisting
                            return stock1;

                        case 2:
                            // Stock 2 is preexisting
                            return stock2;

                        case 3:
                            // Stock 3 does not exist
                            return stock3;

                        default:
                            throw new InvalidOperationException("Too many calls to mock.FindBy.");
                    }
                });

                // Setup target
                var target = new StockRepository(mockGenericRepository.Object);

                // EXECUTE
                var actualCountAdded = target.TryAddNewStocks(preexistingRecords);

                // VERIFY
                Assert.AreEqual(0, actualCountAdded);
                mockGenericRepository.Verify(mock => mock.Add(It.IsAny<Stock>()), Times.Never());
                mockGenericRepository.Verify(mock => mock.Add(It.IsAny<IEnumerable<Stock>>()), Times.Never());
                mockGenericRepository.Verify(mock => mock.FindBy(It.IsAny<Expression<Func<Stock, bool>>>()), Times.Exactly(3));
            }
            public void AddsOnlyNewSymbolsWhenSomeExist()
            {
                //// SETUP

                //// Test Data

                var stock1 = new Stock { Ticker = "FLWS", CompanyName = "1-800 FLOWERS.COM" };
                var stock2 = new Stock { Ticker = "FCTY", CompanyName = "1st Century Bancshares" };
                var stock3 = new Stock { Ticker = "FCCY", CompanyName = "1st Constitution Bancorp (NJ)" };
                var stock4 = new Stock { Ticker = "SRCE", CompanyName = "1st Source Corporation" };

                var preexistingRecords = new List<Stock> { stock1, stock2 };
                var newRecords = new List<Stock> { stock3, stock4 };

                var testData = new List<Stock>();
                testData.AddRange(preexistingRecords);
                testData.AddRange(newRecords);

                // Create a mock generic repository.
                var mockGenericRepository = new Mock<IRepository<Stock>>();

                var numFindByCalls = 0;
                mockGenericRepository.Setup(mock => mock.FindBy(It.IsAny<Expression<Func<Stock, bool>>>())).Returns(() =>
                    {
                        numFindByCalls++;

                        switch (numFindByCalls)
                        {
                            case 1:
                                // Stock 1 is preexisting
                                 return stock1;

                            case 2:
                                // Stock 2 is preexisting
                                return stock2;

                            case 3:
                                // Stock 3 does not exist
                                return (Stock)null;

                            case 4:
                                // Stock 4 does not exist
                                return (Stock)null;

                            default:
                                throw new InvalidOperationException("Too many calls to mock.FindBy.");
                        }
                    });

                // Setup target
                var target = new StockRepository(mockGenericRepository.Object);

                // EXECUTE
                var actualCountAdded = target.TryAddNewStocks(testData);

                // VERIFY
                Assert.AreEqual(newRecords.Count, actualCountAdded);
                mockGenericRepository.Verify(mock => mock.Add(newRecords));
                mockGenericRepository.Verify(mock => mock.Add(It.IsAny<Stock>()), Times.Never());
                mockGenericRepository.Verify(mock => mock.FindBy(It.IsAny<Expression<Func<Stock, bool>>>()), Times.Exactly(4));
            }
        /// <summary>
        /// Determines whether the specified Stock is equal to this instance.
        /// </summary>
        /// <param name="other">The other stock.</param>
        /// <returns>
        /// True, when the two are considered to be equal.
        /// </returns>
        public virtual bool Equals(Stock other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }

            if (ReferenceEquals(this, other))
            {
                return true;
            }

            return object.Equals(other.Ticker, this.Ticker);
        }