fixing tests

This commit is contained in:
Martin McKeaveney 2020-04-24 18:02:51 +01:00
parent 32dd1213aa
commit 120344ccae
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()
}
export async function saveRecord({ record, instanceId, modelId }) {
const SAVE_RECORDS_URL = `/api/${instanceId}/${modelId}/records`
export async function saveRecord(record, instanceId) {
const SAVE_RECORDS_URL = `/api/${instanceId}/records`
const response = await api.post(SAVE_RECORDS_URL, record)
return await response.json()
}

View File

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

View File

@ -14,7 +14,6 @@
async function createUser() {
const user = { name: username, username, password }
const response = await api.createUser(user, instanceId);
console.log(response);
backendUiStore.actions.users.create(response)
onClosed()
}
@ -22,11 +21,14 @@
<form on:submit|preventDefault class="uk-form-stacked">
<div>
<label class="uk-form-label" for="form-stacked-text">Username</label>
<input class="uk-input" type="text" bind:value={username} />
<label class="uk-form-label" for="form-stacked-text">Password</label>
<input class="uk-input" type="password" bind:value={password} />
<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">Username</label>
<input class="uk-input" type="text" bind:value={username} />
</div>
<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>
<footer>
<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: {
map: function(doc) {
emit([doc.type], doc._id);
}
}.toString()
}
}
});

View File

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

View File

@ -24,9 +24,12 @@ exports.save = async function(ctx) {
if (existingRecord) {
const response = await db.put({ ...record, _id: existingRecord._id });
ctx.body = {
message: "Record updated successfully.",
message: `${model.name} updated successfully.`,
status: 200,
record: response
record: {
...record,
...response
}
}
return
}
@ -40,7 +43,7 @@ exports.save = async function(ctx) {
ctx.body = record
ctx.status = 200
ctx.message = `${model.name} ${record._rev ? "updated" : "created"} successfully`
ctx.message = `${model.name} created successfully`
}
exports.fetch = async function(ctx) {
@ -62,5 +65,5 @@ exports.find = async function(ctx) {
exports.destroy = async function(ctx) {
const databaseId = ctx.params.instanceId;
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 () => {
await destroyDatabase(CLIENT_DB_ID)
server.close();
await destroyDatabase(CLIENT_DB_ID)
})
describe("create", () => {

View File

@ -1,6 +1,6 @@
const supertest = require("supertest");
const app = require("../../../../app");
const { createClientDatabase, destroyDatabase } = require("./couchTestUtils")
const { createClientDatabase, destroyClientDatabase } = require("./couchTestUtils")
const CLIENT_DB_ID = "client-testing";
@ -8,6 +8,7 @@ const CLIENT_DB_ID = "client-testing";
describe("/clients", () => {
let request;
let server;
let db;
beforeAll(async () => {
server = await app({
@ -24,7 +25,7 @@ describe("/clients", () => {
describe("create", () => {
afterEach(async () => {
await destroyDatabase(CLIENT_DB_ID);
await destroyClientDatabase();
});
it("returns a success message when the client database is successfully created", done => {
@ -43,7 +44,7 @@ describe("/clients", () => {
describe("destroy", () => {
beforeEach(async () => {
await createClientDatabase();
db = await createClientDatabase();
});
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 createClientDb = require("../../../../db/initialiseClientDb")
const CLIENT_DB_ID = "client-testing"
=======
const CouchDB = require("../../../../db");
const CLIENT_DB_ID = "client-testing";
>>>>>>> fixing tests
const TEST_APP_ID = "test-app";
exports.destroyDatabase = couchdb.db.destroy;
exports.createModel = async (instanceId, model) => {
model = model || {
"name": "TestModel",
@ -15,8 +18,8 @@ exports.createModel = async (instanceId, model) => {
"name": { "type": "string" }
}
}
const db = couchdb.db.use(instanceId);
const response = await db.insert(model);
const db = new CouchDB(instanceId);
const response = await db.post(model);
const designDoc = await db.get("_design/database");
designDoc.views = {
@ -29,7 +32,7 @@ exports.createModel = async (instanceId, model) => {
}`
}
};
await db.insert(designDoc, designDoc._id);
await db.put(designDoc);
return {
...response,
@ -37,20 +40,36 @@ exports.createModel = async (instanceId, model) => {
};
}
exports.createClientDatabase = async () =>
await createClientDb({
database: "couch",
clientId: CLIENT_DB_ID,
})
exports.createClientDatabase = async () => {
const db = new CouchDB(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 => {
await couchdb.db.create(instanceId);
const db = new CouchDB(instanceId);
const db = couchdb.db.use(instanceId);
await db.insert({
await db.put({
_id: "_design/database",
metadata: {
clientId: CLIENT_DB_ID,
applicationId: TEST_APP_ID
@ -59,17 +78,17 @@ exports.createInstanceDatabase = async instanceId => {
by_type: {
map: function(doc) {
emit([doc.type], doc._id);
}
}.toString()
}
}
}, '_design/database');
});
return instanceId;
return db;
}
exports.insertDocument = async (databaseId, 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) => {

View File

@ -2,7 +2,6 @@ const supertest = require("supertest");
const app = require("../../../../app");
const {
createInstanceDatabase,
destroyDatabase,
createClientDatabase,
destroyClientDatabase
} = require("./couchTestUtils");
@ -29,13 +28,13 @@ describe("/instances", () => {
})
describe("create", () => {
beforeEach(async () => {
await createClientDatabase();
clientDb = await createClientDatabase();
});
afterEach(async () => {
await destroyClientDatabase();
await destroyDatabase(TEST_INSTANCE_ID);
});
it("returns a success message when the instance database is successfully created", done => {
@ -55,7 +54,7 @@ describe("/instances", () => {
describe("destroy", () => {
beforeEach(async () => {
await createClientDatabase();
await createInstanceDatabase(TEST_INSTANCE_ID);
instanceDb = await createInstanceDatabase(TEST_INSTANCE_ID);
});
afterEach(async () => {

View File

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

View File

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

View File

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

View File

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

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.