Coverage for tests / unit / rdflibplus / test_datasetview_invert.py: 100%

57 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-11-26 21:25 +0000

1"""Test DatasetView.invert() method.""" 

2 

3from rdflib import Dataset, Namespace 

4 

5from pythinfer.rdflibplus import DatasetView 

6 

7 

8def test_invert_returns_complementary_view() -> None: 

9 """Test that invert() returns a view with all excluded graphs.""" 

10 ds = Dataset() 

11 EX = Namespace("http://example.org/") 

12 

13 # Create three graphs 

14 g1 = ds.graph(EX.graph1) 

15 g1.add((EX.s1, EX.p1, EX.o1)) 

16 

17 g2 = ds.graph(EX.graph2) 

18 g2.add((EX.s2, EX.p2, EX.o2)) 

19 

20 g3 = ds.graph(EX.graph3) 

21 g3.add((EX.s3, EX.p3, EX.o3)) 

22 

23 # Create view with graph1 and graph2 

24 view = DatasetView(ds, [EX.graph1, EX.graph2]) 

25 assert len(view) == 2 

26 

27 # Invert should give us only graph3 

28 inverted = view.invert() 

29 assert len(inverted) == 1 

30 assert EX.graph3 in inverted.included_graph_ids 

31 assert EX.graph1 not in inverted.included_graph_ids 

32 assert EX.graph2 not in inverted.included_graph_ids 

33 

34 

35def test_invert_twice_returns_original() -> None: 

36 """Test that inverting twice gives back the original view.""" 

37 ds = Dataset() 

38 EX = Namespace("http://example.org/") 

39 

40 g1 = ds.graph(EX.graph1) 

41 g1.add((EX.s1, EX.p1, EX.o1)) 

42 

43 g2 = ds.graph(EX.graph2) 

44 g2.add((EX.s2, EX.p2, EX.o2)) 

45 

46 g3 = ds.graph(EX.graph3) 

47 g3.add((EX.s3, EX.p3, EX.o3)) 

48 

49 # Create view with graph1 and graph2 

50 view = DatasetView(ds, [EX.graph1, EX.graph2]) 

51 

52 # Invert twice 

53 double_inverted = view.invert().invert() 

54 

55 # Should have same graphs as original (order may differ) 

56 assert set(double_inverted.included_graph_ids) == set(view.included_graph_ids) 

57 assert len(double_inverted) == len(view) 

58 

59 

60def test_invert_empty_view() -> None: 

61 """Test that inverting an empty view returns all graphs.""" 

62 ds = Dataset() 

63 EX = Namespace("http://example.org/") 

64 

65 g1 = ds.graph(EX.graph1) 

66 g1.add((EX.s1, EX.p1, EX.o1)) 

67 

68 g2 = ds.graph(EX.graph2) 

69 g2.add((EX.s2, EX.p2, EX.o2)) 

70 

71 # Create empty view 

72 view = DatasetView(ds, []) 

73 assert len(view) == 0 

74 

75 # Invert should give us all graphs 

76 inverted = view.invert() 

77 assert len(inverted) == 2 

78 assert EX.graph1 in inverted.included_graph_ids 

79 assert EX.graph2 in inverted.included_graph_ids 

80 

81 

82def test_invert_preserves_store() -> None: 

83 """Test that inverted view shares the same underlying store.""" 

84 ds = Dataset() 

85 EX = Namespace("http://example.org/") 

86 

87 g1 = ds.graph(EX.graph1) 

88 g1.add((EX.s1, EX.p1, EX.o1)) 

89 

90 g2 = ds.graph(EX.graph2) 

91 g2.add((EX.s2, EX.p2, EX.o2)) 

92 

93 view = DatasetView(ds, [EX.graph1]) 

94 inverted = view.invert() 

95 

96 # Verify they share the same store 

97 assert view.store is inverted.store 

98 assert view.store is ds.store 

99 

100 # Modify through inverted view should affect original 

101 inverted.graph(EX.graph2).add((EX.s3, EX.p3, EX.o3)) 

102 assert len(ds.graph(EX.graph2)) == 2