1010
1111import pytest
1212
13+ from aiida .common import exceptions
1314from aiida .orm import Data
14- from aiida .orm .utils .node import load_node_class
15+ from aiida .orm .utils .node import (
16+ get_type_string_from_class ,
17+ is_valid_node_type_string ,
18+ load_node_class ,
19+ )
1520
1621
1722def test_load_node_class_fallback ():
@@ -23,3 +28,85 @@ def test_load_node_class_fallback():
2328 with pytest .warns (UserWarning ):
2429 loaded_class = load_node_class ('__main__.SubData.' )
2530 assert loaded_class == Data
31+
32+ # Test invalid type string without trailing dot.
33+ with pytest .raises (exceptions .DbContentError , match = 'invalid' ):
34+ load_node_class ('data.dict' )
35+
36+
37+ def test_load_node_class_with_node_prefix ():
38+ """Test the behavior of load_node_class with node prefix."""
39+ # Test node prefix removal
40+ with pytest .warns (UserWarning ):
41+ loaded_class = load_node_class ('node.data.dict.' )
42+ assert loaded_class == Data
43+
44+ # Test node prefix with invalid subtype
45+ with pytest .warns (UserWarning ):
46+ loaded_class = load_node_class ('node.invalid.type.' )
47+ assert loaded_class == Data
48+
49+
50+ def test_load_node_class_with_process_prefix ():
51+ """Test the behavior of load_node_class with process prefix."""
52+ from aiida .orm .nodes .process .calculation .calculation import CalculationNode
53+ from aiida .orm .nodes .process .process import ProcessNode
54+
55+ # Test process prefix with invalid type - should return CalculationNode
56+ loaded_class = load_node_class ('process.calculation.invalid.' )
57+ assert loaded_class == CalculationNode
58+
59+ # Test process prefix with empty subtype - should return ProcessNode
60+ loaded_class = load_node_class ('process.' )
61+ assert loaded_class == ProcessNode
62+
63+
64+ def test_load_node_class_with_data_prefix ():
65+ """Test the behavior of load_node_class with data prefix."""
66+ # Test data prefix with removeprefix
67+ with pytest .warns (UserWarning ):
68+ loaded_class = load_node_class ('data.dict.' )
69+ assert loaded_class == Data
70+
71+ # Test data prefix with empty subtype
72+ with pytest .warns (UserWarning , match = 'unknown type string `data.`' ):
73+ loaded_class = load_node_class ('data.' )
74+ assert loaded_class == Data
75+
76+
77+ def test_load_node_class_empty_string ():
78+ """Test that an empty string returns the base `Node` class."""
79+ from aiida .orm import Node
80+
81+ loaded_class = load_node_class ('' )
82+ assert loaded_class == Node
83+
84+
85+ def test_get_type_string_from_class ():
86+ """Test conversion from class module/name to type string."""
87+ # Test with internal data class
88+ type_string = get_type_string_from_class ('aiida.orm.nodes.data.dict' , 'Dict' )
89+ assert type_string == 'data.core.dict.Dict.' # Changed to include 'core'
90+
91+ # Test with Node class (should return empty string)
92+ type_string = get_type_string_from_class ('aiida.orm.nodes.node' , 'Node' )
93+ assert type_string == ''
94+
95+ # Test with process class
96+ type_string = get_type_string_from_class ('aiida.orm.nodes.process.process' , 'ProcessNode' )
97+ assert type_string == 'process.ProcessNode.'
98+
99+
100+ def test_is_valid_node_type_string ():
101+ """Test validation of node type strings."""
102+ # Test valid cases
103+ assert is_valid_node_type_string ('' )
104+ assert is_valid_node_type_string ('data.dict.Dict.' )
105+
106+ # Test invalid cases
107+ assert not is_valid_node_type_string ('data.dict' ) # No trailing dot
108+ assert not is_valid_node_type_string ('data.' ) # Just one dot
109+
110+ # Test raise_on_false parameter
111+ with pytest .raises (exceptions .DbContentError , match = 'invalid' ):
112+ is_valid_node_type_string ('data.dict' , raise_on_false = True )
0 commit comments