2
0
mirror of https://github.com/frappe/books.git synced 2025-01-03 07:12:21 +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 { assertDoesNotThrow } from 'backend/database/tests/helpers';
import { purchaseItemPartyMap } from 'dummy/helpers'; import { purchaseItemPartyMap } from 'dummy/helpers';
import { Fyo } from 'fyo'; import test from 'tape';
import { DummyAuthDemux } from 'fyo/tests/helpers'; import { getTestDbPath, getTestFyo } from 'tests/helpers';
import 'mocha';
import { getTestDbPath } from 'tests/helpers';
import { setupDummyInstance } from '..'; import { setupDummyInstance } from '..';
describe('dummy', function () {
const dbPath = getTestDbPath(); const dbPath = getTestDbPath();
const fyo = getTestFyo();
let fyo: Fyo; test('setupDummyInstance', async () => {
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 assertDoesNotThrow(async () => {
await setupDummyInstance(dbPath, fyo, 1, 25); await setupDummyInstance(dbPath, fyo, 1, 25);
}, 'setup instance failed'); }, 'setup instance failed');
});
test('purchaseItemParty Existance', async (t) => {
for (const item in purchaseItemPartyMap) { for (const item in purchaseItemPartyMap) {
assert.strictEqual( t.ok(await fyo.db.exists('Item', item), `item exists: ${item}`);
await fyo.db.exists('Item', item),
true,
`not found ${item}`
);
const party = purchaseItemPartyMap[item]; const party = purchaseItemPartyMap[item];
assert.strictEqual( t.ok(await fyo.db.exists('Party', party), `party exists: ${party}`);
await fyo.db.exists('Party', party),
true,
`not found ${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 { getRegionalModels, models } from 'models';
import { getSchemas } from 'schemas'; import { getSchemas } from 'schemas';
import { Fyo } from '..'; import test from 'tape';
import { DatabaseManager } from '../../backend/database/manager'; import { getTestFyo } from 'tests/helpers';
import { DummyAuthDemux } from './helpers';
describe('Fyo Init', function () { test('Fyo Init', async (t) => {
const fyo = new Fyo({ const fyo = getTestFyo();
DatabaseDemux: DatabaseManager, t.equal(Object.keys(fyo.schemaMap).length, 0, 'zero schemas');
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'
);
await fyo.db.createNewDatabase(':memory:', 'in'); await fyo.db.createNewDatabase(':memory:', 'in');
await fyo.initializeAndRegister({}, {}); await fyo.initializeAndRegister({}, {});
assert.strictEqual(
Object.keys(fyo.schemaMap).length > 0,
true,
'non zero schemas'
);
await fyo.db.purgeCache();
});
});
describe('Fyo Docs', function () { t.equal(Object.keys(fyo.schemaMap).length > 0, true, 'non zero schemas');
const countryCode = 'in';
let fyo: Fyo;
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(); await fyo.close();
}); });
specify('getNewDoc', async function () { test('Fyo Docs', async (t) => {
const countryCode = 'in';
const fyo = getTestFyo();
const schemaMap = getSchemas(countryCode);
const regionalModels = await getRegionalModels(countryCode);
await fyo.db.createNewDatabase(':memory:', countryCode);
await fyo.initializeAndRegister(models, regionalModels);
for (const schemaName in schemaMap) { for (const schemaName in schemaMap) {
const schema = schemaMap[schemaName]; const schema = schemaMap[schemaName];
if (schema?.isSingle) { if (schema?.isSingle) {
@ -66,6 +29,8 @@ describe('Fyo Docs', function () {
} }
const doc = fyo.doc.getNewDoc(schemaName); const doc = fyo.doc.getNewDoc(schemaName);
t.equal(doc.schemaName, schemaName, `equal schemaNames: ${schemaName}`);
} }
});
await fyo.close();
}); });

View File

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

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 { 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 setupInstance from 'src/setup/setupInstance';
import { SetupWizardOptions } from 'src/setup/types'; import { SetupWizardOptions } from 'src/setup/types';
import test from 'tape';
import { getValueMapFromList } from 'utils'; import { getValueMapFromList } from 'utils';
import { getTestDbPath, getTestSetupWizardOptions } from './helpers'; import {
getTestDbPath,
getTestFyo,
getTestSetupWizardOptions
} from './helpers';
describe('setupInstance', function () {
const dbPath = getTestDbPath(); const dbPath = getTestDbPath();
const setupOptions = getTestSetupWizardOptions(); const setupOptions = getTestSetupWizardOptions();
const fyo = getTestFyo();
let fyo: Fyo; test('setupInstance', async () => {
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 assertDoesNotThrow(async () => {
await setupInstance(dbPath, setupOptions, fyo); await setupInstance(dbPath, setupOptions, fyo);
}, 'setup instance failed'); }, 'setup instance failed');
}); });
specify('check setup Singles', async function () { test('check setup Singles', async (t) => {
const setupFields = [ const setupFields = [
'companyName', 'companyName',
'country', 'country',
@ -57,22 +42,25 @@ describe('setupInstance', function () {
dbValue = dbValue.toISOString().split('T')[0]; dbValue = dbValue.toISOString().split('T')[0];
} }
assert.strictEqual( t.equal(
dbValue as string, dbValue as string,
optionsValue, 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 nullFields = ['gstin', 'logo', 'phone', 'address'];
const nullSingles = await fyo.db.getSingleValues(...nullFields); const nullSingles = await fyo.db.getSingleValues(...nullFields);
assert.strictEqual( t.equal(
nullSingles.length, nullSingles.length,
0, 0,
`null singles found ${JSON.stringify(nullSingles)}` `null singles: ${JSON.stringify(nullSingles)}`
); );
}); });
test.onFinish(async () => {
await fyo.close();
}); });