2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00
books/tests/test_database.js

93 lines
2.3 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');
2019-12-13 07:13:45 +00:00
describe('Database CRUD', () => {
2019-12-09 19:57:26 +00:00
before(async function() {
await helpers.initSqlite();
});
beforeEach(async () => {
await frappe.db.sql('delete from todo');
await frappe.insert({
doctype: 'ToDo',
subject: 'testing 1',
status: 'Open'
});
await frappe.insert({
doctype: 'ToDo',
subject: 'testing 3',
status: 'Open'
});
await frappe.insert({
doctype: 'ToDo',
subject: 'testing 2',
status: 'Closed'
2018-01-12 12:25:07 +00:00
});
2019-12-09 19:57:26 +00:00
});
2018-01-12 12:25:07 +00:00
2019-12-09 19:57:26 +00:00
it('should insert and get values', async () => {
let subjects = await frappe.db.getAll({
doctype: 'ToDo',
fields: ['name', 'subject']
});
subjects = subjects.map(d => d.subject);
2018-01-12 12:25:07 +00:00
2019-12-09 19:57:26 +00:00
assert.ok(subjects.includes('testing 1'));
assert.ok(subjects.includes('testing 2'));
assert.ok(subjects.includes('testing 3'));
});
2018-01-12 12:25:07 +00:00
2019-12-09 19:57:26 +00:00
it('should filter correct values', async () => {
let todos = await frappe.db.getAll({
doctype: 'ToDo',
fields: ['name', 'subject'],
filters: { status: 'Open' }
2018-01-12 12:25:07 +00:00
});
2019-12-09 19:57:26 +00:00
let subjects = todos.map(d => d.subject);
2018-01-12 12:25:07 +00:00
2019-12-09 19:57:26 +00:00
assert.ok(subjects.includes('testing 1'));
assert.ok(subjects.includes('testing 3'));
assert.equal(subjects.includes('testing 2'), false);
2018-01-12 12:25:07 +00:00
2019-12-09 19:57:26 +00:00
todos = await frappe.db.getAll({
doctype: 'ToDo',
fields: ['name', 'subject'],
filters: { status: 'Closed' }
});
subjects = todos.map(d => d.subject);
2018-01-12 12:25:07 +00:00
2019-12-09 19:57:26 +00:00
assert.equal(subjects.includes('testing 1'), false);
assert.equal(subjects.includes('testing 3'), false);
assert.ok(subjects.includes('testing 2'));
});
2018-01-12 12:25:07 +00:00
2019-12-09 19:57:26 +00:00
it('should delete records', async () => {
let todos = await frappe.db.getAll({ doctype: 'ToDo' });
frappe.db.delete('ToDo', todos[0].name);
2018-01-12 12:25:07 +00:00
2019-12-09 19:57:26 +00:00
todos = await frappe.db.getAll({ doctype: 'ToDo' });
assert.equal(todos.length, 2);
});
2018-01-12 12:25:07 +00:00
2019-12-09 19:57:26 +00:00
it('should update records', async () => {
let todo = (await frappe.db.getAll({ doctype: 'ToDo', limit: 1 }))[0];
2018-01-12 12:25:07 +00:00
2019-12-09 19:57:26 +00:00
frappe.db.update('ToDo', {
name: todo.name,
subject: 'updated subject'
2018-01-12 12:25:07 +00:00
});
2019-12-09 19:57:26 +00:00
todo = (
await frappe.db.getAll({
doctype: 'ToDo',
fields: ['subject'],
filters: { name: todo.name }
})
)[0];
assert.equal(todo.subject, 'updated subject');
});
});