2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 11:29:03 +00:00

Merge pull request #491 from 18alantom/update-tests

refactor: mocha → tape
This commit is contained in:
Alan 2022-10-31 01:38:26 -07:00 committed by GitHub
commit f281c891e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 1468 additions and 1268 deletions

View File

@ -1,10 +1,6 @@
name: Build
on:
push:
branches: [$default-branch]
paths-ignore:
- '**.md'
pull_request:
paths-ignore:
- '**.md'
@ -25,7 +21,7 @@ jobs:
- name: Checkout Books
uses: actions/checkout@v2
- name: Setup Books
- name: Install Dependencies
run: yarn
- name: Install RPM

View File

@ -14,7 +14,7 @@ jobs:
- name: Checkout Books
uses: actions/checkout@v2
- name: Setup Books
- name: Install Dependencies
run: |
yarn set version 1.22.18
yarn
@ -65,7 +65,7 @@ jobs:
- name: Checkout Books
uses: actions/checkout@v2
- name: Setup Books
- name: Install Dependencies
run: |
yarn set version 1.22.18
yarn
@ -113,7 +113,7 @@ jobs:
- name: Checkout Books
uses: actions/checkout@v2
- name: Setup Books
- name: Install Dependencies
run: yarn
- name: Set Error Log Creds

32
.github/workflows/test.yml vendored Normal file
View File

@ -0,0 +1,32 @@
name: Test
on:
push:
branches: [$default-branch]
paths-ignore:
- '**.md'
pull_request:
paths-ignore:
- '**.md'
workflow_dispatch:
jobs:
setup_and_test:
runs-on: macos-11
steps:
- name: Setup node
uses: actions/setup-node@v2
with:
node-version: '16.13.1'
- name: Set yarn version
run: yarn set version 1.22.18
- name: Checkout Books
uses: actions/checkout@v2
- name: Install Dependencies
run: yarn
- name: Yarn Tests
run: yarn test

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;
}
this.afterEach(async function () {
await db.close();
});
test('db init, migrate, close', async (t) => {
const db = new DatabaseCore();
t.equal(db.dbPath, ':memory:');
specify(`Pre Migrate TableInfo`, async function () {
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());
});
/**
* 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);
assert.strictEqual(columns.length, 0, `column count ${schemaName}`);
t.equal(columns.length, 0, `column count ${schemaName}`);
}
});
await db.close();
});
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 () {
}
}
}
});
await db.close();
});
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');
}
});
specify('CRUD nondependent schema', async function () {
await db.close();
});
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();
});
test('CRUD dependent schema', async function (t) {
const db = await getDb();
specify('CRUD dependent schema', async function () {
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();
});

View File

@ -1,49 +1,27 @@
import * as assert from 'assert';
import { DatabaseManager } from 'backend/database/manager';
import { assertDoesNotThrow } from 'backend/database/tests/helpers';
import { purchaseItemPartyMap } from 'dummy/helpers';
import { Fyo } from 'fyo';
import { DummyAuthDemux } from 'fyo/tests/helpers';
import 'mocha';
import { getTestDbPath } from 'tests/helpers';
import test from 'tape';
import { getTestDbPath, getTestFyo } from 'tests/helpers';
import { setupDummyInstance } from '..';
describe('dummy', function () {
const dbPath = getTestDbPath();
const dbPath = getTestDbPath();
const fyo = getTestFyo();
let fyo: Fyo;
this.beforeAll(function () {
fyo = new Fyo({
DatabaseDemux: DatabaseManager,
AuthDemux: DummyAuthDemux,
isTest: true,
isElectron: false,
});
});
this.afterAll(async function () {
await fyo.close();
});
specify('setupDummyInstance', async function () {
test('setupDummyInstance', async () => {
await assertDoesNotThrow(async () => {
await setupDummyInstance(dbPath, fyo, 1, 25);
}, 'setup instance failed');
});
test('purchaseItemParty Existance', async (t) => {
for (const item in purchaseItemPartyMap) {
assert.strictEqual(
await fyo.db.exists('Item', item),
true,
`not found ${item}`
);
t.ok(await fyo.db.exists('Item', item), `item exists: ${item}`);
const party = purchaseItemPartyMap[item];
assert.strictEqual(
await fyo.db.exists('Party', party),
true,
`not found ${party}`
);
t.ok(await fyo.db.exists('Party', party), `party exists: ${party}`);
}
}).timeout(120_000);
});
test.onFinish(async () => {
await fyo.close();
});

View File

@ -1,64 +1,27 @@
import * as assert from 'assert';
import 'mocha';
import { getRegionalModels, models } from 'models';
import { getSchemas } from 'schemas';
import { Fyo } from '..';
import { DatabaseManager } from '../../backend/database/manager';
import { DummyAuthDemux } from './helpers';
import test from 'tape';
import { getTestFyo } from 'tests/helpers';
describe('Fyo Init', function () {
const fyo = new Fyo({
DatabaseDemux: DatabaseManager,
AuthDemux: DummyAuthDemux,
isTest: true,
isElectron: false,
});
specify('Init', async function () {
assert.strictEqual(
Object.keys(fyo.schemaMap).length,
0,
'zero schemas one'
);
assert.strictEqual(
Object.keys(fyo.schemaMap).length,
0,
'zero schemas two'
);
test('Fyo Init', async (t) => {
const fyo = getTestFyo();
t.equal(Object.keys(fyo.schemaMap).length, 0, 'zero schemas');
await fyo.db.createNewDatabase(':memory:', 'in');
await fyo.initializeAndRegister({}, {});
assert.strictEqual(
Object.keys(fyo.schemaMap).length > 0,
true,
'non zero schemas'
);
await fyo.db.purgeCache();
});
t.equal(Object.keys(fyo.schemaMap).length > 0, true, 'non zero schemas');
await fyo.close();
});
describe('Fyo Docs', function () {
test('Fyo Docs', async (t) => {
const countryCode = 'in';
let fyo: Fyo;
const fyo = getTestFyo();
const schemaMap = getSchemas(countryCode);
this.beforeEach(async function () {
fyo = new Fyo({
DatabaseDemux: DatabaseManager,
isTest: true,
isElectron: false,
});
const regionalModels = await getRegionalModels(countryCode);
await fyo.db.createNewDatabase(':memory:', countryCode);
await fyo.initializeAndRegister(models, regionalModels);
});
this.afterEach(async function () {
await fyo.close();
});
specify('getNewDoc', async function () {
for (const schemaName in schemaMap) {
const schema = schemaMap[schemaName];
if (schema?.isSingle) {
@ -66,6 +29,8 @@ describe('Fyo Docs', function () {
}
const doc = fyo.doc.getNewDoc(schemaName);
t.equal(doc.schemaName, schemaName, `equal schemaNames: ${schemaName}`);
}
});
await fyo.close();
});

View File

@ -1,131 +1,84 @@
import * as assert from 'assert';
import { strictEqual } from 'assert';
import Observable from 'fyo/utils/observable';
import 'mocha';
import test from 'tape';
enum ObsEvent {
A = 'event-a',
B = 'event-b',
}
describe('Observable', function () {
const obs = new Observable();
let counter = 0;
const params = { aOne: 18, aTwo: 21, b: 42 };
const obs = new Observable();
let counter = 0;
const params = { aOne: 18, aTwo: 21, b: 42 };
const listenerAOnce = (value: number) => {
assert.strictEqual(params.aOne, value, 'listenerAOnce');
};
const listenerAOnce = (value: number) => {
strictEqual(params.aOne, value, 'listenerAOnce');
};
const listenerAEvery = (value: number) => {
const listenerAEvery = (value: number) => {
if (counter === 0) {
assert.strictEqual(params.aOne, value, 'listenerAEvery 0');
strictEqual(params.aOne, value, 'listenerAEvery 0');
} else if (counter === 1) {
assert.strictEqual(params.aTwo, value, 'listenerAEvery 1');
strictEqual(params.aTwo, value, 'listenerAEvery 1');
} else {
throw new Error("this shouldn't run");
}
counter += 1;
};
};
const listenerBOnce = (value: number) => {
assert.strictEqual(params.b, value, 'listenerBOnce');
};
const listenerBOnce = (value: number) => {
strictEqual(params.b, value, 'listenerBOnce');
};
specify('set A One', function () {
assert.strictEqual(obs.hasListener(ObsEvent.A), false, 'pre');
test('set A One', function (t) {
t.equal(obs.hasListener(ObsEvent.A), false, 'pre');
obs.once(ObsEvent.A, listenerAOnce);
assert.strictEqual(obs.hasListener(ObsEvent.A), true, 'non specific');
assert.strictEqual(
obs.hasListener(ObsEvent.A, listenerAOnce),
true,
'specific once'
);
assert.strictEqual(
obs.hasListener(ObsEvent.A, listenerAEvery),
false,
'specific every'
);
});
t.equal(obs.hasListener(ObsEvent.A), true, 'non specific');
t.equal(obs.hasListener(ObsEvent.A, listenerAOnce), true, 'specific once');
t.equal(obs.hasListener(ObsEvent.A, listenerAEvery), false, 'specific every');
t.end()
});
specify('set A Two', function () {
test('set A Two', function (t) {
obs.on(ObsEvent.A, listenerAEvery);
assert.strictEqual(obs.hasListener(ObsEvent.A), true, 'non specific');
assert.strictEqual(
obs.hasListener(ObsEvent.A, listenerAOnce),
true,
'specific once'
);
assert.strictEqual(
obs.hasListener(ObsEvent.A, listenerAEvery),
true,
'specific every'
);
});
t.equal(obs.hasListener(ObsEvent.A), true, 'non specific');
t.equal(obs.hasListener(ObsEvent.A, listenerAOnce), true, 'specific once');
t.equal(obs.hasListener(ObsEvent.A, listenerAEvery), true, 'specific every');
t.end()
});
specify('set B', function () {
assert.strictEqual(obs.hasListener(ObsEvent.B), false, 'pre');
test('set B', function (t) {
t.equal(obs.hasListener(ObsEvent.B), false, 'pre');
obs.once(ObsEvent.B, listenerBOnce);
assert.strictEqual(
obs.hasListener(ObsEvent.A, listenerBOnce),
false,
'specific false'
);
assert.strictEqual(
obs.hasListener(ObsEvent.B, listenerBOnce),
true,
'specific true'
);
});
specify('trigger A 0', async function () {
await obs.trigger(ObsEvent.A, params.aOne);
assert.strictEqual(obs.hasListener(ObsEvent.A), true, 'non specific');
assert.strictEqual(
obs.hasListener(ObsEvent.A, listenerAOnce),
false,
'specific'
);
});
specify('trigger A 1', async function () {
assert.strictEqual(
obs.hasListener(ObsEvent.A, listenerAEvery),
true,
'specific pre'
);
await obs.trigger(ObsEvent.A, params.aTwo);
assert.strictEqual(
obs.hasListener(ObsEvent.A, listenerAEvery),
true,
'specific post'
);
});
specify('trigger B', async function () {
assert.strictEqual(
obs.hasListener(ObsEvent.B, listenerBOnce),
true,
'specific pre'
);
await obs.trigger(ObsEvent.B, params.b);
assert.strictEqual(
obs.hasListener(ObsEvent.B, listenerBOnce),
false,
'specific post'
);
});
specify('remove A', async function () {
obs.off(ObsEvent.A, listenerAEvery);
assert.strictEqual(
obs.hasListener(ObsEvent.A, listenerAEvery),
false,
'specific pre'
);
assert.strictEqual(counter, 2, 'incorrect counter');
await obs.trigger(ObsEvent.A, 777);
});
t.equal(obs.hasListener(ObsEvent.A, listenerBOnce), false, 'specific false');
t.equal(obs.hasListener(ObsEvent.B, listenerBOnce), true, 'specific true');
t.end()
});
test('trigger A 0', async function (t) {
await obs.trigger(ObsEvent.A, params.aOne);
t.equal(obs.hasListener(ObsEvent.A), true, 'non specific');
t.equal(obs.hasListener(ObsEvent.A, listenerAOnce), false, 'specific');
});
test('trigger A 1', async function (t) {
t.equal(obs.hasListener(ObsEvent.A, listenerAEvery), true, 'specific pre');
await obs.trigger(ObsEvent.A, params.aTwo);
t.equal(obs.hasListener(ObsEvent.A, listenerAEvery), true, 'specific post');
});
test('trigger B', async function (t) {
t.equal(obs.hasListener(ObsEvent.B, listenerBOnce), true, 'specific pre');
await obs.trigger(ObsEvent.B, params.b);
t.equal(obs.hasListener(ObsEvent.B, listenerBOnce), false, 'specific post');
});
test('remove A', async function (t) {
obs.off(ObsEvent.A, listenerAEvery);
t.equal(obs.hasListener(ObsEvent.A, listenerAEvery), false, 'specific pre');
t.equal(counter, 2, 'incorrect counter');
await obs.trigger(ObsEvent.A, 777);
});

View File

@ -17,7 +17,7 @@
"electron:serve": "vue-cli-service electron:serve",
"script:translate": "scripts/runner.sh scripts/generateTranslations.ts",
"script:profile": "scripts/profile.sh",
"test": "scripts/runner.sh ./node_modules/.bin/mocha ./**/tests/**/*.spec.ts --exit"
"test": "scripts/runner.sh ./node_modules/.bin/tape ./**/tests/**/*.spec.ts | tap-spec"
},
"dependencies": {
"@popperjs/core": "^2.10.2",
@ -43,6 +43,7 @@
"@types/mocha": "^9.1.0",
"@types/node": "^17.0.23",
"@types/node-fetch": "^2.6.1",
"@types/tape": "^4.13.2",
"@typescript-eslint/eslint-plugin": "^4.15.1",
"@typescript-eslint/parser": "^4.15.1",
"@vue/cli-plugin-babel": "^4.5.0",
@ -65,11 +66,12 @@
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-vue": "^7.0.0",
"lint-staged": "^11.2.6",
"mocha": "^9.2.2",
"postcss": "^8",
"prettier": "^2.4.1",
"raw-loader": "^4.0.2",
"tailwindcss": "npm:@tailwindcss/postcss7-compat",
"tap-spec": "^5.0.0",
"tape": "^5.6.1",
"ts-node": "^10.7.0",
"tsconfig-paths": "^3.14.1",
"tslib": "^2.3.1",

View File

@ -1,173 +1,161 @@
import * as assert from 'assert';
import { cloneDeep, isEqual } from 'lodash';
import { describe } from 'mocha';
import test from 'tape';
import { getMapFromList } from 'utils';
import {
addMetaFields,
cleanSchemas,
getAbstractCombinedSchemas,
getRegionalCombinedSchemas,
setSchemaNameOnFields,
setSchemaNameOnFields
} from '../index';
import { metaSchemas } from '../schemas';
import {
everyFieldExists,
getTestSchemaMap,
someFieldExists,
subtract,
subtract
} from './helpers';
describe('Schema Builder', function () {
const { appSchemaMap, regionalSchemaMap } = getTestSchemaMap();
describe('Raw Schemas', function () {
specify('Meta Properties', function () {
assert.strictEqual(appSchemaMap.Party.isAbstract, true);
assert.strictEqual(appSchemaMap.Customer.extends, 'Party');
assert.strictEqual(appSchemaMap.Account.isTree, true);
assert.strictEqual(appSchemaMap.JournalEntryAccount.isChild, true);
});
const { appSchemaMap, regionalSchemaMap } = getTestSchemaMap();
test('Meta Properties', function (t) {
t.equal(appSchemaMap.Party.isAbstract, true);
t.equal(appSchemaMap.Customer.extends, 'Party');
t.equal(appSchemaMap.Account.isTree, true);
t.equal(appSchemaMap.JournalEntryAccount.isChild, true);
t.end();
});
specify('Field Counts', function () {
assert.strictEqual(appSchemaMap.Account.fields?.length, 6);
assert.strictEqual(appSchemaMap.JournalEntry.fields?.length, 9);
assert.strictEqual(appSchemaMap.JournalEntryAccount.fields?.length, 3);
assert.strictEqual(appSchemaMap.Party.fields?.length, 9);
assert.strictEqual(appSchemaMap.Customer.fields?.length, undefined);
assert.strictEqual(regionalSchemaMap.Party.fields?.length, 2);
});
test('Field Counts', function (t) {
t.equal(appSchemaMap.Account.fields?.length, 6);
t.equal(appSchemaMap.JournalEntry.fields?.length, 9);
t.equal(appSchemaMap.JournalEntryAccount.fields?.length, 3);
t.equal(appSchemaMap.Party.fields?.length, 9);
t.equal(appSchemaMap.Customer.fields?.length, undefined);
t.equal(regionalSchemaMap.Party.fields?.length, 2);
t.end();
});
specify('Quick Edit Field Counts', function () {
assert.strictEqual(appSchemaMap.Party.quickEditFields?.length, 5);
assert.strictEqual(regionalSchemaMap.Party.quickEditFields?.length, 8);
});
});
test('Quick Edit Field Counts', function (t) {
t.equal(appSchemaMap.Party.quickEditFields?.length, 5);
t.equal(regionalSchemaMap.Party.quickEditFields?.length, 8);
t.end();
});
const regionalCombined = getRegionalCombinedSchemas(
const regionalCombined = getRegionalCombinedSchemas(
appSchemaMap,
regionalSchemaMap
);
describe('Regional Combined Schemas', function () {
specify('Field Counts', function () {
assert.strictEqual(regionalCombined.Party.fields?.length, 11);
});
);
specify('Quick Edit Field Counts', function () {
assert.strictEqual(regionalSchemaMap.Party.quickEditFields?.length, 8);
});
test('Field Counts', function (t) {
t.equal(regionalCombined.Party.fields?.length, 11);
t.end();
});
specify('Schema Equality with App Schemas', function () {
assert.strictEqual(
isEqual(regionalCombined.Account, appSchemaMap.Account),
true
);
assert.strictEqual(
test('Quick Edit Field Counts', function (t) {
t.equal(regionalSchemaMap.Party.quickEditFields?.length, 8);
t.end();
});
test('Schema Equality with App Schemas', function (t) {
t.equal(isEqual(regionalCombined.Account, appSchemaMap.Account), true);
t.equal(
isEqual(regionalCombined.JournalEntry, appSchemaMap.JournalEntry),
true
);
assert.strictEqual(
t.equal(
isEqual(
regionalCombined.JournalEntryAccount,
appSchemaMap.JournalEntryAccount
),
true
);
assert.strictEqual(
isEqual(regionalCombined.Customer, appSchemaMap.Customer),
true
);
assert.strictEqual(
isEqual(regionalCombined.Party, appSchemaMap.Party),
false
);
});
});
t.equal(isEqual(regionalCombined.Customer, appSchemaMap.Customer), true);
t.equal(isEqual(regionalCombined.Party, appSchemaMap.Party), false);
t.end();
});
const abstractCombined = cleanSchemas(
const abstractCombined = cleanSchemas(
getAbstractCombinedSchemas(regionalCombined)
);
describe('Abstract Combined Schemas', function () {
specify('Meta Properties', function () {
assert.strictEqual(abstractCombined.Customer!.extends, undefined);
});
);
specify('Abstract Schema Existance', function () {
assert.strictEqual(abstractCombined.Party, undefined);
});
test('Meta Properties', function (t) {
t.equal(abstractCombined.Customer!.extends, undefined);
t.end();
});
specify('Field Counts', function () {
assert.strictEqual(abstractCombined.Customer!.fields?.length, 11);
});
test('Abstract Schema Existance', function (t) {
t.equal(abstractCombined.Party, undefined);
t.end();
});
specify('Quick Edit Field Counts', function () {
assert.strictEqual(abstractCombined.Customer!.quickEditFields?.length, 8);
});
test('Field Counts', function (t) {
t.equal(abstractCombined.Customer!.fields?.length, 11);
t.end();
});
specify('Schema Equality with App Schemas', function () {
assert.strictEqual(
isEqual(abstractCombined.Account, appSchemaMap.Account),
true
);
assert.strictEqual(
test('Quick Edit Field Counts', function (t) {
t.equal(abstractCombined.Customer!.quickEditFields?.length, 8);
t.end();
});
test('Schema Equality with App Schemas', function (t) {
t.equal(isEqual(abstractCombined.Account, appSchemaMap.Account), true);
t.equal(
isEqual(abstractCombined.JournalEntry, appSchemaMap.JournalEntry),
true
);
assert.strictEqual(
t.equal(
isEqual(
abstractCombined.JournalEntryAccount,
appSchemaMap.JournalEntryAccount
),
true
);
assert.strictEqual(
isEqual(abstractCombined.Customer, appSchemaMap.Customer),
false
);
});
t.equal(isEqual(abstractCombined.Customer, appSchemaMap.Customer), false);
t.end();
});
specify('Schema Field Existance', function () {
assert.strictEqual(
test('Schema Field Existance', function (t) {
t.equal(
everyFieldExists(
regionalSchemaMap.Party.quickEditFields ?? [],
abstractCombined.Customer!
),
true
);
});
});
t.end();
});
let almostFinalSchemas = cloneDeep(abstractCombined);
almostFinalSchemas = addMetaFields(almostFinalSchemas);
const finalSchemas = setSchemaNameOnFields(almostFinalSchemas);
const metaSchemaMap = getMapFromList(metaSchemas, 'name');
const baseFieldNames = metaSchemaMap.base.fields!.map((f) => f.fieldname);
const childFieldNames = metaSchemaMap.child.fields!.map((f) => f.fieldname);
const treeFieldNames = metaSchemaMap.tree.fields!.map((f) => f.fieldname);
const submittableFieldNames = metaSchemaMap.submittable.fields!.map(
let almostFinalSchemas = cloneDeep(abstractCombined);
almostFinalSchemas = addMetaFields(almostFinalSchemas);
const finalSchemas = setSchemaNameOnFields(almostFinalSchemas);
const metaSchemaMap = getMapFromList(metaSchemas, 'name');
const baseFieldNames = metaSchemaMap.base.fields!.map((f) => f.fieldname);
const childFieldNames = metaSchemaMap.child.fields!.map((f) => f.fieldname);
const treeFieldNames = metaSchemaMap.tree.fields!.map((f) => f.fieldname);
const submittableFieldNames = metaSchemaMap.submittable.fields!.map(
(f) => f.fieldname
);
const allFieldNames = [
);
const allFieldNames = [
...baseFieldNames,
...childFieldNames,
...treeFieldNames,
...submittableFieldNames,
];
];
describe('Final Schemas', function () {
specify('Schema Name Existsance', function () {
test('Schema Name Existsance', function (t) {
for (const schemaName in finalSchemas) {
for (const field of finalSchemas[schemaName]?.fields!) {
assert.strictEqual(field.schemaName, schemaName);
t.equal(field.schemaName, schemaName);
}
}
});
t.end();
});
specify('Schema Field Existance', function () {
assert.strictEqual(
everyFieldExists(baseFieldNames, finalSchemas.Customer!),
true
);
test('Schema Field Existance', function (t) {
t.equal(everyFieldExists(baseFieldNames, finalSchemas.Customer!), true);
assert.strictEqual(
t.equal(
someFieldExists(
subtract(allFieldNames, baseFieldNames),
finalSchemas.Customer!
@ -175,7 +163,7 @@ describe('Schema Builder', function () {
false
);
assert.strictEqual(
t.equal(
everyFieldExists(
[...baseFieldNames, ...submittableFieldNames],
finalSchemas.JournalEntry!
@ -183,7 +171,7 @@ describe('Schema Builder', function () {
true
);
assert.strictEqual(
t.equal(
someFieldExists(
subtract(allFieldNames, baseFieldNames, submittableFieldNames),
finalSchemas.JournalEntry!
@ -191,12 +179,12 @@ describe('Schema Builder', function () {
false
);
assert.strictEqual(
t.equal(
everyFieldExists(childFieldNames, finalSchemas.JournalEntryAccount!),
true
);
assert.strictEqual(
t.equal(
someFieldExists(
subtract(allFieldNames, childFieldNames),
finalSchemas.JournalEntryAccount!
@ -204,7 +192,7 @@ describe('Schema Builder', function () {
false
);
assert.strictEqual(
t.equal(
everyFieldExists(
[...treeFieldNames, ...baseFieldNames],
finalSchemas.Account!
@ -212,13 +200,13 @@ describe('Schema Builder', function () {
true
);
assert.strictEqual(
t.equal(
someFieldExists(
subtract(allFieldNames, treeFieldNames, baseFieldNames),
finalSchemas.Account!
),
false
);
});
});
t.end();
});

View File

@ -1,4 +1,7 @@
import { DatabaseManager } from 'backend/database/manager';
import { config } from 'dotenv';
import { Fyo } from 'fyo';
import { DummyAuthDemux } from 'fyo/tests/helpers';
import { SetupWizardOptions } from 'src/setup/types';
import { getFiscalYear } from 'utils/misc';
@ -21,3 +24,12 @@ export function getTestDbPath(dbPath?: string) {
config();
return dbPath ?? process.env.TEST_DB_PATH ?? ':memory:';
}
export function getTestFyo(): Fyo {
return new Fyo({
DatabaseDemux: DatabaseManager,
AuthDemux: DummyAuthDemux,
isTest: true,
isElectron: false,
});
}

View File

@ -1,40 +1,25 @@
import * as assert from 'assert';
import { DatabaseManager } from 'backend/database/manager';
import { assertDoesNotThrow } from 'backend/database/tests/helpers';
import { Fyo } from 'fyo';
import { DummyAuthDemux } from 'fyo/tests/helpers';
import 'mocha';
import setupInstance from 'src/setup/setupInstance';
import { SetupWizardOptions } from 'src/setup/types';
import test from 'tape';
import { getValueMapFromList } from 'utils';
import { getTestDbPath, getTestSetupWizardOptions } from './helpers';
import {
getTestDbPath,
getTestFyo,
getTestSetupWizardOptions
} from './helpers';
describe('setupInstance', function () {
const dbPath = getTestDbPath();
const setupOptions = getTestSetupWizardOptions();
const dbPath = getTestDbPath();
const setupOptions = getTestSetupWizardOptions();
const fyo = getTestFyo();
let fyo: Fyo;
this.beforeAll(function () {
fyo = new Fyo({
DatabaseDemux: DatabaseManager,
AuthDemux: DummyAuthDemux,
isTest: true,
isElectron: false,
});
});
this.afterAll(async function () {
await fyo.close();
});
specify('setupInstance', async function () {
test('setupInstance', async () => {
await assertDoesNotThrow(async () => {
await setupInstance(dbPath, setupOptions, fyo);
}, 'setup instance failed');
});
});
specify('check setup Singles', async function () {
test('check setup Singles', async (t) => {
const setupFields = [
'companyName',
'country',
@ -57,22 +42,25 @@ describe('setupInstance', function () {
dbValue = dbValue.toISOString().split('T')[0];
}
assert.strictEqual(
t.equal(
dbValue as string,
optionsValue,
`${field} mismatch (${dbValue},${optionsValue})`
`${field}: (${dbValue}, ${optionsValue})`
);
}
});
});
specify('check null singles', async function () {
test('check null singles', async (t) => {
const nullFields = ['gstin', 'logo', 'phone', 'address'];
const nullSingles = await fyo.db.getSingleValues(...nullFields);
assert.strictEqual(
t.equal(
nullSingles.length,
0,
`null singles found ${JSON.stringify(nullSingles)}`
`null singles: ${JSON.stringify(nullSingles)}`
);
});
});
test.onFinish(async () => {
await fyo.close();
});

767
yarn.lock

File diff suppressed because it is too large Load Diff