green for client and database tests

This commit is contained in:
Martin McKeaveney 2020-04-10 11:18:15 +01:00
parent 2ddd463790
commit 8500c52056
9 changed files with 95 additions and 27 deletions

View File

View File

@ -19,7 +19,10 @@ exports.create = async function(ctx) {
}; };
exports.destroy = async function(ctx) { exports.destroy = async function(ctx) {
const databaseId = ctx.params.databaseId; const dbId = `client-${ctx.params.clientId}`;
const database = couchdb.db.use(databaseId) await couchdb.db.destroy(dbId);
ctx.body = await database.destroy(ctx.params.recordId); ctx.body = {
status: 200,
message: `Client Database ${dbId} successfully deleted.`
}
}; };

View File

@ -2,8 +2,8 @@ const couchdb = require("../../db")
const { mapValues, keyBy } = require("lodash/fp") const { mapValues, keyBy } = require("lodash/fp")
const { const {
validateRecord, validateRecord,
} = require("../../../common/src/records/validateRecord.mjs") } = require("../../../common/src/records/validateRecord")
const { events } = require("../../../common/src/common/events.mjs") const { events } = require("../../../common/src/common/events")
const { $ } = require("../../../common/src/common") const { $ } = require("../../../common/src/common")
const { safeParseField } = require("../../../common/src/schema/types"); const { safeParseField } = require("../../../common/src/schema/types");
@ -13,7 +13,7 @@ exports.save = async function(ctx) {
if (!ctx.schema.findModel(record._modelId)) { if (!ctx.schema.findModel(record._modelId)) {
ctx.status = 400 ctx.status = 400
ctx.message = `do not recognise modelId : ${record._modelId}` ctx.message = `Model ${record._modelId} does not exist in schema.`
return return
} }

View File

@ -1,12 +1,12 @@
const Router = require("@koa/router"); const Router = require("@koa/router");
const controller = require("../../controllers/record"); // const controller = require("../../controllers/record");
const router = Router(); const router = Router();
router // router
.get("/api/:databaseId/records/:modelname", controller.fetch) // .get("/api/:databaseId/records/:modelname", controller.fetch)
.post("/api/:databaseId/record", controller.save) // .post("/api/:databaseId/record", controller.save)
.get("/api/:databaseId/record/:recordId", controller.find) // .get("/api/:databaseId/record/:recordId", controller.find)
.delete("/api/:databaseId/record/:recordId", controller.destroy) // .delete("/api/:databaseId/record/:recordId", controller.destroy)
module.exports = router; module.exports = router;

View File

@ -0,0 +1,62 @@
const supertest = require("supertest");
const couchdb = require("../../../../db");
const app = require("../../../../app");
const { createClientDatabase, destroyDatabase } = require("./couchTestUtils")
const CLIENT_DB_ID = "client-testing";
describe("/clients", () => {
let request;
let server;
beforeAll(async () => {
server = await app({
config: {
port: 3000
}
});
request = supertest(server);
});
afterAll(async () => {
server.close();
})
describe("create", () => {
afterEach(async () => {
await destroyDatabase(CLIENT_DB_ID);
});
it("returns a success message when the client database is successfully created", done => {
request
.post("/api/clients")
.send({ clientId: "testing" })
.set("Accept", "application/json")
.expect('Content-Type', /json/)
.expect(200)
.end((err, res) => {
expect(res.body.message).toEqual(`Client Database ${CLIENT_DB_ID} successfully provisioned.`);
done();
});
})
});
describe("destroy", () => {
beforeEach(async () => {
await createClientDatabase();
});
it("returns a success message when the client database is successfully destroyed", async done => {
request
.delete(`/api/clients/testing`)
.set("Accept", "application/json")
.expect('Content-Type', /json/)
.expect(200)
.end((err, res) => {
expect(res.body.message).toEqual(`Client Database ${CLIENT_DB_ID} successfully deleted.`);
done();
});
})
});
});

View File

@ -2,6 +2,8 @@ const couchdb = require("../../../../db");
const CLIENT_DB_ID = "client-testing"; const CLIENT_DB_ID = "client-testing";
exports.destroyDatabase = couchdb.db.destroy;
exports.createClientDatabase = async () => { exports.createClientDatabase = async () => {
await couchdb.db.create(CLIENT_DB_ID); await couchdb.db.create(CLIENT_DB_ID);

View File

@ -1,16 +1,17 @@
const couchdb = require("../../../../db"); const couchdb = require("../../../../db");
const supertest = require("supertest"); const supertest = require("supertest");
const app = require("../../../../app"); const app = require("../../../../app");
const { createInstanceDatabase } = require("./couchTestUtils"); const { createInstanceDatabase, destroyDatabase } = require("./couchTestUtils");
const TEST_INSTANCE_ID = "testing-123"; const TEST_INSTANCE_ID = "testing-123";
describe("/databases", () => { describe("/databases", () => {
let request; let request;
let server;
beforeAll(async () => { beforeAll(async () => {
const server = await app({ server = await app({
config: { config: {
port: 3000 port: 3000
} }
@ -19,10 +20,14 @@ describe("/databases", () => {
}); });
afterAll(async () => { afterAll(async () => {
app.close(); server.close();
}) })
describe("create", () => { describe("create", () => {
afterEach(async () => {
await destroyDatabase(TEST_INSTANCE_ID);
});
it("returns a success message when the instance database is successfully created", done => { it("returns a success message when the instance database is successfully created", done => {
request request
.post(`/api/databases`) .post(`/api/databases`)
@ -38,6 +43,10 @@ describe("/databases", () => {
}); });
describe("destroy", () => { describe("destroy", () => {
beforeEach(async () => {
await createInstanceDatabase(TEST_INSTANCE_ID);
});
it("returns a success message when the instance database is successfully deleted", done => { it("returns a success message when the instance database is successfully deleted", done => {
request request
.delete(`/api/databases/${TEST_INSTANCE_ID}`) .delete(`/api/databases/${TEST_INSTANCE_ID}`)

View File

@ -16,6 +16,7 @@ describe("/users", () => {
} }
}); });
request = supertest(server); request = supertest(server);
createInstanceDatabase(TEST_INSTANCE_ID);
}); });
afterAll(async () => { afterAll(async () => {
@ -23,10 +24,10 @@ describe("/users", () => {
}) })
describe("create", () => { describe("create", () => {
it("returns a success message when the instance database is successfully created", done => { it("returns a success message when a user is successfully created", done => {
request request
.post(`/api/users`) .post(`/api/${TEST_INSTANCE_ID}/users`)
.send({ name: TEST_INSTANCE_ID }) .send({ name: "John" })
.set("Accept", "application/json") .set("Accept", "application/json")
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)

View File

@ -1,9 +0,0 @@
const { testSchema } = require("../../common/test/testSchema")
describe("record persistence", () => {
it("should save a record", async () => {
})
})