示例#1
0
        internal static async Task CreateItemInNonPartitionedContainer(
            ContainerCore container,
            string itemId)
        {
            (string endpoint, string authKey)accountInfo = TestCommon.GetAccountInfo();
            //Creating non partition Container item.
            HttpClient client       = new System.Net.Http.HttpClient();
            Uri        baseUri      = new Uri(accountInfo.endpoint);
            string     dateTimeUtc  = NonPartitionedContainerHelper.GetUtcDateTime();
            string     verb         = "POST";
            string     resourceType = "docs";
            string     resourceLink = string.Format("dbs/{0}/colls/{1}/docs", container.Database.Id, container.Id);
            string     authHeader   = NonPartitionedContainerHelper.GenerateMasterKeyAuthorizationSignature(
                verb, container.LinkUri.OriginalString,
                resourceType,
                accountInfo.authKey,
                "master",
                "1.0",
                dateTimeUtc);

            client.DefaultRequestHeaders.Add("x-ms-date", dateTimeUtc);
            client.DefaultRequestHeaders.Add("x-ms-version", NonPartitionedContainerHelper.PreNonPartitionedMigrationApiVersion);
            client.DefaultRequestHeaders.Add("authorization", authHeader);

            string itemDefinition = JsonConvert.SerializeObject(ToDoActivity.CreateRandomToDoActivity(id: itemId));
            {
                StringContent       itemContent = new StringContent(itemDefinition);
                Uri                 requestUri  = new Uri(baseUri, resourceLink);
                HttpResponseMessage response    = await client.PostAsync(requestUri.ToString(), itemContent);

                Assert.AreEqual(HttpStatusCode.Created, response.StatusCode, response.ToString());
            }
        }
        internal static async Task CreateUndefinedPartitionItem(
            ContainerCore container,
            string itemId)
        {
            (string endpoint, string authKey)accountInfo = TestCommon.GetAccountInfo();
            //Creating undefined partition key  item, rest api used instead of .NET SDK api as it is not supported anymore.
            HttpClient client  = new System.Net.Http.HttpClient();
            Uri        baseUri = new Uri(accountInfo.endpoint);

            client.DefaultRequestHeaders.Add("x-ms-date", utc_date);
            client.DefaultRequestHeaders.Add("x-ms-version", NonPartitionedContainerHelper.PreNonPartitionedMigrationApiVersion);
            client.DefaultRequestHeaders.Add("x-ms-documentdb-partitionkey", "[{}]");

            //Creating undefined partition Container item.
            string verb         = "POST";
            string resourceType = "docs";
            string resourceId   = container.LinkUri.OriginalString;
            string resourceLink = string.Format("dbs/{0}/colls/{1}/docs", container.Database.Id, container.Id);
            string authHeader   = NonPartitionedContainerHelper.GenerateMasterKeyAuthorizationSignature(
                verb,
                resourceId,
                resourceType,
                accountInfo.authKey,
                "master",
                "1.0");

            client.DefaultRequestHeaders.Remove("authorization");
            client.DefaultRequestHeaders.Add("authorization", authHeader);

            var           payload        = new { id = itemId, user = itemId };
            string        itemDefinition = JsonConvert.SerializeObject(payload);
            StringContent itemContent    = new StringContent(itemDefinition);
            Uri           requestUri     = new Uri(baseUri, resourceLink);
            await client.PostAsync(requestUri.ToString(), itemContent);
        }
        public async Task ExecuteNonePkTest()
        {
            // Create a container in v2 without a partition key
            string            containerId     = "SprocPkNone" + Guid.NewGuid().ToString();
            ContainerInternal containerNonePk = await NonPartitionedContainerHelper.CreateNonPartitionedContainer(this.database, containerId);

            Scripts scriptsNonePk = containerNonePk.Scripts;

            string sprocId   = Guid.NewGuid().ToString();
            string sprocBody = @"function() {
                var context = getContext();
                var response = context.getResponse();
                var collection = context.getCollection();
                var collectionLink = collection.getSelfLink();

                var filterQuery = 'SELECT * FROM c';

                collection.queryDocuments(collectionLink, filterQuery, { },
                    function(err, documents) {
                        response.setBody(documents);
                    }
                );
            }";

            StoredProcedureResponse storedProcedureResponse =
                await scriptsNonePk.CreateStoredProcedureAsync(new StoredProcedureProperties(sprocId, sprocBody));

            Assert.AreEqual(HttpStatusCode.Created, storedProcedureResponse.StatusCode);
            StoredProcedureTests.ValidateStoredProcedureSettings(sprocId, sprocBody, storedProcedureResponse);

            // Insert document and then query
            string testPartitionId = Guid.NewGuid().ToString();
            var    payload         = new { id = testPartitionId, user = testPartitionId };
            ItemResponse <dynamic> createItemResponse = await containerNonePk.CreateItemAsync <dynamic>(payload);

            Assert.AreEqual(HttpStatusCode.Created, createItemResponse.StatusCode);

            StoredProcedureProperties storedProcedure             = storedProcedureResponse;
            StoredProcedureExecuteResponse <JArray> sprocResponse = await scriptsNonePk.ExecuteStoredProcedureAsync <JArray>(
                sprocId,
                Cosmos.PartitionKey.None,
                parameters : null);

            Assert.AreEqual(HttpStatusCode.OK, sprocResponse.StatusCode);

            JArray jArray = sprocResponse;

            Assert.AreEqual(1, jArray.Count);
            Assert.AreEqual(testPartitionId, jArray[0]["id"]);

            StoredProcedureResponse deleteResponse = await scriptsNonePk.DeleteStoredProcedureAsync(sprocId);

            Assert.AreEqual(HttpStatusCode.NoContent, deleteResponse.StatusCode);
        }
示例#4
0
        internal static async Task <DocumentCollection> CreateNonPartitionedContainer(
            Cosmos.Database database,
            DocumentCollection documentCollection)
        {
            (string endpoint, string authKey)accountInfo = TestCommon.GetAccountInfo();

            //Creating non partition Container, rest api used instead of .NET SDK api as it is not supported anymore.
            HttpClient client      = new System.Net.Http.HttpClient();
            Uri        baseUri     = new Uri(accountInfo.endpoint);
            string     dateTimeUtc = NonPartitionedContainerHelper.GetUtcDateTime();

            string verb         = "POST";
            string resourceType = "colls";
            string resourceId   = string.Format("dbs/{0}", database.Id);
            string resourceLink = string.Format("dbs/{0}/colls", database.Id);

            client.DefaultRequestHeaders.Add("x-ms-date", dateTimeUtc);
            client.DefaultRequestHeaders.Add("x-ms-version", NonPartitionedContainerHelper.PreNonPartitionedMigrationApiVersion);

            string authHeader = NonPartitionedContainerHelper.GenerateMasterKeyAuthorizationSignature(
                verb,
                resourceId,
                resourceType,
                accountInfo.authKey,
                "master",
                "1.0",
                dateTimeUtc);

            client.DefaultRequestHeaders.Add("authorization", authHeader);
            string        containerDefinition = documentCollection.ToString();
            StringContent containerContent    = new StringContent(containerDefinition);
            Uri           requestUri          = new Uri(baseUri, resourceLink);

            DocumentCollection responseCollection = null;

            using (HttpResponseMessage response = await client.PostAsync(requestUri.ToString(), containerContent))
            {
                response.EnsureSuccessStatusCode();
                Assert.AreEqual(HttpStatusCode.Created, response.StatusCode, response.ToString());
                responseCollection = await response.Content.ToResourceAsync <DocumentCollection>();
            }

            return(responseCollection);
        }
示例#5
0
        internal static async Task <ContainerCore> CreateNonPartitionedContainer(
            Cosmos.Database database,
            string containerId,
            string indexingPolicy = null)
        {
            DocumentCollection documentCollection = new DocumentCollection()
            {
                Id = containerId
            };

            if (indexingPolicy != null)
            {
                documentCollection.IndexingPolicy = JsonConvert.DeserializeObject <IndexingPolicy>(indexingPolicy);
            }

            await NonPartitionedContainerHelper.CreateNonPartitionedContainer(
                database,
                documentCollection);

            return((ContainerInlineCore)database.GetContainer(containerId));
        }
        public async Task ContainerMigrationTest()
        {
            string containerName = "MigrationIndexTest";

            Documents.Index index1 = new Documents.RangeIndex(Documents.DataType.String, -1);
            Documents.Index index2 = new Documents.RangeIndex(Documents.DataType.Number, -1);
            Documents.DocumentCollection documentCollection = new Microsoft.Azure.Documents.DocumentCollection()
            {
                Id             = containerName,
                IndexingPolicy = new Documents.IndexingPolicy()
                {
                    IncludedPaths = new Collection <Documents.IncludedPath>()
                    {
                        new Documents.IncludedPath()
                        {
                            Path    = "/*",
                            Indexes = new Collection <Documents.Index>()
                            {
                                index1,
                                index2
                            }
                        }
                    }
                }
            };

            Documents.DocumentCollection createResponse = await NonPartitionedContainerHelper.CreateNonPartitionedContainer(this.database, documentCollection);

            // Verify the collection was created with deprecated Index objects
            Assert.AreEqual(2, createResponse.IndexingPolicy.IncludedPaths.First().Indexes.Count);
            Documents.Index createIndex = createResponse.IndexingPolicy.IncludedPaths.First().Indexes.First();
            Assert.AreEqual(index1.Kind, createIndex.Kind);

            // Verify v3 can add composite indexes and update the container
            Container           container           = this.database.GetContainer(containerName);
            ContainerProperties containerProperties = await container.ReadContainerAsync();

            Assert.IsNotNull(containerProperties.SelfLink);
            string cPath0 = "/address/city";
            string cPath1 = "/address/state";

            containerProperties.IndexingPolicy.CompositeIndexes.Add(new Collection <CompositePath>()
            {
                new CompositePath()
                {
                    Path  = cPath0,
                    Order = CompositePathSortOrder.Descending
                },
                new CompositePath()
                {
                    Path  = cPath1,
                    Order = CompositePathSortOrder.Ascending
                }
            });

            containerProperties.IndexingPolicy.SpatialIndexes.Add(
                new SpatialPath()
            {
                Path         = "/address/test/*",
                SpatialTypes = new Collection <SpatialType>()
                {
                    SpatialType.Point
                }
            });

            ContainerProperties propertiesAfterReplace = await container.ReplaceContainerAsync(containerProperties);

            Assert.AreEqual(0, propertiesAfterReplace.IndexingPolicy.IncludedPaths.First().Indexes.Count);
            Assert.AreEqual(1, propertiesAfterReplace.IndexingPolicy.CompositeIndexes.Count);
            Collection <CompositePath> compositePaths = propertiesAfterReplace.IndexingPolicy.CompositeIndexes.First();

            Assert.AreEqual(2, compositePaths.Count);
            CompositePath compositePath0 = compositePaths.ElementAt(0);
            CompositePath compositePath1 = compositePaths.ElementAt(1);

            Assert.IsTrue(string.Equals(cPath0, compositePath0.Path) || string.Equals(cPath1, compositePath0.Path));
            Assert.IsTrue(string.Equals(cPath0, compositePath1.Path) || string.Equals(cPath1, compositePath1.Path));

            Assert.AreEqual(1, propertiesAfterReplace.IndexingPolicy.SpatialIndexes.Count);
            Assert.AreEqual("/address/test/*", propertiesAfterReplace.IndexingPolicy.SpatialIndexes.First().Path);
        }