public async void AddCountEntity_AddEnttiy_ReturnCountByBranch()
        {
            //Arrange
            StockRepository stubStockRepository = FakesFactory.CreateStockRepositoryFake();

            IBranch <StockEntity> sectorBranch = new RedisBranch <StockEntity>();

            sectorBranch.SetBranchId("BRANCH_SECTOR");
            sectorBranch.FilterBy(i => i.IsActive).GroupBy("Sector");
            stubStockRepository.AddBranch(sectorBranch);

            //Act
            StockEntity teslaEntity = new StockEntity("TESLA", StockSector.Technology, 229.00, 12.5);
            await stubStockRepository.AddAsync(teslaEntity);

            StockEntity microsoftEntity = new StockEntity("MICROSOFT", StockSector.Technology, 204.00, 9.5);
            await stubStockRepository.AddAsync(microsoftEntity);

            StockEntity appleEntity = new StockEntity("APPLE", StockSector.Technology, 294.21, 8.5);
            await stubStockRepository.AddAsync(appleEntity);

            long actualCount = await stubStockRepository.CountAsync("BRANCH_SECTOR", "Technology");

            //Assert
            Assert.Equal(3, actualCount);
        }
        public async void AddGetEntity_AddEnttiy_ReturnEntityById()
        {
            //Arrange
            StockRepository stubStockRepository = FakesFactory.CreateStockRepositoryFake();

            IBranch <StockEntity> allBranch = new RedisBranch <StockEntity>();

            allBranch.SetBranchId("BRANCH_ALL");
            allBranch.FilterBy(i => i.IsActive).GroupBy("All", i => "All");
            stubStockRepository.AddBranch(allBranch);

            //Act
            StockEntity teslaEntity = new StockEntity("TESLA", StockSector.Technology, 229.00, 12.5);
            await stubStockRepository.AddAsync(teslaEntity);

            StockEntity expectedTeslaEntity = await stubStockRepository.GetByIdAsync(teslaEntity.Id);

            //Assert
            Assert.Equal(expectedTeslaEntity.Id, teslaEntity.Id);
            Assert.Equal(expectedTeslaEntity.Name, teslaEntity.Name);
            Assert.Equal(expectedTeslaEntity.Sector, teslaEntity.Sector);
            Assert.Equal(expectedTeslaEntity.Price, teslaEntity.Price);
            Assert.Equal(expectedTeslaEntity.PriceChangeRate, teslaEntity.PriceChangeRate);
            Assert.Equal(expectedTeslaEntity.CreatedDateTime, teslaEntity.CreatedDateTime);
            Assert.Equal(expectedTeslaEntity.IsActive, teslaEntity.IsActive);
        }
        public async void AddGetEntity_AddEnttiy_ReturnEntityByPropertyGroupBranch()
        {
            //Arrange
            StockRepository stubStockRepository = FakesFactory.CreateStockRepositoryFake();

            IBranch <StockEntity> sectorBranch = new RedisBranch <StockEntity>();

            sectorBranch.SetBranchId("BRANCH_SECTOR");
            sectorBranch.FilterBy(i => i.IsActive).GroupBy("Sector");
            stubStockRepository.AddBranch(sectorBranch);

            //Act
            StockEntity teslaEntity = new StockEntity("TESLA", StockSector.Technology, 229.00, 12.5);
            await stubStockRepository.AddAsync(teslaEntity);

            IEnumerable <StockEntity> expectedEntities = await stubStockRepository.GetAsync("BRANCH_SECTOR", "Technology");

            //Assert
            Assert.Single(expectedEntities);
            Assert.Equal(expectedEntities.ElementAt(0).Id, teslaEntity.Id);
            Assert.Equal(expectedEntities.ElementAt(0).Name, teslaEntity.Name);
            Assert.Equal(expectedEntities.ElementAt(0).Sector, teslaEntity.Sector);
            Assert.Equal(expectedEntities.ElementAt(0).Price, teslaEntity.Price);
            Assert.Equal(expectedEntities.ElementAt(0).PriceChangeRate, teslaEntity.PriceChangeRate);
            Assert.Equal(expectedEntities.ElementAt(0).CreatedDateTime, teslaEntity.CreatedDateTime);
            Assert.Equal(expectedEntities.ElementAt(0).IsActive, teslaEntity.IsActive);
        }
        public async void AddCountEntity_AddEnttiy_ReturnCountBySortedBranch()
        {
            //Arrange
            StockRepository stubStockRepository = FakesFactory.CreateStockRepositoryFake();

            IBranch <StockEntity> sectorAndSortedCreatedDateTimeBranch = new RedisBranch <StockEntity>();

            sectorAndSortedCreatedDateTimeBranch.SetBranchId("BRANCH_SECTOR_SORT_PRICE");
            sectorAndSortedCreatedDateTimeBranch.FilterBy(i => i.IsActive).GroupBy("Sector").SortBy("Price");
            stubStockRepository.AddBranch(sectorAndSortedCreatedDateTimeBranch);

            //Act
            StockEntity teslaEntity = new StockEntity("TESLA", StockSector.Technology, 229.00, 12.5);

            teslaEntity.CreatedDateTime = DateTimeOffset.UtcNow.AddSeconds(-15).DateTime;
            await stubStockRepository.AddAsync(teslaEntity);

            StockEntity microsoftEntity = new StockEntity("MICROSOFT", StockSector.Technology, 204.00, 9.5);
            await stubStockRepository.AddAsync(microsoftEntity);

            StockEntity appleEntity = new StockEntity("APPLE", StockSector.Technology, 294.21, 8.5);
            await stubStockRepository.AddAsync(appleEntity);

            long actualCount = await stubStockRepository.CountAsync("BRANCH_SECTOR_SORT_PRICE", 210, 250, "Technology");

            //Assert
            Assert.Equal(1, actualCount);
        }
        public void AddBranch_AddSingleBranch_ReturnCreatedBranchAndDefaultDataBranch()
        {
            //Arrange
            StockRepository stubStockRepository = FakesFactory.CreateStockRepositoryFake();

            //Act
            IBranch <StockEntity> allBranch = new RedisBranch <StockEntity>();

            allBranch.SetBranchId("BRANCH_ALL");
            allBranch.FilterBy(i => i.IsActive).GroupBy("All", i => "All");
            stubStockRepository.AddBranch(allBranch);

            //Assert
            Assert.Equal(20, stubStockRepository.GetBranches().Count());
        }
        public async void AddGetEntity_AddEnttiy_ReturnEntityByFunctionSortBranch()
        {
            //Arrange
            StockRepository stubStockRepository = FakesFactory.CreateStockRepositoryFake();

            IBranch <StockEntity> sectorAndSortedCreatedDateTimeBranch = new RedisBranch <StockEntity>();

            sectorAndSortedCreatedDateTimeBranch.SetBranchId("BRANCH_SECTOR_SORT_CREATED_DATETIME");
            sectorAndSortedCreatedDateTimeBranch.FilterBy(i => i.IsActive).GroupBy("Sector").SortBy("CreatedDateTimeSort", x => x.CreatedDateTime.Ticks);
            stubStockRepository.AddBranch(sectorAndSortedCreatedDateTimeBranch);

            //Act
            StockEntity teslaEntity = new StockEntity("TESLA", StockSector.Technology, 229.00, 12.5);

            teslaEntity.CreatedDateTime = DateTimeOffset.UtcNow.AddSeconds(-15).DateTime;
            await stubStockRepository.AddAsync(teslaEntity);

            StockEntity microsoftEntity = new StockEntity("MICROSOFT", StockSector.Technology, 204.00, 9.5);
            await stubStockRepository.AddAsync(microsoftEntity);

            StockEntity appleEntity = new StockEntity("APPLE", StockSector.Technology, 294.21, 8.5);
            await stubStockRepository.AddAsync(appleEntity);

            IEnumerable <StockEntity> expectedEntities = await stubStockRepository.GetAsync("BRANCH_SECTOR_SORT_CREATED_DATETIME", DateTimeOffset.UtcNow.AddSeconds(-10).Ticks, StockSector.Technology.ToString());

            //Assert
            Assert.Equal(2, expectedEntities.Count());

            Assert.Equal(expectedEntities.ElementAt(0).Id, microsoftEntity.Id);
            Assert.Equal(expectedEntities.ElementAt(0).Name, microsoftEntity.Name);
            Assert.Equal(expectedEntities.ElementAt(0).Sector, microsoftEntity.Sector);
            Assert.Equal(expectedEntities.ElementAt(0).Price, microsoftEntity.Price);
            Assert.Equal(expectedEntities.ElementAt(0).PriceChangeRate, microsoftEntity.PriceChangeRate);
            Assert.Equal(expectedEntities.ElementAt(0).CreatedDateTime, microsoftEntity.CreatedDateTime);
            Assert.Equal(expectedEntities.ElementAt(0).IsActive, microsoftEntity.IsActive);

            Assert.Equal(expectedEntities.ElementAt(1).Id, appleEntity.Id);
            Assert.Equal(expectedEntities.ElementAt(1).Name, appleEntity.Name);
            Assert.Equal(expectedEntities.ElementAt(1).Sector, appleEntity.Sector);
            Assert.Equal(expectedEntities.ElementAt(1).Price, appleEntity.Price);
            Assert.Equal(expectedEntities.ElementAt(1).PriceChangeRate, appleEntity.PriceChangeRate);
            Assert.Equal(expectedEntities.ElementAt(1).CreatedDateTime, appleEntity.CreatedDateTime);
            Assert.Equal(expectedEntities.ElementAt(1).IsActive, appleEntity.IsActive);
        }
        public async void AddGetEntity_AddEnttiy_ReturnEntityByPropertySortBranch()
        {
            //Arrange
            StockRepository stubStockRepository = FakesFactory.CreateStockRepositoryFake();

            IBranch <StockEntity> sortByPriceChangeRateBranch = new RedisBranch <StockEntity>();

            sortByPriceChangeRateBranch.SetBranchId("BRANCH_SORT_PRICE_CHANGE_RATE");
            sortByPriceChangeRateBranch.FilterBy(i => i.IsActive).SortBy("PriceChangeRate");
            stubStockRepository.AddBranch(sortByPriceChangeRateBranch);

            //Act
            StockEntity teslaEntity = new StockEntity("TESLA", StockSector.Technology, 229.00, 12.5);
            await stubStockRepository.AddAsync(teslaEntity);

            StockEntity microsoftEntity = new StockEntity("MICROSOFT", StockSector.Technology, 204.00, 9.5);
            await stubStockRepository.AddAsync(microsoftEntity);

            StockEntity appleEntity = new StockEntity("APPLE", StockSector.Technology, 294.21, 8.5);
            await stubStockRepository.AddAsync(appleEntity);

            IEnumerable <StockEntity> expectedEntities = await stubStockRepository.GetAsync("BRANCH_SORT_PRICE_CHANGE_RATE", (long)9.5);

            //Assert
            Assert.Equal(2, expectedEntities.Count());

            Assert.Equal(expectedEntities.ElementAt(0).Id, microsoftEntity.Id);
            Assert.Equal(expectedEntities.ElementAt(0).Name, microsoftEntity.Name);
            Assert.Equal(expectedEntities.ElementAt(0).Sector, microsoftEntity.Sector);
            Assert.Equal(expectedEntities.ElementAt(0).Price, microsoftEntity.Price);
            Assert.Equal(expectedEntities.ElementAt(0).PriceChangeRate, microsoftEntity.PriceChangeRate);
            Assert.Equal(expectedEntities.ElementAt(0).CreatedDateTime, microsoftEntity.CreatedDateTime);
            Assert.Equal(expectedEntities.ElementAt(0).IsActive, microsoftEntity.IsActive);

            Assert.Equal(expectedEntities.ElementAt(1).Id, teslaEntity.Id);
            Assert.Equal(expectedEntities.ElementAt(1).Name, teslaEntity.Name);
            Assert.Equal(expectedEntities.ElementAt(1).Sector, teslaEntity.Sector);
            Assert.Equal(expectedEntities.ElementAt(1).Price, teslaEntity.Price);
            Assert.Equal(expectedEntities.ElementAt(1).PriceChangeRate, teslaEntity.PriceChangeRate);
            Assert.Equal(expectedEntities.ElementAt(1).CreatedDateTime, teslaEntity.CreatedDateTime);
            Assert.Equal(expectedEntities.ElementAt(1).IsActive, teslaEntity.IsActive);
        }
        public async void AddCountEntity_AddEnttiy_ReturnCountBySortedBranch_ThrowsKeyNotFoundException()
        {
            //Arrange
            StockRepository stubStockRepository = FakesFactory.CreateStockRepositoryFake();

            IBranch <StockEntity> sortByPriceChangeRateBranch = new RedisBranch <StockEntity>();

            sortByPriceChangeRateBranch.SetBranchId("BRANCH_SORT_PRICE_CHANGE_RATE");
            sortByPriceChangeRateBranch.FilterBy(i => i.IsActive).SortBy("PriceChangeRate");
            stubStockRepository.AddBranch(sortByPriceChangeRateBranch);

            //Act
            StockEntity teslaEntity = new StockEntity("TESLA", StockSector.Technology, 229.00, 12.5);
            await stubStockRepository.AddAsync(teslaEntity);

            Func <Task> act = async() => await stubStockRepository.CountAsync("NotExist", "");

            //Assert
            KeyNotFoundException exception = await Assert.ThrowsAsync <KeyNotFoundException>(act);

            Assert.StartsWith("branchId not found: NotExist.", exception.Message);
        }
        public async void AddGetEntity_AddEntiy_ReturnEntityByPropertyInvalidBranchId_ThrowsKeyNotFoundException()
        {
            //Arrange
            StockRepository stubStockRepository = FakesFactory.CreateStockRepositoryFake();

            IBranch <StockEntity> sectorBranch = new RedisBranch <StockEntity>();

            sectorBranch.SetBranchId("BRANCH_SECTOR");
            sectorBranch.FilterBy(i => i.IsActive).GroupBy("Sector");
            stubStockRepository.AddBranch(sectorBranch);

            //Act
            StockEntity teslaEntity = new StockEntity("TESLA", StockSector.Technology, 229.00, 12.5);
            await stubStockRepository.AddAsync(teslaEntity);

            Func <Task> act = async() => await stubStockRepository.GetAsync("NotExist", "");

            //Assert
            KeyNotFoundException exception = await Assert.ThrowsAsync <KeyNotFoundException>(act);

            Assert.StartsWith("branchId not found: NotExist.", exception.Message);
        }
        public async void AddGetEntity_AddEnttiy_ReturnEntityByFunctionGroupSortBranch()
        {
            //Arrange
            StockRepository stubStockRepository = FakesFactory.CreateStockRepositoryFake();

            IBranch <StockEntity> groupFunctionProfitLevelSortedPriceRateBranch = new RedisBranch <StockEntity>();

            groupFunctionProfitLevelSortedPriceRateBranch.SetBranchId("BRANCH_PROFIT_LEVEL_SORT_PRICE_RATE");
            groupFunctionProfitLevelSortedPriceRateBranch.FilterBy(i => i.IsActive).GroupBy("Profit", x => stubStockRepository.GetProfitLevel(x)).SortBy("CreatedDateTimeSort", x => x.CreatedDateTime.Ticks);
            stubStockRepository.AddBranch(groupFunctionProfitLevelSortedPriceRateBranch);

            //Act
            StockEntity teslaEntity = new StockEntity("TESLA", StockSector.Technology, 229.00, 12.5);
            await stubStockRepository.AddAsync(teslaEntity);

            StockEntity amazonEntity = new StockEntity("AMAZON", StockSector.Technology, 329.00, 11.5);
            await stubStockRepository.AddAsync(amazonEntity);

            StockEntity microsoftEntity = new StockEntity("MICROSOFT", StockSector.Technology, 204.00, 9.5);
            await stubStockRepository.AddAsync(microsoftEntity);

            StockEntity appleEntity = new StockEntity("APPLE", StockSector.Technology, 294.21, -0.5);
            await stubStockRepository.AddAsync(appleEntity);

            IEnumerable <StockEntity> expectedGreatProfitEntities = await stubStockRepository.GetAsync("BRANCH_PROFIT_LEVEL_SORT_PRICE_RATE", "GreatProfit");

            IEnumerable <StockEntity> expectedProfitEntities = await stubStockRepository.GetAsync("BRANCH_PROFIT_LEVEL_SORT_PRICE_RATE", "NormalProfit");

            IEnumerable <StockEntity> expectedLossProfitEntities = await stubStockRepository.GetAsync("BRANCH_PROFIT_LEVEL_SORT_PRICE_RATE", "Loss");

            //Assert
            Assert.Equal(2, expectedGreatProfitEntities.Count());

            Assert.Equal(expectedGreatProfitEntities.ElementAt(0).Id, teslaEntity.Id);
            Assert.Equal(expectedGreatProfitEntities.ElementAt(0).Name, teslaEntity.Name);
            Assert.Equal(expectedGreatProfitEntities.ElementAt(0).Sector, teslaEntity.Sector);
            Assert.Equal(expectedGreatProfitEntities.ElementAt(0).Price, teslaEntity.Price);
            Assert.Equal(expectedGreatProfitEntities.ElementAt(0).PriceChangeRate, teslaEntity.PriceChangeRate);
            Assert.Equal(expectedGreatProfitEntities.ElementAt(0).CreatedDateTime, teslaEntity.CreatedDateTime);
            Assert.Equal(expectedGreatProfitEntities.ElementAt(0).IsActive, teslaEntity.IsActive);

            Assert.Equal(expectedGreatProfitEntities.ElementAt(1).Id, amazonEntity.Id);
            Assert.Equal(expectedGreatProfitEntities.ElementAt(1).Name, amazonEntity.Name);
            Assert.Equal(expectedGreatProfitEntities.ElementAt(1).Sector, amazonEntity.Sector);
            Assert.Equal(expectedGreatProfitEntities.ElementAt(1).Price, amazonEntity.Price);
            Assert.Equal(expectedGreatProfitEntities.ElementAt(1).PriceChangeRate, amazonEntity.PriceChangeRate);
            Assert.Equal(expectedGreatProfitEntities.ElementAt(1).CreatedDateTime, amazonEntity.CreatedDateTime);
            Assert.Equal(expectedGreatProfitEntities.ElementAt(1).IsActive, amazonEntity.IsActive);


            Assert.Single(expectedProfitEntities);

            Assert.Equal(expectedProfitEntities.ElementAt(0).Id, microsoftEntity.Id);
            Assert.Equal(expectedProfitEntities.ElementAt(0).Name, microsoftEntity.Name);
            Assert.Equal(expectedProfitEntities.ElementAt(0).Sector, microsoftEntity.Sector);
            Assert.Equal(expectedProfitEntities.ElementAt(0).Price, microsoftEntity.Price);
            Assert.Equal(expectedProfitEntities.ElementAt(0).PriceChangeRate, microsoftEntity.PriceChangeRate);
            Assert.Equal(expectedProfitEntities.ElementAt(0).CreatedDateTime, microsoftEntity.CreatedDateTime);
            Assert.Equal(expectedProfitEntities.ElementAt(0).IsActive, microsoftEntity.IsActive);


            Assert.Single(expectedLossProfitEntities);

            Assert.Equal(expectedLossProfitEntities.ElementAt(0).Id, appleEntity.Id);
            Assert.Equal(expectedLossProfitEntities.ElementAt(0).Name, appleEntity.Name);
            Assert.Equal(expectedLossProfitEntities.ElementAt(0).Sector, appleEntity.Sector);
            Assert.Equal(expectedLossProfitEntities.ElementAt(0).Price, appleEntity.Price);
            Assert.Equal(expectedLossProfitEntities.ElementAt(0).PriceChangeRate, appleEntity.PriceChangeRate);
            Assert.Equal(expectedLossProfitEntities.ElementAt(0).CreatedDateTime, appleEntity.CreatedDateTime);
            Assert.Equal(expectedLossProfitEntities.ElementAt(0).IsActive, appleEntity.IsActive);
        }