2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 15:50:56 +00:00
books/tests/test_rest_api.js
2018-02-08 17:58:51 +05:30

89 lines
2.9 KiB
JavaScript

const assert = require('assert');
const frappe = require('frappejs');
const fetch = require('node-fetch');
const helpers = require('./helpers');
const { spawn } = require('child_process');
const process = require('process');
const RESTClient = require('frappejs/backends/rest_client')
// 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 RESTClient({server: 'localhost:8000'});
frappe.fetch = fetch;
// wait for server to start
await frappe.sleep(1);
});
after(() => {
frappe.close();
test_server.kill();
});
it('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('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('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('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('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);
});
});