public async Task ExecutionLogsTests()
        {
            const string testLogsText    = "this is a test";
            const string testPartitionId = "1";
            string       sprocId         = Guid.NewGuid().ToString();
            string       sprocBody       = @"function(name) { var context = getContext(); console.log('" + testLogsText + "'); var response = context.getResponse(); response.setBody('hello there ' + name); }";

            StoredProcedureResponse storedProcedureResponse =
                await this.scripts.CreateStoredProcedureAsync(new CosmosStoredProcedureProperties(sprocId, sprocBody));

            double requestCharge = storedProcedureResponse.RequestCharge;

            Assert.IsTrue(requestCharge > 0);
            Assert.AreEqual(HttpStatusCode.Created, storedProcedureResponse.StatusCode);
            StoredProcedureTests.ValidateStoredProcedureSettings(sprocId, sprocBody, storedProcedureResponse);

            CosmosStoredProcedureProperties         storedProcedure = storedProcedureResponse;
            StoredProcedureExecuteResponse <string> sprocResponse   = await this.scripts.ExecuteStoredProcedureAsync <string, string>(
                new Cosmos.PartitionKey(testPartitionId),
                sprocId,
                Guid.NewGuid().ToString(),
                new StoredProcedureRequestOptions()
            {
                EnableScriptLogging = true
            });

            Assert.AreEqual(HttpStatusCode.OK, sprocResponse.StatusCode);
            Assert.AreEqual(testLogsText, sprocResponse.ScriptLog);
        }
        public async Task ExecuteTestWithParameter()
        {
            string sprocId   = Guid.NewGuid().ToString();
            string sprocBody = @"function(param1) {
                var context = getContext();
                var response = context.getResponse();
                response.setBody(param1);
            }";

            StoredProcedureResponse storedProcedureResponse =
                await this.scripts.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 this.container.CreateItemAsync <dynamic>(payload);

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

            StoredProcedureExecuteResponse <string> sprocResponse = await this.scripts.ExecuteStoredProcedureAsync <string>(
                sprocId,
                new Cosmos.PartitionKey(testPartitionId),
                parameters : new dynamic[] { "one" });

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

            string stringResponse = sprocResponse.Resource;

            Assert.IsNotNull(stringResponse);
            Assert.AreEqual("one", stringResponse);

            ResponseMessage response = await this.scripts.ExecuteStoredProcedureStreamAsync(
                sprocId,
                new Cosmos.PartitionKey(testPartitionId),
                parameters : new dynamic[] { null });

            using (StreamReader reader = new StreamReader(response.Content))
            {
                string text = await reader.ReadToEndAsync();

                Assert.AreEqual("null", text);
            }

            sprocResponse = await this.scripts.ExecuteStoredProcedureAsync <string>(
                sprocId,
                new Cosmos.PartitionKey(testPartitionId),
                parameters : new dynamic[] { null });

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

            stringResponse = sprocResponse.Resource;
            Assert.IsNull(stringResponse);

            StoredProcedureResponse deleteResponse = await this.scripts.DeleteStoredProcedureAsync(sprocId);

            Assert.AreEqual(HttpStatusCode.NoContent, deleteResponse.StatusCode);
        }
        public async Task ExecutionLogsAsStreamTests()
        {
            const string testLogsText    = "this is a test";
            const string testPartitionId = "1";
            string       sprocId         = Guid.NewGuid().ToString();
            string       sprocBody       = @"function(name) { var context = getContext(); console.log('" + testLogsText + "'); var response = context.getResponse(); response.setBody('hello there ' + name); }";

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

            double requestCharge = storedProcedureResponse.RequestCharge;

            Assert.IsTrue(requestCharge > 0);
            Assert.AreEqual(HttpStatusCode.Created, storedProcedureResponse.StatusCode);
            StoredProcedureTests.ValidateStoredProcedureSettings(sprocId, sprocBody, storedProcedureResponse);



            StoredProcedureProperties storedProcedure = storedProcedureResponse;
            ResponseMessage           sprocResponse   = await this.scripts.ExecuteStoredProcedureStreamAsync(
                sprocId,
                new Cosmos.PartitionKey(testPartitionId),
                new dynamic[] { Guid.NewGuid().ToString() },
                new StoredProcedureRequestOptions()
            {
                EnableScriptLogging = true
            });

            Assert.AreEqual(HttpStatusCode.OK, sprocResponse.StatusCode);
            Assert.AreEqual(testLogsText, Uri.UnescapeDataString(sprocResponse.Headers["x-ms-documentdb-script-log-results"]));
        }
        public async Task CRUDTest()
        {
            string sprocId   = Guid.NewGuid().ToString();
            string sprocBody = "function() { { var x = 42; } }";

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

            double requestCharge = storedProcedureResponse.RequestCharge;

            Assert.IsTrue(requestCharge > 0);
            Assert.AreEqual(HttpStatusCode.Created, storedProcedureResponse.StatusCode);
            Assert.IsNotNull(storedProcedureResponse.Diagnostics);
            string diagnostics = storedProcedureResponse.Diagnostics.ToString();

            Assert.IsFalse(string.IsNullOrEmpty(diagnostics));
            Assert.IsTrue(diagnostics.Contains("StatusCode"));

            StoredProcedureTests.ValidateStoredProcedureSettings(sprocId, sprocBody, storedProcedureResponse);

            storedProcedureResponse = await this.scripts.ReadStoredProcedureAsync(sprocId);

            requestCharge = storedProcedureResponse.RequestCharge;
            Assert.IsTrue(requestCharge > 0);
            Assert.AreEqual(HttpStatusCode.OK, storedProcedureResponse.StatusCode);
            Assert.IsNotNull(storedProcedureResponse.Diagnostics);
            diagnostics = storedProcedureResponse.Diagnostics.ToString();
            Assert.IsFalse(string.IsNullOrEmpty(diagnostics));
            Assert.IsTrue(diagnostics.Contains("StatusCode"));
            StoredProcedureTests.ValidateStoredProcedureSettings(sprocId, sprocBody, storedProcedureResponse);

            string updatedBody = @"function(name) { var context = getContext();
                    var response = context.getResponse();
                    response.setBody(""hello there "" + name);
                }";
            StoredProcedureResponse replaceResponse = await this.scripts.ReplaceStoredProcedureAsync(new StoredProcedureProperties(sprocId, updatedBody));

            StoredProcedureTests.ValidateStoredProcedureSettings(sprocId, updatedBody, replaceResponse);
            requestCharge = replaceResponse.RequestCharge;
            Assert.IsTrue(requestCharge > 0);
            Assert.AreEqual(HttpStatusCode.OK, replaceResponse.StatusCode);
            Assert.IsNotNull(replaceResponse.Diagnostics);
            diagnostics = replaceResponse.Diagnostics.ToString();
            Assert.IsFalse(string.IsNullOrEmpty(diagnostics));
            Assert.IsTrue(diagnostics.Contains("StatusCode"));
            StoredProcedureTests.ValidateStoredProcedureSettings(sprocId, updatedBody, replaceResponse);


            StoredProcedureResponse deleteResponse = await this.scripts.DeleteStoredProcedureAsync(sprocId);

            requestCharge = deleteResponse.RequestCharge;
            Assert.IsTrue(requestCharge > 0);
            Assert.AreEqual(HttpStatusCode.NoContent, deleteResponse.StatusCode);
            Assert.IsNotNull(deleteResponse.Diagnostics);
            diagnostics = deleteResponse.Diagnostics.ToString();
            Assert.IsFalse(string.IsNullOrEmpty(diagnostics));
            Assert.IsTrue(diagnostics.Contains("StatusCode"));
        }
        public async Task ExecuteTestWithArraylikeParameters()
        {
            string sprocId   = Guid.NewGuid().ToString();
            string sprocBody = @"function(param1, param2, param3) {
                var context = getContext();
                var response = context.getResponse();
                response.setBody(param1 + (param2 || '') + (param3 || ''));
            }";

            StoredProcedureResponse storedProcedureResponse =
                await this.scripts.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 this.container.CreateItemAsync <dynamic>(payload);

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

            MemoryStream[] streamPayloads = new MemoryStream[]
            {
                new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":""onetwothree"", ""length"": 1}")),
                new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":""onetwothree"", ""length"": ""1""}")),                      // Javascript treats length string as a numebr
                new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":""onetwothree"", ""length"": 10000}")),                      // Tons of extra arguments which aren't actually there
                new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":""onetwothree"", ""1"": ""test"", ""length"": 1}")),         // function.apply will not see the [1] parameter because length == 1
                new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":""onetwothree"", ""1"": ""test"", ""length"": 1.5}")),       // function.apply will not see the [1] parameter because length rounds down to 1
                new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":""one"", ""1"": ""twothree"", ""length"": 2}")),
                new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":""one"", ""2"": ""twothree"", ""length"": 3}")),             // Skipping a parameter is okay
                new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":""one"", ""2"": ""three"", ""1"": ""two"", ""length"": 3}")) // out of order is okay because arrays are just numeric indexers in javascript
            };

            foreach (MemoryStream streamPayload in streamPayloads)
            {
                ResponseMessage response = await this.scripts.ExecuteStoredProcedureStreamAsync(
                    storedProcedureId : sprocId,
                    streamPayload : streamPayload,
                    partitionKey : new Cosmos.PartitionKey(testPartitionId),
                    requestOptions : null,
                    cancellationToken : default(CancellationToken));

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

                using (StreamReader reader = new StreamReader(response.Content))
                {
                    string text = await reader.ReadToEndAsync();

                    Assert.AreEqual(@"""onetwothree""", text);
                }
            }

            StoredProcedureResponse deleteResponse = await this.scripts.DeleteStoredProcedureAsync(sprocId);

            Assert.AreEqual(HttpStatusCode.NoContent, deleteResponse.StatusCode);
        }
        public async Task ExecuteTestWithStreamParameter()
        {
            string sprocId   = Guid.NewGuid().ToString();
            string sprocBody = @"function(param1) {
                var context = getContext();
                var response = context.getResponse();
                response.setBody(param1);
            }";

            StoredProcedureResponse storedProcedureResponse =
                await this.scripts.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 this.container.CreateItemAsync <dynamic>(payload);

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

            MemoryStream streamPayload = new MemoryStream(Encoding.UTF8.GetBytes(@"[""one""]"));

            ResponseMessage response = await this.scripts.ExecuteStoredProcedureStreamAsync(
                sprocId,
                streamPayload : streamPayload,
                partitionKey : new Cosmos.PartitionKey(testPartitionId));

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

            using (StreamReader reader = new StreamReader(response.Content))
            {
                string text = await reader.ReadToEndAsync();

                Assert.AreEqual(@"""one""", text);
            }

            streamPayload = new MemoryStream(Encoding.UTF8.GetBytes("[null]"));

            response = await this.scripts.ExecuteStoredProcedureStreamAsync(
                sprocId,
                streamPayload,
                new Cosmos.PartitionKey(testPartitionId));

            using (StreamReader reader = new StreamReader(response.Content))
            {
                string text = await reader.ReadToEndAsync();

                Assert.AreEqual("null", text);
            }

            StoredProcedureResponse deleteResponse = await this.scripts.DeleteStoredProcedureAsync(sprocId);

            Assert.AreEqual(HttpStatusCode.NoContent, deleteResponse.StatusCode);
        }
        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);
        }
        public async Task ExecuteTestWithTechnicallyJSONParseableButNotArraylikeParameters()
        {
            string sprocId   = Guid.NewGuid().ToString();
            string sprocBody = @"function(param1) {
                var context = getContext();
                var response = context.getResponse();
                response.setBody(typeof param1);
            }";

            StoredProcedureResponse storedProcedureResponse =
                await this.scripts.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 this.container.CreateItemAsync <dynamic>(payload);

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

            MemoryStream[] streamPayloads = new MemoryStream[]
            {
                new MemoryStream(Encoding.UTF8.GetBytes(@"null")),
                new MemoryStream(Encoding.UTF8.GetBytes(@"{}")),
                new MemoryStream(Encoding.UTF8.GetBytes(@"{""param1"":4}")),
                new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":4}"))
            };

            foreach (MemoryStream streamPayload in streamPayloads)
            {
                ResponseMessage response = await this.scripts.ExecuteStoredProcedureStreamAsync(
                    storedProcedureId : sprocId,
                    streamPayload : streamPayload,
                    partitionKey : new Cosmos.PartitionKey(testPartitionId),
                    requestOptions : null,
                    cancellationToken : default(CancellationToken));

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

                using (StreamReader reader = new StreamReader(response.Content))
                {
                    string text = await reader.ReadToEndAsync();

                    Assert.AreEqual(@"""undefined""", text);
                }
            }

            StoredProcedureResponse deleteResponse = await this.scripts.DeleteStoredProcedureAsync(sprocId);

            Assert.AreEqual(HttpStatusCode.NoContent, deleteResponse.StatusCode);
        }
        public async Task ExecuteTestAsStream()
        {
            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 this.scripts.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 this.container.CreateItemAsync <dynamic>(payload);

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

            StoredProcedureProperties storedProcedure = storedProcedureResponse;
            ResponseMessage           sprocResponse   = await this.scripts.ExecuteStoredProcedureStreamAsync(
                sprocId,
                new Cosmos.PartitionKey(testPartitionId),
                null);

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

            using (StreamReader sr = new System.IO.StreamReader(sprocResponse.Content))
            {
                string stringResponse = sr.ReadToEnd();
                JArray jArray         = JArray.Parse(stringResponse);
                Assert.AreEqual(1, jArray.Count);
            }

            StoredProcedureResponse deleteResponse = await this.scripts.DeleteStoredProcedureAsync(sprocId);

            Assert.AreEqual(HttpStatusCode.NoContent, deleteResponse.StatusCode);
        }
        public async Task ExecuteTestWithNonObjectStreamParameters()
        {
            string sprocId   = Guid.NewGuid().ToString();
            string sprocBody = @"function(param1) {
                var context = getContext();
                var response = context.getResponse();
                response.setBody(param1);
            }";

            StoredProcedureResponse storedProcedureResponse =
                await this.scripts.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 this.container.CreateItemAsync <dynamic>(payload);

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

            MemoryStream[] streamPayloads = new MemoryStream[]
            {
                new MemoryStream(Encoding.UTF8.GetBytes(@"""""")),
                new MemoryStream(Encoding.UTF8.GetBytes(@"""hello""")),
                new MemoryStream(Encoding.UTF8.GetBytes(@"0")),
                new MemoryStream(Encoding.UTF8.GetBytes(@"undefined")),
                new MemoryStream(Encoding.UTF8.GetBytes(@"-1.25")),
                new MemoryStream(Encoding.UTF8.GetBytes(@"false")),
                new MemoryStream(Encoding.UTF8.GetBytes(@"[x:4]")) // invalid json
            };

            foreach (MemoryStream streamPayload in streamPayloads)
            {
                ResponseMessage response = await this.scripts.ExecuteStoredProcedureStreamAsync(
                    storedProcedureId : sprocId,
                    streamPayload : streamPayload,
                    partitionKey : new Cosmos.PartitionKey(testPartitionId),
                    requestOptions : null,
                    cancellationToken : default(CancellationToken));

                Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
            }

            StoredProcedureResponse deleteResponse = await this.scripts.DeleteStoredProcedureAsync(sprocId);

            Assert.AreEqual(HttpStatusCode.NoContent, deleteResponse.StatusCode);
        }
        public async Task ExecuteTestWithParameter()
        {
            string sprocId   = Guid.NewGuid().ToString();
            string sprocBody = @"function(param1) {
                var context = getContext();
                var response = context.getResponse();
                response.setBody(param1);
            }";

            CosmosStoredProcedureResponse storedProcedureResponse =
                await this.container.StoredProcedures.CreateStoredProceducreAsync(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 };
            CosmosItemResponse <dynamic> createItemResponse = await this.container.Items.CreateItemAsync <dynamic>(testPartitionId, payload);

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

            CosmosStoredProcedure       storedProcedure = storedProcedureResponse.StoredProcedure;
            CosmosItemResponse <string> sprocResponse   = await storedProcedure.ExecuteAsync <string[], string>(testPartitionId, new string[] { "one" });

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

            string stringResponse = sprocResponse.Resource;

            Assert.IsNotNull(stringResponse);
            Assert.AreEqual("one", stringResponse);

            CosmosItemResponse <string> sprocResponse2 = await storedProcedure.ExecuteAsync <string, string>(testPartitionId, "one");

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

            string stringResponse2 = sprocResponse2.Resource;

            Assert.IsNotNull(stringResponse2);
            Assert.AreEqual("one", stringResponse2);

            storedProcedureResponse = await storedProcedureResponse.StoredProcedure.DeleteAsync();

            Assert.AreEqual(HttpStatusCode.NoContent, storedProcedureResponse.StatusCode);
        }
        public async Task ExecuteTest()
        {
            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);
                    }
                );
            }";

            CosmosStoredProcedureResponse storedProcedureResponse =
                await this.container.StoredProcedures.CreateStoredProceducreAsync(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 };
            CosmosItemResponse <dynamic> createItemResponse = await this.container.Items.CreateItemAsync <dynamic>(testPartitionId, payload);

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

            CosmosStoredProcedure       storedProcedure = storedProcedureResponse;
            CosmosItemResponse <JArray> sprocResponse   = await storedProcedure.ExecuteAsync <object, JArray>(testPartitionId, null);

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

            JArray jArray = sprocResponse;

            Assert.AreEqual(1, jArray.Count);

            storedProcedureResponse = await storedProcedureResponse.StoredProcedure.DeleteAsync();

            Assert.AreEqual(HttpStatusCode.NoContent, storedProcedureResponse.StatusCode);
        }
        public async Task ExecuteTestWithNonAppliableArraylikeParameters()
        {
            string sprocId   = Guid.NewGuid().ToString();
            string sprocBody = @"function() {
                var context = getContext();
                var response = context.getResponse();
                response.setBody(true);
            }";

            StoredProcedureResponse storedProcedureResponse =
                await this.scripts.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 this.container.CreateItemAsync <dynamic>(payload);

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

            MemoryStream[] streamPayloads = new MemoryStream[]
            {
                new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":""onetwothree"", ""length"": 600000}")), // 600000 parameters -> "out of stack space"
                new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":""onetwothree"", ""length"": 1e9}")),    // 1 billion parameters -> exceeds javascript .apply maximum
            };

            foreach (MemoryStream streamPayload in streamPayloads)
            {
                ResponseMessage response = await this.scripts.ExecuteStoredProcedureStreamAsync(
                    storedProcedureId : sprocId,
                    streamPayload : streamPayload,
                    partitionKey : new Cosmos.PartitionKey(testPartitionId),
                    requestOptions : null,
                    cancellationToken : default(CancellationToken));

                Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
            }

            StoredProcedureResponse deleteResponse = await this.scripts.DeleteStoredProcedureAsync(sprocId);

            Assert.AreEqual(HttpStatusCode.NoContent, deleteResponse.StatusCode);
        }
        public async Task CRUDTest()
        {
            string sprocId   = Guid.NewGuid().ToString();
            string sprocBody = "function() { { var x = 42; } }";

            CosmosStoredProcedureResponse storedProcedureResponse =
                await this.container.StoredProcedures.CreateStoredProceducreAsync(sprocId, sprocBody);

            double requestCharge = storedProcedureResponse.RequestCharge;

            Assert.IsTrue(requestCharge > 0);
            Assert.AreEqual(HttpStatusCode.Created, storedProcedureResponse.StatusCode);
            StoredProcedureTests.ValidateStoredProcedureSettings(sprocId, sprocBody, storedProcedureResponse);

            storedProcedureResponse = await storedProcedureResponse.StoredProcedure.ReadAsync();

            requestCharge = storedProcedureResponse.RequestCharge;
            Assert.IsTrue(requestCharge > 0);
            Assert.AreEqual(HttpStatusCode.OK, storedProcedureResponse.StatusCode);
            StoredProcedureTests.ValidateStoredProcedureSettings(sprocId, sprocBody, storedProcedureResponse);

            string updatedBody = @"function(name) { var context = getContext();
                    var response = context.getResponse();
                    response.setBody(""hello there "" + name);
                }";
            CosmosStoredProcedureResponse replaceResponse = await storedProcedureResponse.StoredProcedure.ReplaceAsync(updatedBody);

            StoredProcedureTests.ValidateStoredProcedureSettings(sprocId, updatedBody, replaceResponse);
            requestCharge = replaceResponse.RequestCharge;
            Assert.IsTrue(requestCharge > 0);
            Assert.AreEqual(HttpStatusCode.OK, replaceResponse.StatusCode);
            StoredProcedureTests.ValidateStoredProcedureSettings(sprocId, updatedBody, replaceResponse);


            storedProcedureResponse = await replaceResponse.StoredProcedure.DeleteAsync();

            requestCharge = storedProcedureResponse.RequestCharge;
            Assert.IsTrue(requestCharge > 0);
            Assert.AreEqual(HttpStatusCode.NoContent, storedProcedureResponse.StatusCode);
        }
        public async Task ExecuteTestWithMultipleParameters()
        {
            string sprocId   = Guid.NewGuid().ToString();
            string sprocBody = @"function(param1, param2, param3) {
                var context = getContext();
                var response = context.getResponse();
                response.setBody(param1+param2+param3);
            }";

            StoredProcedureResponse storedProcedureResponse =
                await this.scripts.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 this.container.CreateItemAsync <dynamic>(payload);

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

            StoredProcedureExecuteResponse <string> sprocResponse2 = await this.scripts.ExecuteStoredProcedureAsync <string>(
                storedProcedureId : sprocId,
                partitionKey : new Cosmos.PartitionKey(testPartitionId),
                parameters : new dynamic[] { "one", "two", "three" },
                requestOptions : null,
                cancellationToken : default(CancellationToken));

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

            string stringResponse2 = sprocResponse2.Resource;

            Assert.IsNotNull(stringResponse2);
            Assert.AreEqual("onetwothree", stringResponse2);

            StoredProcedureResponse deleteResponse = await this.scripts.DeleteStoredProcedureAsync(sprocId);

            Assert.AreEqual(HttpStatusCode.NoContent, deleteResponse.StatusCode);
        }