Skip to content

Commit

Permalink
feat: add jest test cases for new logic's utils, functions and proces…
Browse files Browse the repository at this point in the history
…sors - dashboardVariables
  • Loading branch information
SagarRajput-7 committed Dec 12, 2024
1 parent 421d355 commit f098518
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { onUpdateVariableNode } from '../util';
import { onUpdateVariableNodeMock } from './mock';

describe('dashboardVariables - utilities and processors', () => {
const { graph, topologicalOrder } = onUpdateVariableNodeMock;

test.each([
{
scenario: 'root element',
nodeToUpdate: 'deployment_environment',
expected: [
'deployment_environment',
'service_name',
'endpoint',
'http_status_code',
],
},
{
scenario: 'middle child',
nodeToUpdate: 'k8s_node_name',
expected: ['k8s_node_name', 'k8s_namespace_name'],
},
{
scenario: 'leaf element',
nodeToUpdate: 'http_status_code',
expected: ['http_status_code'],
},
{
scenario: 'node not in graph',
nodeToUpdate: 'unknown',
expected: [],
},
{
scenario: 'node not in topological order',
nodeToUpdate: 'unknown',
expected: [],
},
])(
'should update variable node when $scenario',
({ nodeToUpdate, expected }) => {
const updatedVariables: string[] = [];

onUpdateVariableNode(nodeToUpdate, graph, topologicalOrder, (node) =>
updatedVariables.push(node),
);

expect(updatedVariables).toEqual(expected);
},
);

it('should return empty array when topological order is empty', () => {
const updatedVariables: string[] = [];

onUpdateVariableNode('http_status_code', graph, [], (node) =>
updatedVariables.push(node),
);

expect(updatedVariables).toEqual([]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
export const checkAPIInvocationMock = {
variablesToGetUpdated: [],
variableData: {
name: 'k8s_namespace_name',
key: '937ecbae-b24b-4d6d-8cc4-5d5b8d53569b',
customValue: '',
description: '',
id: '937ecbae-b24b-4d6d-8cc4-5d5b8d53569b',
modificationUUID: '8ad2442d-8b4d-4c64-848e-af847d1d0eec',
multiSelect: false,
order: 7,
queryValue:
"SELECT JSONExtractString(labels, 'k8s_namespace_name') AS k8s_namespace_name\nFROM signoz_metrics.distributed_time_series_v4_1day\nWHERE metric_name = 'k8s_pod_cpu_time' AND JSONExtractString(labels, 'k8s_cluster_name') = {{.k8s_cluster_name}} AND JSONExtractString(labels, 'k8s_node_name') IN {{.k8s_node_name}}\nGROUP BY k8s_namespace_name",
showALLOption: false,
sort: 'DISABLED',
textboxValue: '',
type: 'QUERY',
selectedValue: 'saasmonitor',
allSelected: false,
},
parentDependencyGraph: {
deployment_environment: [],
service_name: ['deployment_environment'],
endpoint: ['deployment_environment', 'service_name'],
http_status_code: ['endpoint'],
k8s_cluster_name: [],
environment: [],
k8s_node_name: ['k8s_cluster_name'],
k8s_namespace_name: ['k8s_cluster_name', 'k8s_node_name'],
},
};

export const onUpdateVariableNodeMock = {
nodeToUpdate: 'deployment_environment',
graph: {
deployment_environment: ['service_name', 'endpoint'],
service_name: ['endpoint'],
endpoint: ['http_status_code'],
http_status_code: [],
k8s_cluster_name: ['k8s_node_name', 'k8s_namespace_name'],
environment: [],
k8s_node_name: ['k8s_namespace_name'],
k8s_namespace_name: [],
},
topologicalOrder: [
'deployment_environment',
'k8s_cluster_name',
'environment',
'service_name',
'k8s_node_name',
'endpoint',
'k8s_namespace_name',
'http_status_code',
],
callback: jest.fn(),
};
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const convertVariablesToDbFormat = (
}, {});

const getDependentVariables = (queryValue: string): string[] => {
console.log('getDependentVariables', queryValue);
const variableRegexPattern = /\{\{\s*?\.([^\s}]+)\s*?\}\}/g;

const matches = queryValue.match(variableRegexPattern);
Expand All @@ -46,6 +47,7 @@ export type VariableGraph = Record<string, string[]>;
export const buildDependencies = (
variables: IDashboardVariable[],
): VariableGraph => {
console.log('buildDependencies', variables);
const graph: VariableGraph = {};

// Initialize empty arrays for all variables first
Expand Down Expand Up @@ -78,6 +80,7 @@ export const buildDependencies = (
export const buildDependencyGraph = (
dependencies: VariableGraph,
): { order: string[]; graph: VariableGraph } => {
console.log('buildDependencyGraph', dependencies);
const inDegree: Record<string, number> = {};
const adjList: VariableGraph = {};

Expand Down Expand Up @@ -124,6 +127,7 @@ export const onUpdateVariableNode = (
topologicalOrder: string[],
callback: (node: string) => void,
): void => {
console.log('onUpdateVariableNode', nodeToUpdate, graph, topologicalOrder);
const visited = new Set<string>();

// Start processing from the node to update
Expand All @@ -141,6 +145,7 @@ export const onUpdateVariableNode = (
export const buildParentDependencyGraph = (
graph: VariableGraph,
): VariableGraph => {
console.log('buildParentDependencyGraph', graph);
const parentGraph: VariableGraph = {};

// Initialize empty arrays for all nodes
Expand Down

0 comments on commit f098518

Please sign in to comment.