Skip to content

Commit 8965a8b

Browse files
committed
testing separate db per func
1 parent e67771a commit 8965a8b

File tree

8 files changed

+64
-75
lines changed

8 files changed

+64
-75
lines changed

test/Resources/TestScripts/Node/CosmosDBIn/function.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
{
44
"type": "queueTrigger",
55
"name": "input",
6-
"queueName": "documentdb-input-node",
6+
"queueName": "cosmosdb-input-node",
77
"direction": "in"
88
},
99
{
1010
"type": "cosmosDB",
1111
"name": "itemIn",
1212
"direction": "in",
13-
"databaseName": "ItemDb",
13+
"databaseName": "InOutItemDb",
1414
"containerName": "ItemCollection",
1515
"partitionKey": "{id}",
1616
"id": "{id}"
@@ -19,7 +19,7 @@
1919
"type": "cosmosDB",
2020
"name": "itemOut",
2121
"direction": "out",
22-
"databaseName": "ItemDb",
22+
"databaseName": "InOutItemDb",
2323
"containerName": "ItemCollection",
2424
"createIfNotExists": true,
2525
"partitionKey": "{id}"

test/Resources/TestScripts/Node/CosmosDBInMultiple/function.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
{
44
"type": "queueTrigger",
55
"name": "input",
6-
"queueName": "documentdb-input-node",
6+
"queueName": "cosmosdb-input-multiple-node",
77
"direction": "in"
88
},
99
{
1010
"name": "items",
1111
"type": "cosmosDB",
1212
"direction": "in",
13-
"databaseName": "ItemDb",
13+
"databaseName": "MultipleInOutItemDb",
1414
"containerName": "ItemCollection",
1515
"sqlQuery": "SELECT * from c where c.text = {id}"
1616
},
1717
{
1818
"type": "cosmosDB",
1919
"name": "itemOut",
2020
"direction": "out",
21-
"databaseName": "ItemDb",
21+
"databaseName": "MultipleInOutItemDb",
2222
"containerName": "ItemCollection",
2323
"createIfNotExists": true
2424
}

test/Resources/TestScripts/Node/CosmosDBOut/function.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"type": "cosmosDB",
1010
"name": "item",
1111
"direction": "out",
12-
"databaseName": "ItemDb",
12+
"databaseName": "InOutItemDb",
1313
"containerName": "ItemCollection",
1414
"createIfNotExists": true
1515
}

test/Resources/TestScripts/Node/CosmosDBOutMultiple/function.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"type": "cosmosDB",
1010
"name": "items",
1111
"direction": "out",
12-
"databaseName": "ItemDb",
12+
"databaseName": "MultipleInOutItemDb",
1313
"containerName": "ItemCollection",
1414
"createIfNotExists": true
1515
}

test/Resources/TestScripts/Node/CosmosDBTrigger/function.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "cosmosDBTrigger",
55
"name": "input",
66
"direction": "in",
7-
"databaseName": "ItemDb",
7+
"databaseName": "TriggerItemDb",
88
"containerName": "ItemCollection",
99
"connection": "CosmosDB",
1010
"leaseContainerName": "leases",
Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,101 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4-
using System;
5-
using System.Threading.Tasks;
64
using Microsoft.Azure.Cosmos;
75
using Microsoft.Azure.Storage.Queue;
6+
using System;
7+
using System.Threading.Tasks;
8+
using System.Xml.Linq;
89
using Xunit;
910

1011
namespace Microsoft.Azure.WebJobs.Script.Tests.CosmosDB
1112
{
1213
public abstract class CosmosDBEndToEndTestsBase<TTestFixture> :
1314
EndToEndTestsBase<TTestFixture> where TTestFixture : CosmosDBEndtoEndTestFixture, new()
1415
{
16+
private CosmosDBEndtoEndTestFixture _fixture;
17+
1518
public CosmosDBEndToEndTestsBase(TTestFixture fixture) : base(fixture)
1619
{
20+
_fixture = fixture;
1721
}
1822

1923
protected async Task CosmosDBTriggerToBlobTest()
2024
{
2125
// Waiting for the Processor to acquire leases
22-
await Task.Delay(10000);
26+
await Task.Delay(5000);
27+
28+
var dbName = "TriggerItemDb";
29+
await SetUpTriggerListener();
2330

24-
bool collectionsCreated = await Fixture.CreateContainers();
25-
var resultBlob = Fixture.TestOutputContainer.GetBlockBlobReference("cosmosdbtriggere2e-completed");
31+
var resultBlob = _fixture.TestOutputContainer.GetBlockBlobReference("cosmosdbtriggere2e-completed");
2632
await resultBlob.DeleteIfExistsAsync();
2733

2834
string id = Guid.NewGuid().ToString();
2935

3036
var documentToTest = new { id };
3137

32-
await Fixture.CosmosClient.GetContainer("ItemDb", "ItemCollection")
38+
await _fixture.CosmosClient.GetContainer(dbName, "ItemCollection")
3339
.CreateItemAsync(documentToTest, new PartitionKey(id));
3440

3541
// now wait for function to be invoked
3642
string result = await TestHelpers.WaitForBlobAndGetStringAsync(resultBlob,
37-
() => string.Join(Environment.NewLine, Fixture.Host.GetScriptHostLogMessages()));
38-
39-
if (collectionsCreated)
40-
{
41-
// cleanup collections
42-
await Fixture.DeleteContainers();
43-
}
43+
() => string.Join(Environment.NewLine, _fixture.Host.GetScriptHostLogMessages()));
4444

4545
Assert.False(string.IsNullOrEmpty(result));
46+
47+
await _fixture.DeleteCosmosDbResources(dbName);
4648
}
4749

4850
protected async Task CosmosDBTest()
4951
{
50-
bool collectionsCreated = await Fixture.CreateContainers();
51-
string id = Guid.NewGuid().ToString();
52+
var dbName = "InOutItemDb";
53+
await _fixture.CreateContainers(dbName);
54+
await SetUpTriggerListener();
5255

53-
await Fixture.Host.BeginFunctionAsync("CosmosDBOut", id);
54-
55-
dynamic doc = await WaitForItemAsync(id);
56+
string id = Guid.NewGuid().ToString();
57+
await _fixture.Host.BeginFunctionAsync("CosmosDBOut", id);
5658

59+
dynamic doc = await WaitForItemAsync(id, dbName);
5760
Assert.Equal((string)doc.id, id);
5861

59-
var queue = await Fixture.GetNewQueue("documentdb-input");
62+
var queue = await _fixture.GetNewQueue("cosmosdb-input");
6063
string messageContent = string.Format("{{ \"id\": \"{0}\" }}", id);
6164
await queue.AddMessageAsync(new CloudQueueMessage(messageContent));
6265

6366
// And wait for the text to be updated
64-
dynamic updatedDoc = await WaitForItemAsync(id, "This was updated!");
67+
dynamic updatedDoc = await WaitForItemAsync(id, dbName, "This was updated!");
6568

6669
Assert.Equal(updatedDoc.id, doc.id);
6770
Assert.NotEqual(doc._etag, updatedDoc._etag);
71+
72+
await _fixture.DeleteCosmosDbResources(dbName);
6873
}
6974

7075
protected async Task CosmosDBMultipleItemsTest()
7176
{
72-
bool collectionsCreated = await Fixture.CreateContainers();
73-
var resultBlob = Fixture.TestOutputContainer.GetBlockBlobReference("cosmosdbin-multiple-e2e-completed");
74-
await resultBlob.DeleteIfExistsAsync();
77+
var dbName = "MultipleInOutItemDb";
78+
await _fixture.CreateContainers(dbName);
79+
await SetUpTriggerListener();
7580

7681
string id = Guid.NewGuid().ToString();
77-
78-
await Fixture.Host.BeginFunctionAsync("CosmosDBOutMultiple", id);
79-
82+
await _fixture.Host.BeginFunctionAsync("CosmosDBOutMultiple", id);
8083
var testId = id + "-0";
81-
dynamic doc = await WaitForItemAsync(testId);
84+
dynamic doc = await WaitForItemAsync(testId, dbName);
8285

83-
var queue = await Fixture.GetNewQueue("documentdb-input");
86+
var queue = await _fixture.GetNewQueue("cosmosdb-input-multiple");
8487
string messageContent = string.Format("{{ \"id\": \"{0}\" }}", id);
8588
await queue.AddMessageAsync(new CloudQueueMessage(messageContent));
8689

8790
// And wait for the text to be updated
88-
dynamic updatedDoc = await WaitForItemAsync(id, "Hello from Node with multiple input bindings!");
89-
}
90-
91-
protected async Task TestConnectToEmulator()
92-
{
93-
bool collectionsCreated = await Fixture.CreateContainers();
94-
95-
var container = Fixture.CosmosClient.GetContainer("ItemDb", "ItemCollection");
96-
97-
string id = Guid.NewGuid().ToString();
98-
var documentToTest = new { id, text = "connect to emulator test" };
99-
100-
await container.CreateItemAsync(documentToTest, new PartitionKey(id));
101-
dynamic doc = await WaitForItemAsync(id);
91+
dynamic updatedDoc = await WaitForItemAsync(id, dbName, "Hello from Node with multiple input bindings!");
10292

103-
Assert.Equal((string)doc.id, id);
104-
Assert.Equal((string)doc.text, "connect to emulator test");
93+
await _fixture.DeleteCosmosDbResources(dbName);
10594
}
106-
10795

108-
protected async Task<dynamic> WaitForItemAsync(string itemId, string textToMatch = null)
96+
protected async Task<dynamic> WaitForItemAsync(string itemId, string itemDb, string textToMatch = null)
10997
{
110-
var container = Fixture.CosmosClient.GetContainer("ItemDb", "ItemCollection");
98+
var container = _fixture.CosmosClient.GetContainer(itemDb, "ItemCollection");
11199

112100
dynamic document = null;
113101

@@ -139,11 +127,24 @@ await TestHelpers.Await(async () =>
139127
},
140128
userMessageCallback: () =>
141129
{
142-
var logs = string.Join(Environment.NewLine, Fixture.Host.GetScriptHostLogMessages());
130+
var logs = string.Join(Environment.NewLine, _fixture.Host.GetScriptHostLogMessages());
143131
return logs;
144132
});
145133

146134
return document;
147135
}
136+
137+
// Regardless of which function is being tested, the trigger listener needs to be set up or the test host fails
138+
private async Task SetUpTriggerListener()
139+
{
140+
var dbName = "TriggerItemDb";
141+
bool collectionsCreated = await _fixture.CreateContainers(dbName);
142+
}
143+
144+
private async Task RemoveTriggerDb()
145+
{
146+
var dbName = "TriggerItemDb";
147+
await _fixture.DeleteCosmosDbResources(dbName);
148+
}
148149
}
149150
}

test/WebJobs.Script.Tests.Integration/CosmosDB/CosmosDBEndtoEndTestFixture.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,20 @@ public void InitializeCosmosClient()
6565
public override async Task InitializeAsync()
6666
{
6767
InitializeCosmosClient();
68-
await CreateContainers();
6968
await base.InitializeAsync();
7069
}
7170

7271
public override async Task DisposeAsync()
7372
{
7473
await base.DisposeAsync();
75-
await DeleteDatabase();
7674
CosmosClient?.Dispose();
7775
}
7876

79-
public async Task<bool> CreateContainers()
77+
public async Task<bool> CreateContainers(string dbName)
8078
{
8179
bool collectionsCreated = false;
8280

83-
DatabaseResponse databaseResponse = await CosmosClient.CreateDatabaseIfNotExistsAsync("ItemDb");
81+
DatabaseResponse databaseResponse = await CosmosClient.CreateDatabaseIfNotExistsAsync(dbName);
8482
Database database = databaseResponse.Database;
8583

8684
ContainerProperties itemCollectionProperties = new ContainerProperties("ItemCollection", "/id");
@@ -98,15 +96,9 @@ public async Task<bool> CreateContainers()
9896
return collectionsCreated;
9997
}
10098

101-
public async Task DeleteDatabase()
99+
public async Task DeleteCosmosDbResources(string dbName)
102100
{
103-
Database database = CosmosClient.GetDatabase("ItemDb");
104-
await database.DeleteAsync();
105-
}
106-
107-
public async Task DeleteContainers()
108-
{
109-
Database database = CosmosClient.GetDatabase("ItemDb");
101+
Database database = CosmosClient.GetDatabase(dbName);
110102

111103
// Delete the "ItemCollection" container
112104
Container itemCollectionContainer = database.GetContainer("ItemCollection");
@@ -115,6 +107,8 @@ public async Task DeleteContainers()
115107
// Delete the "leases" container
116108
Container leasesContainer = database.GetContainer("leases");
117109
await leasesContainer.DeleteContainerAsync();
110+
111+
await database.DeleteAsync();
118112
}
119113
}
120114
}

test/WebJobs.Script.Tests.Integration/CosmosDB/CosmosDBNodeEndToEndTests.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ public Task CosmosDBMultipleItems()
3232
return CosmosDBMultipleItemsTest();
3333
}
3434

35-
[Fact]
36-
public Task TestConnection()
37-
{
38-
return TestConnectToEmulator();
39-
}
40-
4135
public class TestFixture : CosmosDBEndtoEndTestFixture
4236
{
4337
private const string ScriptRoot = @"TestScripts\Node";

0 commit comments

Comments
 (0)