Coverage for test\test_config.py: 100%

50 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-21 20:31 +0100

1# test_config.py 

2import pytest 

3from nexios.config import MakeConfig, set_config, get_config, DEFAULT_CONFIG 

4from typing import Dict, Any, Callable 

5 

6 

7# Test MakeConfig class 

8def test_makeconfig_initialization(): 

9 # Test basic initialization 

10 config = MakeConfig({"debug": True, "db": {"host": "localhost"}}) 

11 assert config.debug is True 

12 assert config.db.host == "localhost" 

13 

14 # Test with defaults 

15 config = MakeConfig( 

16 {"debug": False}, defaults={"debug": True, "db": {"port": 5432}} 

17 ) 

18 assert config.debug is False # Overrides default 

19 assert config.db.port == 5432 # From defaults 

20 

21 # Test immutable config 

22 config = MakeConfig({"debug": True}, immutable=True) 

23 with pytest.raises(AttributeError): 

24 config.debug = False 

25 

26 

27def test_makeconfig_nested_access(): 

28 config = MakeConfig({"app": {"name": "MyApp", "settings": {"timeout": 30}}}) 

29 

30 # Test attribute access 

31 assert config.app.name == "MyApp" 

32 assert config.app.settings.timeout == 30 

33 

34 # Test dictionary-style access 

35 assert config["app.name"] == "MyApp" 

36 assert config["app.settings.timeout"] == 30 

37 assert config["nonexistent.key"] is None 

38 

39 

40def test_makeconfig_conversion_methods(): 

41 config = MakeConfig({"debug": True, "db": {"host": "localhost", "port": 5432}}) 

42 

43 # Test to_dict 

44 config_dict = config.to_dict() 

45 assert config_dict == {"debug": True, "db": {"host": "localhost", "port": 5432}} 

46 

47 # Test to_json 

48 config_json = config.to_json() 

49 assert '"debug": true' in config_json 

50 assert '"host": "localhost"' in config_json 

51 

52 

53def test_makeconfig_repr(): 

54 config = MakeConfig({"debug": True}) 

55 assert repr(config) == "MakeConfig({'debug': True})" 

56 

57 

58def test_config_immutability(): 

59 # Create mutable config 

60 config = MakeConfig({"debug": True}) 

61 set_config(config) 

62 

63 # Should be able to modify 

64 get_config().debug = False 

65 assert get_config().debug is False 

66 

67 # Create immutable config 

68 immutable_config = MakeConfig({"debug": True}, immutable=True) 

69 set_config(immutable_config) 

70 

71 # Should not be able to modify 

72 with pytest.raises(AttributeError): 

73 get_config().debug = False 

74 

75 

76# Test edge cases 

77def test_makeconfig_edge_cases(): 

78 # Test empty config 

79 empty_config = MakeConfig({}) 

80 assert empty_config.to_dict() == {} 

81 

82 # Test None values 

83 config_with_none = MakeConfig({"debug": None}) 

84 assert config_with_none.debug is None 

85 

86 # Test non-dict values 

87 config = MakeConfig({"version": "1.0"}) 

88 assert config.version == "1.0" 

89 

90 

91def test_makeconfig_nested_immutability(): 

92 # Test nested immutability 

93 config = MakeConfig({"db": {"host": "localhost"}}, immutable=True) 

94 

95 with pytest.raises(AttributeError): 

96 config.db.host = "127.0.0.1"