Пример #1
0
        private async Task TestQueryDistinctBaseAsync(Query query)
        {
            int  seed = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
            uint numberOfDocuments = 100;

            Random        rand   = new Random(seed);
            List <Person> people = new List <Person>();

            for (int i = 0; i < numberOfDocuments; i++)
            {
                // Generate random people
                Person person = PersonGenerator.GetRandomPerson(rand);
                for (int j = 0; j < rand.Next(0, 4); j++)
                {
                    // Force an exact duplicate
                    people.Add(person);
                }
            }

            List <string> documents = new List <string>();

            // Shuffle them so they end up in different pages
            people = people.OrderBy((person) => Guid.NewGuid()).ToList();
            foreach (Person person in people)
            {
                documents.Add(JsonConvert.SerializeObject(person));
            }

            await this.CreateIngestQueryDeleteAsync(
                ConnectionModes.Direct,
                CollectionTypes.MultiPartition,
                documents,
                query,
                "/id");
        }
Пример #2
0
        private static Pet GetRandomPet(Random rand)
        {
            string name = PersonGenerator.GetRandomName(rand);
            int    age  = PersonGenerator.GetRandomAge(rand);

            return(new Pet(name, age));
        }
Пример #3
0
        public static Person GetRandomPerson(Random rand)
        {
            string        name   = PersonGenerator.GetRandomName(rand);
            City          city   = PersonGenerator.GetRandomCity(rand);
            double        income = PersonGenerator.GetRandomIncome(rand);
            List <Person> people = new List <Person>();

            if (rand.Next(0, 11) % 10 == 0)
            {
                for (int i = 0; i < rand.Next(0, 5); i++)
                {
                    people.Add(PersonGenerator.GetRandomPerson(rand));
                }
            }

            Person[] children = people.ToArray();
            int      age      = PersonGenerator.GetRandomAge(rand);
            Pet      pet      = PersonGenerator.GetRandomPet(rand);
            Guid     guid     = Guid.NewGuid();

            object mixedTypeField;

            switch (rand.Next(0, 7))
            {
            case 0:
                mixedTypeField = name;
                break;

            case 1:
                mixedTypeField = city;
                break;

            case 2:
                mixedTypeField = income;
                break;

            case 3:
                mixedTypeField = children;
                break;

            case 4:
                mixedTypeField = age;
                break;

            case 5:
                mixedTypeField = pet;
                break;

            case 6:
                mixedTypeField = guid;
                break;

            default:
                throw new ArgumentException();
            }
            return(new Person(name, city, income, children, age, pet, guid, mixedTypeField));
        }
Пример #4
0
        public async Task TestQueryCrossPartitionOffsetLimitAsync()
        {
            int  seed = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
            uint numberOfDocuments = 10;

            Random        rand   = new Random(seed);
            List <Person> people = new List <Person>();

            for (int i = 0; i < numberOfDocuments; i++)
            {
                // Generate random people
                Person person = PersonGenerator.GetRandomPerson(rand);
                for (int j = 0; j < rand.Next(0, 4); j++)
                {
                    // Force an exact duplicate
                    people.Add(person);
                }
            }

            List <string> documentsToInsert = new List <string>();

            // Shuffle them so that they end up in different pages.
            people = people.OrderBy((person) => Guid.NewGuid()).ToList();
            foreach (Person person in people)
            {
                documentsToInsert.Add(JsonConvert.SerializeObject(person));
            }

            await this.CreateIngestQueryDeleteAsync(
                ConnectionModes.Direct,
                CollectionTypes.SinglePartition | CollectionTypes.MultiPartition,
                documentsToInsert,
                ImplementationAsync,
                "/id");

            async Task ImplementationAsync(
                Container container,
                IReadOnlyList <CosmosObject> documents)
            {
                foreach (int offsetCount in new int[] { 0, 1, 10, documents.Count })
                {
                    foreach (int limitCount in new int[] { 0, 1, 10, documents.Count })
                    {
                        foreach (int pageSize in new int[] { 1, 10, documents.Count })
                        {
                            string query = $@"
                                SELECT VALUE c.guid
                                FROM c
                                ORDER BY c.guid
                                OFFSET {offsetCount} LIMIT {limitCount}";

                            QueryRequestOptions queryRequestOptions = new QueryRequestOptions()
                            {
                                MaxItemCount         = pageSize,
                                MaxBufferedItemCount = 1000,
                                MaxConcurrency       = 2
                            };

                            IEnumerable <CosmosElement> expectedResults = documents;

                            // ORDER BY
                            expectedResults = expectedResults.OrderBy(x => (x as CosmosObject)["guid"].ToString(), StringComparer.Ordinal);

                            // SELECT VALUE c.name
                            expectedResults = expectedResults.Select(document => (document as CosmosObject)["guid"]);

                            // SKIP TAKE
                            expectedResults = expectedResults.Skip(offsetCount);
                            expectedResults = expectedResults.Take(limitCount);

                            List <CosmosElement> queryResults = await QueryTestsBase.RunQueryAsync(
                                container,
                                query,
                                queryRequestOptions : queryRequestOptions);

                            Assert.IsTrue(
                                expectedResults.SequenceEqual(queryResults),
                                $@"
                                {query} (without continuations) didn't match
                                expected: {JsonConvert.SerializeObject(expectedResults)}
                                actual: {JsonConvert.SerializeObject(queryResults)}");
                        }
                    }
                }
            }
        }