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) {
const databaseId = ctx.params.databaseId;
const database = couchdb.db.use(databaseId)
ctx.body = await database.destroy(ctx.params.recordId);
const dbId = `client-${ctx.params.clientId}`;
await couchdb.db.destroy(dbId);
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 {
validateRecord,
} = require("../../../common/src/records/validateRecord.mjs")
const { events } = require("../../../common/src/common/events.mjs")
} = require("../../../common/src/records/validateRecord")
const { events } = require("../../../common/src/common/events")
const { $ } = require("../../../common/src/common")
const { safeParseField } = require("../../../common/src/schema/types");
@ -13,7 +13,7 @@ exports.save = async function(ctx) {
if (!ctx.schema.findModel(record._modelId)) {
ctx.status = 400
ctx.message = `do not recognise modelId : ${record._modelId}`
ctx.message = `Model ${record._modelId} does not exist in schema.`
return
}

View File

@ -1,12 +1,12 @@
const Router = require("@koa/router");
const controller = require("../../controllers/record");
// const controller = require("../../controllers/record");
const router = Router();
router
.get("/api/:databaseId/records/:modelname", controller.fetch)
.post("/api/:databaseId/record", controller.save)
.get("/api/:databaseId/record/:recordId", controller.find)
.delete("/api/:databaseId/record/:recordId", controller.destroy)
// router
// .get("/api/:databaseId/records/:modelname", controller.fetch)
// .post("/api/:databaseId/record", controller.save)
// .get("/api/:databaseId/record/:recordId", controller.find)
// .delete("/api/:databaseId/record/:recordId", controller.destroy)
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";
exports.destroyDatabase = couchdb.db.destroy;
exports.createClientDatabase = async () => {
await couchdb.db.create(CLIENT_DB_ID);

View File

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

View File

@ -16,6 +16,7 @@ describe("/users", () => {
}
});
request = supertest(server);
createInstanceDatabase(TEST_INSTANCE_ID);
});
afterAll(async () => {
@ -23,10 +24,10 @@ describe("/users", () => {
})
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
.post(`/api/users`)
.send({ name: TEST_INSTANCE_ID })
.post(`/api/${TEST_INSTANCE_ID}/users`)
.send({ name: "John" })
.set("Accept", "application/json")
.expect('Content-Type', /json/)
.expect(200)

View File

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