From cb56846f7f9418735fefd6472b3f8aa4f14c5a96 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Tue, 19 Jul 2022 12:08:58 +0200 Subject: [PATCH] Chore: extract variant creation arbitrary. --- src/test/arbitraries.test.ts | 66 +++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/src/test/arbitraries.test.ts b/src/test/arbitraries.test.ts index e6def58648..1bacdcbd69 100644 --- a/src/test/arbitraries.test.ts +++ b/src/test/arbitraries.test.ts @@ -2,7 +2,7 @@ import fc, { Arbitrary } from 'fast-check'; import { ALL_OPERATORS } from '../lib/util/constants'; import { ClientFeatureSchema } from '../lib/openapi/spec/client-feature-schema'; -import { WeightType } from '../lib/types/model'; +import { IVariant, WeightType } from '../lib/types/model'; import { FeatureStrategySchema } from '../lib/openapi/spec/feature-strategy-schema'; import { ConstraintSchema } from 'lib/openapi/spec/constraint-schema'; @@ -92,6 +92,43 @@ export const strategies = (): Arbitrary => ), ); +export const variant = (): Arbitrary => + fc.record({ + name: urlFriendlyString(), + weight: fc.nat({ max: 1000 }), + weightType: fc.constant(WeightType.VARIABLE), + stickiness: fc.constant('default'), + payload: fc.option( + fc.oneof( + fc.record({ + type: fc.constant('json'), + value: fc.json(), + }), + fc.record({ + type: fc.constant('csv'), + value: fc.array(fc.lorem()).map((words) => words.join(',')), + }), + fc.record({ + type: fc.constant('string'), + value: fc.string(), + }), + ), + ), + }); + +export const variants = (): Arbitrary => + fc + .uniqueArray(variant(), { + maxLength: 1000, + selector: (variantInstance) => variantInstance.name, + }) + .map((allVariants) => + allVariants.map((variantInstance) => ({ + ...variantInstance, + weight: Math.round(1000 / allVariants.length), + })), + ); + export const clientFeature = (name?: string): Arbitrary => fc.record( { @@ -111,32 +148,7 @@ export const clientFeature = (name?: string): Arbitrary => stale: fc.boolean(), impressionData: fc.option(fc.boolean()), strategies: strategies(), - variants: fc.array( - fc.record({ - name: urlFriendlyString(), - weight: fc.nat({ max: 1000 }), - weightType: fc.constant(WeightType.VARIABLE), - stickiness: fc.constant('default'), - payload: fc.option( - fc.oneof( - fc.record({ - type: fc.constant('json'), - value: fc.json(), - }), - fc.record({ - type: fc.constant('csv'), - value: fc - .array(fc.lorem()) - .map((words) => words.join(',')), - }), - fc.record({ - type: fc.constant('string'), - value: fc.string(), - }), - ), - ), - }), - ), + variants: variants(), }, { requiredKeys: ['name', 'enabled', 'project', 'strategies'] }, );