public async Task CRUDTest() { TriggerProperties settings = new TriggerProperties { Id = Guid.NewGuid().ToString(), Body = TriggersTests.GetTriggerFunction(".05"), TriggerOperation = Cosmos.Scripts.TriggerOperation.Create, TriggerType = Cosmos.Scripts.TriggerType.Pre }; TriggerResponse triggerResponse = await this.scripts.CreateTriggerAsync(settings); double reqeustCharge = triggerResponse.RequestCharge; Assert.IsTrue(reqeustCharge > 0); Assert.AreEqual(HttpStatusCode.Created, triggerResponse.StatusCode); Assert.IsNotNull(triggerResponse.Diagnostics); string diagnostics = triggerResponse.Diagnostics.ToString(); Assert.IsFalse(string.IsNullOrEmpty(diagnostics)); Assert.IsTrue(diagnostics.Contains("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); Assert.IsNotNull(triggerResponse.Diagnostics); diagnostics = triggerResponse.Diagnostics.ToString(); Assert.IsFalse(string.IsNullOrEmpty(diagnostics)); Assert.IsTrue(diagnostics.Contains("StatusCode")); TriggersTests.ValidateTriggerSettings(settings, triggerResponse); TriggerProperties 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); Assert.IsNotNull(replaceResponse.Diagnostics); diagnostics = replaceResponse.Diagnostics.ToString(); Assert.IsFalse(string.IsNullOrEmpty(diagnostics)); Assert.IsTrue(diagnostics.Contains("StatusCode")); replaceResponse = await this.scripts.DeleteTriggerAsync(updatedSettings.Id); reqeustCharge = replaceResponse.RequestCharge; Assert.IsTrue(reqeustCharge > 0); Assert.AreEqual(HttpStatusCode.NoContent, replaceResponse.StatusCode); Assert.IsNotNull(replaceResponse.Diagnostics); diagnostics = replaceResponse.Diagnostics.ToString(); Assert.IsFalse(string.IsNullOrEmpty(diagnostics)); Assert.IsTrue(diagnostics.Contains("StatusCode")); }
public async Task ValidateTriggersTest() { // Prevent failures if previous test did not clean up correctly try { await this.scripts.DeleteTriggerAsync("addTax"); } catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.NotFound) { //swallow } ToDoActivity item = new ToDoActivity() { id = Guid.NewGuid().ToString(), cost = 9001, description = "trigger_test_item", status = "Done", taskNum = 1 }; TriggerProperties cosmosTrigger = await this.scripts.CreateTriggerAsync( new TriggerProperties { Id = "addTax", Body = TriggersTests.GetTriggerFunction(".20"), TriggerOperation = Cosmos.Scripts.TriggerOperation.All, TriggerType = Cosmos.Scripts.TriggerType.Pre }); ItemRequestOptions options = new ItemRequestOptions() { PreTriggers = new List <string>() { cosmosTrigger.Id }, }; ItemResponse <dynamic> createdItem = await this.container.CreateItemAsync <dynamic>(item, requestOptions : options); double itemTax = createdItem.Resource.tax; Assert.AreEqual(item.cost * .20, itemTax); // Delete existing user defined functions. await this.scripts.DeleteTriggerAsync("addTax"); }
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 ValidateTriggersTest() { // Prevent failures if previous test did not clean up correctly await this.container.Triggers["addTax"].DeleteAsync(); ToDoActivity item = new ToDoActivity() { id = Guid.NewGuid().ToString(), cost = 9001, description = "trigger_test_item", status = "Done", taskNum = 1 }; CosmosTrigger cosmosTrigger = await this.container.Triggers.CreateTriggerAsync( new CosmosTriggerSettings { Id = "addTax", Body = TriggersTests.GetTriggerFunction(".20"), TriggerOperation = TriggerOperation.All, TriggerType = Cosmos.TriggerType.Pre }); CosmosItemRequestOptions options = new CosmosItemRequestOptions() { PreTriggers = new List <string>() { cosmosTrigger.Id }, }; CosmosItemResponse <dynamic> createdItem = await this.container.Items.CreateItemAsync <dynamic>(item.status, item, options); double itemTax = createdItem.Resource.tax; Assert.AreEqual(item.cost * .20, itemTax); // Delete existing user defined functions. await cosmosTrigger.DeleteAsync(); }