Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public enum TriggerOperation : short
/// <summary>
/// Specifies replace operations only.
/// </summary>
Replace = 0x4
Replace = 0x4,

/// <summary>
/// Specifies upsert operations only.
/// </summary>
Upsert = 0x5
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ public async Task CRUDTest()
}

[TestMethod]
public async Task ValidatePreTriggerTest()
[DataRow(TriggerOperation.Create)]
[DataRow(TriggerOperation.Upsert)]
public async Task ValidatePreTriggerTest(TriggerOperation triggerOperation)
{
string triggerId = "SetJobNumber";

Expand All @@ -124,7 +126,7 @@ public async Task ValidatePreTriggerTest()
{
Id = triggerId,
TriggerType = TriggerType.Pre,
TriggerOperation = TriggerOperation.Create,
TriggerOperation = triggerOperation,
Body = @"function setJobNumber() {
var context = getContext();
var request = context.getRequest();
Expand All @@ -150,15 +152,27 @@ public async Task ValidatePreTriggerTest()

Job value = new Job() { Id = Guid.NewGuid(), InvestigationKey = "investigation~1" };

// this should create the document successfully with jobnumber of 1
Job createdItem = await this.container.CreateItemAsync<Job>(item: value, partitionKey: null, requestOptions: new ItemRequestOptions
Job item = null;
if (triggerOperation == TriggerOperation.Create)
{
PreTriggers = new List<string> { triggerId }
});

Assert.AreEqual(value.Id, createdItem.Id);
Assert.AreEqual(value.InvestigationKey, createdItem.InvestigationKey);
Assert.AreEqual(1, createdItem.JobNumber);
// this should create the document successfully with jobnumber of 1
item = await this.container.CreateItemAsync<Job>(item: value, partitionKey: null, requestOptions: new ItemRequestOptions
{
PreTriggers = new List<string> { triggerId }
});
}
else if(triggerOperation == TriggerOperation.Upsert)
{
// this should create the document successfully with jobnumber of 1
item = await this.container.UpsertItemAsync<Job>(item: value, partitionKey: null, requestOptions: new ItemRequestOptions
{
PreTriggers = new List<string> { triggerId }
});
}

Assert.AreEqual(value.Id, item.Id);
Assert.AreEqual(value.InvestigationKey, item.InvestigationKey);
Assert.AreEqual(1, item.JobNumber);

List<Job> result = this.container.GetItemLinqQueryable<Job>(allowSynchronousQueryExecution: true).Where(x => x.InvestigationKey == "investigation~1").ToList();
Assert.IsNotNull(result);
Expand All @@ -171,15 +185,27 @@ public async Task ValidatePreTriggerTest()

value.Id = Guid.NewGuid();

// this should create the document successfully with jobnumber of 2
Job createdItem2 = await this.container.CreateItemAsync<Job>(item: value, partitionKey: null, requestOptions: new ItemRequestOptions
Job item2 = null;
if (triggerOperation == TriggerOperation.Create)
{
PreTriggers = new List<string> { "SetJobNumber" }
});
// this should create the document successfully with jobnumber of 2
item2 = await this.container.CreateItemAsync<Job>(item: value, partitionKey: null, requestOptions: new ItemRequestOptions
{
PreTriggers = new List<string> { "SetJobNumber" }
});
}
else if (triggerOperation == TriggerOperation.Upsert)
{
// this should create the document successfully with jobnumber of 1
item2 = await this.container.UpsertItemAsync<Job>(item: value, partitionKey: null, requestOptions: new ItemRequestOptions
{
PreTriggers = new List<string> { "SetJobNumber" }
});
}

Assert.AreEqual(value.Id, createdItem2.Id);
Assert.AreEqual(value.InvestigationKey, createdItem2.InvestigationKey);
Assert.AreEqual(2, createdItem2.JobNumber);
Assert.AreEqual(value.Id, item2.Id);
Assert.AreEqual(value.InvestigationKey, item2.InvestigationKey);
Assert.AreEqual(2, item2.JobNumber);

result = this.container.GetItemLinqQueryable<Job>(allowSynchronousQueryExecution: true).Where(x => x.InvestigationKey == "investigation~1").ToList();
Assert.IsNotNull(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8799,6 +8799,11 @@
"Type": "Field",
"Attributes": [],
"MethodInfo": "Microsoft.Azure.Cosmos.Scripts.TriggerOperation Update;IsInitOnly:False;IsStatic:True;"
},
"Microsoft.Azure.Cosmos.Scripts.TriggerOperation Upsert": {
"Type": "Field",
"Attributes": [],
"MethodInfo": "Microsoft.Azure.Cosmos.Scripts.TriggerOperation Upsert;IsInitOnly:False;IsStatic:True;"
}
},
"NestedTypes": {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void OperationKindMatchesDirect()
[TestMethod]
public void TriggerOperationMatchesDirect()
{
this.AssertEnums<Cosmos.Scripts.TriggerOperation, Documents.TriggerOperation>();
this.AssertEnumsContains<Cosmos.Scripts.TriggerOperation, Documents.TriggerOperation>();
}

[TestMethod]
Expand Down Expand Up @@ -1147,5 +1147,16 @@ private void AssertEnums<TFirstEnum, TSecondEnum>() where TFirstEnum : struct, I
Assert.AreEqual(Convert.ToInt32(documentssVersion), Convert.ToInt32(cosmosVersion));
}
}

private void AssertEnumsContains<TFirstEnum, TSecondEnum>() where TFirstEnum : struct, IConvertible where TSecondEnum : struct, IConvertible
{
string[] allCosmosEntries = Enum.GetNames(typeof(TFirstEnum));
string[] allDocumentsEntries = Enum.GetNames(typeof(TSecondEnum));

foreach(string entry in allDocumentsEntries)
{
Assert.IsTrue(allCosmosEntries.Contains(entry));
}
}
}
}