Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: node filtering edge rules also filtering out edges #234

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions lib/network-query/__tests__/filter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,26 @@ describe('filter', () => {
});

describe('Edges', () => {
it('can filter edges by type', () => {

it('can filter nodes by edge and trim orphaned edges', () => {
const filterConfig = {
rules: [
generateRuleConfig('edge', {
type: 'band',
operator: operators.EXISTS,
}),
]
};

const filter = getFilter(filterConfig);
const result = filter(network);
const names = result.nodes.map(
(node) => node[entityAttributesProperty].name,
)
expect(names).toEqual(['William', 'Theodore']);
expect(result.edges.length).toEqual(2); // should be band edge AND friend edge between valid nodes
})
it('can filter nodes by edge type', () => {
const filterConfig = {
rules: [
generateRuleConfig('edge', {
Expand All @@ -262,7 +281,7 @@ describe('filter', () => {
(node) => node[entityAttributesProperty].name,
);
expect(names).toEqual(['William', 'Theodore', 'Rufus']);
expect(result.edges.length).toEqual(3);
expect(result.edges.length).toEqual(4);
});

it.todo('can filter edges by type (not)');
Expand All @@ -282,7 +301,7 @@ describe('filter', () => {
const filter = getFilter(filterConfig);

const result = filter(network);
expect(result.edges.length).toEqual(2);
expect(result.edges.length).toEqual(4);
});
});
});
Expand Down
10 changes: 5 additions & 5 deletions lib/network-query/__tests__/rules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ describe('rules', () => {
const { nodeNames, edges } = runRuleHelper(ruleConfig);

expect(nodeNames).toEqual(['William', 'Theodore', 'Rufus']);
expect(edges.length).toEqual(2);
expect(edges.length).toEqual(3);
});

it('NOT_EXISTS', () => {
Expand All @@ -248,7 +248,7 @@ describe('rules', () => {
'Phone Box',
'Pillar Box',
]);
expect(edges.length).toEqual(2);
expect(edges.length).toEqual(3);
});

describe('attribute rules', () => {
Expand Down Expand Up @@ -281,7 +281,7 @@ describe('rules', () => {
const { nodeNames, edges } = runRuleHelper(ruleConfig);

expect(nodeNames).toEqual(['Theodore', 'Rufus']);
expect(edges.length).toEqual(1);
expect(edges.length).toEqual(2);
});

it('GREATER_THAN', () => {
Expand All @@ -307,7 +307,7 @@ describe('rules', () => {
const { nodeNames, edges } = runRuleHelper(ruleConfig);

expect(nodeNames).toEqual(['Theodore', 'Rufus']);
expect(edges.length).toEqual(1);
expect(edges.length).toEqual(2);
});

it('GREATER_THAN_OR_EQUAL', () => {
Expand All @@ -333,7 +333,7 @@ describe('rules', () => {
const { nodeNames, edges } = runRuleHelper(ruleConfig);

expect(nodeNames).toEqual(['Theodore', 'Rufus']);
expect(edges.length).toEqual(1);
expect(edges.length).toEqual(2);
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion lib/network-query/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { entityPrimaryKeyProperty } from '@codaco/shared-consts';
import { getRule } from './rules';

// remove orphaned edges
const trimEdges = (network) => {
export const trimEdges = (network) => {
const uids = new Set(
network.nodes.map((node) => node[entityPrimaryKeyProperty]),
);
Expand Down
15 changes: 12 additions & 3 deletions lib/network-query/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {
entityAttributesProperty,
entityPrimaryKeyProperty,
} from '@codaco/shared-consts';
import { operators } from './predicate';
import predicate from './predicate';
import { trimEdges } from './filter';
import predicate, { operators } from './predicate';

const singleEdgeRule =
({ type, attribute, operator, value: other }) =>
Expand Down Expand Up @@ -62,6 +62,8 @@ const singleNodeRule =

// Reduce edges to any that match the rule
// Filter nodes by the resulting edges
// Remove orphaned edges from original edges
// Return filtered nodes and valid, non-orphaned edges
const edgeRule =
({ attribute, operator, type, value: other }) =>
(nodes, edges) => {
Expand Down Expand Up @@ -93,9 +95,16 @@ const edgeRule =
edgeMap.includes(node[entityPrimaryKeyProperty]),
);

// remove orphaned edges

const validEdges = trimEdges({
nodes: filteredNodes,
edges: edges,
}).edges;

return {
nodes: filteredNodes,
edges: filteredEdges,
edges: validEdges,
};
};

Expand Down
Loading