azure-cosmos-js/test/functional/database.spec.ts

132 строки
4.9 KiB
TypeScript
Исходник Обычный вид История

import assert from "assert";
import { CosmosClient, DatabaseDefinition } from "../../dist-esm";
import { endpoint, masterKey } from "../common/_testConfig";
import { addEntropy, removeAllDatabases } from "../common/TestHelpers";
2019-06-11 23:19:24 +03:00
const client = new CosmosClient({ endpoint, key: masterKey });
describe("NodeJS CRUD Tests", function() {
this.timeout(process.env.MOCHA_TIMEOUT || 10000);
beforeEach(async function() {
await removeAllDatabases();
});
describe("Validate Database CRUD", async function() {
const databaseCRUDTest = async function() {
// read databases
2019-03-07 23:32:56 +03:00
const { resources: databases } = await client.databases.readAll().fetchAll();
assert.equal(databases.constructor, Array, "Value should be an array");
Feature/object model refactor (#26) * Move foreach to async iterator * Mock out new APIs * remove legacy tests * remove old ntvs stuff for samples * Add shimmed implemention + bug fixes * Update (some) samples to use new OM * refactor interfaces into their own files * refactor om into new directory structure * small tweak to lib in gitignore * renamed document to item * update tests * Move Item/Permission/User tests * Remove Attachment from OM * Update item, container, database, and database account tests * Permission tests * User tests * Aggregate query tests moved to new OM * collectionNaming tests obsolete due to attachment/media * Update crosspartition test * Add encoding test * Update incremental feed tests * update proxy test * Update query test * update ruPerMind test * Update session tests * Update session container tests (moved to unit test) * Update SSL tests * Update UriFactory tests (moved to unit) * Update document client tests * Update authorization tests * Fix lint * Refactor of UDF test * Refactor trigger test * Refactor functional TTL test * Remove UriFactory * Type assertion due to intermediate implementation details * Query needs to support string * Support any overload * Query e2e tests * Spatial e2e test updated * Refactor functional sproc test * Add await to fix TS errors * Type assertion on container def for now * Remove offer tests for now * authorization tests updated * bug in test * Comment out and skip HashPartitionResolver test * Use UriFactory for urls * rids make me sad * types are fun * bugs in permission test * Symbol Shim for forEach * Fix user test * stringify the funciton bodys to avoid TypeScript weirdness * Support string body for UDF * Small bug in TTL test * Fix Authorization tests * remove bad options object * One more sproc fix * Need a blank object for parition key (if none specified) * One tests is broken still for now * Expose ErrorResponse * Handle undefined in forEach * Better error handling * Better error handling for crossParititon tests * Update tslint to catch dangling promises * Port over existing docs to TS files * Fix Mock toArray to promise based * Include tests in lint and fix errors * No assignment to Symbol.asyncIterator in node 10 * tslint fix * Remove unnecessary test (forEach is an iterable now) * no more generic database names in test * Index kind mistake * Missing promise * Shorten timeout (flakey test) * rename \w+s\.read to \w+s\.readAll * Move all #.get\w+ to #.get * Remove rid test. Pick up global timeout in auth test * Fix tslint * Fix test timeouts against production, except for TTL. Run TS tests directly * Add files * remove some collection/document mentions * fix sproc mistake from rename * Add CosmosClientOptions to export
2018-06-30 02:51:57 +03:00
// create a database
const beforeCreateDatabasesCount = databases.length;
const databaseDefinition = { id: "database test database", throughput: 400 };
2019-03-07 23:32:56 +03:00
const { resource: db } = await client.databases.create(databaseDefinition);
assert.equal(db.id, databaseDefinition.id);
Feature/object model refactor (#26) * Move foreach to async iterator * Mock out new APIs * remove legacy tests * remove old ntvs stuff for samples * Add shimmed implemention + bug fixes * Update (some) samples to use new OM * refactor interfaces into their own files * refactor om into new directory structure * small tweak to lib in gitignore * renamed document to item * update tests * Move Item/Permission/User tests * Remove Attachment from OM * Update item, container, database, and database account tests * Permission tests * User tests * Aggregate query tests moved to new OM * collectionNaming tests obsolete due to attachment/media * Update crosspartition test * Add encoding test * Update incremental feed tests * update proxy test * Update query test * update ruPerMind test * Update session tests * Update session container tests (moved to unit test) * Update SSL tests * Update UriFactory tests (moved to unit) * Update document client tests * Update authorization tests * Fix lint * Refactor of UDF test * Refactor trigger test * Refactor functional TTL test * Remove UriFactory * Type assertion due to intermediate implementation details * Query needs to support string * Support any overload * Query e2e tests * Spatial e2e test updated * Refactor functional sproc test * Add await to fix TS errors * Type assertion on container def for now * Remove offer tests for now * authorization tests updated * bug in test * Comment out and skip HashPartitionResolver test * Use UriFactory for urls * rids make me sad * types are fun * bugs in permission test * Symbol Shim for forEach * Fix user test * stringify the funciton bodys to avoid TypeScript weirdness * Support string body for UDF * Small bug in TTL test * Fix Authorization tests * remove bad options object * One more sproc fix * Need a blank object for parition key (if none specified) * One tests is broken still for now * Expose ErrorResponse * Handle undefined in forEach * Better error handling * Better error handling for crossParititon tests * Update tslint to catch dangling promises * Port over existing docs to TS files * Fix Mock toArray to promise based * Include tests in lint and fix errors * No assignment to Symbol.asyncIterator in node 10 * tslint fix * Remove unnecessary test (forEach is an iterable now) * no more generic database names in test * Index kind mistake * Missing promise * Shorten timeout (flakey test) * rename \w+s\.read to \w+s\.readAll * Move all #.get\w+ to #.get * Remove rid test. Pick up global timeout in auth test * Fix tslint * Fix test timeouts against production, except for TTL. Run TS tests directly * Add files * remove some collection/document mentions * fix sproc mistake from rename * Add CosmosClientOptions to export
2018-06-30 02:51:57 +03:00
// read databases after creation
2019-03-07 23:32:56 +03:00
const { resources: databases2 } = await client.databases.readAll().fetchAll();
assert.equal(databases2.length, beforeCreateDatabasesCount + 1, "create should increase the number of databases");
// query databases
const querySpec = {
query: "SELECT * FROM root r WHERE r.id=@id",
parameters: [
{
name: "@id",
value: databaseDefinition.id
}
]
};
2019-03-07 23:32:56 +03:00
const { resources: results } = await client.databases.query(querySpec).fetchAll();
assert(results.length > 0, "number of results for the query should be > 0");
// delete database
await client.database(db.id).delete();
try {
// read database after deletion
await client.database(db.id).read();
assert.fail("Read database on non-existent database should fail");
} catch (err) {
const notFoundErrorCode = 404;
assert.equal(err.code, notFoundErrorCode, "response should return error code 404");
}
};
it("nativeApi Should do database CRUD operations successfully name based", async function() {
await databaseCRUDTest();
});
describe("databases.createIfNotExists", function() {
it("should handle does not exist", async function() {
const def: DatabaseDefinition = { id: addEntropy("does not exist") };
const { database } = await client.databases.createIfNotExists(def);
2019-03-07 23:32:56 +03:00
const { resource: readDef } = await database.read();
assert.equal(def.id, readDef.id);
});
it("should handle does exist", async function() {
const def: DatabaseDefinition = { id: addEntropy("does exist") };
// Set up
await client.databases.create(def);
// Now call createIfNotExists on existing db
const { database } = await client.databases.createIfNotExists(def);
2019-03-07 23:32:56 +03:00
const { resource: readDef } = await database.read();
assert.equal(def.id, readDef.id);
});
});
});
// TODO: These are unit tests, not e2e tests like above, so maybe should seperate these.
describe("Validate Id validation", function() {
it("nativeApi Should fail on ends with a space", async function() {
// Id shoudn't end with a space.
try {
await client.databases.create({ id: "id_ends_with_space " });
assert.fail("Must throw if id ends with a space");
} catch (err) {
assert.equal("Id ends with a space.", err.message);
}
});
it("nativeAPI Should fail on contains '/'", async function() {
// Id shoudn't contain "/".
try {
await client.databases.create({ id: "id_with_illegal/_char" });
assert.fail("Must throw if id has illegal characters");
} catch (err) {
assert.equal("Id contains illegal chars.", err.message);
}
});
it("nativeAPI Should fail on contains '\\'", async function() {
// Id shoudn't contain "\\".
try {
await client.databases.create({ id: "id_with_illegal\\_char" });
assert.fail("Must throw if id contains illegal characters");
} catch (err) {
assert.equal("Id contains illegal chars.", err.message);
}
});
it("nativeAPI Should fail on contains '?'", async function() {
// Id shoudn't contain "?".
try {
await client.databases.create({ id: "id_with_illegal?_?char" });
assert.fail("Must throw if id contains illegal characters");
} catch (err) {
assert.equal("Id contains illegal chars.", err.message);
}
});
it("nativeAPI should fail on contains '#'", async function() {
// Id shoudn't contain "#".
try {
await client.databases.create({ id: "id_with_illegal#_char" });
assert.fail("Must throw if id contains illegal characters");
} catch (err) {
assert.equal("Id contains illegal chars.", err.message);
}
});
});
});