* Fix dev

* Fix tests
This commit is contained in:
Steve Faulkner 2018-07-11 16:06:33 -04:00 коммит произвёл GitHub
Родитель 0f253456a5
Коммит b22194817b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 60 добавлений и 86 удалений

Просмотреть файл

@ -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;

Просмотреть файл

@ -37,7 +37,7 @@ export class Items {
public async create<T>(body: T, options?: RequestOptions): Promise<ItemResponse<T>>;
public async create<T>(body: T, options?: RequestOptions): Promise<ItemResponse<T>> {
const response = await (this.client.createDocument(this.container.url, body, options) as Promise<Response<T>>);
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<T>(body: T, options?: RequestOptions): Promise<ItemResponse<T>>;
public async upsert<T>(body: T, options?: RequestOptions): Promise<ItemResponse<T>> {
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};
}
}

Просмотреть файл

@ -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;

Просмотреть файл

@ -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();

Просмотреть файл

@ -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) {

Просмотреть файл

@ -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);

Просмотреть файл

@ -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);