Coverage for intelligence_toolkit/tests/unit/detect_case_patterns/test_graph_functions.py: 100%

28 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-10-16 13:41 -0300

1# Copyright (c) 2024 Microsoft Corporation. All rights reserved. 

2# Licensed under the MIT license. See LICENSE file in the project. 

3# 

4 

5import networkx as nx 

6import pandas as pd 

7import pytest 

8 

9from intelligence_toolkit.detect_case_patterns.config import ( 

10 min_edge_weight, 

11 missing_edge_prop, 

12) 

13from intelligence_toolkit.detect_case_patterns.graph_functions import ( 

14 convert_edge_df_to_graph, 

15 create_edge_df_from_atts, 

16) 

17 

18 

19def test_convert_edge_df_to_graph_default(): 

20 # Create a sample edge DataFrame 

21 edge_df = pd.DataFrame( 

22 { 

23 "source": ["A", "B", "C", "A"], 

24 "target": ["B", "C", "D", "A"], 

25 "weight": [1, 2, 3, 3], 

26 } 

27 ) 

28 

29 # Call the function 

30 G, lcc = convert_edge_df_to_graph(edge_df) 

31 

32 # Check if the graph is of type nx.Graph 

33 assert isinstance(G, nx.Graph) 

34 

35 # Check if the largest connected component is correct 

36 expected_lcc = {"A", "B", "C", "D"} 

37 assert set(lcc) == expected_lcc 

38 

39 

40def test_convert_edge_df_to_graph_more_nodes(): 

41 # Create a sample edge DataFrame 

42 edge_df = pd.DataFrame( 

43 { 

44 "source": [1, 2, 3, 4], 

45 "target": [2, 3, 4, 5], 

46 "weight": [0.5, 0.6, 0.7, 0.8], 

47 } 

48 ) 

49 

50 # Call the function 

51 G, lcc = convert_edge_df_to_graph(edge_df) 

52 

53 # Check if the graph is of type nx.Graph 

54 assert isinstance(G, nx.Graph) 

55 

56 # Check if the largest connected component is correct 

57 expected_lcc = {1, 2, 3, 4, 5} 

58 assert set(lcc) == expected_lcc 

59 

60 

61@pytest.fixture() 

62def sample_input_data(): 

63 # Generate sample input data 

64 all_atts = ["A", "B", "C", "D"] 

65 pdf = pd.DataFrame( 

66 {"Full Attribute": [["A", "B"], ["B", "C"], ["C", "B"], ["B", "A"]]} 

67 ) 

68 return all_atts, pdf 

69 

70 

71def test_create_edge_df_from_atts(sample_input_data): 

72 # Call the function with the sample input data 

73 all_atts, pdf = sample_input_data 

74 edge_df = create_edge_df_from_atts( 

75 all_atts, pdf, min_edge_weight, missing_edge_prop 

76 ) 

77 

78 # Assert that the output DataFrame has the correct columns 

79 assert set(edge_df.columns) == {"edge", "count", "source", "target", "weight"} 

80 

81 # Assert that the output DataFrame is not empty 

82 assert not edge_df.empty 

83 

84 assert not (edge_df["weight"] > 1).any() 

85 # assert not (edge_df["weight"] < 0.001).any()