Skip to content

Commit

Permalink
fix(findSubgraph): added tests for findSubgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
asalem1 committed May 19, 2020
1 parent e86b4a0 commit ba108df
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 89 deletions.
143 changes: 63 additions & 80 deletions ui/src/variables/mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,71 +635,74 @@ export const defaultGraph: VariableNode[] = [
},
]

export const defaultVariables: Variable[] = [
{
id: '05b740973c68e000',
orgID: '05b740945a91b000',
name: 'static',
description: '',
selected: ['defbuck'],
arguments: {
type: 'constant',
values: ['beans', 'defbuck'],
},
createdAt: '2020-05-19T06:00:00.113169-07:00',
updatedAt: '2020-05-19T06:00:00.113169-07:00',
labels: [],
links: {
self: '/api/v2/variables/05b740973c68e000',
labels: '/api/v2/variables/05b740973c68e000/labels',
org: '/api/v2/orgs/05b740945a91b000',
},
status: RemoteDataState.Done,
export const defaultVariable: Variable = {
id: '05b73f4bffe8e000',
orgID: '05b73f49a1d1b000',
name: 'static',
description: '',
selected: ['defbuck'],
arguments: {type: 'constant', values: ['beans', 'defbuck']},
createdAt: '2020-05-19T05:54:20.927477-07:00',
updatedAt: '2020-05-19T05:54:20.927477-07:00',
labels: [],
links: {
self: '/api/v2/variables/05b73f4bffe8e000',
labels: '/api/v2/variables/05b73f4bffe8e000/labels',
org: '/api/v2/orgs/05b73f49a1d1b000',
},
{
id: '05b740974228e000',
orgID: '05b740945a91b000',
name: 'dependent',
description: '',
selected: [],
arguments: {
type: 'query',
values: {
query:
'from(bucket: v.static)\n |> range(start: v.timeRangeStart, stop: v.timeRangeStop)\n |> filter(fn: (r) => r["_measurement"] == "test")\n |> keep(columns: ["container_name"])\n |> rename(columns: {"container_name": "_value"})\n |> last()\n |> group()',
language: 'flux',
results: [],
},
},
createdAt: '2020-05-19T06:00:00.136597-07:00',
updatedAt: '2020-05-19T06:00:00.136597-07:00',
labels: [],
links: {
self: '/api/v2/variables/05b740974228e000',
labels: '/api/v2/variables/05b740974228e000/labels',
org: '/api/v2/orgs/05b740945a91b000',
status: RemoteDataState.Done,
}

export const associatedVariable: Variable = {
id: '05b740974228e000',
orgID: '05b740945a91b000',
name: 'dependent',
description: '',
selected: [],
arguments: {
type: 'query',
values: {
query:
'from(bucket: v.static)\n |> range(start: v.timeRangeStart, stop: v.timeRangeStop)\n |> filter(fn: (r) => r["_measurement"] == "test")\n |> keep(columns: ["container_name"])\n |> rename(columns: {"container_name": "_value"})\n |> last()\n |> group()',
language: 'flux',
results: [],
},
status: RemoteDataState.Loading,
},
{
orgID: '',
id: 'timeRangeStart',
name: 'timeRangeStart',
arguments: {
type: 'system',
values: [
[
{
magnitude: 1,
unit: 'h',
},
],
createdAt: '2020-05-19T06:00:00.136597-07:00',
updatedAt: '2020-05-19T06:00:00.136597-07:00',
labels: [],
links: {
self: '/api/v2/variables/05b740974228e000',
labels: '/api/v2/variables/05b740974228e000/labels',
org: '/api/v2/orgs/05b740945a91b000',
},
status: RemoteDataState.Loading,
}

export const timeRangeStartVariable: Variable = {
orgID: '',
id: 'timeRangeStart',
name: 'timeRangeStart',
arguments: {
type: 'system',
values: [
[
{
magnitude: 1,
unit: 'h',
},
],
},
status: RemoteDataState.Done,
labels: [],
selected: [],
],
},
status: RemoteDataState.Done,
labels: [],
selected: [],
}

export const defaultVariables: Variable[] = [
defaultVariable,
associatedVariable,
timeRangeStartVariable,
{
orgID: '',
id: 'timeRangeStop',
Expand All @@ -725,23 +728,3 @@ export const defaultVariables: Variable[] = [
selected: [],
},
]

export const defaultVariable: Variable[] = [
{
id: '05b73f4bffe8e000',
orgID: '05b73f49a1d1b000',
name: 'static',
description: '',
selected: ['defbuck'],
arguments: {type: 'constant', values: ['beans', 'defbuck']},
createdAt: '2020-05-19T05:54:20.927477-07:00',
updatedAt: '2020-05-19T05:54:20.927477-07:00',
labels: [],
links: {
self: '/api/v2/variables/05b73f4bffe8e000',
labels: '/api/v2/variables/05b73f4bffe8e000/labels',
org: '/api/v2/orgs/05b73f49a1d1b000',
},
status: RemoteDataState.Done,
},
]
59 changes: 51 additions & 8 deletions ui/src/variables/utils/hydrateVars.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
// Utils
import {ValueFetcher} from 'src/variables/utils/ValueFetcher'
import {hydrateVars} from 'src/variables/utils/hydrateVars'
import {
hydrateVars,
createVariableGraph,
findSubgraph,
} from 'src/variables/utils/hydrateVars'

// Mocks
import {createVariable} from 'src/variables/mocks'
import {
createVariable,
associatedVariable,
defaultVariable,
defaultVariables,
timeRangeStartVariable,
} from 'src/variables/mocks'

// Types
import {Variable, CancellationError, RemoteDataState} from 'src/types'
Expand Down Expand Up @@ -114,16 +124,16 @@ describe('hydrate vars', () => {
// within hydrateVars as not belonging to the graph
expect(
actual.filter(v => v.id === 'a')[0].arguments.values.results
).toBeFalsy()
).toEqual([])
expect(
actual.filter(v => v.id === 'b')[0].arguments.values.results
).toBeFalsy()
).toEqual([])
expect(
actual.filter(v => v.id === 'c')[0].arguments.values.results
).toBeFalsy()
).toEqual([])
expect(
actual.filter(v => v.id === 'd')[0].arguments.values.results
).toBeFalsy()
).toEqual([])

expect(
actual.filter(v => v.id === 'e')[0].arguments.values.results
Expand Down Expand Up @@ -241,11 +251,9 @@ describe('hydrate vars', () => {
actual.filter(v => v.id === 'a')[0].arguments.values.results
).toEqual(['aVal'])
expect(actual.filter(v => v.id === 'a')[0].selected).toEqual(['aVal'])

expect(actual.filter(v => v.id === 'b')[0].arguments.values).toEqual({
k: 'v',
})
expect(actual.filter(v => v.id === 'b')[0].selected).toEqual(['k'])
})

// This ensures that the update of a dependant variable updates the
Expand Down Expand Up @@ -319,3 +327,38 @@ describe('hydrate vars', () => {
cancel()
})
})

describe('findSubgraph', () => {
test('should return the update variable with all associated parents', async () => {
const variableGraph = await createVariableGraph(defaultVariables)
const actual = await findSubgraph(variableGraph, [defaultVariable])
// returns the single subgraph result
expect(actual.length).toEqual(1)
const [subgraph] = actual
// expect the subgraph to return the passed in variable
expect(subgraph.variable).toEqual(defaultVariable)
// expect the parent to be returned with the returning variable
expect(subgraph.parents[0].variable).toEqual(associatedVariable)
})
test('should return the variable with no parents when no association exists', async () => {
const a = createVariable('a', 'f(x: v.b)')
const variableGraph = await createVariableGraph([...defaultVariables, a])
const actual = await findSubgraph(variableGraph, [a])
expect(actual.length).toEqual(1)
const [subgraph] = actual
// expect the subgraph to return the passed in variable
expect(subgraph.variable).toEqual(a)
// expect the parent to be returned with the returning variable
expect(subgraph.parents).toEqual([])
})
test('should return the update default (timeRange) variable with associated parents', async () => {
const variableGraph = await createVariableGraph(defaultVariables)
const actual = await findSubgraph(variableGraph, [timeRangeStartVariable])
expect(actual.length).toEqual(1)
const [subgraph] = actual
// expect the subgraph to return the passed in variable
expect(subgraph.variable).toEqual(timeRangeStartVariable)
// expect the parent to be returned with the returning variable
expect(subgraph.parents[0].variable).toEqual(associatedVariable)
})
})
2 changes: 1 addition & 1 deletion ui/src/variables/utils/hydrateVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const collectAncestors = (
- The node for one of the passed variables depends on this node
*/
const findSubgraph = (
export const findSubgraph = (
graph: VariableNode[],
variables: Variable[]
): VariableNode[] => {
Expand Down

0 comments on commit ba108df

Please sign in to comment.