public void TestInitialize() { this.client = TestCommon.CreateClient(true); this.database = TestCommon.CreateOrGetDatabase(this.client); this.collection = new CosmosContainerSettings() { Id = Guid.NewGuid().ToString() }; this.collection.IndexingPolicy.IndexingMode = IndexingMode.Lazy; try { this.collection = this.client.CreateDocumentCollectionAsync(this.database, this.collection).Result; } catch (DocumentClientException exception) { Assert.Fail(exception.InnerException.Message); } this.triggerName = "uniqueConstraint_" + Guid.NewGuid().ToString("N"); string triggerContent = File.ReadAllText("ScriptSampleTests_UniqueConstraint.js"); CosmosTriggerSettings triggerResource = new CosmosTriggerSettings { Id = this.triggerName, Body = triggerContent, TriggerOperation = TriggerOperation.All, TriggerType = TriggerType.Pre }; CosmosTriggerSettings trigger = this.client.CreateTriggerAsync(this.collection.SelfLink, triggerResource).Result; }
private static void ValidateTriggerSettings(CosmosTriggerSettings triggerSettings, TriggerResponse cosmosResponse) { CosmosTriggerSettings settings = cosmosResponse.Resource; Assert.AreEqual(triggerSettings.Body, settings.Body, "Trigger function do not match"); Assert.AreEqual(triggerSettings.Id, settings.Id, "Trigger id do not match"); Assert.IsTrue(cosmosResponse.RequestCharge > 0); Assert.IsNotNull(cosmosResponse.MaxResourceQuota); Assert.IsNotNull(cosmosResponse.CurrentResourceQuotaUsage); }
public async Task CRUDTest() { CosmosTriggerSettings settings = new CosmosTriggerSettings { Id = Guid.NewGuid().ToString(), Body = TriggersTests.GetTriggerFunction(".05"), TriggerOperation = Scripts.TriggerOperation.Create, TriggerType = Scripts.TriggerType.Pre }; TriggerResponse triggerResponse = await this.scripts.CreateTriggerAsync(settings); double reqeustCharge = triggerResponse.RequestCharge; Assert.IsTrue(reqeustCharge > 0); Assert.AreEqual(HttpStatusCode.Created, triggerResponse.StatusCode); TriggersTests.ValidateTriggerSettings(settings, triggerResponse); triggerResponse = await this.scripts.ReadTriggerAsync(settings.Id); reqeustCharge = triggerResponse.RequestCharge; Assert.IsTrue(reqeustCharge > 0); Assert.AreEqual(HttpStatusCode.OK, triggerResponse.StatusCode); TriggersTests.ValidateTriggerSettings(settings, triggerResponse); CosmosTriggerSettings updatedSettings = triggerResponse.Resource; updatedSettings.Body = TriggersTests.GetTriggerFunction(".42"); TriggerResponse replaceResponse = await this.scripts.ReplaceTriggerAsync(updatedSettings); TriggersTests.ValidateTriggerSettings(updatedSettings, replaceResponse); reqeustCharge = replaceResponse.RequestCharge; Assert.IsTrue(reqeustCharge > 0); Assert.AreEqual(HttpStatusCode.OK, replaceResponse.StatusCode); replaceResponse = await this.scripts.DeleteTriggerAsync(updatedSettings.Id); reqeustCharge = replaceResponse.RequestCharge; Assert.IsTrue(reqeustCharge > 0); Assert.AreEqual(HttpStatusCode.NoContent, replaceResponse.StatusCode); }
public async Task TriggersIteratorTest() { CosmosTriggerSettings cosmosTrigger = await CreateRandomTrigger(); HashSet <string> settings = new HashSet <string>(); FeedIterator <CosmosTriggerSettings> iter = this.scripts.GetTriggersIterator();; while (iter.HasMoreResults) { foreach (CosmosTriggerSettings storedProcedureSettingsEntry in await iter.FetchNextSetAsync()) { settings.Add(storedProcedureSettingsEntry.Id); } } Assert.IsTrue(settings.Contains(cosmosTrigger.Id), "The iterator did not return the user defined function definition."); // Delete existing user defined functions. await this.scripts.DeleteTriggerAsync(cosmosTrigger.Id); }
public async Task ValidateTriggersTest() { // Prevent failures if previous test did not clean up correctly await this.scripts.DeleteTriggerAsync("addTax"); ToDoActivity item = new ToDoActivity() { id = Guid.NewGuid().ToString(), cost = 9001, description = "trigger_test_item", status = "Done", taskNum = 1 }; CosmosTriggerSettings cosmosTrigger = await this.scripts.CreateTriggerAsync( new CosmosTriggerSettings { Id = "addTax", Body = TriggersTests.GetTriggerFunction(".20"), TriggerOperation = Scripts.TriggerOperation.All, TriggerType = Scripts.TriggerType.Pre }); ItemRequestOptions options = new ItemRequestOptions() { PreTriggers = new List <string>() { cosmosTrigger.Id }, }; ItemResponse <dynamic> createdItem = await this.container.CreateItemAsync <dynamic>(item.status, item, options); double itemTax = createdItem.Resource.tax; Assert.AreEqual(item.cost * .20, itemTax); // Delete existing user defined functions. await this.scripts.DeleteTriggerAsync("addTax"); }
private async Task <TriggerResponse> CreateRandomTrigger() { string id = Guid.NewGuid().ToString(); string function = GetTriggerFunction(".05"); CosmosTriggerSettings settings = new CosmosTriggerSettings { Id = id, Body = function, TriggerOperation = Scripts.TriggerOperation.Create, TriggerType = Scripts.TriggerType.Pre }; //Create a user defined function TriggerResponse createResponse = await this.scripts.CreateTriggerAsync( triggerSettings : settings, cancellationToken : this.cancellationToken); ValidateTriggerSettings(settings, createResponse); return(createResponse); }
public async Task ValidateResponseFactoryJsonSerializer() { CosmosResponseMessage databaseResponse = this.CreateResponse(); CosmosResponseMessage containerResponse = this.CreateResponse(); CosmosResponseMessage storedProcedureExecuteResponse = this.CreateResponse(); CosmosResponseMessage storedProcedureResponse = this.CreateResponse(); CosmosResponseMessage triggerResponse = this.CreateResponse(); CosmosResponseMessage udfResponse = this.CreateResponse(); CosmosResponseMessage itemResponse = this.CreateResponse(); CosmosResponseMessage feedResponse = this.CreateResponse(); Mock <CosmosJsonSerializer> mockUserJsonSerializer = new Mock <CosmosJsonSerializer>(); Mock <CosmosJsonSerializer> mockDefaultJsonSerializer = new Mock <CosmosJsonSerializer>(); CosmosResponseFactory cosmosResponseFactory = new CosmosResponseFactory( defaultJsonSerializer: mockDefaultJsonSerializer.Object, userJsonSerializer: mockUserJsonSerializer.Object); // Test the user specified response mockUserJsonSerializer.Setup(x => x.FromStream <ToDoActivity>(itemResponse.Content)).Returns(new ToDoActivity()); mockUserJsonSerializer.Setup(x => x.FromStream <ToDoActivity>(storedProcedureExecuteResponse.Content)).Returns(new ToDoActivity()); mockUserJsonSerializer.Setup(x => x.FromStream <CosmosFeedResponseUtil <ToDoActivity> >(feedResponse.Content)).Returns(new CosmosFeedResponseUtil <ToDoActivity>() { Data = new Collection <ToDoActivity>() }); // Verify all the user types use the user specified version await cosmosResponseFactory.CreateItemResponseAsync <ToDoActivity>(Task.FromResult(itemResponse)); await cosmosResponseFactory.CreateStoredProcedureExecuteResponseAsync <ToDoActivity>(Task.FromResult(storedProcedureExecuteResponse)); cosmosResponseFactory.CreateResultSetQueryResponse <ToDoActivity>(feedResponse); // Throw if the setups were not called mockUserJsonSerializer.VerifyAll(); // Test the system specified response CosmosContainerSettings containerSettings = new CosmosContainerSettings("mockId", "/pk"); CosmosDatabaseSettings databaseSettings = new CosmosDatabaseSettings() { Id = "mock" }; CosmosStoredProcedureSettings cosmosStoredProcedureSettings = new CosmosStoredProcedureSettings() { Id = "mock" }; CosmosTriggerSettings cosmosTriggerSettings = new CosmosTriggerSettings() { Id = "mock" }; CosmosUserDefinedFunctionSettings cosmosUserDefinedFunctionSettings = new CosmosUserDefinedFunctionSettings() { Id = "mock" }; mockDefaultJsonSerializer.Setup(x => x.FromStream <CosmosDatabaseSettings>(databaseResponse.Content)).Returns(databaseSettings); mockDefaultJsonSerializer.Setup(x => x.FromStream <CosmosContainerSettings>(containerResponse.Content)).Returns(containerSettings); mockDefaultJsonSerializer.Setup(x => x.FromStream <CosmosStoredProcedureSettings>(storedProcedureResponse.Content)).Returns(cosmosStoredProcedureSettings); mockDefaultJsonSerializer.Setup(x => x.FromStream <CosmosTriggerSettings>(triggerResponse.Content)).Returns(cosmosTriggerSettings); mockDefaultJsonSerializer.Setup(x => x.FromStream <CosmosUserDefinedFunctionSettings>(udfResponse.Content)).Returns(cosmosUserDefinedFunctionSettings); Mock <CosmosContainer> mockContainer = new Mock <CosmosContainer>(); Mock <CosmosDatabase> mockDatabase = new Mock <CosmosDatabase>(); // Verify all the system types that should always use default await cosmosResponseFactory.CreateContainerResponseAsync(mockContainer.Object, Task.FromResult(containerResponse)); await cosmosResponseFactory.CreateDatabaseResponseAsync(mockDatabase.Object, Task.FromResult(databaseResponse)); await cosmosResponseFactory.CreateStoredProcedureResponseAsync(Task.FromResult(storedProcedureResponse)); await cosmosResponseFactory.CreateTriggerResponseAsync(Task.FromResult(triggerResponse)); await cosmosResponseFactory.CreateUserDefinedFunctionResponseAsync(Task.FromResult(udfResponse)); // Throw if the setups were not called mockDefaultJsonSerializer.VerifyAll(); }
internal static async Task PartitionedCollectionSmokeTest(DocumentClient client, bool sharedOffer = false, bool sharedThroughputCollections = false, int numberOfCollections = 1) { if (!sharedOffer && sharedThroughputCollections) { throw new ArgumentException("Shared throughput collections are not supported without shared offer"); } string uniqDatabaseName = string.Format("SmokeTest_{0}", Guid.NewGuid().ToString("N")); RequestOptions options = new RequestOptions { OfferThroughput = 50000 }; CosmosDatabaseSettings database = sharedOffer ? await client.CreateDatabaseAsync(new CosmosDatabaseSettings { Id = uniqDatabaseName }, options) : await client.CreateDatabaseAsync(new CosmosDatabaseSettings { Id = uniqDatabaseName }); Assert.AreEqual(database.AltLink, ClientTestsUtils.GenerateAltLink(uniqDatabaseName)); CosmosDatabaseSettings readbackdatabase = await client.ReadDatabaseAsync(database.SelfLink); List <dynamic> results = await ClientTestsUtils.SqlQueryDatabases(client, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqDatabaseName), 10); Assert.AreEqual(1, results.Count, "Should have queried and found 1 document"); Assert.AreEqual(database.ResourceId, ((QueryResult)results[0]).ResourceId); Assert.IsTrue((await ClientTestsUtils.ReadFeedDatabases(client)).Any((db) => db.Id == uniqDatabaseName)); results = await ClientTestsUtils.SqlQueryDatabases(client, string.Format(@"select r._rid, r.id from root r where r.id = ""{0}""", uniqDatabaseName), 10); Assert.AreEqual(1, results.Count, "Should have queried and found 1 database"); Assert.AreEqual(database.ResourceId, ((QueryResult)results[0]).ResourceId); Assert.AreEqual(database.ResourceId, (await client.ReadDatabaseAsync(database.SelfLink)).Resource.ResourceId); Assert.AreEqual(((CosmosDatabaseSettings)results[0]).AltLink, ClientTestsUtils.GenerateAltLink(uniqDatabaseName)); ArrayList testCollections = new ArrayList(); for (int i = 0; i < numberOfCollections; i++) { string uniqCollectionName = "SmokeTestCollection" + Guid.NewGuid().ToString("N"); var partitionKeyDefinition = new PartitionKeyDefinition { Paths = new System.Collections.ObjectModel.Collection <string> { "/id" }, Kind = PartitionKind.Hash }; CosmosContainerSettings collection; if (sharedThroughputCollections) { collection = await TestCommon.CreateCollectionAsync(client, database.SelfLink, new CosmosContainerSettings { Id = uniqCollectionName, PartitionKey = partitionKeyDefinition }); } else { collection = await TestCommon.CreateCollectionAsync(client, database.SelfLink, new CosmosContainerSettings { Id = uniqCollectionName, PartitionKey = partitionKeyDefinition }, options); } Assert.AreEqual(collection.AltLink, ClientTestsUtils.GenerateAltLink(uniqDatabaseName, uniqCollectionName, typeof(CosmosContainerSettings))); results = await SqlQueryCollections(client, database.SelfLink, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqCollectionName), 10); // query through database link Assert.AreEqual(1, results.Count, "Should have queried and found 1 collection"); Assert.AreEqual(collection.ResourceId, ((QueryResult)results[0]).ResourceId); results = await SqlQueryCollections(client, database.CollectionsLink, string.Format(@"select r._rid, r.id from root r where r.id = ""{0}""", uniqCollectionName), 10); // query through CollectionsLink Assert.AreEqual(1, results.Count, "Should have queried and found 1 collection"); Assert.AreEqual(collection.ResourceId, ((QueryResult)results[0]).ResourceId); Assert.AreEqual(1, (await ReadFeedCollections(client, database.SelfLink)).Count(item => item.Id == uniqCollectionName)); // read through database link Assert.AreEqual(1, (await ReadFeedCollections(client, database.CollectionsLink)).Count(item => item.Id == uniqCollectionName)); // read through CollectionsLink Assert.AreEqual(collection.ResourceId, (await client.ReadDocumentCollectionAsync(collection.SelfLink)).Resource.ResourceId); Assert.AreEqual(((CosmosContainerSettings)results[0]).AltLink, ClientTestsUtils.GenerateAltLink(uniqDatabaseName, uniqCollectionName, typeof(CosmosContainerSettings))); testCollections.Add(collection); string uniqDocumentName = "SmokeTestDocument" + Guid.NewGuid().ToString("N"); LinqGeneralBaselineTests.Book myDocument = new LinqGeneralBaselineTests.Book(); myDocument.Id = uniqDocumentName; myDocument.Title = "My Book"; //Simple Property. myDocument.Languages = new LinqGeneralBaselineTests.Language[] { new LinqGeneralBaselineTests.Language { Name = "English", Copyright = "London Publication" }, new LinqGeneralBaselineTests.Language { Name = "French", Copyright = "Paris Publication" } }; //Array Property myDocument.Author = new LinqGeneralBaselineTests.Author { Name = "Don", Location = "France" }; //Complex Property myDocument.Price = 9.99; myDocument.Editions = new List <LinqGeneralBaselineTests.Edition>() { new LinqGeneralBaselineTests.Edition() { Name = "First", Year = 2001 }, new LinqGeneralBaselineTests.Edition() { Name = "Second", Year = 2005 } }; Document document = await client.CreateDocumentAsync(collection.SelfLink, myDocument); Assert.AreEqual(document.AltLink, ClientTestsUtils.GenerateAltLink(uniqDatabaseName, uniqCollectionName, uniqDocumentName, typeof(Document))); results = await SqlQueryDocuments(client, collection.SelfLink, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqDocumentName), 10); // query through collection link Assert.AreEqual(1, results.Count, "Should have queried and found 1 document"); Assert.AreEqual(document.ResourceId, ((QueryResult)results[0]).ResourceId); results = await SqlQueryDocuments(client, collection.DocumentsLink, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqDocumentName), 10); // query through DocumentsLink Assert.AreEqual(1, results.Count, "Should have queried and found 1 document"); Assert.AreEqual(document.ResourceId, ((QueryResult)results[0]).ResourceId); Assert.AreEqual(1, (await ReadFeedDocuments(client, collection.SelfLink)).Count(item => item.Id == uniqDocumentName)); // read through collection link Assert.AreEqual(1, (await ReadFeedDocuments(client, collection.DocumentsLink)).Count(item => item.Id == uniqDocumentName)); // read through DocumentsLink if (client.QueryCompatibilityMode != QueryCompatibilityMode.SqlQuery) { //Test query with parameters results = await SqlQueryDocuments(client, collection.SelfLink, new SqlQuerySpec { QueryText = @"select r._rid from root r where r.id = @id", Parameters = new SqlParameterCollection() { new SqlParameter("@id", uniqDocumentName) } }, 10); // query through collection link Assert.AreEqual(1, results.Count, "Should have queried and found 1 document"); Assert.AreEqual(document.ResourceId, ((QueryResult)results[0]).ResourceId); } RequestOptions docReplaceRequestOptions = new RequestOptions { PartitionKey = new PartitionKey(document.Id) }; FeedOptions docReplaceFeedOptions = new FeedOptions { EnableCrossPartitionQuery = true, PartitionKey = new PartitionKey(document.Id) }; myDocument.Title = "My_Book_v2"; document = await client.ReplaceDocumentAsync(document.AltLink, myDocument); results = await SqlQueryDocuments(client, collection.SelfLink, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqDocumentName), 10, docReplaceFeedOptions); Assert.AreEqual(1, results.Count, "Should have queried and found 1 document"); Assert.AreEqual(document.ResourceId, ((QueryResult)results[0]).ResourceId); results = await SqlQueryDocuments(client, collection.SelfLink, string.Format(@"SELECT r.id, r._rid FROM root r WHERE r.id=""{0}""", uniqDocumentName), 10); // query through collection Assert.AreEqual(1, results.Count, "Should have queried and found 1 document"); results = await SqlQueryDocuments(client, collection.DocumentsLink, string.Format(@"SELECT r.id, r._rid FROM root r WHERE r.id=""{0}""", uniqDocumentName), 10); // query through DocumentsLink Assert.AreEqual(1, results.Count, "Should have queried and found 1 document"); Assert.AreEqual(((Document)results[0]).AltLink, ClientTestsUtils.GenerateAltLink(uniqDatabaseName, uniqCollectionName, uniqDocumentName, typeof(Document))); // No Range Index on ts - override with scan FeedOptions queryFeedOptions1 = new FeedOptions() { EnableScanInQuery = true, EnableCrossPartitionQuery = true }; results = await SqlQueryDocuments(client, collection.SelfLink, string.Format(@"SELECT r.name FROM root r WHERE r.Price>0"), 10, queryFeedOptions1); Assert.AreEqual(1, results.Count, "Should have queried and found 1 document"); FeedOptions queryFeedOptions2 = new FeedOptions() { EmitVerboseTracesInQuery = true, EnableCrossPartitionQuery = true }; results = await SqlQueryDocuments(client, collection.SelfLink, string.Format(@"SELECT r.name FROM root r WHERE r.Price=9.99"), 10, queryFeedOptions2); Assert.AreEqual(1, results.Count, "Should have queried and found 1 document"); FeedOptions queryFeedOptions3 = new FeedOptions() { EmitVerboseTracesInQuery = false, EnableCrossPartitionQuery = true }; results = await SqlQueryDocuments(client, collection.SelfLink, string.Format(@"SELECT r.name FROM root r WHERE r.Price=9.99"), 10, queryFeedOptions3); Assert.AreEqual(1, results.Count, "Should have queried and found 1 document"); string uniqStoredProcedureName = "SmokeTestStoredProcedure" + Guid.NewGuid().ToString(); CosmosStoredProcedureSettings storedProcedure = await client.CreateStoredProcedureAsync(collection.SelfLink, new CosmosStoredProcedureSettings { Id = uniqStoredProcedureName, Body = "function f() {var x = 10;}" }); results = await SqlQueryStoredProcedures(client, collection.SelfLink, string.Format(@"SELECT r.id, r._rid FROM root r WHERE r.id = ""{0}""", uniqStoredProcedureName), 10); // query through collection link Assert.AreEqual(1, results.Count, "Should have queried and found 1 storedProcedure"); Assert.AreEqual(storedProcedure.ResourceId, ((QueryResult)results[0]).ResourceId); results = await SqlQueryStoredProcedures(client, collection.StoredProceduresLink, string.Format(@"SELECT r.id, r._rid FROM root r WHERE r.id = ""{0}""", uniqStoredProcedureName), 10); // query through StoredProceduresLink Assert.AreEqual(1, results.Count, "Should have queried and found 1 storedProcedure"); Assert.AreEqual(storedProcedure.ResourceId, ((QueryResult)results[0]).ResourceId); Assert.AreEqual(1, (await ReadFeedStoredProcedures(client, collection.SelfLink)).Count(item => item.Id == uniqStoredProcedureName)); // read through collection link Assert.AreEqual(1, (await ReadFeedStoredProcedures(client, collection.StoredProceduresLink)).Count(item => item.Id == uniqStoredProcedureName)); // read through StoredProceduresLink storedProcedure.Body = "function f() {var x= 20;}"; storedProcedure = await client.ReplaceStoredProcedureAsync(storedProcedure); results = await SqlQueryStoredProcedures(client, collection.StoredProceduresLink, string.Format(@"SELECT r.id, r._rid FROM root r WHERE r.id=""{0}""", storedProcedure.Id), 10); // query through StoredProceduresLink Assert.AreEqual(1, results.Count, "Should have queried and found 1 storedProcedure"); Assert.AreEqual(storedProcedure.ResourceId, ((QueryResult)results[0]).ResourceId); Assert.AreEqual(storedProcedure.ResourceId, (await client.ReadStoredProcedureAsync(storedProcedure.SelfLink)).Resource.ResourceId); Assert.AreEqual(1, results.Count, "Should have queried and found 1 storedProcedure"); string uniqTriggerName = "SmokeTestTrigger" + Guid.NewGuid().ToString("N"); CosmosTriggerSettings trigger = await client.CreateTriggerAsync(collection.SelfLink, new CosmosTriggerSettings { Id = uniqTriggerName, Body = "function f() {var x = 10;}", TriggerOperation = TriggerOperation.All, TriggerType = TriggerType.Pre }); results = await SqlQueryTriggers(client, collection.SelfLink, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqTriggerName), 10); // query through collection link Assert.AreEqual(1, results.Count, "Should have queried and found 1 trigger"); Assert.AreEqual(trigger.ResourceId, ((QueryResult)results[0]).ResourceId); results = await SqlQueryTriggers(client, collection.TriggersLink, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqTriggerName), 10); // query through TriggersLink Assert.AreEqual(1, results.Count, "Should have queried and found 1 trigger"); Assert.AreEqual(trigger.ResourceId, ((QueryResult)results[0]).ResourceId); Assert.AreEqual(1, (await ReadFeedTriggers(client, collection.SelfLink)).Count(item => item.Id == uniqTriggerName)); // read through collection link Assert.AreEqual(1, (await ReadFeedTriggers(client, collection.TriggersLink)).Count(item => item.Id == uniqTriggerName)); // read through TriggersLink trigger.Body = "function f() {var x = 10;}"; trigger = await client.ReplaceTriggerAsync(trigger); results = await SqlQueryTriggers(client, collection.SelfLink, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqTriggerName), 10); Assert.AreEqual(1, results.Count, "Should have queried and found 1 trigger"); Assert.AreEqual(trigger.ResourceId, ((QueryResult)results[0]).ResourceId); Assert.AreEqual(trigger.ResourceId, (await client.ReadTriggerAsync(trigger.SelfLink)).Resource.ResourceId); results = await SqlQueryTriggers(client, collection.SelfLink, string.Format(@"SELECT r.id, r._rid FROM root r WHERE r.id=""{0}""", uniqTriggerName), 10); // query through collection link Assert.AreEqual(1, results.Count, "Should have queried and found 1 trigger"); results = await SqlQueryTriggers(client, collection.TriggersLink, string.Format(@"SELECT r.id, r._rid FROM root r WHERE r.id=""{0}""", uniqTriggerName), 10); // query through TriggersLink Assert.AreEqual(1, results.Count, "Should have queried and found 1 trigger"); string uniqUserDefinedFunctionName = "SmokeTestUserDefinedFunction" + Guid.NewGuid().ToString("N"); CosmosUserDefinedFunctionSettings userDefinedFunction = await client.CreateUserDefinedFunctionAsync(collection.SelfLink, new CosmosUserDefinedFunctionSettings { Id = uniqUserDefinedFunctionName, Body = "function (){ var x = 10;}" }); results = await SqlQueryUserDefinedFunctions(client, collection.SelfLink, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqUserDefinedFunctionName), 10); // query through collection link Assert.AreEqual(1, results.Count, "Should have queried and found 1 userDefinedFunction"); Assert.AreEqual(userDefinedFunction.ResourceId, ((QueryResult)results[0]).ResourceId); results = await SqlQueryUserDefinedFunctions(client, collection.UserDefinedFunctionsLink, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqUserDefinedFunctionName), 10); // query through UserDefinedFunctionsLink Assert.AreEqual(1, results.Count, "Should have queried and found 1 userDefinedFunction"); Assert.AreEqual(userDefinedFunction.ResourceId, ((QueryResult)results[0]).ResourceId); Assert.AreEqual(1, (await ReadFeedUserDefinedFunctions(client, collection.SelfLink)).Count(item => item.Id == uniqUserDefinedFunctionName)); // read through collection link Assert.AreEqual(1, (await ReadFeedUserDefinedFunctions(client, collection.UserDefinedFunctionsLink)).Count(item => item.Id == uniqUserDefinedFunctionName)); // read through UserDefinedFunctionsLink userDefinedFunction.Body = "function (){ var x = 10;}"; userDefinedFunction = await client.ReplaceUserDefinedFunctionAsync(userDefinedFunction); results = await SqlQueryUserDefinedFunctions(client, collection.SelfLink, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqUserDefinedFunctionName), 10); Assert.AreEqual(1, results.Count, "Should have queried and found 1 userDefinedFunction"); Assert.AreEqual(userDefinedFunction.ResourceId, ((QueryResult)results[0]).ResourceId); Assert.AreEqual(userDefinedFunction.ResourceId, (await client.ReadUserDefinedFunctionAsync(userDefinedFunction.SelfLink)).Resource.ResourceId); results = await SqlQueryUserDefinedFunctions(client, collection.SelfLink, string.Format(@"SELECT r.id, r._rid FROM root r WHERE r.id=""{0}""", uniqUserDefinedFunctionName), 10); // query through collection link Assert.AreEqual(1, results.Count, "Should have queried and found 1 userDefinedFunction"); results = await SqlQueryUserDefinedFunctions(client, collection.UserDefinedFunctionsLink, string.Format(@"SELECT r.id, r._rid FROM root r WHERE r.id=""{0}""", uniqUserDefinedFunctionName), 10); // query through UserDefinedFunctionsLink Assert.AreEqual(1, results.Count, "Should have queried and found 1 userDefinedFunction"); //Test select array var queryArray = client.CreateDocumentQuery(collection.SelfLink, "SELECT VALUE [1, 2, 3, 4]").AsDocumentQuery(); JArray result = queryArray.ExecuteNextAsync().Result.FirstOrDefault(); Assert.AreEqual(result[0], 1); Assert.AreEqual(result[1], 2); Assert.AreEqual(result[2], 3); Assert.AreEqual(result[3], 4); RequestOptions requestOptions = new RequestOptions { PartitionKey = new PartitionKey(document.Id) }; await client.DeleteDocumentAsync(document.SelfLink, requestOptions); } foreach (CosmosContainerSettings collection in testCollections) { await client.DeleteDocumentCollectionAsync(collection.SelfLink); } await client.DeleteDatabaseAsync(database.SelfLink); }