From 2df09dab61f3ec17faebeaa8611f72cc1d250281 Mon Sep 17 00:00:00 2001 From: Ake Tangkananond Date: Thu, 28 Jun 2018 23:46:36 +0700 Subject: [PATCH] test multiple recursion of the same level --- src/costAnalysis.test.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/costAnalysis.test.js b/src/costAnalysis.test.js index 2796833..1b2026b 100644 --- a/src/costAnalysis.test.js +++ b/src/costAnalysis.test.js @@ -48,6 +48,9 @@ const typeDefs = ` second (limit: Int): Second @cost( multipliers: ["limit"], useMultipliers: true, complexity: ${secondComplexity} ) + anotherSecond (limit: Int): Second @cost( + multipliers: ["limit"], useMultipliers: true, complexity: ${secondComplexity} + ) } type Second implements BasicInterface { @@ -163,6 +166,37 @@ describe('Cost analysis Tests', () => { expect(visitor.operationMultipliers).toEqual([limit, limit, limit]) }) + test('should consider multiple recursive cost computation', () => { + const limit = 10 + const ast = parse(` + query { + first(limit: ${limit}) { + second(limit: ${limit}) { + int + } + anotherSecond(limit: ${limit}) { + int + } + } + } + `) + + const context = new ValidationContext(schema, ast, typeInfo) + const visitor = new CostAnalysis(context, { + maximumCost: 10000 + }) + + visit(ast, visitWithTypeInfo(typeInfo, visitor)) + + const firstCost = limit * firstComplexity + const secondCost = limit * limit * secondComplexity + const anotherSecondCost = limit * limit * secondComplexity + + const result = firstCost + secondCost + anotherSecondCost + expect(visitor.cost).toEqual(result) + // expect(visitor.operationMultipliers).toEqual([limit, limit]) + }) + test(`should consider recursive cost computation + empty multipliers array when the node is of kind operation definition`, () => { const limit = 10 const ast = parse(`