Skip to content

Commit

Permalink
test: add property based tests to DataBus (apitable#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
ziqiangai committed Feb 27, 2023
1 parent 59c42f7 commit 5c2fe97
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"@types/node": "^14.14.34",
"@types/qs": "^6.9.4",
"@types/string.prototype.matchall": "^4.0.1",
"fast-check": "^3.6.3",
"jest": "^29.2.2",
"jest-environment-jsdom": "^29.2.2",
"mockjs": "1.1.0",
Expand Down
121 changes: 121 additions & 0 deletions packages/core/src/databus/__tests__/databus-fast-check.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { MockDataBus, resetDataLoader } from 'databus/__tests__/mock.databus';
import { ResourceType } from 'types';
import { CollaCommandName, } from 'commands';
import { ExecuteResult } from 'command_manager';
import * as fc from 'fast-check';
import { mockGetViewInfo } from 'databus/__tests__/mock.view';
import * as console from 'console';

const db = MockDataBus.getDatabase();

beforeAll(resetDataLoader);

describe('fast check try', () => {
const contains = (text: string, pattern: string) => {
return text.length > 3 ? (text.substr(1).indexOf(pattern) === -1) : (text.indexOf(pattern) >= 0);
};

test('should always contain itself', () => {
fc.assert(fc.asyncProperty(fc.string(), (text) => {
return new Promise(resolve => {
resolve(contains(text, text));
});
}), { verbose: true });
});

});

describe('fast check doCommand Operation', () => {

test('check revision and datasheet pros after a AddRecords doCommand operation', async() => {
const dst1 = await db.getDatasheet('dst1', {});
expect(dst1).toBeTruthy();

const oldRevision = dst1!.revision;
expect(oldRevision).toStrictEqual(12);
let expectRevisionChangedCount = 0;

await fc.assert(fc.asyncProperty(fc.integer(),
async(aInt: number) => {
const result = await dst1!.doCommand(
{
cmd: CollaCommandName.AddRecords,
viewId: 'viw1',
index: 3,
count: aInt % 10,
},
{},
);
// check Revision change count. only the result is success will call saveOps
if (result.result === ExecuteResult.Success) {
expectRevisionChangedCount += 1;
}

// the id, name, and type field of the Datasheet are not changed.
const prosEq = (dst1!.id === 'dst1' && dst1!.type === ResourceType.Datasheet && dst1!.name === 'datasheet 1');
expect(prosEq).toBeTruthy();
return new Promise(resolve => {
resolve(prosEq);
});
}), { verbose: true, timeout: 30000 },
);
expect(dst1?.revision).toBe(expectRevisionChangedCount + oldRevision);
});

test('check datasheet\'s field count after a AddFields doCommand operation', async() => {
const dst1 = await db.getDatasheet('dst1', {});
expect(dst1).toBeTruthy();
if (dst1 == null) {
return;
}
const dstId = dst1?.id || '';
const view1 = await dst1!.getView({
getViewInfo: mockGetViewInfo('dst1', 'viw1'),
});
const oldViewColumnCount = view1?.columns.length || 0;
expect(oldViewColumnCount).toStrictEqual(2);
let expectFieldAddedCount = 0;
await fc.assert(fc.asyncProperty(fc.string(), fc.integer({ min: 2 , max: 200 }),
async(aStr: string, aInt: number) => {
const oldFieldCount = Object.keys(dst1.fields).length;
const result = await dst1!.doCommand(
{
cmd: CollaCommandName.AddFields,
data: [{
data: {
id: aStr + aInt,
name: aStr,
property: null,
type: 1,
},
index: aInt,
viewId: 'viw1',
}],
copyCell: false,
datasheetId: dstId,
},
{},
);
// check Revision change count. only the result is success will call saveOps
let fieldAddedCount = 0;
if (result.result === ExecuteResult.Success) {
expectFieldAddedCount += 1;
fieldAddedCount = 1;
}else {
console.error(`some error happens and the str var is ${aStr} and field count is ${oldFieldCount}`);
}

const fieldCount = Object.keys(dst1.fields).length;
expect(fieldCount).toEqual(oldFieldCount + fieldAddedCount);
return new Promise(resolve => {
resolve(fieldCount >= (oldFieldCount + fieldAddedCount));
});
}), { verbose: true, timeout: 30000 },
);
const view2 = await dst1!.getView({
getViewInfo: mockGetViewInfo('dst1', 'viw1'),
});
expect(view2?.columns.length).toStrictEqual(oldViewColumnCount + expectFieldAddedCount);
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { IDataSaver, ISaveOpsOptions } from 'databus/data.saver.interface';
import { IBaseDatasheetPack, Selectors, StoreActions } from 'exports/store';
import { IDataLoader } from '../data.loader.interface';
import { mockDatasheetMap } from './mock.datasheets';
import { ResourceType } from '../../types';

export class MockDataLoaderSaver implements IDataLoader, IDataSaver {
datasheets!: Record<string, IBaseDatasheetPack>;
Expand All @@ -45,7 +46,9 @@ export class MockDataLoaderSaver implements IDataLoader, IDataSaver {
const changesets = resourceOpsToChangesets(ops, store.getState());
changesets.forEach(cs => {
store.dispatch(StoreActions.applyJOTOperations(cs.operations, cs.resourceType, cs.resourceId));

if (cs.baseRevision !== undefined) {
store.dispatch(StoreActions.updateRevision(cs.baseRevision + 1, cs.resourceId, ResourceType.Datasheet));
}
this.datasheets[cs.resourceId] = {
datasheet: Selectors.getDatasheet(store.getState())!,
snapshot: Selectors.getSnapshot(store.getState())!,
Expand Down
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ __metadata:
axios: 0.21.2
color: ^3.1.3
dayjs: ^1.11.7
fast-check: ^3.6.3
fuse.js: ^6.4.1
immer: 9.0.16
jest: ^29.2.2
Expand Down Expand Up @@ -23238,6 +23239,15 @@ __metadata:
languageName: node
linkType: hard

"fast-check@npm:^3.6.3":
version: 3.6.3
resolution: "fast-check@npm:3.6.3"
dependencies:
pure-rand: ^6.0.0
checksum: c3732011ceef2c3a999678a932cf051946a484d564a73bac695066a255510b2e6a1102bd5cacc189919326ea5c7a515e0247b376416f348c3bc323ee4f4ec7c8
languageName: node
linkType: hard

"fast-csv@npm:^3.4.0":
version: 3.7.0
resolution: "fast-csv@npm:3.7.0"
Expand Down Expand Up @@ -37219,6 +37229,13 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"pure-rand@npm:^6.0.0":
version: 6.0.0
resolution: "pure-rand@npm:6.0.0"
checksum: ad1378d0a4859482d053a5264b2b485b445ece4bbc56f8959c233ea678b81ac2d613737925d496ded134eff5f29cc5546bf7492b6bce319ee27bebbad8a0c612
languageName: node
linkType: hard

"q@npm:^1.5.1":
version: 1.5.1
resolution: "q@npm:1.5.1"
Expand Down

0 comments on commit 5c2fe97

Please sign in to comment.