Skip to content

Commit

Permalink
done tests
Browse files Browse the repository at this point in the history
Signed-off-by: rashidakanchwala <[email protected]>
  • Loading branch information
rashidakanchwala committed Nov 14, 2024
1 parent 030bd8e commit dc1b05f
Show file tree
Hide file tree
Showing 5 changed files with 418 additions and 18 deletions.
28 changes: 27 additions & 1 deletion package/tests/test_services/test_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,32 @@
{"node_1": {}, "node_2": {}},
["a", "b"],
),
(
# Case where if two layers e.g. `int` and `primary` layers share the same dependencies, they get sorted alphabetically.
"""
node_1(layer=raw) -> node_3(layer=int)
node_2(layer=raw) -> node_4(layer=primary)
node_3(layer=int) -> node_5(layer=feature)
node_4(layer=primary) -> node_6(layer=feature)
""",
{
"node_1": {"id": "node_1", "layer": "raw"},
"node_2": {"id": "node_2", "layer": "raw"},
"node_3": {"id": "node_3", "layer": "int"},
"node_4": {"id": "node_4", "layer": "primary"},
"node_5": {"id": "node_5", "layer": "feature"},
"node_6": {"id": "node_6", "layer": "feature"},
},
{
"node_1": {"node_3"},
"node_2": {"node_4"},
"node_3": {"node_5"},
"node_4": {"node_6"},
"node_5": set(),
"node_6": set(),
},
["raw", "int", "primary", "feature"],
),
],
)
def test_sort_layers(graph_schema, nodes, node_dependencies, expected):
Expand All @@ -170,7 +196,7 @@ def test_sort_layers(graph_schema, nodes, node_dependencies, expected):
for node_id, node_dict in nodes.items()
}
sorted_layers = sort_layers(nodes, node_dependencies)
assert sorted(sorted_layers) == sorted(expected), graph_schema
assert sorted_layers == expected, graph_schema


def test_sort_layers_should_return_empty_list_on_cyclic_layers(mocker):
Expand Down
78 changes: 61 additions & 17 deletions src/actions/graph.test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
import { createStore } from 'redux';
import reducer from '../reducers';
import { mockState } from '../utils/state.mock';
import { calculateGraph, updateGraph } from './graph';
import { getGraphInput } from '../selectors/layout';
import { prepareState } from '../utils/state.mock';
import spaceflights from '../utils/data/spaceflights.mock.json';
import spaceflightsReordered from '../utils/data/spaceflights_reordered.mock.json';
import { toggleModularPipelinesExpanded } from '../actions/modular-pipelines';

describe('graph actions', () => {
const getMockState = (data) =>
prepareState({
data,
beforeLayoutActions: [
() =>
toggleModularPipelinesExpanded(['data_science', 'data_processing']),
],
});

describe('calculateGraph', () => {
it('returns updateGraph action if input is falsey', () => {
expect(calculateGraph(null)).toEqual(updateGraph(null));
});

it('sets loading to true immediately', () => {
const store = createStore(reducer, mockState.spaceflights);
const store = createStore(reducer, getMockState(spaceflights));
expect(store.getState().loading.graph).not.toBe(true);
calculateGraph(getGraphInput(mockState.spaceflights))(store.dispatch);
calculateGraph(getGraphInput(getMockState(spaceflights)))(store.dispatch);
expect(store.getState().loading.graph).toBe(true);
});

it('sets loading to false and graph visibility to true after finishing calculation', () => {
const store = createStore(reducer, mockState.spaceflights);
return calculateGraph(getGraphInput(mockState.spaceflights))(
const store = createStore(reducer, getMockState(spaceflights));
return calculateGraph(getGraphInput(getMockState(spaceflights)))(
store.dispatch
).then(() => {
const state = store.getState();
Expand All @@ -29,19 +41,51 @@ describe('graph actions', () => {
});

it('calculates a graph', () => {
const state = Object.assign({}, mockState.spaceflights);
delete state.graph;
const store = createStore(reducer, state);
const initialState = { ...getMockState(spaceflights), graph: {} };
const store = createStore(reducer, initialState);
expect(store.getState().graph).toEqual({});
return calculateGraph(getGraphInput(state))(store.dispatch).then(() => {
expect(store.getState().graph).toEqual(
expect.objectContaining({
nodes: expect.any(Array),
edges: expect.any(Array),
size: expect.any(Object),
})
);
});
return calculateGraph(getGraphInput(initialState))(store.dispatch).then(
() => {
expect(store.getState().graph).toEqual(
expect.objectContaining({
nodes: expect.any(Array),
edges: expect.any(Array),
size: expect.any(Object),
})
);
}
);
});

it('compares deterministic flowchart of two differently ordered same projects', () => {
const store1 = createStore(reducer, getMockState(spaceflights));
const store2 = createStore(reducer, getMockState(spaceflightsReordered));

return calculateGraph(getGraphInput(getMockState(spaceflights)))(
store1.dispatch
)
.then(() =>
calculateGraph(getGraphInput(getMockState(spaceflightsReordered)))(
store2.dispatch
)
)
.then(() => {
// Get node coordinates for both graphs
const graph1Coords = store1.getState().graph.nodes.map((node) => ({
id: node.id,
x: node.x,
y: node.y,
}));

const graph2Coords = store2.getState().graph.nodes.map((node) => ({
id: node.id,
x: node.x,
y: node.y,
}));

// Verify coordinates consistency between both graphs
expect(graph1Coords).toEqual(expect.arrayContaining(graph2Coords));
});
});
});
});
17 changes: 17 additions & 0 deletions src/store/normalize-data.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import normalizeData, { createInitialPipelineState } from './normalize-data';
import spaceflights from '../utils/data/spaceflights.mock.json';
import spaceflightsReordered from '../utils/data/spaceflights_reordered.mock.json';

const initialState = createInitialPipelineState();

Expand Down Expand Up @@ -90,4 +91,20 @@ describe('normalizeData', () => {
expect(node).toHaveProperty('name');
});
});

it('should have identical nodes and edges, in the same order, regardless of the different ordering from the api', () => {
// Normalize both datasets
const initialState = normalizeData(spaceflights, true);
const reorderedState = normalizeData(spaceflightsReordered, true);

// Compare nodes and edges by converting to JSON for deep equality
// Directly compare specific properties of nodes and edges, ensuring order and content
expect(initialState.node.ids).toEqual(reorderedState.node.ids);
expect(initialState.node.name).toEqual(reorderedState.node.name);
expect(initialState.node.type).toEqual(reorderedState.node.type);

expect(initialState.edge.ids).toEqual(reorderedState.edge.ids);
expect(initialState.edge.sources).toEqual(reorderedState.edge.sources);
expect(initialState.edge.targets).toEqual(reorderedState.edge.targets);
});
});
Loading

0 comments on commit dc1b05f

Please sign in to comment.