2018-08-28 16:35:28 +03:00
|
|
|
import assert from "assert";
|
2019-03-19 20:29:27 +03:00
|
|
|
import { CosmosClient, DatabaseDefinition } from "../../dist-esm";
|
2018-08-07 20:34:38 +03:00
|
|
|
import { endpoint, masterKey } from "../common/_testConfig";
|
|
|
|
import { addEntropy, removeAllDatabases } from "../common/TestHelpers";
|
2018-06-05 00:03:49 +03:00
|
|
|
|
2019-06-11 23:19:24 +03:00
|
|
|
const client = new CosmosClient({ endpoint, key: masterKey });
|
2018-06-05 00:03:49 +03:00
|
|
|
|
2018-07-13 02:30:18 +03:00
|
|
|
describe("NodeJS CRUD Tests", function() {
|
|
|
|
this.timeout(process.env.MOCHA_TIMEOUT || 10000);
|
|
|
|
beforeEach(async function() {
|
2018-07-24 04:06:35 +03:00
|
|
|
await removeAllDatabases();
|
2018-07-13 02:30:18 +03:00
|
|
|
});
|
2018-06-05 00:03:49 +03:00
|
|
|
|
2018-07-13 02:30:18 +03:00
|
|
|
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();
|
2018-07-13 02:30:18 +03:00
|
|
|
assert.equal(databases.constructor, Array, "Value should be an array");
|
2018-06-30 02:51:57 +03:00
|
|
|
|
2018-07-13 02:30:18 +03:00
|
|
|
// create a database
|
|
|
|
const beforeCreateDatabasesCount = databases.length;
|
2019-01-24 20:58:04 +03:00
|
|
|
const databaseDefinition = { id: "database test database", throughput: 400 };
|
2019-03-07 23:32:56 +03:00
|
|
|
const { resource: db } = await client.databases.create(databaseDefinition);
|
2018-07-13 02:30:18 +03:00
|
|
|
assert.equal(db.id, databaseDefinition.id);
|
2018-06-30 02:51:57 +03:00
|
|
|
|
2018-07-13 02:30:18 +03:00
|
|
|
// read databases after creation
|
2019-03-07 23:32:56 +03:00
|
|
|
const { resources: databases2 } = await client.databases.readAll().fetchAll();
|
2018-07-13 02:30:18 +03:00
|
|
|
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();
|
2018-07-13 02:30:18 +03:00
|
|
|
assert(results.length > 0, "number of results for the query should be > 0");
|
2018-06-05 00:03:49 +03:00
|
|
|
|
2018-07-13 02:30:18 +03:00
|
|
|
// 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");
|
|
|
|
}
|
|
|
|
};
|
2018-06-05 00:03:49 +03:00
|
|
|
|
2018-07-13 02:30:18 +03:00
|
|
|
it("nativeApi Should do database CRUD operations successfully name based", async function() {
|
|
|
|
await databaseCRUDTest();
|
2018-06-05 00:03:49 +03:00
|
|
|
});
|
2018-07-24 19:38:03 +03:00
|
|
|
|
2018-08-10 00:19:31 +03:00
|
|
|
describe("databases.createIfNotExists", function() {
|
2018-07-24 19:38:03 +03:00
|
|
|
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();
|
2018-07-24 19:38:03 +03:00
|
|
|
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();
|
2018-07-24 19:38:03 +03:00
|
|
|
assert.equal(def.id, readDef.id);
|
|
|
|
});
|
|
|
|
});
|
2018-07-13 02:30:18 +03:00
|
|
|
});
|
2018-06-05 00:03:49 +03:00
|
|
|
|
2018-07-13 02:30:18 +03:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
});
|
2018-06-05 00:03:49 +03:00
|
|
|
|
2018-07-13 02:30:18 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
2018-06-05 00:03:49 +03:00
|
|
|
|
2018-07-13 02:30:18 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
2018-06-05 00:03:49 +03:00
|
|
|
|
2018-07-13 02:30:18 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
2018-06-05 00:03:49 +03:00
|
|
|
|
2018-07-13 02:30:18 +03:00
|
|
|
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);
|
|
|
|
}
|
2018-06-05 00:03:49 +03:00
|
|
|
});
|
2018-07-13 02:30:18 +03:00
|
|
|
});
|
2018-06-05 00:03:49 +03:00
|
|
|
});
|