2
0
mirror of https://github.com/frappe/books.git synced 2024-12-22 02:49:03 +00:00

refactor: dummy and fyo tests use tape

This commit is contained in:
18alantom 2022-10-31 13:03:50 +05:30
parent 6806c42daf
commit 652a3c1139
4 changed files with 175 additions and 291 deletions

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 () {
await assertDoesNotThrow(async () => {
await setupDummyInstance(dbPath, fyo, 1, 25);
}, 'setup instance failed');
for (const item in purchaseItemPartyMap) {
assert.strictEqual(
await fyo.db.exists('Item', item),
true,
`not found ${item}`
);
const party = purchaseItemPartyMap[item];
assert.strictEqual(
await fyo.db.exists('Party', party),
true,
`not found ${party}`
);
}
}).timeout(120_000);
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) {
t.ok(await fyo.db.exists('Item', item), `item exists: ${item}`);
const party = purchaseItemPartyMap[item];
t.ok(await fyo.db.exists('Party', party), `party exists: ${party}`);
}
});
test.onFinish(async () => {
await fyo.close();
});

View File

@ -1,71 +1,36 @@
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,
});
test('Fyo Init', async (t) => {
const fyo = getTestFyo();
t.equal(Object.keys(fyo.schemaMap).length, 0, 'zero schemas');
specify('Init', async function () {
assert.strictEqual(
Object.keys(fyo.schemaMap).length,
0,
'zero schemas one'
);
await fyo.db.createNewDatabase(':memory:', 'in');
await fyo.initializeAndRegister({}, {});
assert.strictEqual(
Object.keys(fyo.schemaMap).length,
0,
'zero schemas two'
);
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);
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) {
continue;
}
const doc = fyo.doc.getNewDoc(schemaName);
for (const schemaName in schemaMap) {
const schema = schemaMap[schemaName];
if (schema?.isSingle) {
continue;
}
});
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) => {
if (counter === 0) {
assert.strictEqual(params.aOne, value, 'listenerAEvery 0');
} else if (counter === 1) {
assert.strictEqual(params.aTwo, value, 'listenerAEvery 1');
} else {
throw new Error("this shouldn't run");
}
counter += 1;
};
const listenerAEvery = (value: number) => {
if (counter === 0) {
strictEqual(params.aOne, value, 'listenerAEvery 0');
} else if (counter === 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'
);
});
specify('set A Two', function () {
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'
);
});
specify('set B', function () {
assert.strictEqual(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);
});
obs.once(ObsEvent.A, listenerAOnce);
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()
});
test('set A Two', function (t) {
obs.on(ObsEvent.A, listenerAEvery);
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()
});
test('set B', function (t) {
t.equal(obs.hasListener(ObsEvent.B), false, 'pre');
obs.once(ObsEvent.B, listenerBOnce);
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

@ -1,78 +1,66 @@
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 () {
await assertDoesNotThrow(async () => {
await setupInstance(dbPath, setupOptions, fyo);
}, 'setup instance failed');
});
specify('check setup Singles', async function () {
const setupFields = [
'companyName',
'country',
'fullname',
'email',
'bankName',
'fiscalYearStart',
'fiscalYearEnd',
'currency',
];
const setupSingles = await fyo.db.getSingleValues(...setupFields);
const singlesMap = getValueMapFromList(setupSingles, 'fieldname', 'value');
for (const field of setupFields) {
let dbValue = singlesMap[field];
const optionsValue = setupOptions[field as keyof SetupWizardOptions];
if (dbValue instanceof Date) {
dbValue = dbValue.toISOString().split('T')[0];
}
assert.strictEqual(
dbValue as string,
optionsValue,
`${field} mismatch (${dbValue},${optionsValue})`
);
}
});
specify('check null singles', async function () {
const nullFields = ['gstin', 'logo', 'phone', 'address'];
const nullSingles = await fyo.db.getSingleValues(...nullFields);
assert.strictEqual(
nullSingles.length,
0,
`null singles found ${JSON.stringify(nullSingles)}`
);
});
test('setupInstance', async () => {
await assertDoesNotThrow(async () => {
await setupInstance(dbPath, setupOptions, fyo);
}, 'setup instance failed');
});
test('check setup Singles', async (t) => {
const setupFields = [
'companyName',
'country',
'fullname',
'email',
'bankName',
'fiscalYearStart',
'fiscalYearEnd',
'currency',
];
const setupSingles = await fyo.db.getSingleValues(...setupFields);
const singlesMap = getValueMapFromList(setupSingles, 'fieldname', 'value');
for (const field of setupFields) {
let dbValue = singlesMap[field];
const optionsValue = setupOptions[field as keyof SetupWizardOptions];
if (dbValue instanceof Date) {
dbValue = dbValue.toISOString().split('T')[0];
}
t.equal(
dbValue as string,
optionsValue,
`${field}: (${dbValue}, ${optionsValue})`
);
}
});
test('check null singles', async (t) => {
const nullFields = ['gstin', 'logo', 'phone', 'address'];
const nullSingles = await fyo.db.getSingleValues(...nullFields);
t.equal(
nullSingles.length,
0,
`null singles: ${JSON.stringify(nullSingles)}`
);
});
test.onFinish(async () => {
await fyo.close();
});