view endpoints with tests
This commit is contained in:
parent
093e52afb0
commit
a2f5459a4b
|
@ -2,13 +2,26 @@ const couchdb = require("../../db");
|
||||||
|
|
||||||
const controller = {
|
const controller = {
|
||||||
fetch: async ctx => {
|
fetch: async ctx => {
|
||||||
const database = couchdb.db.use(ctx.params.databaseId);
|
const db = couchdb.db.use(ctx.params.instanceId);
|
||||||
ctx.body = await database.list({ include_docs: true });
|
const designDoc = await db.get("_design/database");
|
||||||
|
ctx.body = designDoc.views;
|
||||||
},
|
},
|
||||||
create: async ctx => {
|
create: async ctx => {
|
||||||
const clientDb = couchdb.db.use(ctx.params.clientId);
|
const db = couchdb.db.use(ctx.params.instanceId);
|
||||||
clientDb.insert();
|
const { name, ...viewDefinition } = ctx.request.body;
|
||||||
ctx.body = await database.insert(ctx.request.body);
|
|
||||||
|
const designDoc = await db.get("_design/database");
|
||||||
|
designDoc.views = {
|
||||||
|
...designDoc.views,
|
||||||
|
[name]: viewDefinition
|
||||||
|
};
|
||||||
|
const newView = await db.insert(designDoc, designDoc._id);
|
||||||
|
|
||||||
|
ctx.body = {
|
||||||
|
...newView,
|
||||||
|
message: `View ${name} created successfully.`,
|
||||||
|
status: 200,
|
||||||
|
}
|
||||||
},
|
},
|
||||||
destroy: async ctx => {
|
destroy: async ctx => {
|
||||||
const database = couchdb.db.use(ctx.params.databaseId);
|
const database = couchdb.db.use(ctx.params.databaseId);
|
||||||
|
|
|
@ -21,6 +21,7 @@ const neoUserRoutes = require("./routes/neo/user");
|
||||||
const clientRoutes = require("./routes/neo/client");
|
const clientRoutes = require("./routes/neo/client");
|
||||||
const applicationRoutes = require("./routes/neo/application");
|
const applicationRoutes = require("./routes/neo/application");
|
||||||
const modelsRoutes = require("./routes/neo/model");
|
const modelsRoutes = require("./routes/neo/model");
|
||||||
|
const viewsRoutes = require("./routes/neo/view");
|
||||||
|
|
||||||
const builderPath = resolve(__dirname, "../builder")
|
const builderPath = resolve(__dirname, "../builder")
|
||||||
|
|
||||||
|
@ -134,6 +135,9 @@ module.exports = (config, app) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.use(viewsRoutes.routes());
|
||||||
|
router.use(viewsRoutes.allowedMethods());
|
||||||
|
|
||||||
router.use(modelsRoutes.routes());
|
router.use(modelsRoutes.routes());
|
||||||
router.use(modelsRoutes.allowedMethods());
|
router.use(modelsRoutes.allowedMethods());
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||||
|
|
||||||
|
exports[`/views fetch returns a list of all the views that exist in the instance database 1`] = `
|
||||||
|
Object {
|
||||||
|
"by_type": Object {
|
||||||
|
"map": "function (doc) {
|
||||||
|
emit([doc.type], doc._id);
|
||||||
|
}",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
`;
|
|
@ -8,20 +8,21 @@ const CLIENT_DB_ID = "client-testing";
|
||||||
|
|
||||||
describe("/applications", () => {
|
describe("/applications", () => {
|
||||||
let request;
|
let request;
|
||||||
|
let server;
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
const server = await app({
|
server = await app({
|
||||||
config: {
|
config: {
|
||||||
port: 3000
|
port: 3000
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
request = supertest(server);
|
request = supertest(server);
|
||||||
createClientDatabase();
|
await createClientDatabase();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await couchdb.db.destroy(CLIENT_DB_ID)
|
await couchdb.db.destroy(CLIENT_DB_ID)
|
||||||
app.close();
|
server.close();
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("create", () => {
|
describe("create", () => {
|
||||||
|
|
|
@ -0,0 +1,80 @@
|
||||||
|
const supertest = require("supertest");
|
||||||
|
const app = require("../../../../app");
|
||||||
|
const { createInstanceDatabase, createModel, destroyDatabase } = require("./couchTestUtils");
|
||||||
|
|
||||||
|
|
||||||
|
const TEST_INSTANCE_ID = "testing-123";
|
||||||
|
|
||||||
|
describe("/views", () => {
|
||||||
|
let request;
|
||||||
|
let server;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
server = await app({
|
||||||
|
config: {
|
||||||
|
port: 3000
|
||||||
|
}
|
||||||
|
});
|
||||||
|
request = supertest(server);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
server.close();
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("create", () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
await createInstanceDatabase(TEST_INSTANCE_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
await destroyDatabase(TEST_INSTANCE_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns a success message when the view is successfully created", done => {
|
||||||
|
request
|
||||||
|
.post(`/api/${TEST_INSTANCE_ID}/views`)
|
||||||
|
.send({
|
||||||
|
name: "TestView",
|
||||||
|
map: `function(doc) {
|
||||||
|
if (doc.id) {
|
||||||
|
emit(doc.name, doc._id);
|
||||||
|
}
|
||||||
|
}`,
|
||||||
|
reduce: `function(keys, values) { }`
|
||||||
|
})
|
||||||
|
.set("Accept", "application/json")
|
||||||
|
.expect('Content-Type', /json/)
|
||||||
|
.expect(200)
|
||||||
|
.end(async (err, res) => {
|
||||||
|
expect(res.body.message).toEqual("View TestView created successfully.");
|
||||||
|
expect(res.body.id).toEqual("_design/database");
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("fetch", () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
await createInstanceDatabase(TEST_INSTANCE_ID);
|
||||||
|
await createModel(TEST_INSTANCE_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
await destroyDatabase(TEST_INSTANCE_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns a list of all the views that exist in the instance database", done => {
|
||||||
|
request
|
||||||
|
.get(`/api/${TEST_INSTANCE_ID}/views`)
|
||||||
|
.set("Accept", "application/json")
|
||||||
|
.expect('Content-Type', /json/)
|
||||||
|
.expect(200)
|
||||||
|
.end(async (_, res) => {
|
||||||
|
expect(res.body.by_type).toBeDefined();
|
||||||
|
expect(res.body).toMatchSnapshot();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
})
|
||||||
|
});
|
||||||
|
});
|
|
@ -4,8 +4,8 @@ const controller = require("../../controllers/view");
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
router
|
router
|
||||||
.get("/api/:databaseId/views", controller.fetch)
|
.get("/api/:instanceId/views", controller.fetch)
|
||||||
.post("/api/:databaseId/views", controller.create)
|
.post("/api/:instanceId/views", controller.create);
|
||||||
.patch("/api/:databaseId/views", controller.update);
|
// .patch("/api/:databaseId/views", controller.update);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
Loading…
Reference in New Issue