1import gfunc from '../gfunc'; 2 3describe('when creating func instance from func names', () => { 4 it('should return func instance', () => { 5 const func = gfunc.createFuncInstance('sumSeries'); 6 expect(func).toBeTruthy(); 7 expect(func.def.name).toEqual('sumSeries'); 8 expect(func.def.params.length).toEqual(1); 9 expect(func.def.params[0].multiple).toEqual(true); 10 expect(func.def.defaultParams.length).toEqual(1); 11 }); 12 13 it('should return func instance with shortName', () => { 14 const func = gfunc.createFuncInstance('sum'); 15 expect(func).toBeTruthy(); 16 }); 17 18 it('should return func instance from funcDef', () => { 19 const func = gfunc.createFuncInstance('sum'); 20 const func2 = gfunc.createFuncInstance(func.def); 21 expect(func2).toBeTruthy(); 22 }); 23 24 it('func instance should have text representation', () => { 25 const func = gfunc.createFuncInstance('groupByNode'); 26 func.params[0] = 5; 27 func.params[1] = 'avg'; 28 func.updateText(); 29 expect(func.text).toEqual('groupByNode(5, avg)'); 30 }); 31}); 32 33function replaceVariablesDummy(str: string) { 34 // important that this does replace 35 return str.replace('asdasdas', 'asdsad'); 36} 37 38describe('when rendering func instance', () => { 39 it('should handle single metric param', () => { 40 const func = gfunc.createFuncInstance('sumSeries'); 41 expect(func.render('hello.metric', replaceVariablesDummy)).toEqual('sumSeries(hello.metric)'); 42 }); 43 44 it('should include default params if options enable it', () => { 45 const func = gfunc.createFuncInstance('scaleToSeconds', { 46 withDefaultParams: true, 47 }); 48 expect(func.render('hello', replaceVariablesDummy)).toEqual('scaleToSeconds(hello, 1)'); 49 }); 50 51 it('should handle int or interval params with number', () => { 52 const func = gfunc.createFuncInstance('movingMedian'); 53 func.params[0] = '5'; 54 expect(func.render('hello', replaceVariablesDummy)).toEqual('movingMedian(hello, 5)'); 55 }); 56 57 it('should handle int or interval params with interval string', () => { 58 const func = gfunc.createFuncInstance('movingMedian'); 59 func.params[0] = '5min'; 60 expect(func.render('hello', replaceVariablesDummy)).toEqual("movingMedian(hello, '5min')"); 61 }); 62 63 it('should never quote boolean paramater', () => { 64 const func = gfunc.createFuncInstance('sortByName'); 65 func.params[0] = '$natural'; 66 expect(func.render('hello', replaceVariablesDummy)).toEqual('sortByName(hello, $natural)'); 67 }); 68 69 it('should never quote int paramater', () => { 70 const func = gfunc.createFuncInstance('maximumAbove'); 71 func.params[0] = '$value'; 72 expect(func.render('hello', replaceVariablesDummy)).toEqual('maximumAbove(hello, $value)'); 73 }); 74 75 it('should never quote node paramater', () => { 76 const func = gfunc.createFuncInstance('aliasByNode'); 77 func.params[0] = '$node'; 78 expect(func.render('hello', replaceVariablesDummy)).toEqual('aliasByNode(hello, $node)'); 79 }); 80 81 it('should handle metric param and int param and string param', () => { 82 const func = gfunc.createFuncInstance('groupByNode'); 83 func.params[0] = 5; 84 func.params[1] = 'avg'; 85 expect(func.render('hello.metric', replaceVariablesDummy)).toEqual("groupByNode(hello.metric, 5, 'avg')"); 86 }); 87 88 it('should handle function with no metric param', () => { 89 const func = gfunc.createFuncInstance('randomWalk'); 90 func.params[0] = 'test'; 91 expect(func.render((undefined as unknown) as string, replaceVariablesDummy)).toEqual("randomWalk('test')"); 92 }); 93 94 it('should handle function multiple series params', () => { 95 const func = gfunc.createFuncInstance('asPercent'); 96 func.params[0] = '#B'; 97 expect(func.render('#A', replaceVariablesDummy)).toEqual('asPercent(#A, #B)'); 98 }); 99 100 it('should not quote variables that have numeric value', () => { 101 const func = gfunc.createFuncInstance('movingAverage'); 102 func.params[0] = '$variable'; 103 104 const replaceVariables = (str: string) => { 105 return str.replace('$variable', '60'); 106 }; 107 108 expect(func.render('metric', replaceVariables)).toBe('movingAverage(metric, $variable)'); 109 }); 110 111 it('should quote variables that have string value', () => { 112 const func = gfunc.createFuncInstance('movingAverage'); 113 func.params[0] = '$variable'; 114 115 const replaceVariables = (str: string) => { 116 return str.replace('$variable', '10min'); 117 }; 118 119 expect(func.render('metric', replaceVariables)).toBe("movingAverage(metric, '$variable')"); 120 }); 121}); 122 123describe('when requesting function definitions', () => { 124 it('should return function definitions', () => { 125 const funcIndex = gfunc.getFuncDefs('1.0'); 126 expect(Object.keys(funcIndex).length).toBeGreaterThan(8); 127 }); 128}); 129 130describe('when updating func param', () => { 131 it('should update param value and update text representation', () => { 132 const func = gfunc.createFuncInstance('summarize', { 133 withDefaultParams: true, 134 }); 135 func.updateParam('1h', 0); 136 expect(func.params[0]).toBe('1h'); 137 expect(func.text).toBe('summarize(1h, sum, false)'); 138 }); 139 140 it('should parse numbers as float', () => { 141 const func = gfunc.createFuncInstance('scale'); 142 func.updateParam('0.001', 0); 143 expect(func.params[0]).toBe('0.001'); 144 }); 145}); 146 147describe('when updating func param with optional second parameter', () => { 148 it('should update value and text', () => { 149 const func = gfunc.createFuncInstance('aliasByNode'); 150 func.updateParam('1', 0); 151 expect(func.params[0]).toBe('1'); 152 }); 153 154 it('should slit text and put value in second param', () => { 155 const func = gfunc.createFuncInstance('aliasByNode'); 156 func.updateParam('4,-5', 0); 157 expect(func.params[0]).toBe('4'); 158 expect(func.params[1]).toBe('-5'); 159 expect(func.text).toBe('aliasByNode(4, -5)'); 160 }); 161 162 it('should remove second param when empty string is set', () => { 163 const func = gfunc.createFuncInstance('aliasByNode'); 164 func.updateParam('4,-5', 0); 165 func.updateParam('', 1); 166 expect(func.params[0]).toBe('4'); 167 expect(func.params[1]).toBe(undefined); 168 expect(func.text).toBe('aliasByNode(4)'); 169 }); 170}); 171