fixing tests

This commit is contained in:
Martin McKeaveney 2020-04-24 18:02:51 +01:00
parent b2b94c8ccc
commit 5385357b9d
21 changed files with 178 additions and 115 deletions

View File

@ -27,8 +27,8 @@ export async function loadRecord(key, { appname, instanceId }) {
return await response.json() return await response.json()
} }
export async function saveRecord({ record, instanceId, modelId }) { export async function saveRecord(record, instanceId) {
const SAVE_RECORDS_URL = `/api/${instanceId}/${modelId}/records` const SAVE_RECORDS_URL = `/api/${instanceId}/records`
const response = await api.post(SAVE_RECORDS_URL, record) const response = await api.post(SAVE_RECORDS_URL, record)
return await response.json() return await response.json()
} }

View File

@ -43,19 +43,22 @@
} }
async function saveRecord() { async function saveRecord() {
const recordResponse = await api.saveRecord({ const recordResponse = await api.saveRecord(
record, {
instanceId, ...record,
modelId: $backendUiStore.selectedModel._id modelId: $backendUiStore.selectedModel._id,
}) },
instanceId
)
if (recordResponse.errors) { if (recordResponse.errors) {
errors = recordResponse.errors; errors = recordResponse.errors
return; return
} }
backendUiStore.update(state => { backendUiStore.update(state => {
state.selectedView = state.selectedView state.selectedView = state.selectedView
return state return state
onClosed();
}) })
} }
</script> </script>

View File

@ -14,7 +14,6 @@
async function createUser() { async function createUser() {
const user = { name: username, username, password } const user = { name: username, username, password }
const response = await api.createUser(user, instanceId); const response = await api.createUser(user, instanceId);
console.log(response);
backendUiStore.actions.users.create(response) backendUiStore.actions.users.create(response)
onClosed() onClosed()
} }
@ -22,11 +21,14 @@
<form on:submit|preventDefault class="uk-form-stacked"> <form on:submit|preventDefault class="uk-form-stacked">
<div> <div>
<label class="uk-form-label" for="form-stacked-text">Username</label> <div class="uk-margin">
<input class="uk-input" type="text" bind:value={username} /> <label class="uk-form-label" for="form-stacked-text">Username</label>
<label class="uk-form-label" for="form-stacked-text">Password</label> <input class="uk-input" type="text" bind:value={username} />
<input class="uk-input" type="password" bind:value={password} /> </div>
<label class="uk-form-label" for="form-stacked-text">Access Levels</label> <div class="uk-margin">
<label class="uk-form-label" for="form-stacked-text">Password</label>
<input class="uk-input" type="password" bind:value={password} />
</div>
</div> </div>
<footer> <footer>
<ActionButton alert on:click={onClosed}>Cancel</ActionButton> <ActionButton alert on:click={onClosed}>Cancel</ActionButton>

13
packages/server/config.js Normal file
View File

@ -0,0 +1,13 @@
module.exports = () => ({
datastore: "local",
datastoreConfig: {
rootPath: "./myapps/.data",
},
keys: ["secret1", "secret2"],
port: 4001,
latestPackagesFolder: "./myapps",
extraMasterPlugins: {},
dev: true,
customizeMaster: appDefinition => appDefinition,
useAppRootPath: true,
})

View File

@ -10,7 +10,7 @@ exports.create = async function(ctx) {
by_type: { by_type: {
map: function(doc) { map: function(doc) {
emit([doc.type], doc._id); emit([doc.type], doc._id);
} }.toString()
} }
} }
}); });

View File

@ -5,7 +5,7 @@ exports.create = async function(ctx) {
// await couchdb.db.create(instanceName); // await couchdb.db.create(instanceName);
const { clientId, applicationId } = ctx.params; const { clientId, applicationId } = ctx.params;
const db = new CouchDB(ctx.config)(instanceName); const db = new CouchDB(instanceName);
await db.put({ await db.put({
_id: "_design/database", _id: "_design/database",
metadata: { metadata: {
@ -46,7 +46,7 @@ exports.destroy = async function(ctx) {
const clientDb = new CouchDB(metadata.clientId); const clientDb = new CouchDB(metadata.clientId);
const budibaseApp = await clientDb.get(metadata.applicationId); const budibaseApp = await clientDb.get(metadata.applicationId);
budibaseApp.instances = budibaseApp.instances.filter(instance => instance !== ctx.params.instanceId); budibaseApp.instances = budibaseApp.instances.filter(instance => instance !== ctx.params.instanceId);
await clientDb.put(budibaseApp); const updatedApp = await clientDb.put(budibaseApp);
ctx.body = { ctx.body = {
message: `Instance Database ${ctx.params.instanceId} successfully destroyed.`, message: `Instance Database ${ctx.params.instanceId} successfully destroyed.`,

View File

@ -24,9 +24,12 @@ exports.save = async function(ctx) {
if (existingRecord) { if (existingRecord) {
const response = await db.put({ ...record, _id: existingRecord._id }); const response = await db.put({ ...record, _id: existingRecord._id });
ctx.body = { ctx.body = {
message: "Record updated successfully.", message: `${model.name} updated successfully.`,
status: 200, status: 200,
record: response record: {
...record,
...response
}
} }
return return
} }
@ -40,7 +43,7 @@ exports.save = async function(ctx) {
ctx.body = record ctx.body = record
ctx.status = 200 ctx.status = 200
ctx.message = `${model.name} ${record._rev ? "updated" : "created"} successfully` ctx.message = `${model.name} created successfully`
} }
exports.fetch = async function(ctx) { exports.fetch = async function(ctx) {
@ -62,5 +65,5 @@ exports.find = async function(ctx) {
exports.destroy = async function(ctx) { exports.destroy = async function(ctx) {
const databaseId = ctx.params.instanceId; const databaseId = ctx.params.instanceId;
const db = new CouchDB(databaseId) const db = new CouchDB(databaseId)
ctx.body = await db.destroy(ctx.params.recordId, ctx.params.revId); ctx.body = await db.remove(ctx.params.recordId, ctx.params.revId);
}; };

View File

@ -20,8 +20,8 @@ describe("/applications", () => {
}); });
afterAll(async () => { afterAll(async () => {
await destroyDatabase(CLIENT_DB_ID)
server.close(); server.close();
await destroyDatabase(CLIENT_DB_ID)
}) })
describe("create", () => { describe("create", () => {

View File

@ -1,6 +1,6 @@
const supertest = require("supertest"); const supertest = require("supertest");
const app = require("../../../../app"); const app = require("../../../../app");
const { createClientDatabase, destroyDatabase } = require("./couchTestUtils") const { createClientDatabase, destroyClientDatabase } = require("./couchTestUtils")
const CLIENT_DB_ID = "client-testing"; const CLIENT_DB_ID = "client-testing";
@ -8,6 +8,7 @@ const CLIENT_DB_ID = "client-testing";
describe("/clients", () => { describe("/clients", () => {
let request; let request;
let server; let server;
let db;
beforeAll(async () => { beforeAll(async () => {
server = await app({ server = await app({
@ -24,7 +25,7 @@ describe("/clients", () => {
describe("create", () => { describe("create", () => {
afterEach(async () => { afterEach(async () => {
await destroyDatabase(CLIENT_DB_ID); await destroyClientDatabase();
}); });
it("returns a success message when the client database is successfully created", done => { it("returns a success message when the client database is successfully created", done => {
@ -43,7 +44,7 @@ describe("/clients", () => {
describe("destroy", () => { describe("destroy", () => {
beforeEach(async () => { beforeEach(async () => {
await createClientDatabase(); db = await createClientDatabase();
}); });
it("returns a success message when the client database is successfully destroyed", async done => { it("returns a success message when the client database is successfully destroyed", async done => {

View File

@ -1,11 +1,14 @@
<<<<<<< HEAD
const couchdb = require("../../../../db") const couchdb = require("../../../../db")
const createClientDb = require("../../../../db/initialiseClientDb") const createClientDb = require("../../../../db/initialiseClientDb")
const CLIENT_DB_ID = "client-testing" const CLIENT_DB_ID = "client-testing"
=======
const CouchDB = require("../../../../db");
const CLIENT_DB_ID = "client-testing";
>>>>>>> fixing tests
const TEST_APP_ID = "test-app"; const TEST_APP_ID = "test-app";
exports.destroyDatabase = couchdb.db.destroy;
exports.createModel = async (instanceId, model) => { exports.createModel = async (instanceId, model) => {
model = model || { model = model || {
"name": "TestModel", "name": "TestModel",
@ -15,8 +18,8 @@ exports.createModel = async (instanceId, model) => {
"name": { "type": "string" } "name": { "type": "string" }
} }
} }
const db = couchdb.db.use(instanceId); const db = new CouchDB(instanceId);
const response = await db.insert(model); const response = await db.post(model);
const designDoc = await db.get("_design/database"); const designDoc = await db.get("_design/database");
designDoc.views = { designDoc.views = {
@ -29,7 +32,7 @@ exports.createModel = async (instanceId, model) => {
}` }`
} }
}; };
await db.insert(designDoc, designDoc._id); await db.put(designDoc);
return { return {
...response, ...response,
@ -37,20 +40,36 @@ exports.createModel = async (instanceId, model) => {
}; };
} }
exports.createClientDatabase = async () => exports.createClientDatabase = async () => {
await createClientDb({ const db = new CouchDB(CLIENT_DB_ID);
database: "couch",
clientId: CLIENT_DB_ID,
})
exports.destroyClientDatabase = async () => await couchdb.db.destroy(CLIENT_DB_ID); await db.put({
_id: "_design/client",
views: {
by_type: {
map: function(doc) {
emit([doc.type], doc._id);
}
}.toString()
}
});
await db.put({
_id: TEST_APP_ID,
type: "app",
instances: []
});
return db;
}
exports.destroyClientDatabase = async () => new CouchDB(CLIENT_DB_ID).destroy();
exports.createInstanceDatabase = async instanceId => { exports.createInstanceDatabase = async instanceId => {
await couchdb.db.create(instanceId); const db = new CouchDB(instanceId);
const db = couchdb.db.use(instanceId); await db.put({
_id: "_design/database",
await db.insert({
metadata: { metadata: {
clientId: CLIENT_DB_ID, clientId: CLIENT_DB_ID,
applicationId: TEST_APP_ID applicationId: TEST_APP_ID
@ -59,17 +78,17 @@ exports.createInstanceDatabase = async instanceId => {
by_type: { by_type: {
map: function(doc) { map: function(doc) {
emit([doc.type], doc._id); emit([doc.type], doc._id);
} }.toString()
} }
} }
}, '_design/database'); });
return instanceId; return db;
} }
exports.insertDocument = async (databaseId, document) => { exports.insertDocument = async (databaseId, document) => {
const { id, ...documentFields } = document; const { id, ...documentFields } = document;
await couchdb.db.use(databaseId).insert(documentFields, id); await new CouchDB(databaseId).put({ _id: id, ...documentFields });
} }
exports.createSchema = async (request, instanceId, schema) => { exports.createSchema = async (request, instanceId, schema) => {

View File

@ -2,7 +2,6 @@ const supertest = require("supertest");
const app = require("../../../../app"); const app = require("../../../../app");
const { const {
createInstanceDatabase, createInstanceDatabase,
destroyDatabase,
createClientDatabase, createClientDatabase,
destroyClientDatabase destroyClientDatabase
} = require("./couchTestUtils"); } = require("./couchTestUtils");
@ -29,13 +28,13 @@ describe("/instances", () => {
}) })
describe("create", () => { describe("create", () => {
beforeEach(async () => { beforeEach(async () => {
await createClientDatabase(); clientDb = await createClientDatabase();
}); });
afterEach(async () => { afterEach(async () => {
await destroyClientDatabase(); await destroyClientDatabase();
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 => {
@ -55,7 +54,7 @@ describe("/instances", () => {
describe("destroy", () => { describe("destroy", () => {
beforeEach(async () => { beforeEach(async () => {
await createClientDatabase(); await createClientDatabase();
await createInstanceDatabase(TEST_INSTANCE_ID); instanceDb = await createInstanceDatabase(TEST_INSTANCE_ID);
}); });
afterEach(async () => { afterEach(async () => {

View File

@ -1,6 +1,6 @@
const supertest = require("supertest"); const supertest = require("supertest");
const app = require("../../../../app"); const app = require("../../../../app");
const { createInstanceDatabase, createModel, destroyDatabase } = require("./couchTestUtils"); const { createInstanceDatabase, createModel } = require("./couchTestUtils");
const TEST_INSTANCE_ID = "testing-123"; const TEST_INSTANCE_ID = "testing-123";
@ -23,12 +23,14 @@ describe("/models", () => {
}) })
describe("create", () => { describe("create", () => {
let db;
beforeEach(async () => { beforeEach(async () => {
await createInstanceDatabase(TEST_INSTANCE_ID); db = await createInstanceDatabase(TEST_INSTANCE_ID);
}); });
afterEach(async () => { afterEach(async () => {
await destroyDatabase(TEST_INSTANCE_ID); await db.destroy();
}); });
it("returns a success message when the model is successfully created", done => { it("returns a success message when the model is successfully created", done => {
@ -54,14 +56,15 @@ describe("/models", () => {
describe("fetch", () => { describe("fetch", () => {
let testModel; let testModel;
let db;
beforeEach(async () => { beforeEach(async () => {
await createInstanceDatabase(TEST_INSTANCE_ID); db = await createInstanceDatabase(TEST_INSTANCE_ID);
testModel = await createModel(TEST_INSTANCE_ID); testModel = await createModel(TEST_INSTANCE_ID);
}); });
afterEach(async () => { afterEach(async () => {
await destroyDatabase(TEST_INSTANCE_ID); await db.destroy();
}); });
it("returns all the models for that instance in the response body", done => { it("returns all the models for that instance in the response body", done => {
@ -81,14 +84,15 @@ describe("/models", () => {
describe("destroy", () => { describe("destroy", () => {
let testModel; let testModel;
let db;
beforeEach(async () => { beforeEach(async () => {
await createInstanceDatabase(TEST_INSTANCE_ID); db = await createInstanceDatabase(TEST_INSTANCE_ID);
testModel = await createModel(TEST_INSTANCE_ID); testModel = await createModel(TEST_INSTANCE_ID);
}); });
afterEach(async () => { afterEach(async () => {
await destroyDatabase(TEST_INSTANCE_ID); await db.destroy();
}); });
it("returns a success response when a model is deleted.", done => { it("returns a success response when a model is deleted.", done => {

View File

@ -1,14 +1,23 @@
const supertest = require("supertest"); const supertest = require("supertest");
const app = require("../../../../app"); const app = require("../../../../app");
const { createInstanceDatabase, createModel, destroyDatabase } = require("./couchTestUtils"); const { createInstanceDatabase, createModel } = require("./couchTestUtils");
const { getNewRecord } = require("../../../../../common/lib/records/getNewRecord")
const { testSchema } = require("../../../../../common/lib/testUtils/testSchema")
const TEST_INSTANCE_ID = "testing-123"; const TEST_INSTANCE_ID = "testing-123";
const CONTACT_MODEL = {
"name": "Contact",
"type": "model",
"key": "name",
"schema": {
"name": { "type": "string" },
"age": { "type": "number" }
}
};
describe("/records", () => { describe("/records", () => {
let request; let request;
let server; let server;
let db;
beforeAll(async () => { beforeAll(async () => {
server = await app({ server = await app({
@ -24,21 +33,23 @@ describe("/records", () => {
}) })
describe("save, load, update, delete", () => { describe("save, load, update, delete", () => {
const schema = testSchema() let record;
let model;
beforeAll(async () => { beforeEach(async () => {
await createInstanceDatabase(TEST_INSTANCE_ID); db = await createInstanceDatabase(TEST_INSTANCE_ID);
await createModel(TEST_INSTANCE_ID, schema.findModel("Contact")) model = await createModel(TEST_INSTANCE_ID, CONTACT_MODEL)
record = {
name: "Test Contact",
status: "new",
modelId: model.id
}
}); });
afterAll(async () => { afterEach(async () => {
await destroyDatabase(TEST_INSTANCE_ID); await db.destroy();
}); });
let record = getNewRecord(schema, "Contact")
record.name = "Test Contact"
record.Status = "new"
it("returns a success message when the record is created", done => { it("returns a success message when the record is created", done => {
request request
.post(`/api/${TEST_INSTANCE_ID}/records`) .post(`/api/${TEST_INSTANCE_ID}/records`)
@ -47,69 +58,65 @@ describe("/records", () => {
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
.end(async (err, res) => { .end(async (err, res) => {
expect(res.res.statusMessage.toLowerCase()).toEqual("contact created successfully") expect(res.res.statusMessage).toEqual("Contact created successfully")
expect(res.body.name).toEqual("Test Contact") expect(res.body.name).toEqual("Test Contact")
expect(res.body._rev).toBeDefined() expect(res.body._rev).toBeDefined()
record = res.body
done(); done();
}); });
}) })
it("updates a record successfully", async () => { it("updates a record successfully", async () => {
record.name = "Updated Name" const existing = await db.post(record);
const res = await request const res = await request
.post(`/api/${TEST_INSTANCE_ID}/records`) .post(`/api/${TEST_INSTANCE_ID}/records`)
.send(record) .send({
_id: existing.id,
_rev: existing.rev,
modelId: model.id,
name: "Updated Name",
})
.set("Accept", "application/json") .set("Accept", "application/json")
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
expect(res.res.statusMessage.toLowerCase()).toEqual("contact updated successfully") expect(res.body.message).toEqual("Contact updated successfully.")
expect(res.body.name).toEqual("Updated Name") expect(res.body.record.name).toEqual("Updated Name")
record = res.body
}) })
it("should load a record", async () => { it("should load a record", async () => {
const existing = await db.post(record);
const res = await request const res = await request
.get(`/api/${TEST_INSTANCE_ID}/records/${record._id}`) .get(`/api/${TEST_INSTANCE_ID}/records/${existing.id}`)
.set("Accept", "application/json") .set("Accept", "application/json")
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
expect(res.body).toEqual(record)
expect(res.body).toEqual({
...record,
_id: existing.id,
_rev: existing.rev
})
}) })
it("should list all records for given modelId", async () => { it("should list all records for given modelId", async () => {
const secondRecord = getNewRecord(schema, "Contact") const newRecord = {
secondRecord.name = "Second Contact" modelId: model.id,
secondRecord.Status = "new" name: "Second Contact",
status: "new"
}
await request await db.post(newRecord);
.post(`/api/${TEST_INSTANCE_ID}/records`)
.send(secondRecord)
.set("Accept", "application/json")
const res = await request const res = await request
.get(`/api/${TEST_INSTANCE_ID}/${record.modelId}/records`) .get(`/api/${TEST_INSTANCE_ID}/all_${newRecord.modelId}/records`)
.set("Accept", "application/json") .set("Accept", "application/json")
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
expect(res.body.length).toBe(2) expect(res.body.length).toBe(1)
console.log(JSON.stringify(res.body, undefined, 2)) expect(res.body[0].name).toEqual(newRecord.name);
expect(res.body.find(c => c.name === record.name)).toBeDefined()
expect(res.body.find(c => c.name === secondRecord.name)).toBeDefined()
})
it("should return 404 when load, after a delete", async () => {
await request
.delete(`/api/${TEST_INSTANCE_ID}/records/${record._id}/${record._rev}`)
.expect(200)
await request
.get(`/api/${TEST_INSTANCE_ID}/records/${record._id}`)
.set("Accept", "application/json")
.expect('Content-Type', /json/)
.expect(404)
}) })
it("load should return 404 when record does not exist", async () => { it("load should return 404 when record does not exist", async () => {
@ -119,6 +126,5 @@ describe("/records", () => {
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(404) .expect(404)
}) })
}) })
}) })

View File

@ -1,8 +1,7 @@
const supertest = require("supertest"); const supertest = require("supertest");
const app = require("../../../../app"); const app = require("../../../../app");
const { const {
createInstanceDatabase, createInstanceDatabase
destroyDatabase
} = require("./couchTestUtils"); } = require("./couchTestUtils");
@ -29,12 +28,14 @@ describe("/users", () => {
}) })
describe("fetch", () => { describe("fetch", () => {
let db;
beforeEach(async () => { beforeEach(async () => {
await createInstanceDatabase(TEST_INSTANCE_ID); db = await createInstanceDatabase(TEST_INSTANCE_ID);
}); });
afterEach(async () => { afterEach(async () => {
await destroyDatabase(TEST_INSTANCE_ID); await db.destroy();
}); });
it("returns a list of users from an instance db", done => { it("returns a list of users from an instance db", done => {
@ -52,18 +53,20 @@ describe("/users", () => {
}); });
describe("create", () => { describe("create", () => {
let db;
beforeEach(async () => { beforeEach(async () => {
await createInstanceDatabase(TEST_INSTANCE_ID); db = await createInstanceDatabase(TEST_INSTANCE_ID);
}); });
afterEach(async () => { afterEach(async () => {
await destroyDatabase(TEST_INSTANCE_ID); await db.destroy();
}); });
it("returns a success message when a user is successfully created", done => { it("returns a success message when a user is successfully created", done => {
request request
.post(`/api/${TEST_INSTANCE_ID}/users`) .post(`/api/${TEST_INSTANCE_ID}/users`)
.send({ name: "John" }) .send({ name: "Bill", username: "bill1", password: "password" })
.set("Accept", "application/json") .set("Accept", "application/json")
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)

View File

@ -8,6 +8,7 @@ const TEST_INSTANCE_ID = "testing-123";
describe("/views", () => { describe("/views", () => {
let request; let request;
let server; let server;
let db;
beforeAll(async () => { beforeAll(async () => {
server = await app({ server = await app({
@ -24,11 +25,11 @@ describe("/views", () => {
describe("create", () => { describe("create", () => {
beforeEach(async () => { beforeEach(async () => {
await createInstanceDatabase(TEST_INSTANCE_ID); db = await createInstanceDatabase(TEST_INSTANCE_ID);
}); });
afterEach(async () => { afterEach(async () => {
await destroyDatabase(TEST_INSTANCE_ID); await db.destroy();
}); });
it("returns a success message when the view is successfully created", done => { it("returns a success message when the view is successfully created", done => {
@ -55,13 +56,15 @@ describe("/views", () => {
}); });
describe("fetch", () => { describe("fetch", () => {
let db;
beforeEach(async () => { beforeEach(async () => {
await createInstanceDatabase(TEST_INSTANCE_ID); db = await createInstanceDatabase(TEST_INSTANCE_ID);
await createModel(TEST_INSTANCE_ID); await createModel(TEST_INSTANCE_ID);
}); });
afterEach(async () => { afterEach(async () => {
await destroyDatabase(TEST_INSTANCE_ID); await db.destroy();
}); });
it("returns a list of all the views that exist in the instance database", done => { it("returns a list of all the views that exist in the instance database", done => {

Binary file not shown.

View File

@ -0,0 +1 @@
MANIFEST-000004

View File

View File

@ -0,0 +1,5 @@
2020/04/24-17:59:26.471929 70000b6a2000 Recovering log #3
2020/04/24-17:59:26.472213 70000b6a2000 Level-0 table #5: started
2020/04/24-17:59:26.472546 70000b6a2000 Level-0 table #5: 237 bytes OK
2020/04/24-17:59:26.473181 70000b6a2000 Delete type=0 #3
2020/04/24-17:59:26.473287 70000b6a2000 Delete type=3 #2

View File

@ -0,0 +1 @@
2020/04/24-17:59:03.648971 700012783000 Delete type=3 #1

Binary file not shown.