-
-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathazure-cosmosdb-emulator-container.test.ts
More file actions
94 lines (80 loc) · 3.26 KB
/
azure-cosmosdb-emulator-container.test.ts
File metadata and controls
94 lines (80 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { CosmosClient, PartitionKeyKind } from "@azure/cosmos";
import https from "node:https";
import { getImage } from "../../../testcontainers/src/utils/test-helper";
import { AzureCosmosDbEmulatorContainer } from "./azure-cosmosdb-emulator-container";
const IMAGE = getImage(__dirname);
describe("AzureCosmosDbEmulatorContainer", { timeout: 180_000 }, async () => {
it("should set https protocol", async () => {
await using container = await new AzureCosmosDbEmulatorContainer(IMAGE).withProtocol("https").start();
const connectionUri = container.getConnectionUri();
expect(connectionUri).toContain("AccountEndpoint=https://");
});
it("should set http protocol if no protocol is specified", async () => {
await using container = await new AzureCosmosDbEmulatorContainer(IMAGE).start();
const connectionUri = container.getConnectionUri();
expect(connectionUri).toContain("AccountEndpoint=http://");
});
// httpCreateDB {
it("should be able to create a database using http", async () => {
await using container = await new AzureCosmosDbEmulatorContainer(IMAGE).withProtocol("http").start();
const cosmosClient = new CosmosClient({
endpoint: container.getEndpoint(),
key: container.getKey(),
});
const dbName = "testdb";
const createResponse = await cosmosClient.databases.createIfNotExists({
id: dbName,
});
expect(createResponse.statusCode).toBe(201);
const db = await cosmosClient.database(dbName).read();
expect(db.database.id).toBe(dbName);
});
// }
// httpsCreateDB {
it("should be able to create a database using https", async () => {
await using container = await new AzureCosmosDbEmulatorContainer(IMAGE).withProtocol("https").start();
const cosmosClient = new CosmosClient({
endpoint: container.getEndpoint(),
key: container.getKey(),
agent: new https.Agent({
rejectUnauthorized: false, //allows insecure TLS; import * as https from "node:https";
}),
});
const dbName = "testdb";
const createResponse = await cosmosClient.databases.createIfNotExists({
id: dbName,
});
expect(createResponse.statusCode).toBe(201);
const db = await cosmosClient.database(dbName).read();
expect(db.database.id).toBe(dbName);
});
// }
// createAndRead {
it("should be able to create a container and store and retrieve items", async () => {
await using container = await new AzureCosmosDbEmulatorContainer(IMAGE).withProtocol("http").start();
const cosmosClient = new CosmosClient({
endpoint: container.getEndpoint(),
key: container.getKey(),
});
const dbName = "testdb";
await cosmosClient.databases.createIfNotExists({
id: dbName,
});
const dbClient = cosmosClient.database(dbName);
const containerName = "testcontainer";
await dbClient.containers.createIfNotExists({
id: containerName,
partitionKey: {
kind: PartitionKeyKind.Hash,
paths: ["/foo"],
},
});
const containerClient = dbClient.container(containerName);
const createResponse = await containerClient.items.create({
foo: "bar",
});
const readItem = await containerClient.item(createResponse.item.id, "bar").read();
expect(readItem.resource.foo).toEqual("bar");
});
// }
});