Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scripts: Adds Stream APIs for CRUD Operations #4978

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
368 changes: 367 additions & 1 deletion Microsoft.Azure.Cosmos/src/Resource/Scripts/Scripts.cs

Large diffs are not rendered by default.

339 changes: 339 additions & 0 deletions Microsoft.Azure.Cosmos/src/Resource/Scripts/ScriptsCore.cs

Large diffs are not rendered by default.

286 changes: 286 additions & 0 deletions Microsoft.Azure.Cosmos/src/Resource/Scripts/ScriptsInlineCore.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@ private CosmosSerializer GetSerializer<T>()
string directAssemblyName = typeof(Documents.PartitionKeyRange).Assembly.GetName().Name;
string inputAssemblyName = inputType.Assembly.GetName().Name;
bool inputIsClientOrDirect = string.Equals(inputAssemblyName, clientAssemblyName) || string.Equals(inputAssemblyName, directAssemblyName);
bool typeIsWhiteListed = inputType == typeof(Document) || (inputType.IsGenericType && inputType.GetGenericTypeDefinition() == typeof(ChangeFeedItem<>));
bool typeIsWhiteListed = inputType == typeof(Document)
|| (inputType.IsGenericType && inputType.GetGenericTypeDefinition() == typeof(ChangeFeedItem<>))
|| inputType == typeof(StoredProcedureResponse)
|| inputType == typeof(TriggerResponse)
|| inputType == typeof(UserDefinedFunctionResponse);

if (!typeIsWhiteListed && inputIsClientOrDirect)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,52 @@ public async Task CRUDTest()
Assert.IsTrue(diagnostics.Contains("StatusCode"));
}

[TestMethod]
public async Task CRUDStreamTest()
{
string sprocId = Guid.NewGuid().ToString();
string sprocBody = "function() { { var x = 42; } }";

StoredProcedureProperties storedProcedureProperties = new StoredProcedureProperties(sprocId, sprocBody);
ResponseMessage responseMessage = await this.scripts.CreateStoredProcedureStreamAsync(
storedProcedureProperties);
Assert.AreEqual(HttpStatusCode.Created, responseMessage.StatusCode);
Assert.IsNotNull(responseMessage.Diagnostics);
string diagnostics = responseMessage.Diagnostics.ToString();
Assert.IsFalse(string.IsNullOrEmpty(diagnostics));
Assert.IsTrue(diagnostics.Contains("StatusCode"));

responseMessage = await this.scripts.ReadStoredProcedureStreamAsync(sprocId);
Assert.AreEqual(HttpStatusCode.OK, responseMessage.StatusCode);
Assert.IsNotNull(responseMessage.Diagnostics);
diagnostics = responseMessage.Diagnostics.ToString();
Assert.IsFalse(string.IsNullOrEmpty(diagnostics));
Assert.IsTrue(diagnostics.Contains("StatusCode"));

string updatedBody = @"function(name) { var context = getContext();
var response = context.getResponse();
response.setBody(""hello there "" + name);
}";

storedProcedureProperties = new StoredProcedureProperties(sprocId, updatedBody);
ResponseMessage replaceResponseMessage = await this.scripts.ReplaceStoredProcedureStreamAsync(
storedProcedureProperties);

Assert.AreEqual(HttpStatusCode.OK, replaceResponseMessage.StatusCode);
Assert.IsNotNull(replaceResponseMessage.Diagnostics);
diagnostics = replaceResponseMessage.Diagnostics.ToString();
Assert.IsFalse(string.IsNullOrEmpty(diagnostics));
Assert.IsTrue(diagnostics.Contains("StatusCode"));


ResponseMessage deleteResponseMessage = await this.scripts.DeleteStoredProcedureStreamAsync(sprocId);
Assert.AreEqual(HttpStatusCode.NoContent, deleteResponseMessage.StatusCode);
Assert.IsNotNull(deleteResponseMessage.Diagnostics);
diagnostics = deleteResponseMessage.Diagnostics.ToString();
Assert.IsFalse(string.IsNullOrEmpty(diagnostics));
Assert.IsTrue(diagnostics.Contains("StatusCode"));
}

[TestMethod]
public async Task ExecutionLogsTests()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,53 @@ public async Task CRUDTest()
Assert.IsTrue(diagnostics.Contains("StatusCode"));
}

[TestMethod]
public async Task CRUDStreamTest()
{
TriggerProperties settings = new TriggerProperties
{
Id = Guid.NewGuid().ToString(),
Body = TriggersTests.GetTriggerFunction(".05"),
TriggerOperation = Cosmos.Scripts.TriggerOperation.Create,
TriggerType = Cosmos.Scripts.TriggerType.Pre
};


ResponseMessage triggerResponseMessage =
await this.scripts.CreateTriggerStreamAsync(
settings);
Assert.AreEqual(HttpStatusCode.Created, triggerResponseMessage.StatusCode);
Assert.IsNotNull(triggerResponseMessage.Diagnostics);
string diagnostics = triggerResponseMessage.Diagnostics.ToString();
Assert.IsFalse(string.IsNullOrEmpty(diagnostics));
Assert.IsTrue(diagnostics.Contains("StatusCode"));

triggerResponseMessage = await this.scripts.ReadTriggerStreamAsync(settings.Id);
Assert.AreEqual(HttpStatusCode.OK, triggerResponseMessage.StatusCode);
Assert.IsNotNull(triggerResponseMessage.Diagnostics);
diagnostics = triggerResponseMessage.Diagnostics.ToString();
Assert.IsFalse(string.IsNullOrEmpty(diagnostics));
Assert.IsTrue(diagnostics.Contains("StatusCode"));

TriggerProperties updatedSettings = settings;
updatedSettings.Body = TriggersTests.GetTriggerFunction(".42");

ResponseMessage replaceResponseMessage = await this.scripts.ReplaceTriggerStreamAsync(
updatedSettings);
Assert.AreEqual(HttpStatusCode.OK, replaceResponseMessage.StatusCode);
Assert.IsNotNull(replaceResponseMessage.Diagnostics);
diagnostics = replaceResponseMessage.Diagnostics.ToString();
Assert.IsFalse(string.IsNullOrEmpty(diagnostics));
Assert.IsTrue(diagnostics.Contains("StatusCode"));

replaceResponseMessage = await this.scripts.DeleteTriggerStreamAsync(updatedSettings.Id);
Assert.AreEqual(HttpStatusCode.NoContent, replaceResponseMessage.StatusCode);
Assert.IsNotNull(replaceResponseMessage.Diagnostics);
diagnostics = replaceResponseMessage.Diagnostics.ToString();
Assert.IsFalse(string.IsNullOrEmpty(diagnostics));
Assert.IsTrue(diagnostics.Contains("StatusCode"));
}

[TestMethod]
[DataRow(TriggerOperation.Create)]
[DataRow(TriggerOperation.Upsert)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,50 @@ public async Task CRUDTest()
Assert.AreEqual(HttpStatusCode.NoContent, replaceResponse.StatusCode);
}

[TestMethod]
public async Task CRUDStreamTest()
{
UserDefinedFunctionProperties settings = new UserDefinedFunctionProperties
{
Id = Guid.NewGuid().ToString(),
Body = UserDefinedFunctionsTests.function,
};

ResponseMessage responseMessage =
await this.scripts.CreateUserDefinedFunctionStreamAsync(
settings);
Assert.AreEqual(HttpStatusCode.Created, responseMessage.StatusCode);
Assert.IsNotNull(responseMessage.Diagnostics);
string diagnostics = responseMessage.Diagnostics.ToString();
Assert.IsFalse(string.IsNullOrEmpty(diagnostics));
Assert.IsTrue(diagnostics.Contains("StatusCode"));

responseMessage = await this.scripts.ReadUserDefinedFunctionStreamAsync(settings.Id);
Assert.AreEqual(HttpStatusCode.OK, responseMessage.StatusCode);
Assert.IsNotNull(responseMessage.Diagnostics);
diagnostics = responseMessage.Diagnostics.ToString();
Assert.IsFalse(string.IsNullOrEmpty(diagnostics));
Assert.IsTrue(diagnostics.Contains("StatusCode"));

UserDefinedFunctionProperties updatedSettings = settings;
updatedSettings.Body = @"function(amt) { return amt * 0.42; }";

ResponseMessage replaceResponseMessage = await this.scripts.ReplaceUserDefinedFunctionStreamAsync(
updatedSettings);
Assert.IsNotNull(replaceResponseMessage.Diagnostics);
diagnostics = replaceResponseMessage.Diagnostics.ToString();
Assert.IsFalse(string.IsNullOrEmpty(diagnostics));
Assert.IsTrue(diagnostics.Contains("StatusCode"));
Assert.AreEqual(HttpStatusCode.OK, replaceResponseMessage.StatusCode);

replaceResponseMessage = await this.scripts.DeleteUserDefinedFunctionStreamAsync(settings.Id);
Assert.IsNotNull(replaceResponseMessage.Diagnostics);
diagnostics = replaceResponseMessage.Diagnostics.ToString();
Assert.IsFalse(string.IsNullOrEmpty(diagnostics));
Assert.IsTrue(diagnostics.Contains("StatusCode"));
Assert.AreEqual(HttpStatusCode.NoContent, replaceResponseMessage.StatusCode);
}

[TestMethod]
public async Task ValidateUserDefinedFunctionsTest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9175,6 +9175,36 @@
"Attributes": [],
"MethodInfo": "Microsoft.Azure.Cosmos.FeedIterator`1[T] GetUserDefinedFunctionQueryIterator[T](System.String, System.String, Microsoft.Azure.Cosmos.QueryRequestOptions);IsAbstract:True;IsStatic:False;IsVirtual:True;IsGenericMethod:True;IsConstructor:False;IsFinal:False;"
},
"System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] CreateStoredProcedureStreamAsync(Microsoft.Azure.Cosmos.Scripts.StoredProcedureProperties, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] CreateStoredProcedureStreamAsync(Microsoft.Azure.Cosmos.Scripts.StoredProcedureProperties, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken);IsAbstract:True;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] CreateTriggerStreamAsync(Microsoft.Azure.Cosmos.Scripts.TriggerProperties, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] CreateTriggerStreamAsync(Microsoft.Azure.Cosmos.Scripts.TriggerProperties, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken);IsAbstract:True;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] CreateUserDefinedFunctionStreamAsync(Microsoft.Azure.Cosmos.Scripts.UserDefinedFunctionProperties, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] CreateUserDefinedFunctionStreamAsync(Microsoft.Azure.Cosmos.Scripts.UserDefinedFunctionProperties, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken);IsAbstract:True;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] DeleteStoredProcedureStreamAsync(System.String, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] DeleteStoredProcedureStreamAsync(System.String, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken);IsAbstract:True;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] DeleteTriggerStreamAsync(System.String, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] DeleteTriggerStreamAsync(System.String, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken);IsAbstract:True;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] DeleteUserDefinedFunctionStreamAsync(System.String, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] DeleteUserDefinedFunctionStreamAsync(System.String, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken);IsAbstract:True;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] ExecuteStoredProcedureStreamAsync(System.String, Microsoft.Azure.Cosmos.PartitionKey, System.Object[], Microsoft.Azure.Cosmos.Scripts.StoredProcedureRequestOptions, System.Threading.CancellationToken)": {
"Type": "Method",
"Attributes": [],
Expand All @@ -9185,6 +9215,36 @@
"Attributes": [],
"MethodInfo": "System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] ExecuteStoredProcedureStreamAsync(System.String, System.IO.Stream, Microsoft.Azure.Cosmos.PartitionKey, Microsoft.Azure.Cosmos.Scripts.StoredProcedureRequestOptions, System.Threading.CancellationToken);IsAbstract:True;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] ReadStoredProcedureStreamAsync(System.String, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] ReadStoredProcedureStreamAsync(System.String, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken);IsAbstract:True;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] ReadTriggerStreamAsync(System.String, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] ReadTriggerStreamAsync(System.String, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken);IsAbstract:True;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] ReadUserDefinedFunctionStreamAsync(System.String, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] ReadUserDefinedFunctionStreamAsync(System.String, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken);IsAbstract:True;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] ReplaceStoredProcedureStreamAsync(Microsoft.Azure.Cosmos.Scripts.StoredProcedureProperties, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] ReplaceStoredProcedureStreamAsync(Microsoft.Azure.Cosmos.Scripts.StoredProcedureProperties, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken);IsAbstract:True;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] ReplaceTriggerStreamAsync(Microsoft.Azure.Cosmos.Scripts.TriggerProperties, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] ReplaceTriggerStreamAsync(Microsoft.Azure.Cosmos.Scripts.TriggerProperties, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken);IsAbstract:True;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] ReplaceUserDefinedFunctionStreamAsync(Microsoft.Azure.Cosmos.Scripts.UserDefinedFunctionProperties, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] ReplaceUserDefinedFunctionStreamAsync(Microsoft.Azure.Cosmos.Scripts.UserDefinedFunctionProperties, Microsoft.Azure.Cosmos.RequestOptions, System.Threading.CancellationToken);IsAbstract:True;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.Scripts.StoredProcedureExecuteResponse`1[TOutput]] ExecuteStoredProcedureAsync[TOutput](System.String, Microsoft.Azure.Cosmos.PartitionKey, System.Object[], Microsoft.Azure.Cosmos.Scripts.StoredProcedureRequestOptions, System.Threading.CancellationToken)": {
"Type": "Method",
"Attributes": [],
Expand Down
Loading