示例#1
0
        public async void AddAsync_ReturnsCorrectResult()
        {
            var mockStocks = fakeStocks.BuildMockDbSet();

            var dbContextMock = new Mock <StockDbContext>(dbOptions);

            dbContextMock.Setup(t => t.Stocks).Returns(mockStocks.Object).Verifiable();

            var repo = new StockRepository(dbContextMock.Object);

            Stock newStock = new Stock("Zzz Stock Company", "ZSC", 86, 82, "NASDAQ", false);

            await repo.AddAsync(newStock);

            mockStocks.Verify(t => t.Add(It.IsAny <Stock>()), Times.Once());
            dbContextMock.Verify(d => d.SaveChangesAsync(It.IsAny <CancellationToken>()), Times.Once());

            Stock oldStock = new Stock("Tian Stock Company", "TSC", 84, 80, "NASDAQ", false);

            try
            {
                await repo.AddAsync(oldStock);
            }
            catch (Exception ex)
            {
                Assert.Equal("AlreadyInDbException", ex.GetType().Name);
                mockStocks.Verify(t => t.Add(It.IsAny <Stock>()), Times.Once());
                dbContextMock.Verify(d => d.SaveChangesAsync(It.IsAny <CancellationToken>()), Times.Once());
            }
        }
        public async Task QueryBy_Boolean()
        {
            //Arrange
            IConnectionMultiplexer connectionMultiplexer = FakesFactory.CreateConnectionMultiplexerFake();
            StockRepository        stubStockRepository   = FakesFactory.CreateStockRepositoryFake(connectionMultiplexer);

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

            teslaEntity.IsActive = false;
            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);

            //Act
            QueryProvider            provider   = new RedisQueryProvider(connectionMultiplexer);
            RedisQuery <StockEntity> redisQuery = new RedisQuery <StockEntity>(provider);
            IQueryable <StockEntity> query      = redisQuery.Where(s => (s.IsActive == true));
            List <StockEntity>       result     = query.ToList();

            //Assert
            Assert.Equal(2, result.Count);
        }
        public async Task QueryBy_DateTime()
        {
            //Arrange
            DateTime dateTime = new DateTime(2021, 1, 5, 21, 10, 23);
            IConnectionMultiplexer connectionMultiplexer = FakesFactory.CreateConnectionMultiplexerFake();
            StockRepository        stubStockRepository   = FakesFactory.CreateStockRepositoryFake(connectionMultiplexer);

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

            teslaEntity.CreatedDateTime = dateTime;
            await stubStockRepository.AddAsync(teslaEntity);

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

            microsoftEntity.CreatedDateTime = dateTime.AddSeconds(60);
            await stubStockRepository.AddAsync(microsoftEntity);

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

            appleEntity.CreatedDateTime = dateTime.AddSeconds(120);
            await stubStockRepository.AddAsync(appleEntity);

            //Act
            QueryProvider            provider   = new RedisQueryProvider(connectionMultiplexer);
            RedisQuery <StockEntity> redisQuery = new RedisQuery <StockEntity>(provider);
            IQueryable <StockEntity> query      = redisQuery.Where(s => (s.CreatedDateTime < dateTime.AddSeconds(90)));
            List <StockEntity>       result     = query.ToList();

            //Assert
            Assert.Equal(2, result.Count);
        }
        public async Task QueryBy_Complex()
        {
            //Arrange
            DateTime dateTime = new DateTime(2021, 1, 5, 21, 10, 23);
            IConnectionMultiplexer connectionMultiplexer = FakesFactory.CreateConnectionMultiplexerFake();
            StockRepository        stubStockRepository   = FakesFactory.CreateStockRepositoryFake(connectionMultiplexer);

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

            teslaEntity.IsActive        = false;
            teslaEntity.CreatedDateTime = dateTime;
            await stubStockRepository.AddAsync(teslaEntity);

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

            microsoftEntity.CreatedDateTime = dateTime.AddSeconds(60);
            await stubStockRepository.AddAsync(microsoftEntity);

            StockEntity rheinEnergyEntity = new StockEntity("Rhein Energy", StockSector.Energy, 294.21, 8.5);

            rheinEnergyEntity.CreatedDateTime = dateTime.AddSeconds(120);
            await stubStockRepository.AddAsync(rheinEnergyEntity);

            //Act
            QueryProvider            provider   = new RedisQueryProvider(connectionMultiplexer);
            RedisQuery <StockEntity> redisQuery = new RedisQuery <StockEntity>(provider);
            IQueryable <StockEntity> query      = redisQuery.Where(s => (s.IsActive == false && s.FirstLetterOfName == 'T' && s.CreatedDateTime == dateTime && (s.LastByteOfName == (byte)'L' || s.LengthOfNameInt < 6)) ||
                                                                   (s.CreatedDateTime > dateTime.AddSeconds(90) && s.LengthOfNameUlong == (ulong)"Rhein Energy".Length));
            List <StockEntity> result = query.ToList();

            //Assert
            Assert.Equal(2, result.Count);
            Assert.Equal(" {{StockEntity:IsActive-Boolean:False}}  {{StockEntity:FirstLetterOfName-Int32:84:=}} {{Intersection}} {{StockEntity:CreatedDateTime-DateTime:637454778230000000:=}} {{Intersection}} {{StockEntity:LastByteOfName-Int32:76:=}}  {{StockEntity:LengthOfNameInt-Int32:6:<}} {{Union}}{{Intersection}} {{StockEntity:CreatedDateTime-DateTime:637454779130000000:>}}  {{StockEntity:LengthOfNameUlong-UInt64:12:=}} {{Intersection}}{{Union}}", query.ToString());
        }
        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 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_ReturnEntityByIdAndClassProperty()
        {
            //Arrange
            StockRepository stubStockRepository = FakesFactory.CreateStockRepositoryFake();

            //Act
            StockMetaData metaData    = new StockMetaData("USA", CurrencyCode.USD);
            StockEntity   teslaEntity = new StockEntity("TESLA", StockSector.Technology, 229.00, 12.5, metaData);
            string        dummyString = teslaEntity.DummyString;
            string        entityId    = teslaEntity.Id;
            await stubStockRepository.AddAsync(teslaEntity);

            StockEntity expectedEntity = await stubStockRepository.GetByIdAsync(entityId);

            //Assert
            Assert.NotNull(expectedEntity);

            Assert.Equal(expectedEntity.Id, teslaEntity.Id);
            Assert.Equal(expectedEntity.Name, teslaEntity.Name);
            Assert.Equal(expectedEntity.Sector, teslaEntity.Sector);
            Assert.Equal(expectedEntity.Price, teslaEntity.Price);
            Assert.Equal(expectedEntity.PriceChangeRate, teslaEntity.PriceChangeRate);
            Assert.Equal(expectedEntity.CreatedDateTime, teslaEntity.CreatedDateTime);
            Assert.Equal(expectedEntity.IsActive, teslaEntity.IsActive);
            Assert.NotNull(dummyString);
            Assert.True(expectedEntity.DummyString == default);

            Assert.Equal(expectedEntity.MetaData.Currency, metaData.Currency);
            Assert.Equal(expectedEntity.MetaData.Country, metaData.Country);
            Assert.Equal(expectedEntity.MetaData.UpdateDateTime, metaData.UpdateDateTime);
        }
        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 AddGetEntity_AddEnttiy_ReturnEntityByIdAndNullForIgnoreDataMemberProperty()
        {
            //Arrange
            StockRepository stubStockRepository = FakesFactory.CreateStockRepositoryFake();

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

            StockEntity expectedEnty = await stubStockRepository.GetByIdAsync(entityId);

            //Assert
            Assert.NotNull(expectedEnty);

            Assert.Equal(expectedEnty.Id, teslaEntity.Id);
            Assert.Equal(expectedEnty.Name, teslaEntity.Name);
            Assert.Equal(expectedEnty.Sector, teslaEntity.Sector);
            Assert.Equal(expectedEnty.Price, teslaEntity.Price);
            Assert.Equal(expectedEnty.PriceChangeRate, teslaEntity.PriceChangeRate);
            Assert.Equal(expectedEnty.CreatedDateTime, teslaEntity.CreatedDateTime);
            Assert.Equal(expectedEnty.IsActive, teslaEntity.IsActive);
            Assert.NotNull(dummyString);
            Assert.True(expectedEnty.DummyString == default);
        }
        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);
        }
示例#13
0
        public async void AddAsync_ReturnsCorrectResult()
        {
            var stockCollection = new Mock <IMongoCollection <StockDocument> >();
            var context         = new Mock <IStockDbContext>();

            context.Setup(s => s.StockCollection).Returns(stockCollection.Object).Verifiable();

            var repo = new StockRepository(context.Object, mapper);

            Stock newStock = new Stock("Zzz Stock Company", "ZSC", 86, 82, "NASDAQ", false);

            stockCollection.Setup(c => c.CountDocumentsAsync(
                                      It.IsAny <FilterDefinition <StockDocument> >(),
                                      It.IsAny <CountOptions>(),
                                      It.IsAny <CancellationToken>()))
            .ReturnsAsync(0);

            await repo.AddAsync(newStock);

            stockCollection.Verify(c => c.InsertOneAsync(It.IsAny <StockDocument>(), It.IsAny <InsertOneOptions>(), It.IsAny <CancellationToken>()), Times.Once());

            Stock oldStock = new Stock("Tian Stock Company", "TSC", 84, 80, "NASDAQ", false);

            stockCollection.Setup(c => c.CountDocumentsAsync(
                                      It.IsAny <FilterDefinition <StockDocument> >(),
                                      It.IsAny <CountOptions>(),
                                      It.IsAny <CancellationToken>()))
            .ReturnsAsync(1);

            try
            {
                await repo.AddAsync(oldStock);
            }
            catch (Exception ex)
            {
                Assert.Equal("AlreadyInDbException", ex.GetType().Name);
                stockCollection.Verify(c => c.InsertOneAsync(It.IsAny <StockDocument>(), It.IsAny <InsertOneOptions>(), It.IsAny <CancellationToken>()), Times.Once());
            }

            context.Verify();
        }
示例#14
0
        public async void AddAsync_ReturnsCorrectResult()
        {
            var dbContextMock = new Mock <IStockDbContext>();

            dbContextMock.Setup(d => d.QueryAsync <object>(It.IsAny <string>(), It.IsAny <object>()))
            .ReturnsAsync((string _, object p) =>
            {
                string code = p.GetType().GetProperty("Code").GetValue(p) as string;
                return(fakeStocks.Any <Stock>(s => s.Code == code) ? new List <object>()
                {
                    1
                } : new List <object>());
            });
            dbContextMock.Setup(d => d.ExecuteAsync(It.IsAny <string>(), It.IsAny <object>())).ReturnsAsync(1);

            var repo = new StockRepository(dbContextMock.Object);

            Stock newStock = new Stock("Zzz Stock Company", "ZSC", 86, 82, "NASDAQ", false);

            await repo.AddAsync(newStock);

            dbContextMock.Verify(c => c.QueryAsync <object>(It.IsAny <string>(), It.IsAny <object>()), Times.Once());
            dbContextMock.Verify(c => c.ExecuteAsync(It.IsAny <string>(), It.IsAny <object>()), Times.Once());

            Stock oldStock = new Stock("Tian Stock Company", "TSC", 84, 80, "NASDAQ", false);

            try
            {
                await repo.AddAsync(oldStock);
            }
            catch (Exception ex)
            {
                Assert.Equal("AlreadyInDbException", ex.GetType().Name);
                dbContextMock.Verify(c => c.QueryAsync <object>(It.IsAny <string>(), It.IsAny <object>()), Times.Exactly(2));
                dbContextMock.Verify(c => c.ExecuteAsync(It.IsAny <string>(), It.IsAny <object>()), Times.Once());
            }
        }
        public async Task QueryBy_Char()
        {
            //Arrange
            IConnectionMultiplexer connectionMultiplexer = FakesFactory.CreateConnectionMultiplexerFake();
            StockRepository        stubStockRepository   = FakesFactory.CreateStockRepositoryFake(connectionMultiplexer);

            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);

            //Act
            QueryProvider            provider   = new RedisQueryProvider(connectionMultiplexer);
            RedisQuery <StockEntity> redisQuery = new RedisQuery <StockEntity>(provider);
            IQueryable <StockEntity> query      = redisQuery.Where(s => (s.FirstLetterOfName == 'T'));
            List <StockEntity>       result     = query.ToList();

            //Assert
            Assert.Single(result);
        }
        public async Task QueryBy_Enum()
        {
            //Arrange
            IConnectionMultiplexer connectionMultiplexer = FakesFactory.CreateConnectionMultiplexerFake();
            StockRepository        stubStockRepository   = FakesFactory.CreateStockRepositoryFake(connectionMultiplexer);

            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 rheinEnergyEntity = new StockEntity("Rhein Energy", StockSector.Energy, 294.21, 8.5);
            await stubStockRepository.AddAsync(rheinEnergyEntity);

            //Act
            QueryProvider            provider   = new RedisQueryProvider(connectionMultiplexer);
            RedisQuery <StockEntity> redisQuery = new RedisQuery <StockEntity>(provider);
            IQueryable <StockEntity> query      = redisQuery.Where(s => (s.Sector == StockSector.Technology));
            List <StockEntity>       result     = query.ToList();

            //Assert
            Assert.Equal(2, result.Count);
        }
        public async void AddUpdateGetEntity_AddUpdateEntiy_ReturnEntityByIdWithUpdatedValues()
        {
            //Arrange
            StockRepository stubStockRepository = FakesFactory.CreateStockRepositoryFake();

            //Act
            StockMetaData metaData    = new StockMetaData("USA", CurrencyCode.USD);
            StockEntity   teslaEntity = new StockEntity("TESLA", StockSector.Technology, 229.00, 12.5, metaData);
            string        entityId    = teslaEntity.Id;
            await stubStockRepository.AddAsync(teslaEntity);

            StockEntity updatedEntity = await stubStockRepository.GetByIdAsync(entityId);

            updatedEntity.Sector                  = StockSector.Energy;
            updatedEntity.Name                    = "Rhein Energy";
            updatedEntity.CreatedDateTime         = DateTime.UtcNow.AddDays(-1);
            updatedEntity.Price                   = 119.00;
            updatedEntity.PriceChangeRate         = 2.7;
            updatedEntity.MetaData.Country        = "Germany";
            updatedEntity.MetaData.Currency       = CurrencyCode.EURO;
            updatedEntity.MetaData.UpdateDateTime = DateTime.UtcNow;

            await stubStockRepository.UpdateAsync(updatedEntity);

            StockEntity reEntity = await stubStockRepository.GetByIdAsync(entityId);

            //Assert
            Assert.NotNull(reEntity);

            Assert.Equal(updatedEntity.Id, reEntity.Id);
            Assert.Equal(updatedEntity.Name, reEntity.Name);
            Assert.Equal(updatedEntity.Sector, reEntity.Sector);
            Assert.Equal(updatedEntity.Price, reEntity.Price);
            Assert.Equal(updatedEntity.PriceChangeRate, reEntity.PriceChangeRate);
            Assert.Equal(updatedEntity.CreatedDateTime, reEntity.CreatedDateTime);
            Assert.Equal(updatedEntity.IsActive, reEntity.IsActive);
            Assert.True(updatedEntity.DummyString == default);

            Assert.Equal(updatedEntity.MetaData.Currency, reEntity.MetaData.Currency);
            Assert.Equal(updatedEntity.MetaData.Country, reEntity.MetaData.Country);
            Assert.Equal(updatedEntity.MetaData.UpdateDateTime, reEntity.MetaData.UpdateDateTime);
        }
        public async void AddDeleteGetEntity_AddDeleteEntiy_ReturnEntityByIdWithNull()
        {
            //Arrange
            StockRepository stubStockRepository = FakesFactory.CreateStockRepositoryFake();

            //Act
            StockMetaData metaData    = new StockMetaData("USA", CurrencyCode.USD);
            StockEntity   teslaEntity = new StockEntity("TESLA", StockSector.Technology, 229.00, 12.5, metaData);
            string        entityId    = teslaEntity.Id;
            await stubStockRepository.AddAsync(teslaEntity);

            StockEntity savedEntity = await stubStockRepository.GetByIdAsync(entityId);

            await stubStockRepository.DeleteAsync(savedEntity);

            StockEntity deletedEntity = await stubStockRepository.GetByIdAsync(entityId);

            //Assert
            Assert.Null(deletedEntity);
        }
        public async Task ReloadTestDataAsync()
        {
            TestData.Clear();
            using (StreamReader file = File.OpenText(TestSettings.TestDataFilePath))
            {
                Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
                TestData.AddRange((List <StockEntity>)serializer.Deserialize(file, typeof(List <StockEntity>)));
            }

            var connectionMultiplexer = (ConnectionMultiplexer)DI.GetService <IConnectionMultiplexer>();
            var server = connectionMultiplexer.GetServer("localhost:6379");
            await server.FlushDatabaseAsync();

            StockRepository stockRepository = (StockRepository)DI.GetService <IRedisRepository <StockEntity> >();

            foreach (StockEntity entity in TestData)
            {
                entity.FillCalculatedProperties();
                await stockRepository.AddAsync(entity);
            }
        }
        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 Task AddStock_Database_TestAsync()
        {
            using var connection = new SqliteConnection("Data Source=:memory:");
            connection.Open();
            var options = new DbContextOptionsBuilder <mmpproject2Context>().UseSqlite(connection).Options;

            testStock = new Domain.Models.Stock("APPL", "NASDAQ", "Apple Inc.", null);


            using (var context = new mmpproject2Context(options))
            {
                context.Database.EnsureCreated();
                var repo = new StockRepository(options);

                await repo.AddAsync(testStock);
            }

            using var context2 = new mmpproject2Context(options);
            DataAccess.Models.Stock testReal = context2.Stocks
                                               .Single(l => l.Symbol == "APPL");

            Assert.Equal(testStock.Symbol, testReal.Symbol);
            Assert.Equal(testStock.Name, testReal.Name);
        }
        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);
        }