-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: add property based tests to DataBus #454
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { MockDataBus, resetDataLoader } from './mock.databus'; | ||
import { ResourceType } from 'types'; | ||
import { CollaCommandName, } from 'commands'; | ||
import { ExecuteResult } from 'command_manager'; | ||
import * as fc from 'fast-check'; | ||
import { mockGetViewInfo } from './mock.view'; | ||
import * as console from 'console'; | ||
|
||
const db = MockDataBus.getDatabase(); | ||
|
||
beforeAll(resetDataLoader); | ||
|
||
describe('fast check try', () => { | ||
const contains = (text: string, pattern: string) => { | ||
if (text.length > 3) { | ||
return text.substr(1).indexOf(pattern) === -1; | ||
} else { | ||
return text.indexOf(pattern) >= 0; | ||
} | ||
Comment on lines
+17
to
+19
Check warning Code scanning / ESLint disallow `else` blocks after `return` statements in `if` statements
Unnecessary 'else' after 'return'.
|
||
}; | ||
|
||
test('should always contain itself', () => { | ||
fc.assert(fc.asyncProperty(fc.string(), (text: string) => { | ||
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(dst1!.id).toStrictEqual('dst1'); | ||
expect(dst1!.type).toStrictEqual(ResourceType.Datasheet); | ||
expect(dst1!.name).toStrictEqual('datasheet 1'); | ||
return new Promise(resolve => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A simple |
||
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}), | ||
Check warning Code scanning / ESLint enforce consistent spacing inside braces
A space is required after '{'.
Check warning Code scanning / ESLint enforce consistent spacing inside braces
A space is required before '}'.
|
||
async (aStr: string, aInt: number) => { | ||
Check warning Code scanning / ESLint enforce consistent spacing before `function` definition opening parenthesis
Unexpected space before function parentheses.
|
||
const oldFieldCount = Object.keys(dst1.fields).length; | ||
const result = await dst1!.doCommand( | ||
{ | ||
cmd: CollaCommandName.AddFields, | ||
data: [{ | ||
data: { | ||
id: aStr, | ||
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 | ||
if (result.result === ExecuteResult.Success) { | ||
expectFieldAddedCount += 1; | ||
}else { | ||
console.error('some error happens'); | ||
} | ||
|
||
const fieldCount = Object.keys(dst1.fields).length; | ||
return new Promise(resolve => { | ||
resolve(fieldCount >= (oldFieldCount + 1)); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing assertions inside this function |
||
}), { verbose: true, timeout: 30000 }, | ||
); | ||
const view2 = await dst1!.getView({ | ||
getViewInfo: mockGetViewInfo('dst1', 'viw1'), | ||
}); | ||
expect(view2?.columns.length).toStrictEqual(oldViewColumnCount + expectFieldAddedCount); | ||
}); | ||
|
||
}); |
Check warning
Code scanning / ESLint
require or disallow trailing commas