Example #1
0
        public async Task ScriptsUserDefinedFunctionTest(bool directMode)
        {
            CosmosClient client    = directMode ? DirectCosmosClient : GatewayCosmosClient;
            Database     database  = client.GetDatabase(DatabaseId);
            Container    container = await database.CreateContainerAsync(Guid.NewGuid().ToString(), "/pk");

            Scripts scripts = container.Scripts;

            List <string> createdIds = new List <string>()
            {
                "BasicQueryUdf1",
                "BasicQueryUdf2",
                "BasicQueryUdf3"
            };

            //Basic query
            List <UserDefinedFunctionProperties> queryResults = await this.ToListAsync(
                scripts.GetUserDefinedFunctionQueryStreamIterator,
                scripts.GetUserDefinedFunctionQueryIterator <UserDefinedFunctionProperties>,
                "select * from T where STARTSWITH(T.id, \"BasicQueryUdf\")",
                CosmosBasicQueryTests.RequestOptions);

            if (queryResults.Count < 3)
            {
                foreach (string id in createdIds)
                {
                    UserDefinedFunctionProperties properties = await scripts.CreateUserDefinedFunctionAsync(new UserDefinedFunctionProperties()
                    {
                        Id   = id,
                        Body = "function() {var x = 10;}"
                    });
                }

                queryResults = await this.ToListAsync(
                    scripts.GetUserDefinedFunctionQueryStreamIterator,
                    scripts.GetUserDefinedFunctionQueryIterator <UserDefinedFunctionProperties>,
                    "select * from T where STARTSWITH(T.id, \"BasicQueryUdf\")",
                    CosmosBasicQueryTests.RequestOptions);
            }

            CollectionAssert.AreEquivalent(createdIds, queryResults.Select(x => x.Id).ToList());

            //Read All
            List <UserDefinedFunctionProperties> results = await this.ToListAsync(
                scripts.GetUserDefinedFunctionQueryStreamIterator,
                scripts.GetUserDefinedFunctionQueryIterator <UserDefinedFunctionProperties>,
                null,
                CosmosBasicQueryTests.RequestOptions);

            CollectionAssert.IsSubsetOf(createdIds, results.Select(x => x.Id).ToList());
        }
        public async Task CreateUDF(string udfName)
        {
            var    udf     = File.ReadAllText($@"UserDefinedFunctions/{udfName}.js");
            string message = "Cerate new UserDefinedFunction";

            Printer.PrintLine(message: message);
            Scripts scripts = _container.Scripts;
            UserDefinedFunctionProperties udfProperties = new UserDefinedFunctionProperties
            {
                Id   = udfName,
                Body = udf
            };
            UserDefinedFunctionResponse udfResponse = await scripts.CreateUserDefinedFunctionAsync(udfProperties);

            WriteLine($"EDF Created with the Id: {udfResponse.ActivityId}");
            Printer.PrintLine(noOfTimes: (100 + message.Length));
        }