2
0
mirror of https://github.com/frappe/books.git synced 2024-12-26 20:30:25 +00:00
books/tests/test_restAPI.js

92 lines
3.0 KiB
JavaScript
Raw Normal View History

2018-01-12 12:25:07 +00:00
const assert = require('assert');
2018-01-16 06:09:17 +00:00
const frappe = require('frappejs');
2018-01-12 12:25:07 +00:00
const fetch = require('node-fetch');
const { spawn } = require('child_process');
const process = require('process');
2018-02-19 16:41:10 +00:00
const HTTPClient = require('frappejs/backends/http')
const utils = require('frappejs/utils');
const models = require('frappejs/models');
const common = require('frappejs/common');
2018-01-12 12:25:07 +00:00
// create a copy of frappe
var test_server;
describe('REST', () => {
2018-01-12 12:25:07 +00:00
before(async function() {
test_server = spawn('node', ['tests/test_server.js'], { stdio: 'inherit' });
2018-01-12 12:25:07 +00:00
await frappe.init();
await frappe.registerLibs(common);
await frappe.registerModels(models);
2018-07-17 19:29:18 +00:00
await frappe.login('Administrator');
2018-01-12 12:25:07 +00:00
2018-02-19 16:41:10 +00:00
frappe.db = await new HTTPClient({server: 'localhost:8000'});
2018-01-12 12:25:07 +00:00
frappe.fetch = fetch;
// wait for server to start
return await utils.sleep(2);
2018-01-12 12:25:07 +00:00
});
after(() => {
frappe.close();
test_server.kill();
});
2018-03-05 16:45:21 +00:00
it('REST: should create a document', async () => {
2018-02-08 12:28:51 +00:00
let doc = frappe.newDoc({doctype:'ToDo', subject:'test rest insert 1'});
2018-01-12 12:25:07 +00:00
await doc.insert();
2018-02-08 12:28:51 +00:00
let doc1 = await frappe.getDoc('ToDo', doc.name);
2018-01-12 12:25:07 +00:00
assert.equal(doc.subject, doc1.subject);
assert.equal(doc1.status, 'Open');
});
2018-03-05 16:45:21 +00:00
it('REST: should update a document', async () => {
2018-02-08 12:28:51 +00:00
let doc = frappe.newDoc({doctype:'ToDo', subject:'test rest insert 1'});
2018-01-12 12:25:07 +00:00
await doc.insert();
doc.subject = 'subject changed';
await doc.update();
2018-02-08 12:28:51 +00:00
let doc1 = await frappe.getDoc('ToDo', doc.name);
2018-01-12 12:25:07 +00:00
assert.equal(doc.subject, doc1.subject);
});
2018-03-05 16:45:21 +00:00
it('REST: should get multiple documents', async () => {
2018-01-12 12:25:07 +00:00
await frappe.insert({doctype:'ToDo', subject:'all test 1'});
await frappe.insert({doctype:'ToDo', subject:'all test 2'});
let data = await frappe.db.getAll({doctype:'ToDo'});
2018-01-12 12:25:07 +00:00
let subjects = data.map(d => d.subject);
assert.ok(subjects.includes('all test 1'));
assert.ok(subjects.includes('all test 2'));
});
2018-03-05 16:45:21 +00:00
it('REST: should delete a document', async () => {
2018-02-08 12:28:51 +00:00
let doc = frappe.newDoc({doctype:'ToDo', subject:'test rest insert 1'});
2018-02-08 09:38:47 +00:00
await doc.insert();
assert.equal(await frappe.db.exists(doc.doctype, doc.name), true);
await doc.delete();
assert.equal(await frappe.db.exists(doc.doctype, doc.name), false);
});
2018-03-05 16:45:21 +00:00
it('REST: should delete multiple documents', async () => {
2018-02-08 12:28:51 +00:00
let doc1 = frappe.newDoc({doctype:'ToDo', subject:'test rest insert 5'});
let doc2 = frappe.newDoc({doctype:'ToDo', subject:'test rest insert 6'});
2018-02-08 09:38:47 +00:00
await doc1.insert();
await doc2.insert();
assert.equal(await frappe.db.exists(doc1.doctype, doc1.name), true);
assert.equal(await frappe.db.exists(doc2.doctype, doc2.name), true);
await frappe.db.deleteMany(doc1.doctype, [doc1.name, doc2.name]);
assert.equal(await frappe.db.exists(doc1.doctype, doc1.name), false);
assert.equal(await frappe.db.exists(doc2.doctype, doc2.name), false);
});
2018-01-12 12:25:07 +00:00
});