internal Task <UserDefinedFunctionResponse> CreateUserDefinedFunctionResponseAsync(Task <CosmosResponseMessage> cosmosResponseMessageTask) { return(this.ProcessMessageAsync(cosmosResponseMessageTask, (cosmosResponseMessage) => { CosmosUserDefinedFunctionProperties settings = this.ToObjectInternal <CosmosUserDefinedFunctionProperties>(cosmosResponseMessage, this.settingsSerializer); return new UserDefinedFunctionResponse( cosmosResponseMessage.StatusCode, cosmosResponseMessage.Headers, settings); })); }
private static void ValidateUserDefinedFunctionSettings(CosmosUserDefinedFunctionProperties udfSettings, UserDefinedFunctionResponse cosmosResponse) { CosmosUserDefinedFunctionProperties settings = cosmosResponse.Resource; Assert.AreEqual(udfSettings.Body, settings.Body, "User defined function do not match"); Assert.AreEqual(udfSettings.Id, settings.Id, "User defined function id do not match"); Assert.IsTrue(cosmosResponse.RequestCharge > 0); Assert.IsNotNull(cosmosResponse.MaxResourceQuota); Assert.IsNotNull(cosmosResponse.CurrentResourceQuotaUsage); }
public async Task ValidateUserDefinedFunctionsTest() { // Prevent failures if previous test did not clean up correctly await this.scripts.DeleteUserDefinedFunctionAsync("calculateTax"); ToDoActivity item = new ToDoActivity() { id = Guid.NewGuid().ToString(), cost = 9001, description = "udf_test_item", status = "Done", taskNum = 1 }; await this.container.CreateItemAsync <ToDoActivity>(item); CosmosUserDefinedFunctionProperties cosmosUserDefinedFunction = await this.scripts.CreateUserDefinedFunctionAsync( new CosmosUserDefinedFunctionProperties { Id = "calculateTax", Body = @"function(amt) { return amt * 0.05; }" }); CosmosSqlQueryDefinition sqlQuery = new CosmosSqlQueryDefinition( "SELECT t.id, t.status, t.cost, udf.calculateTax(t.cost) as total FROM toDoActivity t where t.cost > @expensive and t.status = @status") .UseParameter("@expensive", 9000) .UseParameter("@status", "Done"); FeedIterator <dynamic> feedIterator = this.container.CreateItemQuery <dynamic>( sqlQueryDefinition: sqlQuery, partitionKey: new Cosmos.PartitionKey("Done")); HashSet <string> iterIds = new HashSet <string>(); while (feedIterator.HasMoreResults) { foreach (var response in await feedIterator.FetchNextSetAsync()) { Assert.IsTrue(response.cost > 9000); Assert.AreEqual(response.cost * .05, response.total); iterIds.Add(response.id.Value); } } Assert.IsTrue(iterIds.Count > 0); Assert.IsTrue(iterIds.Contains(item.id)); // Delete existing user defined functions. await this.scripts.DeleteUserDefinedFunctionAsync(cosmosUserDefinedFunction.Id); }
private async Task <UserDefinedFunctionResponse> CreateRandomUdf() { string id = Guid.NewGuid().ToString(); string function = UserDefinedFunctionsTests.function; CosmosUserDefinedFunctionProperties settings = new CosmosUserDefinedFunctionProperties { Id = id, Body = function, }; //Create a user defined function UserDefinedFunctionResponse createResponse = await this.scripts.CreateUserDefinedFunctionAsync( userDefinedFunctionSettings : settings, cancellationToken : this.cancellationToken); ValidateUserDefinedFunctionSettings(settings, createResponse); return(createResponse); }
public async Task UserDefinedFunctionsIteratorTest() { CosmosUserDefinedFunctionProperties cosmosUserDefinedFunction = await CreateRandomUdf(); HashSet <string> settings = new HashSet <string>(); FeedIterator <CosmosUserDefinedFunctionProperties> iter = this.scripts.GetUserDefinedFunctionsIterator();; while (iter.HasMoreResults) { foreach (CosmosUserDefinedFunctionProperties storedProcedureSettingsEntry in await iter.FetchNextSetAsync()) { settings.Add(storedProcedureSettingsEntry.Id); } } Assert.IsTrue(settings.Contains(cosmosUserDefinedFunction.Id), "The iterator did not return the user defined function definition."); // Delete existing user defined functions. await this.scripts.DeleteUserDefinedFunctionAsync(cosmosUserDefinedFunction.Id); }
public async Task CRUDTest() { CosmosUserDefinedFunctionProperties settings = new CosmosUserDefinedFunctionProperties { Id = Guid.NewGuid().ToString(), Body = UserDefinedFunctionsTests.function, }; UserDefinedFunctionResponse response = await this.scripts.CreateUserDefinedFunctionAsync(settings); double reqeustCharge = response.RequestCharge; Assert.IsTrue(reqeustCharge > 0); Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); UserDefinedFunctionsTests.ValidateUserDefinedFunctionSettings(settings, response); response = await this.scripts.ReadUserDefinedFunctionAsync(settings.Id); reqeustCharge = response.RequestCharge; Assert.IsTrue(reqeustCharge > 0); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); UserDefinedFunctionsTests.ValidateUserDefinedFunctionSettings(settings, response); CosmosUserDefinedFunctionProperties updatedSettings = response.Resource; updatedSettings.Body = @"function(amt) { return amt * 0.42; }"; UserDefinedFunctionResponse replaceResponse = await this.scripts.ReplaceUserDefinedFunctionAsync(updatedSettings); UserDefinedFunctionsTests.ValidateUserDefinedFunctionSettings(updatedSettings, replaceResponse); reqeustCharge = replaceResponse.RequestCharge; Assert.IsTrue(reqeustCharge > 0); Assert.AreEqual(HttpStatusCode.OK, replaceResponse.StatusCode); replaceResponse = await this.scripts.DeleteUserDefinedFunctionAsync(settings.Id); reqeustCharge = replaceResponse.RequestCharge; Assert.IsTrue(reqeustCharge > 0); Assert.AreEqual(HttpStatusCode.NoContent, replaceResponse.StatusCode); }
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 CosmosContainerProperties containerSettings = new CosmosContainerProperties("mockId", "/pk"); CosmosDatabaseProperties databaseSettings = new CosmosDatabaseProperties() { Id = "mock" }; CosmosStoredProcedureProperties cosmosStoredProcedureSettings = new CosmosStoredProcedureProperties() { Id = "mock" }; CosmosTriggerProperties cosmosTriggerSettings = new CosmosTriggerProperties() { Id = "mock" }; CosmosUserDefinedFunctionProperties cosmosUserDefinedFunctionSettings = new CosmosUserDefinedFunctionProperties() { Id = "mock" }; mockDefaultJsonSerializer.Setup(x => x.FromStream <CosmosDatabaseProperties>(databaseResponse.Content)).Returns(databaseSettings); mockDefaultJsonSerializer.Setup(x => x.FromStream <CosmosContainerProperties>(containerResponse.Content)).Returns(containerSettings); mockDefaultJsonSerializer.Setup(x => x.FromStream <CosmosStoredProcedureProperties>(storedProcedureResponse.Content)).Returns(cosmosStoredProcedureSettings); mockDefaultJsonSerializer.Setup(x => x.FromStream <CosmosTriggerProperties>(triggerResponse.Content)).Returns(cosmosTriggerSettings); mockDefaultJsonSerializer.Setup(x => x.FromStream <CosmosUserDefinedFunctionProperties>(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(); }