2
0
mirror of https://github.com/frappe/books.git synced 2025-02-08 23:18:31 +00:00

refactor: database tests

This commit is contained in:
18alantom 2022-10-31 13:39:02 +05:30
parent 9c8077f8cd
commit c3898e0f1c

View File

@ -1,6 +1,5 @@
import * as assert from 'assert';
import 'mocha';
import { FieldTypeEnum, RawValue, Schema } from 'schemas/types';
import test from 'tape';
import { getMapFromList, getValueMapFromList, sleep } from 'utils';
import { getDefaultMetaFieldValueMap, sqliteTypeMap } from '../../helpers';
import DatabaseCore from '../core';
@ -9,7 +8,7 @@ import {
assertDoesNotThrow,
assertThrows,
BaseMetaKey,
getBuiltTestSchemaMap,
getBuiltTestSchemaMap
} from './helpers';
/**
@ -23,54 +22,50 @@ import {
* This also implies that assert calls should have discriptive
*/
describe('DatabaseCore: Connect Migrate Close', function () {
const schemaMap = getBuiltTestSchemaMap();
async function getDb(shouldMigrate: boolean = true): Promise<DatabaseCore> {
const db = new DatabaseCore();
specify('dbPath', function () {
assert.strictEqual(db.dbPath, ':memory:');
});
const schemaMap = getBuiltTestSchemaMap();
db.setSchemaMap(schemaMap);
specify('schemaMap', function () {
assert.strictEqual(schemaMap, db.schemaMap);
});
specify('connect', async function () {
await assertDoesNotThrow(async () => await db.connect());
assert.notStrictEqual(db.knex, undefined);
});
specify('migrate and close', async function () {
// Does not throw
await db.migrate();
// Does not throw
await db.close();
});
});
describe('DatabaseCore: Migrate and Check Db', function () {
let db: DatabaseCore;
const schemaMap = getBuiltTestSchemaMap();
this.beforeEach(async function () {
db = new DatabaseCore();
await db.connect();
db.setSchemaMap(schemaMap);
if (shouldMigrate) {
await db.migrate();
}
return db;
}
test('db init, migrate, close', async (t) => {
const db = new DatabaseCore();
t.equal(db.dbPath, ':memory:');
const schemaMap = getBuiltTestSchemaMap();
db.setSchemaMap(schemaMap);
// Same Object
t.equal(schemaMap, db.schemaMap);
await assertDoesNotThrow(async () => await db.connect());
t.notEqual(db.knex, undefined);
await assertDoesNotThrow(async () => await db.migrate());
await assertDoesNotThrow(async () => await db.close());
});
this.afterEach(async function () {
/**
* DatabaseCore: Migrate and Check Db
*/
test(`Pre Migrate TableInfo`, async function (t) {
const db = await getDb(false);
for (const schemaName in schemaMap) {
const columns = await db.knex?.raw('pragma table_info(??)', schemaName);
t.equal(columns.length, 0, `column count ${schemaName}`);
}
await db.close();
});
specify(`Pre Migrate TableInfo`, async function () {
for (const schemaName in schemaMap) {
const columns = await db.knex?.raw('pragma table_info(??)', schemaName);
assert.strictEqual(columns.length, 0, `column count ${schemaName}`);
}
});
specify('Post Migrate TableInfo', async function () {
await db.migrate();
test('Post Migrate TableInfo', async function (t) {
const db = await getDb();
for (const schemaName in schemaMap) {
const schema = schemaMap[schemaName] as Schema;
const fieldMap = getMapFromList(schema.fields, 'fieldname');
@ -87,7 +82,7 @@ describe('DatabaseCore: Migrate and Check Db', function () {
columnCount = 0;
}
assert.strictEqual(
t.equal(
columns.length,
columnCount,
`${schemaName}:: column count: ${columns.length}, ${columnCount}`
@ -97,26 +92,26 @@ describe('DatabaseCore: Migrate and Check Db', function () {
const field = fieldMap[column.name];
const dbColType = sqliteTypeMap[field.fieldtype];
assert.strictEqual(
t.equal(
column.name,
field.fieldname,
`${schemaName}.${column.name}:: name check: ${column.name}, ${field.fieldname}`
);
assert.strictEqual(
t.equal(
column.type.toLowerCase(),
dbColType,
`${schemaName}.${column.name}:: type check: ${column.type}, ${dbColType}`
);
if (field.required !== undefined) {
assert.strictEqual(
t.equal(
!!column.notnull,
field.required,
`${schemaName}.${column.name}:: iotnull iheck: ${column.notnull}, ${field.required}`
);
} else {
assert.strictEqual(
t.equal(
column.notnull,
0,
`${schemaName}.${column.name}:: notnull check: ${column.notnull}, ${field.required}`
@ -124,13 +119,13 @@ describe('DatabaseCore: Migrate and Check Db', function () {
}
if (column.dflt_value === null) {
assert.strictEqual(
t.equal(
field.default,
undefined,
`${schemaName}.${column.name}:: dflt_value check: ${column.dflt_value}, ${field.default}`
);
} else {
assert.strictEqual(
t.equal(
column.dflt_value.slice(1, -1),
String(field.default),
`${schemaName}.${column.name}:: dflt_value check: ${column.type}, ${dbColType}`
@ -138,36 +133,25 @@ describe('DatabaseCore: Migrate and Check Db', function () {
}
}
}
});
});
describe('DatabaseCore: CRUD', function () {
let db: DatabaseCore;
const schemaMap = getBuiltTestSchemaMap();
this.beforeEach(async function () {
db = new DatabaseCore();
await db.connect();
db.setSchemaMap(schemaMap);
await db.migrate();
});
this.afterEach(async function () {
await db.close();
});
specify('exists() before insertion', async function () {
test('exists() before insertion', async function (t) {
const db = await getDb();
for (const schemaName in schemaMap) {
const doesExist = await db.exists(schemaName);
if (['SingleValue', 'SystemSettings'].includes(schemaName)) {
assert.strictEqual(doesExist, true, `${schemaName} exists`);
t.equal(doesExist, true, `${schemaName} exists`);
} else {
assert.strictEqual(doesExist, false, `${schemaName} exists`);
t.equal(doesExist, false, `${schemaName} exists`);
}
}
await db.close();
});
specify('CRUD single values', async function () {
test('CRUD single values', async function (t) {
const db = await getDb();
/**
* Checking default values which are created when db.migrate
* takes place.
@ -181,7 +165,7 @@ describe('DatabaseCore: CRUD', function () {
'default'
);
for (const row of rows) {
assert.strictEqual(
t.equal(
row.value,
defaultMap[row.fieldname as string],
`${row.fieldname} default values equality`
@ -203,19 +187,15 @@ describe('DatabaseCore: CRUD', function () {
rows = await db.knex!.raw('select * from SingleValue');
localeRow = rows.find((r) => r.fieldname === 'locale');
assert.notStrictEqual(localeEntryName, undefined, 'localeEntryName');
assert.strictEqual(rows.length, 2, 'rows length insert');
assert.strictEqual(
t.notEqual(localeEntryName, undefined, 'localeEntryName');
t.equal(rows.length, 2, 'rows length insert');
t.equal(
localeRow?.name as string,
localeEntryName,
`localeEntryName ${localeRow?.name}, ${localeEntryName}`
);
assert.strictEqual(
localeRow?.value,
locale,
`locale ${localeRow?.value}, ${locale}`
);
assert.strictEqual(
t.equal(localeRow?.value, locale, `locale ${localeRow?.value}, ${locale}`);
t.equal(
localeRow?.created,
localeEntryCreated,
`locale ${localeRow?.value}, ${locale}`
@ -229,19 +209,15 @@ describe('DatabaseCore: CRUD', function () {
rows = await db.knex!.raw('select * from SingleValue');
localeRow = rows.find((r) => r.fieldname === 'locale');
assert.notStrictEqual(localeEntryName, undefined, 'localeEntryName');
assert.strictEqual(rows.length, 2, 'rows length update');
assert.strictEqual(
t.notEqual(localeEntryName, undefined, 'localeEntryName');
t.equal(rows.length, 2, 'rows length update');
t.equal(
localeRow?.name as string,
localeEntryName,
`localeEntryName ${localeRow?.name}, ${localeEntryName}`
);
assert.strictEqual(
localeRow?.value,
locale,
`locale ${localeRow?.value}, ${locale}`
);
assert.strictEqual(
t.equal(localeRow?.value, locale, `locale ${localeRow?.value}, ${locale}`);
t.equal(
localeRow?.created,
localeEntryCreated,
`locale ${localeRow?.value}, ${locale}`
@ -252,15 +228,15 @@ describe('DatabaseCore: CRUD', function () {
*/
await db.delete('SystemSettings', 'locale');
rows = await db.knex!.raw('select * from SingleValue');
assert.strictEqual(rows.length, 1, 'delete one');
t.equal(rows.length, 1, 'delete one');
await db.delete('SystemSettings', 'dateFormat');
rows = await db.knex!.raw('select * from SingleValue');
assert.strictEqual(rows.length, 0, 'delete two');
t.equal(rows.length, 0, 'delete two');
const dateFormat = 'dd/mm/yy';
await db.insert('SystemSettings', { locale, dateFormat });
rows = await db.knex!.raw('select * from SingleValue');
assert.strictEqual(rows.length, 2, 'delete two');
t.equal(rows.length, 2, 'delete two');
/**
* Read
@ -268,14 +244,10 @@ describe('DatabaseCore: CRUD', function () {
* getSingleValues
*/
const svl = await db.getSingleValues('locale', 'dateFormat');
assert.strictEqual(svl.length, 2, 'getSingleValues length');
t.equal(svl.length, 2, 'getSingleValues length');
for (const sv of svl) {
assert.strictEqual(
sv.parent,
'SystemSettings',
`singleValue parent ${sv.parent}`
);
assert.strictEqual(
t.equal(sv.parent, 'SystemSettings', `singleValue parent ${sv.parent}`);
t.equal(
sv.value,
{ locale, dateFormat }[sv.fieldname],
`singleValue value ${sv.value}`
@ -285,16 +257,19 @@ describe('DatabaseCore: CRUD', function () {
* get
*/
const svlMap = await db.get('SystemSettings');
assert.strictEqual(Object.keys(svlMap).length, 2, 'get key length');
assert.strictEqual(svlMap.locale, locale, 'get locale');
assert.strictEqual(svlMap.dateFormat, dateFormat, 'get locale');
t.equal(Object.keys(svlMap).length, 2, 'get key length');
t.equal(svlMap.locale, locale, 'get locale');
t.equal(svlMap.dateFormat, dateFormat, 'get locale');
}
await db.close();
});
specify('CRUD nondependent schema', async function () {
test('CRUD nondependent schema', async function (t) {
const db = await getDb();
const schemaName = 'Customer';
let rows = await db.knex!(schemaName);
assert.strictEqual(rows.length, 0, 'rows length before insertion');
t.equal(rows.length, 0, 'rows length before insertion');
/**
* Insert
@ -311,20 +286,12 @@ describe('DatabaseCore: CRUD', function () {
await db.insert(schemaName, updateMap);
rows = await db.knex!(schemaName);
let firstRow = rows?.[0];
assert.strictEqual(rows.length, 1, `rows length insert ${rows.length}`);
assert.strictEqual(
firstRow.name,
name,
`name check ${firstRow.name}, ${name}`
);
assert.strictEqual(firstRow.email, null, `email check ${firstRow.email}`);
t.equal(rows.length, 1, `rows length insert ${rows.length}`);
t.equal(firstRow.name, name, `name check ${firstRow.name}, ${name}`);
t.equal(firstRow.email, null, `email check ${firstRow.email}`);
for (const key in metaValues) {
assert.strictEqual(
firstRow[key],
metaValues[key as BaseMetaKey],
`${key} check`
);
t.equal(firstRow[key], metaValues[key as BaseMetaKey], `${key} check`);
}
/**
@ -339,17 +306,9 @@ describe('DatabaseCore: CRUD', function () {
});
rows = await db.knex!(schemaName);
firstRow = rows?.[0];
assert.strictEqual(rows.length, 1, `rows length update ${rows.length}`);
assert.strictEqual(
firstRow.name,
name,
`name check update ${firstRow.name}, ${name}`
);
assert.strictEqual(
firstRow.email,
email,
`email check update ${firstRow.email}`
);
t.equal(rows.length, 1, `rows length update ${rows.length}`);
t.equal(firstRow.name, name, `name check update ${firstRow.name}, ${name}`);
t.equal(firstRow.email, email, `email check update ${firstRow.email}`);
const phone = '8149133530';
await sleep(1);
@ -360,28 +319,16 @@ describe('DatabaseCore: CRUD', function () {
});
rows = await db.knex!(schemaName);
firstRow = rows?.[0];
assert.strictEqual(
firstRow.email,
email,
`email check update ${firstRow.email}`
);
assert.strictEqual(
firstRow.phone,
phone,
`email check update ${firstRow.phone}`
);
t.equal(firstRow.email, email, `email check update ${firstRow.email}`);
t.equal(firstRow.phone, phone, `email check update ${firstRow.phone}`);
for (const key in metaValues) {
const val = firstRow[key];
const expected = metaValues[key as BaseMetaKey];
if (key !== 'modified') {
assert.strictEqual(val, expected, `${key} check ${val}, ${expected}`);
t.equal(val, expected, `${key} check ${val}, ${expected}`);
} else {
assert.notStrictEqual(
val,
expected,
`${key} check ${val}, ${expected}`
);
t.notEqual(val, expected, `${key} check ${val}, ${expected}`);
}
}
@ -390,13 +337,13 @@ describe('DatabaseCore: CRUD', function () {
*/
await db.delete(schemaName, name);
rows = await db.knex!(schemaName);
assert.strictEqual(rows.length, 0, `rows length delete ${rows.length}`);
t.equal(rows.length, 0, `rows length delete ${rows.length}`);
/**
* Get
*/
let fvMap = await db.get(schemaName, name);
assert.strictEqual(
t.equal(
Object.keys(fvMap).length,
0,
`key count get ${JSON.stringify(fvMap)}`
@ -411,20 +358,16 @@ describe('DatabaseCore: CRUD', function () {
// Insert
await db.insert(schemaName, cOne);
assert.strictEqual(
(await db.knex!(schemaName)).length,
1,
`rows length minsert`
);
t.equal((await db.knex!(schemaName)).length, 1, `rows length minsert`);
await db.insert(schemaName, cTwo);
rows = await db.knex!(schemaName);
assert.strictEqual(rows.length, 2, `rows length minsert`);
t.equal(rows.length, 2, `rows length minsert`);
const cs = [cOne, cTwo];
for (const i in cs) {
for (const k in cs[i]) {
const val = cs[i][k as BaseMetaKey];
assert.strictEqual(
t.equal(
rows?.[i]?.[k],
val,
`equality check ${i} ${k} ${val} ${rows?.[i]?.[k]}`
@ -435,48 +378,35 @@ describe('DatabaseCore: CRUD', function () {
// Update
await db.update(schemaName, { name: cOne.name, email });
const cOneEmail = await db.get(schemaName, cOne.name, 'email');
assert.strictEqual(
cOneEmail.email,
email,
`mi update check one ${cOneEmail}`
);
t.equal(cOneEmail.email, email, `mi update check one ${cOneEmail}`);
const cTwoEmail = await db.get(schemaName, cTwo.name, 'email');
assert.strictEqual(
cOneEmail.email,
email,
`mi update check two ${cTwoEmail}`
);
t.equal(cOneEmail.email, email, `mi update check two ${cTwoEmail}`);
// Rename
const newName = 'Johnny Whoe';
await db.rename(schemaName, cOne.name, newName);
fvMap = await db.get(schemaName, cOne.name);
assert.strictEqual(
t.equal(
Object.keys(fvMap).length,
0,
`mi rename check old ${JSON.stringify(fvMap)}`
);
fvMap = await db.get(schemaName, newName);
assert.strictEqual(
fvMap.email,
email,
`mi rename check new ${JSON.stringify(fvMap)}`
);
t.equal(fvMap.email, email, `mi rename check new ${JSON.stringify(fvMap)}`);
// Delete
await db.delete(schemaName, newName);
rows = await db.knex!(schemaName);
assert.strictEqual(rows.length, 1, `mi delete length ${rows.length}`);
assert.strictEqual(
rows[0].name,
cTwo.name,
`mi delete name ${rows[0].name}`
);
t.equal(rows.length, 1, `mi delete length ${rows.length}`);
t.equal(rows[0].name, cTwo.name, `mi delete name ${rows[0].name}`);
await db.close();
});
specify('CRUD dependent schema', async function () {
test('CRUD dependent schema', async function (t) {
const db = await getDb();
const Customer = 'Customer';
const SalesInvoice = 'SalesInvoice';
const SalesInvoiceItem = 'SalesInvoiceItem';
@ -527,18 +457,14 @@ describe('DatabaseCore: CRUD', function () {
expected = +expected;
}
assert.strictEqual(
t.equal(
fvMap[key],
expected,
`equality check ${key}: ${fvMap[key]}, ${invoice[key]}`
);
}
assert.strictEqual(
(fvMap.items as unknown[])?.length,
0,
'empty items check'
);
t.equal((fvMap.items as unknown[])?.length, 0, 'empty items check');
const items: FieldValueMap[] = [
{
@ -560,20 +486,20 @@ describe('DatabaseCore: CRUD', function () {
fvMap = await db.get(SalesInvoice, invoice.name as string);
const ct = fvMap.items as FieldValueMap[];
assert.strictEqual(ct.length, 1, `ct length ${ct.length}`);
assert.strictEqual(ct[0].parent, invoice.name, `ct parent ${ct[0].parent}`);
assert.strictEqual(
t.equal(ct.length, 1, `ct length ${ct.length}`);
t.equal(ct[0].parent, invoice.name, `ct parent ${ct[0].parent}`);
t.equal(
ct[0].parentFieldname,
'items',
`ct parentFieldname ${ct[0].parentFieldname}`
);
assert.strictEqual(
t.equal(
ct[0].parentSchemaName,
SalesInvoice,
`ct parentSchemaName ${ct[0].parentSchemaName}`
);
for (const key in items[0]) {
assert.strictEqual(
t.equal(
ct[0][key],
items[0][key],
`ct values ${key}: ${ct[0][key]}, ${items[0][key]}`
@ -594,11 +520,11 @@ describe('DatabaseCore: CRUD', function () {
let rows = await db.getAll(SalesInvoiceItem, {
fields: ['item', 'quantity', 'rate', 'amount'],
});
assert.strictEqual(rows.length, 2, `ct length update ${rows.length}`);
t.equal(rows.length, 2, `ct length update ${rows.length}`);
for (const i in rows) {
for (const key in rows[i]) {
assert.strictEqual(
t.equal(
rows[i][key],
items[i][key],
`ct values ${i},${key}: ${rows[i][key]}`
@ -615,10 +541,11 @@ describe('DatabaseCore: CRUD', function () {
});
rows = await db.knex!(SalesInvoiceItem);
assert.strictEqual(rows.length, 2, `postupdate ct empty ${rows.length}`);
t.equal(rows.length, 2, `postupdate ct empty ${rows.length}`);
await db.delete(SalesInvoice, invoice.name as string);
rows = await db.getAll(SalesInvoiceItem);
assert.strictEqual(rows.length, 0, `ct length delete ${rows.length}`);
});
t.equal(rows.length, 0, `ct length delete ${rows.length}`);
await db.close();
});