import pytest import json import tempfile import os from slic.utils.config import Config def make_temp_json(data: dict) -> str: # Write a dictionary to a temporary JSON file. tmp = tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".json") json.dump(data, tmp) tmp.close() return tmp.name def test_config_with_nested_and_list_data(): json_data = { "user": "charlie", "active": True, "volume": 0.85, "preferences": { "theme": "dark", "notifications": { "email": True, "sms": False } }, "projects": [ {"name": "Project A", "status": "active"}, {"name": "Project B", "status": "archived"} ], "last_login": None, "tags": ["dev", "test", "prod"] } path = make_temp_json(json_data) try: cfg = Config(path) # Top-level checks assert hasattr(cfg, "user") assert cfg.user == "charlie" assert hasattr(cfg, "active") assert cfg.active is True assert hasattr(cfg, "volume") assert isinstance(cfg.volume, float) assert cfg.volume == 0.85 assert hasattr(cfg, "last_login") assert cfg.last_login is None assert hasattr(cfg, "tags") assert isinstance(cfg.tags, list) assert cfg.tags[1] == "test" # Nested dict assert isinstance(cfg.preferences, dict) assert cfg.preferences["theme"] == "dark" assert cfg.preferences["notifications"]["sms"] is False # List of dicts assert isinstance(cfg.projects, list) assert cfg.projects[0]["name"] == "Project A" assert cfg.projects[1]["status"] == "archived" finally: os.remove(path) def test_config_with_strange_and_edge_keys(): json_data = { "": "empty key", "123": "numeric string", "true": False, "None": None, "spaces in key": "with spaces", "[]": "brackets key", "1": "digit string", "class": "reserved word", "user.name": "dot notation", "UPPERCASE": "yes", "camelCase": "also yes", "hyphen-key": "dangerous" } path = make_temp_json(json_data) try: cfg = Config(path) # All keys should be accessible via getattr assert getattr(cfg, "") == "empty key" assert getattr(cfg, "123") == "numeric string" assert getattr(cfg, "true") is False assert getattr(cfg, "None") is None assert getattr(cfg, "spaces in key") == "with spaces" assert getattr(cfg, "[]") == "brackets key" assert getattr(cfg, "1") == "digit string" assert getattr(cfg, "class") == "reserved word" assert getattr(cfg, "user.name") == "dot notation" assert getattr(cfg, "UPPERCASE") == "yes" assert getattr(cfg, "camelCase") == "also yes" assert getattr(cfg, "hyphen-key") == "dangerous" # Check that dot-access fails for bad names for key in ["string", "spaces", "value"]: with pytest.raises(AttributeError): _ = getattr(cfg, key) finally: os.remove(path)