public void GroupBy_GroupByNotExistProperty_ThrowsArgumentException() { //Arrange StockRepository stubStockRepository = FakesFactory.CreateStockRepositoryFake(); IBranch <StockEntity> technologyBranch = new RedisBranch <StockEntity>(); technologyBranch.SetBranchId("BRANCH_NOTVALID"); //Act Action act = () => technologyBranch.GroupBy("NotExist"); //Assert ArgumentException exception = Assert.Throws <ArgumentException>(act); Assert.Equal($"NotExist is not member of {typeof(StockEntity).Name}.", exception.Message); }
public void GroupBy_GroupByInvalidProperty_ThrowsArgumentException() { //Arrange StockRepository stubStockRepository = FakesFactory.CreateStockRepositoryFake(); IBranch <StockEntity> technologyBranch = new RedisBranch <StockEntity>(); technologyBranch.SetBranchId("BRANCH_NOTVALID"); //Act Action act = () => technologyBranch.GroupBy("MetaData"); //Assert ArgumentException exception = Assert.Throws <ArgumentException>(act); Assert.Equal($"MetaData is StockMetaData. GroupByProperty only applied on value types and string.", exception.Message); }
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); }
public override void CreateBranches() { //Basic filter for Active stocks Expression <Func <StockEntity, bool> > activeFilter = i => i.IsActive; //GroupAll RedisBranch <StockEntity> groupAll = new RedisBranch <StockEntity>(); groupAll.SetBranchId(BRANCH_GROUPALL); groupAll.FilterBy(activeFilter).GroupBy("", x => "All"); AddBranch(groupAll); //GroupBySector RedisBranch <StockEntity> groupBySectorBranch = new RedisBranch <StockEntity>(); groupBySectorBranch.SetBranchId(BRANCH_GROUPBY_SECTOR); groupBySectorBranch.FilterBy(activeFilter).GroupBy("Sector"); AddBranch(groupBySectorBranch); //SortByCreatedDateTime RedisBranch <StockEntity> sortByCreatedDateTimeBranch = new RedisBranch <StockEntity>(); sortByCreatedDateTimeBranch.SetBranchId(BRANCH_SORTBY_CREATEDDATETIME); sortByCreatedDateTimeBranch.FilterBy(activeFilter).SortBy("CreatedDateTime", x => x.CreatedDateTime.Ticks); AddBranch(sortByCreatedDateTimeBranch); //GroupBySector SortByPrice RedisBranch <StockEntity> groupBySectorSortByPriceBranch = new RedisBranch <StockEntity>(); groupBySectorSortByPriceBranch.SetBranchId(BRANCH_GROUPBY_SECTOR_SORTBY_PRICE); groupBySectorSortByPriceBranch.FilterBy(activeFilter).GroupBy("Sector").SortBy("Price"); AddBranch(groupBySectorSortByPriceBranch); //GroupBySector SortByPriceChangeRate RedisBranch <StockEntity> groupBySectorSortByPriceChangeRateBranch = new RedisBranch <StockEntity>(); groupBySectorSortByPriceChangeRateBranch.SetBranchId(BRANCH_GROUPBY_SECTOR_SORTBY_PRICECHANGERATE); groupBySectorSortByPriceChangeRateBranch.FilterBy(activeFilter).GroupBy("Sector").SortBy("PriceChangeRate"); AddBranch(groupBySectorSortByPriceChangeRateBranch); //GroupByProfitLevel SortByPriceChangeRate RedisBranch <StockEntity> groupByProfitLevelSortByPriceChangeRateBranch = new RedisBranch <StockEntity>(); groupByProfitLevelSortByPriceChangeRateBranch.SetBranchId(BRANCH_GROUPBY_PROFITLEVEL_SORTBY_PRICECHANGERATE); groupByProfitLevelSortByPriceChangeRateBranch.FilterBy(activeFilter).GroupBy("ProfitLevel", x => GetProfitLevel(x).ToString()).SortBy("PriceChangeRate"); AddBranch(groupByProfitLevelSortByPriceChangeRateBranch); //GroupBySector GroupByProfitLevel SortByPriceChangeRate RedisBranch <StockEntity> groupBySectorGroupByProfitLevelSortByPriceChangeRateBranch = new RedisBranch <StockEntity>(); groupBySectorGroupByProfitLevelSortByPriceChangeRateBranch.SetBranchId(BRANCH_GROUPBY_SECTOR_GROUPBY_PROFITLEVEL_SORTBY_PRICECHANGERATE); groupBySectorGroupByProfitLevelSortByPriceChangeRateBranch.FilterBy(activeFilter).GroupBy("Sector").GroupBy("ProfitLevel", x => GetProfitLevel(x).ToString()).SortBy("PriceChangeRate"); AddBranch(groupBySectorGroupByProfitLevelSortByPriceChangeRateBranch); //GroupByCountry SortByPriceChangeRate RedisBranch <StockEntity> groupByCountrySortByPriceChangeRateBranch = new RedisBranch <StockEntity>(); groupByCountrySortByPriceChangeRateBranch.SetBranchId(BRANCH_GROUPBY_COUNTRY_SORTBY_PRICECHANGERATE); groupByCountrySortByPriceChangeRateBranch.FilterBy(activeFilter).GroupBy("Country", x => x.MetaData.Country).SortBy("PriceChangeRate"); AddBranch(groupByCountrySortByPriceChangeRateBranch); }
public override void CreateBranches() { Expression <Func <PropertyEntity, bool> > validationFilterExpression = i => i.IsActive && i.IsApproved; Expression <Func <PropertyEntity, string> > allBranchExpression = i => "All"; IRedisBranch <PropertyEntity> allBranch = new RedisBranch <PropertyEntity>(); allBranch.SetBranchId(BRANCH_ALL); allBranch.SetEntityType(typeof(PropertyEntity)); allBranch.FilterBy(validationFilterExpression).GroupBy("All", allBranchExpression); AddBranch(allBranch); IRedisBranch <PropertyEntity> locationBranch = new RedisBranch <PropertyEntity>(); locationBranch.SetBranchId(BRANCH_LOCATION); locationBranch.SetEntityType(typeof(PropertyEntity)); locationBranch.FilterBy(validationFilterExpression).GroupBy("Location"); AddBranch(locationBranch); IRedisBranch <PropertyEntity> locationRoomNumberBranch = new RedisBranch <PropertyEntity>(); locationRoomNumberBranch.SetBranchId(BRANCH_LOCATION_ROOMNUMBER); locationRoomNumberBranch.SetEntityType(typeof(PropertyEntity)); locationRoomNumberBranch.FilterBy(validationFilterExpression).GroupBy("Location").GroupBy("RoomNumber"); AddBranch(locationRoomNumberBranch); IRedisBranch <PropertyEntity> locationSortByPriceBranch = new RedisBranch <PropertyEntity>(); locationSortByPriceBranch.SetBranchId(BRANCH_LOCATION_SORTBY_PRICE); locationSortByPriceBranch.SetEntityType(typeof(PropertyEntity)); locationSortByPriceBranch.FilterBy(validationFilterExpression).GroupBy("Location").SortBy("Price"); AddBranch(locationSortByPriceBranch); IRedisBranch <PropertyEntity> locationSortByLastUpdateDateBranch = new RedisBranch <PropertyEntity>(); locationSortByLastUpdateDateBranch.SetBranchId(BRANCH_LOCATION_SORTBY_LASTUPDATEDATE); locationSortByLastUpdateDateBranch.SetEntityType(typeof(PropertyEntity)); locationSortByLastUpdateDateBranch.FilterBy(validationFilterExpression).GroupBy("Location").SortBy("LastUpdateDateTime"); AddBranch(locationSortByLastUpdateDateBranch); IRedisBranch <PropertyEntity> locationRoomNumberSortByPriceBranch = new RedisBranch <PropertyEntity>(); locationRoomNumberSortByPriceBranch.SetBranchId(BRANCH_LOCATION_ROOMNUMBER_SORTBY_PRICE); locationRoomNumberSortByPriceBranch.SetEntityType(typeof(PropertyEntity)); locationRoomNumberSortByPriceBranch.FilterBy(validationFilterExpression).GroupBy("Location").GroupBy("RoomNumber").SortBy("Price"); AddBranch(locationRoomNumberSortByPriceBranch); IRedisBranch <PropertyEntity> locationRoomNumberSortByLastUpdateDateBranch = new RedisBranch <PropertyEntity>(); locationRoomNumberSortByLastUpdateDateBranch.SetBranchId(BRANCH_LOCATION_ROOMNUMBER_LASTUPDATEDATE); locationRoomNumberSortByLastUpdateDateBranch.SetEntityType(typeof(PropertyEntity)); locationRoomNumberSortByLastUpdateDateBranch.FilterBy(validationFilterExpression).GroupBy("Location").GroupBy("RoomNumber").SortBy("LastUpdateDateTime"); AddBranch(locationRoomNumberSortByLastUpdateDateBranch); IRedisBranch <PropertyEntity> fairOfferBranch = new RedisBranch <PropertyEntity>(); fairOfferBranch.SetBranchId(BRANCH_FAIROFFER); fairOfferBranch.SetEntityType(typeof(PropertyEntity)); fairOfferBranch.FilterBy(validationFilterExpression).GroupBy("FairOffer", i => IsOfferFair(i)); AddBranch(fairOfferBranch); }