Example #1
0
        public void TriggerTest()
        {
            var trigger = new SqlTrigger {
                Insert = true, TableName = "dbo.x".SqlQuoteName(), Name = "OnInsert", Body = "print 1;"
            };
            var writer = new SqlTriggerWriter(trigger)
            {
                Dialect = DbDialect.SqlServer, Mode = ScriptMode.Create, NoComment = true
            };
            var res = writer.ToString();

            Console.WriteLine(res);
            Assert.AreEqual(@"IF OBJECT_ID('""dbo"".""xOnInsert""') IS NOT NULL DROP TRIGGER ""dbo"".""xOnInsert"";
GO
CREATE TRIGGER ""dbo"".""xOnInsert"" ON ""dbo"".""x"" FOR INSERT AS BEGIN
print 1;
END;
GO".Trim(), writer.ToString().Trim());

            trigger.Update = true;
            trigger.Delete = true;
            trigger.Before = true;
            writer         = new SqlTriggerWriter(trigger)
            {
                Dialect = DbDialect.SqlServer, Mode = ScriptMode.Create, NoComment = true
            };
            res = writer.ToString();
            Console.WriteLine(res);
            Assert.AreEqual(@"IF OBJECT_ID('""dbo"".""xOnInsert""') IS NOT NULL DROP TRIGGER ""dbo"".""xOnInsert"";
GO
CREATE TRIGGER ""dbo"".""xOnInsert"" ON ""dbo"".""x"" INSTEAD OF INSERT,UPDATE,DELETE AS BEGIN
print 1;
END;
GO".Trim(), writer.ToString().Trim());
        }
Example #2
0
        public async Task TearDown()
        {
            SqlTrigger trigger = await SqlTriggerCollection.GetIfExistsAsync(_triggerName);

            if (trigger != null)
            {
                await trigger.DeleteAsync(WaitUntil.Completed);
            }
        }
 private void VerifySqlTriggers(SqlTrigger expectedValue, SqlTrigger actualValue)
 {
     Assert.AreEqual(expectedValue.Id, actualValue.Id);
     Assert.AreEqual(expectedValue.Name, actualValue.Name);
     Assert.AreEqual(expectedValue.Resource.Id, actualValue.Resource.Id);
     Assert.AreEqual(expectedValue.Resource.Rid, actualValue.Resource.Rid);
     Assert.AreEqual(expectedValue.Resource.Ts, actualValue.Resource.Ts);
     Assert.AreEqual(expectedValue.Resource.Etag, actualValue.Resource.Etag);
     Assert.AreEqual(expectedValue.Resource.Body, actualValue.Resource.Body);
     Assert.AreEqual(expectedValue.Resource.TriggerType, actualValue.Resource.TriggerType);
     Assert.AreEqual(expectedValue.Resource.TriggerOperation, actualValue.Resource.TriggerOperation);
 }
        public async Task SqlTriggerListTest()
        {
            List <SqlTrigger> sqlTriggers = await CosmosDBManagementClient.SqlResources.ListSqlTriggersAsync(
                resourceGroupName, databaseAccountName, databaseName, containerName).ToEnumerableAsync();

            Assert.NotNull(sqlTriggers);
            Assert.AreEqual(1, sqlTriggers.Count);
            SqlTrigger sqlTrigger = await CosmosDBManagementClient.SqlResources.GetSqlTriggerAsync(
                resourceGroupName, databaseAccountName, databaseName, containerName, triggerName);

            VerifySqlTriggers(sqlTrigger, sqlTriggers[0]);
        }
        public async Task SqlTriggerCreateAndUpdateTest()
        {
            SqlTriggerCreateUpdateParameters sqlTriggerCreateUpdateParameters = new SqlTriggerCreateUpdateParameters(
                resource: new SqlTriggerResource(triggerName)
            {
                TriggerOperation = "All",
                TriggerType      = "Pre",
                Body             = "function () { var context = getContext(); " +
                                   "var response = context.getResponse();" +
                                   "response.setBody('First Hello World');" +
                                   "}"
            },
                options: new CreateUpdateOptions()
                );
            SqlTrigger sqlTrigger1 = await WaitForCompletionAsync(
                await CosmosDBManagementClient.SqlResources.StartCreateUpdateSqlTriggerAsync(
                    resourceGroupName, databaseAccountName, databaseName, containerName, triggerName, sqlTriggerCreateUpdateParameters));

            Assert.NotNull(sqlTrigger1);
            SqlTrigger sqlTrigger2 = await CosmosDBManagementClient.SqlResources.GetSqlTriggerAsync(
                resourceGroupName, databaseAccountName, databaseName, containerName, triggerName);

            Assert.NotNull(sqlTrigger2);
            VerifySqlTriggers(sqlTrigger1, sqlTrigger2);

            sqlTriggerCreateUpdateParameters = new SqlTriggerCreateUpdateParameters(
                resource: new SqlTriggerResource(triggerName)
            {
                TriggerOperation = "All",
                TriggerType      = "Post",
                Body             = "function () { var context = getContext(); " +
                                   "var response = context.getResponse();" +
                                   "response.setBody('Second Hello World');" +
                                   "}"
            },
                options: new CreateUpdateOptions()
                );
            SqlTrigger sqlTrigger3 = await WaitForCompletionAsync(
                await CosmosDBManagementClient.SqlResources.StartCreateUpdateSqlTriggerAsync(
                    resourceGroupName, databaseAccountName, databaseName, containerName, triggerName, sqlTriggerCreateUpdateParameters));

            Assert.NotNull(sqlTrigger3);
            SqlTrigger sqlTrigger4 = await CosmosDBManagementClient.SqlResources.GetSqlTriggerAsync(
                resourceGroupName, databaseAccountName, databaseName, containerName, triggerName);

            Assert.NotNull(sqlTrigger4);
            VerifySqlTriggers(sqlTrigger3, sqlTrigger4);
        }
Example #6
0
        public async Task SqlTriggerCreateAndUpdate()
        {
            var trigger = await CreateSqlTrigger(null);

            Assert.AreEqual(_triggerName, trigger.Data.Resource.Id);
            Assert.That(trigger.Data.Resource.Body, Contains.Substring("First Hello World"));
            Assert.AreEqual(trigger.Data.Resource.TriggerOperation, TriggerOperation.All);
            Assert.AreEqual(trigger.Data.Resource.TriggerType, TriggerType.Pre);

            // Seems bug in swagger definition
            //Assert.AreEqual(TestThroughput1, container.Data.Options.Throughput);

            bool ifExists = await SqlTriggerCollection.ExistsAsync(_triggerName);

            Assert.True(ifExists);

            SqlTrigger trigger2 = await SqlTriggerCollection.GetAsync(_triggerName);

            Assert.AreEqual(_triggerName, trigger2.Data.Resource.Id);

            VerifySqlTriggers(trigger, trigger2);

            SqlTriggerCreateUpdateData updateOptions = new SqlTriggerCreateUpdateData(trigger.Id, _triggerName, trigger.Data.ResourceType, null,
                                                                                      new Dictionary <string, string>(),// TODO: use original tags see defect: https://github.com/Azure/autorest.csharp/issues/1590
                                                                                      AzureLocation.WestUS, trigger.Data.Resource, new CreateUpdateOptions());

            updateOptions = new SqlTriggerCreateUpdateData(AzureLocation.WestUS, new SqlTriggerResource(_triggerName)
            {
                TriggerOperation = TriggerOperation.Create,
                TriggerType      = TriggerType.Post,
                Body             = @"function () { var updatetext = getContext();
    var response = context.getResponse();
    response.setBody('Second Hello World');
}"
            });

            trigger = (await SqlTriggerCollection.CreateOrUpdateAsync(WaitUntil.Completed, _triggerName, updateOptions)).Value;
            Assert.AreEqual(_triggerName, trigger.Data.Resource.Id);
            Assert.That(trigger.Data.Resource.Body, Contains.Substring("Second Hello World"));
            Assert.AreEqual(trigger.Data.Resource.TriggerOperation, TriggerOperation.Create);
            Assert.AreEqual(trigger.Data.Resource.TriggerType, TriggerType.Post);

            trigger2 = await SqlTriggerCollection.GetAsync(_triggerName);

            VerifySqlTriggers(trigger, trigger2);
        }
Example #7
0
 /// <summary>
 /// </summary>
 /// <param name="sqlTrigger"></param>
 public SqlTriggerWriter(SqlTrigger sqlTrigger)
 {
     Trigger    = sqlTrigger;
     Parameters = sqlTrigger;
 }
Example #8
0
		public void TriggerTest(){
			var trigger = new SqlTrigger{Insert = true, TableName = "dbo.x".SqlQuoteName(), Name = "OnInsert", Body="print 1;"};
			var writer = new SqlTriggerWriter(trigger){Dialect = DbDialect.SqlServer,Mode = ScriptMode.Create,NoComment = true};
			var res = writer.ToString();
			Console.WriteLine(res);
			Assert.AreEqual(@"IF OBJECT_ID('""dbo"".""xOnInsert""') IS NOT NULL DROP TRIGGER ""dbo"".""xOnInsert"";
GO
CREATE TRIGGER ""dbo"".""xOnInsert"" ON ""dbo"".""x"" FOR INSERT AS BEGIN
print 1;
END;
GO".Trim(), writer.ToString().Trim());

			trigger.Update = true;
			trigger.Delete = true;
			trigger.Before = true;
			writer = new SqlTriggerWriter(trigger) { Dialect = DbDialect.SqlServer, Mode = ScriptMode.Create, NoComment = true };
			res = writer.ToString();
			Console.WriteLine(res);
			Assert.AreEqual(@"IF OBJECT_ID('""dbo"".""xOnInsert""') IS NOT NULL DROP TRIGGER ""dbo"".""xOnInsert"";
GO
CREATE TRIGGER ""dbo"".""xOnInsert"" ON ""dbo"".""x"" INSTEAD OF INSERT,UPDATE,DELETE AS BEGIN
print 1;
END;
GO".Trim(), writer.ToString().Trim());
		}
Example #9
0
		/// <summary>
		/// </summary>
		/// <param name="sqlTrigger"></param>
		public SqlTriggerWriter(SqlTrigger sqlTrigger){
			Trigger = sqlTrigger;
			Parameters = sqlTrigger;
		}