diff --git a/src/client/Database/Database.ts b/src/client/Database/Database.ts index 23bc6d5..76314b8 100644 --- a/src/client/Database/Database.ts +++ b/src/client/Database/Database.ts @@ -3,7 +3,7 @@ import { CosmosClient } from "../../CosmosClient"; import { RequestOptions } from "../../request"; import { Container, Containers } from "../Container"; import { User, Users } from "../User"; -import { DatabaseDefinition } from "./DatabaseDefinition"; +import { DatabaseResponse } from "./DatabaseResponse"; export class Database { public readonly containers: Containers; diff --git a/src/client/Item/Items.ts b/src/client/Item/Items.ts index 3bbe284..0c9d850 100644 --- a/src/client/Item/Items.ts +++ b/src/client/Item/Items.ts @@ -37,7 +37,7 @@ export class Items { public async create(body: T, options?: RequestOptions): Promise>; public async create(body: T, options?: RequestOptions): Promise> { const response = await (this.client.createDocument(this.container.url, body, options) as Promise>); - const ref = new Item(this.container, (response.result as any).id, options.partitionKey as string); + const ref = new Item(this.container, (response.result as any).id, (options && options.partitionKey) as string); return {body: response.result, headers: response.headers, ref, item: ref}; } @@ -52,7 +52,7 @@ export class Items { public async upsert(body: T, options?: RequestOptions): Promise>; public async upsert(body: T, options?: RequestOptions): Promise> { const response = await this.client.upsertDocument(this.container.url, body, options); - const ref = new Item(this.container, (response.result as any).id, options.partitionKey as string); + const ref = new Item(this.container, (response.result as any).id, (options && options.partitionKey) as string); return {body: response.result, headers: response.headers, ref, item: ref}; } } diff --git a/src/test/common/TestHelpers.ts b/src/test/common/TestHelpers.ts index 1a8e216..34dfcde 100644 --- a/src/test/common/TestHelpers.ts +++ b/src/test/common/TestHelpers.ts @@ -67,7 +67,7 @@ export class TestHelpers { // TODO: should we block or do all requests in parallel? const { body: doc } = await container.item(document.id).read(options); - + assert.equal(JSON.stringify(doc), JSON.stringify(document)); } catch (err) { throw err; diff --git a/src/test/functional/container.spec.ts b/src/test/functional/container.spec.ts index 3973f6a..5840065 100644 --- a/src/test/functional/container.spec.ts +++ b/src/test/functional/container.spec.ts @@ -180,7 +180,7 @@ describe("NodeJS CRUD Tests", function () { // create container const { body: containerDef } = await database.containers.create({ id: "container test container" }); const container = database.container(containerDef.id); - + assert.equal(containerDef.indexingPolicy.indexingMode, DocumentBase.IndexingMode.Consistent, "default indexing mode should be consistent"); await container.delete(); diff --git a/src/test/functional/item.spec.ts b/src/test/functional/item.spec.ts index c83b529..a0f3718 100644 --- a/src/test/functional/item.spec.ts +++ b/src/test/functional/item.spec.ts @@ -25,11 +25,11 @@ describe("NodeJS CRUD Tests", function () { const client = new CosmosClient({ endpoint, auth: { masterKey } }); // create database const { body: dbdef } = await client.databases.create({ id: "sample 中文 database" }); - const db: Database = client.databases.get(dbdef.id); + const db: Database = client.database(dbdef.id); // create container const { body: containerdef } = await db.containers.create({ id: "sample container" }); - const container: Container = db.containers.get(containerdef.id); + const container: Container = db.container(containerdef.id); // read items const { result: items } = await container.items.readAll().toArray(); @@ -44,80 +44,54 @@ describe("NodeJS CRUD Tests", function () { replace: "new property", }; try { - const client = new CosmosClient({ endpoint, auth: { masterKey } }); - // create database - const { body: dbdef } = await client.databases.create({ id: "sample 中文 database" }); - const db: Database = client.database(dbdef.id); - // create container - const { body: containerdef } = - await db.containers.create({ id: "sample container" }); - const container: Container = db.container(containerdef.id); + await TestHelpers.createOrUpsertItem(container, itemDefinition, + { disableAutomaticIdGeneration: true }, isUpsertTest); + assert.fail("id generation disabled must throw with invalid id"); + } catch (err) { + assert(err !== undefined, "should throw an error because automatic id generation is disabled"); + } + const { body: document } = await TestHelpers.createOrUpsertItem( + container, itemDefinition, undefined, isUpsertTest); + assert.equal(document.name, itemDefinition.name); + assert(document.id !== undefined); + // read documents after creation + const { result: documents2 } = await container.items.readAll().toArray(); + assert.equal(documents2.length, beforeCreateDocumentsCount + 1, + "create should increase the number of documents"); + // query documents + const querySpec = { + query: "SELECT * FROM root r WHERE r.id=@id", + parameters: [ + { + name: "@id", + value: document.id, + }, + ], + }; + const { result: results } = await container.items.query(querySpec).toArray(); + assert(results.length > 0, "number of results for the query should be > 0"); + const { result: results2 } = await container.items.query( + querySpec, { enableScanInQuery: true }).toArray(); + assert(results2.length > 0, "number of results for the query should be > 0"); - // read items - const { body: items } = await container.items.readAll().toArray(); - assert(Array.isArray(items), "Value should be an array"); + // replace document + document.name = "replaced document"; + document.foo = "not bar"; + const { body: replacedDocument } = await TestHelpers.replaceOrUpsertItem( + container, document, undefined, isUpsertTest); + assert.equal(replacedDocument.name, "replaced document", "document name property should change"); + assert.equal(replacedDocument.foo, "not bar", "property should have changed"); + assert.equal(document.id, replacedDocument.id, "document id should stay the same"); + // read document + const { body: document2 } = await container.item(replacedDocument.id).read(); + assert.equal(replacedDocument.id, document.id); + // delete document + const { body: res } = await container.item(replacedDocument.id).delete(); - // create an item - const beforeCreateDocumentsCount = items.length; - const itemDefinition = { - name: "sample document", - foo: "bar", - key: "value", - replace: "new property", - }; - try { - await TestHelpers.createOrUpsertItem(container, itemDefinition, - { disableAutomaticIdGeneration: true }, isUpsertTest); - assert.fail("id generation disabled must throw with invalid id"); - } catch (err) { - assert(err !== undefined, "should throw an error because automatic id generation is disabled"); - } - const { body: document } = await TestHelpers.createOrUpsertItem( - container, itemDefinition, undefined, isUpsertTest); - assert.equal(document.name, itemDefinition.name); - assert(document.id !== undefined); - // read documents after creation - const { result: documents2 } = await container.items.readAll().toArray(); - assert.equal(documents2.length, beforeCreateDocumentsCount + 1, - "create should increase the number of documents"); - // query documents - const querySpec = { - query: "SELECT * FROM root r WHERE r.id=@id", - parameters: [ - { - name: "@id", - value: document.id, - }, - ], - }; - const { body: results } = await container.items.query(querySpec).toArray(); - assert(results.length > 0, "number of results for the query should be > 0"); - const { body: results2 } = await container.items.query( - querySpec, { enableScanInQuery: true }).toArray(); - assert(results2.length > 0, "number of results for the query should be > 0"); - - // replace document - document.name = "replaced document"; - document.foo = "not bar"; - const { body: replacedDocument } = await TestHelpers.replaceOrUpsertItem( - container, document, undefined, isUpsertTest); - assert.equal(replacedDocument.name, "replaced document", "document name property should change"); - assert.equal(replacedDocument.foo, "not bar", "property should have changed"); - assert.equal(document.id, replacedDocument.id, "document id should stay the same"); - // read document - const { body: document2 } = await container.item(replacedDocument.id).read(); - assert.equal(replacedDocument.id, document.id); - // delete document - const { body: res } = await container.item(replacedDocument.id).delete(); - - // read documents after deletion - try { - const { body: document3 } = await container.item(replacedDocument.id).read(); - assert.fail("must throw if document doesn't exist"); - } catch (err) { - const notFoundErrorCode = 404; - assert.equal(err.code, notFoundErrorCode, "response should return error code 404"); - } + // read documents after deletion + try { + const { body: document3 } = await container.item(replacedDocument.id).read(); + assert.fail("must throw if document doesn't exist"); } catch (err) { const notFoundErrorCode = 404; assert.equal(err.code, notFoundErrorCode, "response should return error code 404"); @@ -127,7 +101,7 @@ describe("NodeJS CRUD Tests", function () { const documentCRUDMultiplePartitionsTest = async function () { const client = new CosmosClient({endpoint, auth: { masterKey }}); // create database - const { result: dbdef } = await client.databases.create({ id: "db1" }); + const { body: dbdef } = await client.databases.create({ id: "db1" }); const db = client.database(dbdef.id); const partitionKey = "key"; @@ -137,7 +111,7 @@ describe("NodeJS CRUD Tests", function () { partitionKey: { paths: ["/" + partitionKey], kind: DocumentBase.PartitionKind.Hash }, }; - const { result: containerdef } = + const { body: containerdef } = await db.containers.create(containerDefinition, { offerThroughput: 12000 }); const container = db.container(containerdef.id); @@ -177,7 +151,7 @@ describe("NodeJS CRUD Tests", function () { query: "SELECT * FROM Root", }; try { - const { body: badUpdate } = await container.items.query( + const { result: badUpdate } = await container.items.query( querySpec, { enableScanInQuery: true }).toArray(); assert.fail("Must fail"); } catch (err) { @@ -185,7 +159,7 @@ describe("NodeJS CRUD Tests", function () { assert.equal(err.code, badRequestErrorCode, "response should return error code " + badRequestErrorCode); } - const { body: results } = await container.items.query( + const { result: results } = await container.items.query( querySpec, { enableScanInQuery: true, enableCrossPartitionQuery: true }).toArray(); assert(results !== undefined, "error querying documents"); results.sort(function (doc1, doc2) { diff --git a/src/test/functional/ttl.spec.ts b/src/test/functional/ttl.spec.ts index 2c29619..6fc7d28 100644 --- a/src/test/functional/ttl.spec.ts +++ b/src/test/functional/ttl.spec.ts @@ -66,7 +66,7 @@ describe("NodeJS CRUD Tests", function () { }; const database = await client.database(db.id); const { body: containerResult } = await database.containers.create(containerDefinition); - + assert.equal(containerDefinition.defaultTtl, containerResult.defaultTtl); const container = database.container(containerResult.id); diff --git a/src/test/integration/session.spec.ts b/src/test/integration/session.spec.ts index 0df43e2..ec0abc5 100644 --- a/src/test/integration/session.spec.ts +++ b/src/test/integration/session.spec.ts @@ -57,7 +57,7 @@ describe("Session Token", function () { let index2; await client.databases.create(databaseBody); - const database = client.database(databaseDef.id); + const database = client.database(databaseId); const { body: createdContainerDef } = await database.containers.create(containerDefinition, containerOptions); @@ -85,7 +85,7 @@ describe("Session Token", function () { let secondPartitionLSN = tokens[index2]; await container.item(document1.id, "1").read(); - + assert.equal(getSpy.lastCall.args[2][Constants.HttpHeaders.SessionToken], client.documentClient.sessionContainer.getCombinedSessionToken(tokens)); tokens = getToken(client.documentClient.sessionContainer.collectionResourceIdToSessionTokens);