Exemplo n.º 1
0
 public AsyncEnumerable(
     RedisDatabase redisDatabase,
     RedisQuery redisQuery)
 {
     _redisDatabase = redisDatabase;
     _redisQuery    = redisQuery;
 }
        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 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_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 void Constructor_stores_EntityType_and_produces_empty_SelectedProperties()
        {
            var entityType = QueryTestType.EntityType();

            var redisQuery = new RedisQuery(entityType);

            Assert.Equal(entityType, redisQuery.EntityType);
            Assert.Empty(redisQuery.SelectedProperties);
        }
        public void QueryBy_Enum()
        {
            //Arrange and Act
            QueryProvider            provider   = new RedisQueryProvider((IConnectionMultiplexer)fixture.DI.GetService(typeof(IConnectionMultiplexer)));
            RedisQuery <StockEntity> redisQuery = new RedisQuery <StockEntity>(provider);
            IQueryable <StockEntity> query      = redisQuery.Where(s => (s.Sector == StockSector.CommunicationServices));
            List <StockEntity>       result     = query.ToList();

            //Assert
            Assert.Equal(fixture.TestData.Count(s => (s.Sector == StockSector.CommunicationServices)), result.Count);
        }
        public void QueryBy_DateTime()
        {
            //Arrange and Act
            QueryProvider            provider   = new RedisQueryProvider((IConnectionMultiplexer)fixture.DI.GetService(typeof(IConnectionMultiplexer)));
            RedisQuery <StockEntity> redisQuery = new RedisQuery <StockEntity>(provider);
            IQueryable <StockEntity> query      = redisQuery.Where(s => (s.CreatedDateTime < DateTime.Now.AddMilliseconds(245)));
            List <StockEntity>       result     = query.ToList();

            //Assert
            Assert.Equal(fixture.TestData.Count(s => (s.CreatedDateTime < DateTime.Now.AddMinutes(245))), result.Count);
        }
        public void QueryBy_String()
        {
            //Arrange and Act
            QueryProvider            provider   = new RedisQueryProvider((IConnectionMultiplexer)fixture.DI.GetService(typeof(IConnectionMultiplexer)));
            RedisQuery <StockEntity> redisQuery = new RedisQuery <StockEntity>(provider);
            IQueryable <StockEntity> query      = redisQuery.Where(s => (s.Name == "Abbott Laboratories"));
            List <StockEntity>       result     = query.ToList();

            //Assert
            Assert.Equal(fixture.TestData.Count(s => (s.Name == "Abbott Laboratories")), result.Count);
        }
        public void QueryBy_Int32()
        {
            //Arrange and Act
            QueryProvider            provider   = new RedisQueryProvider((IConnectionMultiplexer)fixture.DI.GetService(typeof(IConnectionMultiplexer)));
            RedisQuery <StockEntity> redisQuery = new RedisQuery <StockEntity>(provider);
            IQueryable <StockEntity> query      = redisQuery.Where(s => (s.LengthOfNameInt == 10));
            List <StockEntity>       result     = query.ToList();

            //Assert
            Assert.Equal(fixture.TestData.Count(s => (s.LengthOfNameInt == 10)), result.Count);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Gets non-materialized values from database
        /// </summary>
        /// <param name="redisQuery">Query data to decide what is selected from the database</param>
        /// <returns>An Enumerable of non-materialized object[]'s each of which represents one primary key</returns>
        public virtual IEnumerable <object[]> GetResults([NotNull] RedisQuery redisQuery)
        {
            Check.NotNull(redisQuery, "redisQuery");

            var redisPrimaryKeyIndexKeyName =
                ConstructRedisPrimaryKeyIndexKeyName(redisQuery.EntityType);

            var allKeysForEntity = UnderlyingDatabase.SetMembers(redisPrimaryKeyIndexKeyName).AsEnumerable();

            return(allKeysForEntity
                   .Select(compositePrimaryKeyValue =>
                           GetProjectionQueryObjectsFromDatabase(compositePrimaryKeyValue, redisQuery.EntityType, redisQuery.SelectedProperties, DecodeBytes)));
        }
Exemplo n.º 11
0
        // returns the object array representing all the properties
        // from an EntityType with a particular primary key
        private async Task <object[]> GetQueryObjectsFromDatabaseAsync(
            string primaryKey, RedisQuery redisQuery, Func <byte[], IProperty, object> decoder)
        {
            object[] results     = null;
            var      dataKeyName = ConstructRedisDataKeyName(redisQuery.EntityType, primaryKey);

            if (redisQuery.SelectedProperties == null ||
                !redisQuery.SelectedProperties.Any())
            {
                results = new object[redisQuery.EntityType.Properties.Count];

                // Async HGETALL
                var redisHashEntries = await GetUnderlyingDatabase().HashGetAllAsync(dataKeyName);

                foreach (var property in redisQuery.EntityType.Properties)
                {
                    var redisHashEntriesDictionary = redisHashEntries.ToDictionary(he => he.Name, he => he.Value);
                    // Note: since null's are stored in the database as the absence of the column name in the hash
                    // need to insert null's into the objectArray at the appropriate places.
                    RedisValue propertyRedisValue;
                    if (redisHashEntriesDictionary.TryGetValue(property.Name, out propertyRedisValue))
                    {
                        results[property.Index] = decoder(propertyRedisValue, property);
                    }
                    else
                    {
                        results[property.Index] = null;
                    }
                }
            }
            else
            {
                var selectedPropertiesArray = redisQuery.SelectedProperties.ToArray();
                results = new object[selectedPropertiesArray.Length];

                // Async HMGET
                var fields           = selectedPropertiesArray.Select(p => (RedisValue)p.Name).ToArray();
                var redisHashEntries = await GetUnderlyingDatabase().HashGetAsync(dataKeyName, fields);

                for (var i = 0; i < selectedPropertiesArray.Length; i++)
                {
                    results[i] =
                        redisHashEntries[i].IsNull
                            ? null
                            : decoder(redisHashEntries[i], selectedPropertiesArray[i]);
                }
            }

            return(results);
        }
Exemplo n.º 12
0
        /// <summary>
        ///     Gets a set of object[] values from database each of which represents the values
        ///     of the Properties required by the query for a particular EntityType
        /// </summary>
        /// <param name="redisQuery">An object representing the parameters of the query</param>
        /// <returns>
        ///     An Enumerable of object[] values from database each of which represents
        ///     the values of the Properties for the EntityType (either all the propoerties
        ///     or the selected properties as defined by the query)
        /// </returns>
        public virtual IEnumerable <object[]> GetResultsEnumerable([NotNull] RedisQuery redisQuery)
        {
            Check.NotNull(redisQuery, "redisQuery");

            var redisPrimaryKeyIndexKeyName
                = ConstructRedisPrimaryKeyIndexKeyName(redisQuery.EntityType);

            var allKeysForEntity
                = GetUnderlyingDatabase().SetMembers(redisPrimaryKeyIndexKeyName);

            return(allKeysForEntity
                   .Select(compositePrimaryKey
                           => GetQueryObjectsFromDatabase(
                               compositePrimaryKey, redisQuery, DecodeBytes)));
        }
        public void QueryBy_Complex()
        {
            //Arrange and Act
            QueryProvider            provider   = new RedisQueryProvider((IConnectionMultiplexer)fixture.DI.GetService(typeof(IConnectionMultiplexer)));
            RedisQuery <StockEntity> redisQuery = new RedisQuery <StockEntity>(provider);
            IQueryable <StockEntity> query      = redisQuery.Where(s => (s.IsActive == true && s.FirstLetterOfName == 'A' && s.CreatedDateTime < DateTime.Now.AddMilliseconds(-1) &&
                                                                         (s.LastByteOfName == (byte)'.' || s.LengthOfNameInt < 12) || (s.CreatedDateTime == DateTime.Now &&
                                                                                                                                       s.LengthOfNameUlong == (ulong)"Accenture plc".Length)));
            List <StockEntity> result = query.ToList();
            var count = fixture.TestData.Count(s => (s.IsActive == true && s.FirstLetterOfName == 'A' && s.CreatedDateTime < DateTime.Now.AddMilliseconds(-1) &&
                                                     (s.LastByteOfName == (byte)'.' || s.LengthOfNameInt < 12) || (s.CreatedDateTime == DateTime.Now &&
                                                                                                                   s.LengthOfNameUlong == (ulong)"Accenture plc".Length)));

            //Assert
            Assert.Equal(count, result.Count);
        }
Exemplo n.º 14
0
        public static void Main(string[] args)
        {
            RedisQuery <Product> query = new RedisQuery <Product>();

            query.Add(new Product()
            {
                Name = "cxb", CreateTime = "2020-3-17 16:28:31"
            });
            query.Add(new Product()
            {
                Name = "utr", CreateTime = "2020-3-19 16:28:31"
            });
            var nWhere = query.Where(n => n.Name == "cxb");
            var cWhere = nWhere.Where(c => c.CreateTime == "2020-3-17 16:28:31");
            var eWhere = cWhere.AsEnumerable();
            var list   = cWhere.ToList();

            Console.WriteLine("Hello World!");
        }
        public void GetValueReaders_returns_ObjectArrayReaders_over_an_enumerable_of_object_arrays()
        {
            var configurationMock           = new Mock <DbContextConfiguration>();
            var redisDatabaseMock           = new Mock <RedisDatabase>(configurationMock.Object);
            var materializationStrategyMock = new Mock <IQueryBuffer>();
            var stateManagerMockMock        = new Mock <StateManager>();

            var redisQueryContextMock
                = new Mock <RedisQueryContext>(
                      NullLogger.Instance,
                      materializationStrategyMock.Object,
                      stateManagerMockMock.Object,
                      redisDatabaseMock.Object);

            var resultsFromDatabase = new List <object[]>
            {
                new object[] { 1, "SomeValue1" },
                new object[] { 2, "SomeValue2" }
            };

            redisQueryContextMock.Setup(m => m.GetResultsFromRedis(It.IsAny <RedisQuery>())).Returns(resultsFromDatabase);
            var entityType = QueryTestType.EntityType();
            var redisQuery = new RedisQuery(entityType);

            var readers = redisQuery.GetValueReaders(redisQueryContextMock.Object);

            Assert.Equal(2, readers.Count());
            var i = 1;

            foreach (var reader in readers)
            {
                Assert.Equal(i, reader.ReadValue <int>(0));
                Assert.Equal("SomeValue" + i, reader.ReadValue <string>(1));
                i++;
            }
        }
        public void AddProperty_updates_SelectedProperties_and_subsequent_GetProjectionIndex_is_correct()
        {
            // add 1 property
            var entityType        = QueryTestType.EntityType();
            var redisQuery        = new RedisQuery(entityType);
            var someValueProperty = entityType.GetProperty("SomeValue");

            redisQuery.AddProperty(someValueProperty);

            Assert.True((new List <IProperty> {
                someValueProperty
            }).SequenceEqual(redisQuery.SelectedProperties));
            Assert.Equal(0, redisQuery.GetProjectionIndex(someValueProperty));

            // add a different property
            var idProperty = entityType.GetProperty("Id");

            redisQuery.AddProperty(idProperty);

            Assert.True((new List <IProperty> {
                someValueProperty, idProperty
            }).SequenceEqual(redisQuery.SelectedProperties));
            Assert.Equal(0, redisQuery.GetProjectionIndex(someValueProperty));
            Assert.Equal(1, redisQuery.GetProjectionIndex(idProperty));

            // add the 1st property again - adds to end of list
            redisQuery.AddProperty(someValueProperty);

            Assert.True((new List <IProperty> {
                someValueProperty, idProperty, someValueProperty
            }).SequenceEqual(redisQuery.SelectedProperties));

            // Note: GetProjectionIndex(someValueProperty) returns the _first_ index at which that property is returned
            Assert.Equal(0, redisQuery.GetProjectionIndex(someValueProperty));
            Assert.Equal(1, redisQuery.GetProjectionIndex(idProperty));
        }
        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 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);
        }
Exemplo n.º 19
0
        /// <summary>
        ///     Gets a set of object[] values from database each of which represents the values
        ///     of the Properties required by the query for a particular EntityType
        /// </summary>
        /// <param name="redisQuery">An object representing the parameters of the query</param>
        /// <returns>
        ///     An Enumerable of object[] values from database each of which represents
        ///     the values of the Properties for the EntityType (either all the propoerties
        ///     or the selected properties as defined by the query)
        /// </returns>
        public virtual IAsyncEnumerable <object[]> GetResultsAsyncEnumerable([NotNull] RedisQuery redisQuery)
        {
            Check.NotNull(redisQuery, "redisQuery");

            return(new AsyncEnumerable(this, redisQuery));
        }