Coverage for tests / unit / views / test_networkx.py: 100%

30 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-16 21:32 +0000

1import networkx as nx 

2from pytest import raises 

3 

4from graphable.graph import Graph 

5from graphable.graphable import Graphable 

6 

7 

8class TestNetworkX: 

9 def test_to_networkx(self): 

10 a = Graphable("A") 

11 b = Graphable("B") 

12 a.add_tag("source") 

13 g = Graph() 

14 g.add_edge(a, b) 

15 

16 dg = g.to_networkx() 

17 

18 assert isinstance(dg, nx.DiGraph) 

19 assert "A" in dg.nodes 

20 assert "B" in dg.nodes 

21 assert dg.has_edge("A", "B") 

22 

23 # Check metadata 

24 assert dg.nodes["A"]["reference"] == "A" 

25 assert dg.nodes["A"]["tags"] == ["source"] 

26 

27 def test_to_networkx_missing_library(self, monkeypatch): 

28 # Temporarily hide networkx to test ImportError 

29 import builtins 

30 

31 real_import = builtins.__import__ 

32 

33 def mock_import(name, *args, **kwargs): 

34 if name == "networkx": 

35 raise ImportError("mocked error") 

36 return real_import(name, *args, **kwargs) 

37 

38 monkeypatch.setattr(builtins, "__import__", mock_import) 

39 

40 g = Graph() 

41 with raises(ImportError) as excinfo: 

42 g.to_networkx() 

43 assert "networkx is required" in str(excinfo.value)