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);
        }
示例#2
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);
        }