Skip to content

Commit

Permalink
Merge pull request #1486 from demergent-labs/stable_b_tree_map_candid…
Browse files Browse the repository at this point in the history
…_tests

add CandidTypeObject tests to StableBTreeMap property tests
  • Loading branch information
lastmjs authored Dec 8, 2023
2 parents 01a4005 + c39a9fe commit 20bc87f
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 68 deletions.
77 changes: 60 additions & 17 deletions property_tests/arbitraries/stable_b_tree_map_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,76 @@ import { CandidValueAndMetaArb } from './candid/candid_value_and_meta_arb';
export const StableBTreeMapArb = fc
.tuple(
CandidValueAndMetaArb(),
argumentInfoArb(),
CandidValueAndMetaArb(),
argumentInfoArb(),
UniqueIdentifierArb('stableBTreeMap'),
createUniquePrimitiveArb(
fc.nat({
max: 254
})
)
)
.map(([keySample, valueSample, uniqueIdentifier, memoryId]) => {
const name = `stableBTreeMap${uniqueIdentifier}`;

const imports = new Set([
...keySample.src.imports,
...valueSample.src.imports,
'stableJson',
'StableBTreeMap'
]);

return {
name,
imports,
definition: `let ${name} = StableBTreeMap<${keySample.src.candidTypeAnnotation}, ${valueSample.src.candidTypeAnnotation}>(${memoryId});`,
.map(
([
keySample,
valueSample
};
});
keyArgumentInfo,
valueSample,
valueArgumentInfo,
uniqueIdentifier,
memoryId
]) => {
const name = `stableBTreeMap${uniqueIdentifier}`;

const imports = new Set([
...keySample.src.imports,
...valueSample.src.imports,
'stableJson',
'StableBTreeMap'
]);

const serializableArguments = getSerializableArguments(
keySample,
valueSample,
keyArgumentInfo,
valueArgumentInfo
);

return {
name,
imports,
definition: `let ${name} = StableBTreeMap<${keySample.src.candidTypeAnnotation}, ${valueSample.src.candidTypeAnnotation}>(${memoryId}${serializableArguments});`,
keySample,
valueSample
};
}
);

// TODO there must be a better way to get this type out, the sample will actually run
export const StableBTreeMap = fc.sample(StableBTreeMapArb)[0];
export type StableBTreeMap = typeof StableBTreeMap;

function getSerializableArguments(
keySample: StableBTreeMap['keySample'],
valueSample: StableBTreeMap['valueSample'],
keyArgumentInfo: 'STABLE_JSON' | 'CANDID_TYPE_OBJECT',
valueArgumentInfo: 'STABLE_JSON' | 'CANDID_TYPE_OBJECT'
): string {
const keyArgument =
keyArgumentInfo === 'STABLE_JSON'
? 'stableJson'
: keySample.src.candidTypeObject;
const valueArgument =
valueArgumentInfo == 'STABLE_JSON'
? 'stableJson'
: valueSample.src.candidTypeObject;

return `, ${keyArgument}, ${valueArgument}`;
}

function argumentInfoArb(): fc.Arbitrary<'STABLE_JSON' | 'CANDID_TYPE_OBJECT'> {
return fc.oneof(
fc.constant<'STABLE_JSON'>('STABLE_JSON'),
fc.constant<'CANDID_TYPE_OBJECT'>('CANDID_TYPE_OBJECT')
);
}
10 changes: 5 additions & 5 deletions property_tests/tests/stable_b_tree_map/test/contains_key.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fc from 'fast-check';
import { deepEqual } from 'fast-equals';

import { getActor } from '../../../../property_tests';
import { Test } from '../../../../test';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
import { QueryMethod } from '../../../arbitraries/canister_methods/query_method_arb';
import { StableBTreeMap } from '../../../arbitraries/stable_b_tree_map_arb';
import { getActor } from 'azle/property_tests';
import { Test } from 'azle/test';
import { UniqueIdentifierArb } from 'azle/property_tests/arbitraries/unique_identifier_arb';
import { QueryMethod } from 'azle/property_tests/arbitraries/canister_methods/query_method_arb';
import { StableBTreeMap } from 'azle/property_tests/arbitraries/stable_b_tree_map_arb';

export function ContainsKeyTestArb(stableBTreeMap: StableBTreeMap) {
return fc
Expand Down
10 changes: 5 additions & 5 deletions property_tests/tests/stable_b_tree_map/test/get.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fc from 'fast-check';
import { deepEqual } from 'fast-equals';

import { StableBTreeMap } from '../../../arbitraries/stable_b_tree_map_arb';
import { getActor } from '../../../../property_tests';
import { Test } from '../../../../test';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
import { QueryMethod } from '../../../arbitraries/canister_methods/query_method_arb';
import { StableBTreeMap } from 'azle/property_tests/arbitraries/stable_b_tree_map_arb';
import { getActor } from 'azle/property_tests';
import { Test } from 'azle/test';
import { UniqueIdentifierArb } from 'azle/property_tests/arbitraries/unique_identifier_arb';
import { QueryMethod } from 'azle/property_tests/arbitraries/canister_methods/query_method_arb';

export function GetTestArb(stableBTreeMap: StableBTreeMap) {
return fc
Expand Down
10 changes: 5 additions & 5 deletions property_tests/tests/stable_b_tree_map/test/insert.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fc from 'fast-check';
import { deepEqual } from 'fast-equals';

import { StableBTreeMap } from '../../../arbitraries/stable_b_tree_map_arb';
import { getActor } from '../../../../property_tests';
import { Test } from '../../../../test';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
import { QueryMethod } from '../../../arbitraries/canister_methods/query_method_arb';
import { StableBTreeMap } from 'azle/property_tests/arbitraries/stable_b_tree_map_arb';
import { getActor } from 'azle/property_tests';
import { Test } from 'azle/test';
import { UniqueIdentifierArb } from 'azle/property_tests/arbitraries/unique_identifier_arb';
import { QueryMethod } from 'azle/property_tests/arbitraries/canister_methods/query_method_arb';

export function InsertTestArb(stableBTreeMap: StableBTreeMap) {
return fc
Expand Down
10 changes: 5 additions & 5 deletions property_tests/tests/stable_b_tree_map/test/is_empty.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fc from 'fast-check';
import { deepEqual } from 'fast-equals';

import { StableBTreeMap } from '../../../arbitraries/stable_b_tree_map_arb';
import { getActor } from '../../../../property_tests';
import { Test } from '../../../../test';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
import { QueryMethod } from '../../../arbitraries/canister_methods/query_method_arb';
import { StableBTreeMap } from 'azle/property_tests/arbitraries/stable_b_tree_map_arb';
import { getActor } from 'azle/property_tests';
import { Test } from 'azle/test';
import { UniqueIdentifierArb } from 'azle/property_tests/arbitraries/unique_identifier_arb';
import { QueryMethod } from 'azle/property_tests/arbitraries/canister_methods/query_method_arb';

export function IsEmptyTestArb(stableBTreeMap: StableBTreeMap) {
return fc
Expand Down
10 changes: 5 additions & 5 deletions property_tests/tests/stable_b_tree_map/test/items.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fc from 'fast-check';
import { deepEqual } from 'fast-equals';

import { StableBTreeMap } from '../../../arbitraries/stable_b_tree_map_arb';
import { getActor } from '../../../../property_tests';
import { Test } from '../../../../test';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
import { QueryMethod } from '../../../arbitraries/canister_methods/query_method_arb';
import { StableBTreeMap } from 'azle/property_tests/arbitraries/stable_b_tree_map_arb';
import { getActor } from 'azle/property_tests';
import { Test } from 'azle/test';
import { UniqueIdentifierArb } from 'azle/property_tests/arbitraries/unique_identifier_arb';
import { QueryMethod } from 'azle/property_tests/arbitraries/canister_methods/query_method_arb';

export function ItemsTestArb(stableBTreeMap: StableBTreeMap) {
return fc
Expand Down
10 changes: 5 additions & 5 deletions property_tests/tests/stable_b_tree_map/test/keys.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fc from 'fast-check';
import { deepEqual } from 'fast-equals';

import { StableBTreeMap } from '../../../arbitraries/stable_b_tree_map_arb';
import { getActor } from '../../../../property_tests';
import { Test } from '../../../../test';
import { StableBTreeMap } from 'azle/property_tests/arbitraries/stable_b_tree_map_arb';
import { getActor } from 'azle/property_tests';
import { Test } from 'azle/test';
import { getArrayForCandidType, getArrayStringForCandidType } from './utils';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
import { QueryMethod } from '../../../arbitraries/canister_methods/query_method_arb';
import { UniqueIdentifierArb } from 'azle/property_tests/arbitraries/unique_identifier_arb';
import { QueryMethod } from 'azle/property_tests/arbitraries/canister_methods/query_method_arb';

export function KeysTestArb(stableBTreeMap: StableBTreeMap) {
return fc
Expand Down
10 changes: 5 additions & 5 deletions property_tests/tests/stable_b_tree_map/test/len.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fc from 'fast-check';
import { deepEqual } from 'fast-equals';

import { StableBTreeMap } from '../../../arbitraries/stable_b_tree_map_arb';
import { getActor } from '../../../../property_tests';
import { Test } from '../../../../test';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
import { QueryMethod } from '../../../arbitraries/canister_methods/query_method_arb';
import { StableBTreeMap } from 'azle/property_tests/arbitraries/stable_b_tree_map_arb';
import { getActor } from 'azle/property_tests';
import { Test } from 'azle/test';
import { UniqueIdentifierArb } from 'azle/property_tests/arbitraries/unique_identifier_arb';
import { QueryMethod } from 'azle/property_tests/arbitraries/canister_methods/query_method_arb';

export function LenTestArb(stableBTreeMap: StableBTreeMap) {
return fc
Expand Down
10 changes: 5 additions & 5 deletions property_tests/tests/stable_b_tree_map/test/remove.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fc from 'fast-check';
import { deepEqual } from 'fast-equals';

import { StableBTreeMap } from '../../../arbitraries/stable_b_tree_map_arb';
import { getActor } from '../../../../property_tests';
import { Test } from '../../../../test';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
import { QueryMethod } from '../../../arbitraries/canister_methods/query_method_arb';
import { StableBTreeMap } from 'azle/property_tests/arbitraries/stable_b_tree_map_arb';
import { getActor } from 'azle/property_tests';
import { Test } from 'azle/test';
import { UniqueIdentifierArb } from 'azle/property_tests/arbitraries/unique_identifier_arb';
import { QueryMethod } from 'azle/property_tests/arbitraries/canister_methods/query_method_arb';

export function RemoveTestArb(stableBTreeMap: StableBTreeMap) {
return fc
Expand Down
12 changes: 6 additions & 6 deletions property_tests/tests/stable_b_tree_map/test/test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fc from 'fast-check';

import { runPropTests } from '../../../../property_tests';
import { CanisterArb } from '../../../arbitraries/canister_arb';
import { StableBTreeMapArb } from '../../../arbitraries/stable_b_tree_map_arb';
import { runPropTests } from 'azle/property_tests';
import { CanisterArb } from 'azle/property_tests/arbitraries/canister_arb';
import { StableBTreeMapArb } from 'azle/property_tests/arbitraries/stable_b_tree_map_arb';
import { ContainsKeyTestArb } from './contains_key';
import { GetTestArb } from './get';
import { IsEmptyTestArb } from './is_empty';
Expand Down Expand Up @@ -42,11 +42,11 @@ const StableBTreeMapTestArb = fc
]) => {
return {
globalDeclarations: [
stableBTreeMap.definition,
...stableBTreeMap.keySample.src
.variableAliasDeclarations,
...stableBTreeMap.valueSample.src
.variableAliasDeclarations
.variableAliasDeclarations,
stableBTreeMap.definition
],
queryMethods: [],
updateMethods: [
Expand All @@ -65,7 +65,7 @@ const StableBTreeMapTestArb = fc
);
}),
{
minLength: 1,
minLength: 20,
maxLength: 50
}
)
Expand Down
10 changes: 5 additions & 5 deletions property_tests/tests/stable_b_tree_map/test/values.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fc from 'fast-check';
import { deepEqual } from 'fast-equals';

import { StableBTreeMap } from '../../../arbitraries/stable_b_tree_map_arb';
import { getActor } from '../../../../property_tests';
import { Test } from '../../../../test';
import { StableBTreeMap } from 'azle/property_tests/arbitraries/stable_b_tree_map_arb';
import { getActor } from 'azle/property_tests';
import { Test } from 'azle/test';
import { getArrayForCandidType, getArrayStringForCandidType } from './utils';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
import { QueryMethod } from '../../../arbitraries/canister_methods/query_method_arb';
import { UniqueIdentifierArb } from 'azle/property_tests/arbitraries/unique_identifier_arb';
import { QueryMethod } from 'azle/property_tests/arbitraries/canister_methods/query_method_arb';

export function ValuesTestArb(stableBTreeMap: StableBTreeMap) {
return fc
Expand Down

0 comments on commit 20bc87f

Please sign in to comment.