1+ import pytest
2+ import json
3+ from src .settings_manager import SettingsManager
4+
5+ # --- FIXTURES ---
6+
7+ @pytest .fixture
8+ def valid_config_data ():
9+ """Dati validi per i test."""
10+ return {
11+ "active_strategy" : "EMA" ,
12+ "strategies_params" : {
13+ "EMA" : {"short" : 10 , "long" : 20 },
14+ "RSI" : {"period" : 14 }
15+ }
16+ }
17+
18+ @pytest .fixture
19+ def temp_config_file (tmp_path , valid_config_data ):
20+ """
21+ Crea un file JSON reale in una directory temporanea isolata.
22+ Ritorna il percorso completo (Path object).
23+ """
24+ d = tmp_path / "config"
25+ d .mkdir ()
26+ f = d / "strategies.json"
27+
28+ # Scriviamo dati validi iniziali
29+ with open (f , "w" ) as file :
30+ json .dump (valid_config_data , file )
31+
32+ return f
33+
34+ # --- TESTS ---
35+
36+ def test_init_fail_if_missing (tmp_path ):
37+ """Deve crashare se il file non esiste (nessun default automatico)."""
38+ fake_path = tmp_path / "non_existent.json"
39+
40+ # Verifichiamo che alzi l'eccezione giusta
41+ with pytest .raises (FileNotFoundError ):
42+ SettingsManager (config_path = str (fake_path ))
43+
44+ def test_load_success (temp_config_file ):
45+ """Deve caricare correttamente un file valido."""
46+ # Passiamo il path temporaneo al manager
47+ manager = SettingsManager (config_path = str (temp_config_file ))
48+
49+ # Test active strategy
50+ assert manager .get_active_strategy_name () == "EMA"
51+
52+ # Test params
53+ params = manager .get_strategy_params ()
54+ assert params ["short" ] == 10
55+ assert params ["long" ] == 20
56+
57+ def test_missing_active_strategy_key (temp_config_file ):
58+ """Deve alzare ValueError se manca la chiave 'active_strategy'."""
59+ # Sovrascriviamo il file con dati incompleti
60+ bad_data = {"strategies_params" : {}}
61+ with open (temp_config_file , "w" ) as f :
62+ json .dump (bad_data , f )
63+
64+ manager = SettingsManager (config_path = str (temp_config_file ))
65+
66+ with pytest .raises (ValueError , match = "active_strategy" ):
67+ manager .get_active_strategy_name ()
68+
69+ def test_missing_strategy_params (temp_config_file ):
70+ """Deve alzare ValueError se la strategia attiva non ha parametri definiti."""
71+ # Configuriamo EMA come attiva, ma non mettiamo i parametri per EMA
72+ bad_data = {
73+ "active_strategy" : "EMA" ,
74+ "strategies_params" : {
75+ "RSI" : {"period" : 14 }
76+ # Manca EMA!
77+ }
78+ }
79+ with open (temp_config_file , "w" ) as f :
80+ json .dump (bad_data , f )
81+
82+ manager = SettingsManager (config_path = str (temp_config_file ))
83+
84+ with pytest .raises (ValueError , match = "non trovati" ):
85+ manager .get_strategy_params ()
86+
87+ def test_save_config (temp_config_file ):
88+ """Deve salvare correttamente le modifiche su disco."""
89+ manager = SettingsManager (config_path = str (temp_config_file ))
90+
91+ # 1. Modifichiamo la configurazione
92+ new_config = {
93+ "active_strategy" : "RSI" ,
94+ "strategies_params" : {
95+ "RSI" : {"period" : 99 }
96+ }
97+ }
98+
99+ # 2. Salviamo
100+ manager .save_config (new_config )
101+
102+ # 3. Rileggiamo direttamente dal file (bypassando il manager per sicurezza)
103+ with open (temp_config_file , "r" ) as f :
104+ content = json .load (f )
105+
106+ assert content ["active_strategy" ] == "RSI"
107+ assert content ["strategies_params" ]["RSI" ]["period" ] == 99
0 commit comments