2
0
mirror of https://github.com/frappe/books.git synced 2024-11-14 01:14:03 +00:00
books/tests/test_restAPI.js
2018-03-05 22:15:21 +05:30

88 lines
2.9 KiB
JavaScript

const assert = require('assert');
const frappe = require('frappejs');
const fetch = require('node-fetch');
const { spawn } = require('child_process');
const process = require('process');
const HTTPClient = require('frappejs/backends/http')
// create a copy of frappe
var test_server;
describe('REST', () => {
before(async function() {
test_server = spawn('node', ['tests/test_server.js'], {
stdio: [process.stdin, process.stdout, process.stderr, 'pipe', 'pipe']
});
await frappe.init();
await frappe.login();
frappe.db = await new HTTPClient({server: 'localhost:8000'});
frappe.fetch = fetch;
// wait for server to start
await frappe.sleep(1);
});
after(() => {
frappe.close();
test_server.kill();
});
it('REST: should create a document', async () => {
let doc = frappe.newDoc({doctype:'ToDo', subject:'test rest insert 1'});
await doc.insert();
let doc1 = await frappe.getDoc('ToDo', doc.name);
assert.equal(doc.subject, doc1.subject);
assert.equal(doc1.status, 'Open');
});
it('REST: should update a document', async () => {
let doc = frappe.newDoc({doctype:'ToDo', subject:'test rest insert 1'});
await doc.insert();
doc.subject = 'subject changed';
await doc.update();
let doc1 = await frappe.getDoc('ToDo', doc.name);
assert.equal(doc.subject, doc1.subject);
});
it('REST: should get multiple documents', async () => {
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'});
let subjects = data.map(d => d.subject);
assert.ok(subjects.includes('all test 1'));
assert.ok(subjects.includes('all test 2'));
});
it('REST: should delete a document', async () => {
let doc = frappe.newDoc({doctype:'ToDo', subject:'test rest insert 1'});
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);
});
it('REST: should delete multiple documents', async () => {
let doc1 = frappe.newDoc({doctype:'ToDo', subject:'test rest insert 5'});
let doc2 = frappe.newDoc({doctype:'ToDo', subject:'test rest insert 6'});
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);
});
});