2
0
mirror of https://github.com/frappe/books.git synced 2024-11-14 01:14:03 +00:00
books/tests/test_document.js

129 lines
3.9 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 helpers = require('./helpers');
describe('Document', () => {
before(async function() {
await helpers.initSqlite();
2018-01-12 12:25:07 +00:00
});
it('should insert a doc', async () => {
2018-02-01 11:07:36 +00:00
let doc1 = test_doc();
2018-01-12 12:25:07 +00:00
doc1.subject = 'insert subject 1';
doc1.description = 'insert description 1';
await doc1.insert();
// get it back from the db
2018-02-08 12:28:51 +00:00
let doc2 = await frappe.getDoc(doc1.doctype, doc1.name);
2018-01-12 12:25:07 +00:00
assert.equal(doc1.subject, doc2.subject);
assert.equal(doc1.description, doc2.description);
2018-02-07 13:23:52 +00:00
// test frappe.db.exists
assert.ok(await frappe.db.exists(doc1.doctype, doc1.name));
assert.equal(await frappe.db.exists(doc1.doctype, doc1.name + '---'), false);
2018-01-12 12:25:07 +00:00
});
it('should update a doc', async () => {
2018-02-01 11:07:36 +00:00
let doc = test_doc();
2018-01-12 12:25:07 +00:00
await doc.insert();
2018-02-08 09:38:47 +00:00
assert.notEqual(await frappe.db.getValue(doc.doctype, doc.name, 'subject'), 'subject 2');
2018-01-12 12:25:07 +00:00
doc.subject = 'subject 2'
await doc.update();
2018-02-08 09:38:47 +00:00
assert.equal(await frappe.db.getValue(doc.doctype, doc.name, 'subject'), 'subject 2');
2018-01-12 12:25:07 +00:00
})
it('should get a value', async () => {
2018-02-01 11:07:36 +00:00
let doc = test_doc();
2018-01-12 12:25:07 +00:00
assert.equal(doc.get('subject'), 'testing 1');
});
it('should set a value', async () => {
2018-02-01 11:07:36 +00:00
let doc = test_doc();
2018-01-12 12:25:07 +00:00
doc.set('subject', 'testing 1')
assert.equal(doc.get('subject'), 'testing 1');
});
it('should not allow incorrect Select option', async () => {
2018-02-21 09:43:21 +00:00
let doc = test_doc();
try {
await doc.set('status', 'Illegal');
assert.fail();
} catch (e) {
assert.ok(e instanceof frappe.errors.ValueError);
}
2018-01-12 12:25:07 +00:00
});
it('should delete a document', async () => {
2018-02-01 11:07:36 +00:00
let doc = test_doc();
2018-01-12 12:25:07 +00:00
await doc.insert();
2018-02-08 09:38:47 +00:00
assert.equal(await frappe.db.getValue(doc.doctype, doc.name), doc.name);
2018-01-12 12:25:07 +00:00
await doc.delete();
2018-02-08 09:38:47 +00:00
assert.equal(await frappe.db.getValue(doc.doctype, doc.name), null);
2018-01-12 12:25:07 +00:00
});
2018-02-01 11:07:36 +00:00
it('should add, fetch and delete documents with children', async() => {
2018-03-05 16:45:21 +00:00
if (!await frappe.db.exists('Role', 'Test Role 1')) {
await frappe.insert({doctype: 'Role', name: 'Test Role'});
await frappe.insert({doctype: 'Role', name: 'Test Role 1'});
}
2018-02-01 11:07:36 +00:00
2018-02-08 12:28:51 +00:00
let user = frappe.newDoc({
2018-02-01 11:07:36 +00:00
doctype: 'User',
2018-03-05 16:45:21 +00:00
name: frappe.getRandomString(),
2018-02-01 11:07:36 +00:00
full_name: 'Test User',
roles: [
{
role: 'Test Role'
}
]
});
await user.insert();
assert.equal(user.roles.length, 1);
2018-02-07 13:23:52 +00:00
assert.equal(user.roles[0].role, 'Test Role');
2018-02-01 11:07:36 +00:00
assert.equal(user.roles[0].parent, user.name);
assert.equal(user.roles[0].parenttype, user.doctype);
assert.equal(user.roles[0].parentfield, 'roles');
// add another role
user.roles.push({role: 'Test Role 1'});
2018-02-07 13:23:52 +00:00
await user.update();
assert.equal(user.roles.length, 2);
assert.equal(user.roles[1].role, 'Test Role 1');
assert.equal(user.roles[1].parent, user.name);
assert.equal(user.roles[1].parenttype, user.doctype);
assert.equal(user.roles[1].parentfield, 'roles');
// remove the first row
user.roles = user.roles.filter((d, i) => i === 1);
await user.update();
2018-02-08 12:28:51 +00:00
user = await frappe.getDoc('User', user.name);
2018-02-07 13:23:52 +00:00
assert.equal(user.roles.length, 1);
assert.equal(user.roles[0].role, 'Test Role 1');
assert.equal(user.roles[0].idx, 0);
assert.equal(user.roles[0].parent, user.name);
assert.equal(user.roles[0].parenttype, user.doctype);
assert.equal(user.roles[0].parentfield, 'roles');
2018-02-01 11:07:36 +00:00
});
2018-01-12 12:25:07 +00:00
});
2018-02-01 11:07:36 +00:00
function test_doc() {
2018-02-08 12:28:51 +00:00
return frappe.newDoc({
2018-01-12 12:25:07 +00:00
doctype: 'ToDo',
status: 'Open',
subject: 'testing 1',
description: 'test description 1'
});
}