topology-utils-test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { fromJS } from 'immutable';
  2. describe('TopologyUtils', () => {
  3. let TopologyUtils;
  4. let nodes;
  5. const nodeSets = {
  6. initial4: {
  7. edges: fromJS({
  8. 'n1-n3': {id: 'n1-n3', source: 'n1', target: 'n3'},
  9. 'n1-n4': {id: 'n1-n4', source: 'n1', target: 'n4'},
  10. 'n2-n4': {id: 'n2-n4', source: 'n2', target: 'n4'}
  11. }),
  12. nodes: fromJS({
  13. n1: {id: 'n1'},
  14. n2: {id: 'n2'},
  15. n3: {id: 'n3'},
  16. n4: {id: 'n4'}
  17. })
  18. },
  19. removeEdge24: {
  20. edges: fromJS({
  21. 'n1-n3': {id: 'n1-n3', source: 'n1', target: 'n3'},
  22. 'n1-n4': {id: 'n1-n4', source: 'n1', target: 'n4'}
  23. }),
  24. nodes: fromJS({
  25. n1: {id: 'n1'},
  26. n2: {id: 'n2'},
  27. n3: {id: 'n3'},
  28. n4: {id: 'n4'}
  29. })
  30. },
  31. removeNode2: {
  32. edges: fromJS({
  33. 'n1-n3': {id: 'n1-n3', source: 'n1', target: 'n3'},
  34. 'n1-n4': {id: 'n1-n4', source: 'n1', target: 'n4'}
  35. }),
  36. nodes: fromJS({
  37. n1: {id: 'n1'},
  38. n3: {id: 'n3'},
  39. n4: {id: 'n4'}
  40. })
  41. },
  42. removeNode23: {
  43. edges: fromJS({
  44. 'n1-n4': {id: 'n1-n4', source: 'n1', target: 'n4'}
  45. }),
  46. nodes: fromJS({
  47. n1: {id: 'n1'},
  48. n4: {id: 'n4'}
  49. })
  50. },
  51. single3: {
  52. edges: fromJS({}),
  53. nodes: fromJS({
  54. n1: {id: 'n1'},
  55. n2: {id: 'n2'},
  56. n3: {id: 'n3'}
  57. })
  58. },
  59. singlePortrait: {
  60. edges: fromJS({
  61. 'n1-n4': {id: 'n1-n4', source: 'n1', target: 'n4'}
  62. }),
  63. nodes: fromJS({
  64. n1: {id: 'n1'},
  65. n2: {id: 'n2'},
  66. n3: {id: 'n3'},
  67. n4: {id: 'n4'},
  68. n5: {id: 'n5'}
  69. })
  70. }
  71. };
  72. beforeEach(() => {
  73. TopologyUtils = require('../topology-utils');
  74. });
  75. it('sets node degrees', () => {
  76. nodes = TopologyUtils.updateNodeDegrees(
  77. nodeSets.initial4.nodes,
  78. nodeSets.initial4.edges
  79. ).toJS();
  80. expect(nodes.n1.degree).toEqual(2);
  81. expect(nodes.n2.degree).toEqual(1);
  82. expect(nodes.n3.degree).toEqual(1);
  83. expect(nodes.n4.degree).toEqual(2);
  84. nodes = TopologyUtils.updateNodeDegrees(
  85. nodeSets.removeEdge24.nodes,
  86. nodeSets.removeEdge24.edges
  87. ).toJS();
  88. expect(nodes.n1.degree).toEqual(2);
  89. expect(nodes.n2.degree).toEqual(0);
  90. expect(nodes.n3.degree).toEqual(1);
  91. expect(nodes.n4.degree).toEqual(1);
  92. nodes = TopologyUtils.updateNodeDegrees(
  93. nodeSets.single3.nodes,
  94. nodeSets.single3.edges
  95. ).toJS();
  96. expect(nodes.n1.degree).toEqual(0);
  97. expect(nodes.n2.degree).toEqual(0);
  98. expect(nodes.n3.degree).toEqual(0);
  99. });
  100. describe('buildTopologyCacheId', () => {
  101. it('should generate a cache ID', () => {
  102. const fun = TopologyUtils.buildTopologyCacheId;
  103. expect(fun()).toEqual('');
  104. expect(fun('test')).toEqual('test');
  105. expect(fun(undefined, 'test')).toEqual('');
  106. expect(fun('test', {a: 1})).toEqual('test{"a":1}');
  107. });
  108. });
  109. describe('filterHiddenTopologies', () => {
  110. it('should filter out empty topos that set hide_if_empty=true', () => {
  111. const topos = [
  112. {hide_if_empty: true, id: 'a', stats: {filtered_nodes: 0, node_count: 0}},
  113. {hide_if_empty: true, id: 'b', stats: {filtered_nodes: 0, node_count: 1}},
  114. {hide_if_empty: true, id: 'c', stats: {filtered_nodes: 1, node_count: 0}},
  115. {hide_if_empty: false, id: 'd', stats: {filtered_nodes: 0, node_count: 0}}
  116. ];
  117. const res = TopologyUtils.filterHiddenTopologies(topos);
  118. expect(res.map(t => t.id)).toEqual(['b', 'c', 'd']);
  119. });
  120. });
  121. });