44"""Test for Config"""
55import pathlib
66import re
7+
8+ # pylint: disable = no-name-in-module
9+ import tomllib
710from typing import Any , Dict , List , Optional , Set
811
912import pytest
10-
11- # pylint: disable = no-name-in-module
12- import toml
1313from pydantic import BaseModel , StrictBool , StrictFloat , StrictInt
1414
1515from frequenz .sdk .config import Config
@@ -54,29 +54,32 @@ def config_file(self, tmp_path: pathlib.Path) -> pathlib.Path:
5454 file_path .write_text (TestConfig .conf_content )
5555 return file_path
5656
57- def test_get (self , config_file : pathlib .Path ) -> None :
57+ @pytest .fixture ()
58+ def conf_vars (self , config_file : pathlib .Path ) -> dict [str , Any ]:
59+ """Load the created test config file."""
60+ with config_file .open ("rb" ) as file :
61+ return tomllib .load (file )
62+
63+ def test_get (self , conf_vars : dict [str , Any ]) -> None :
5864 """Test get function"""
59- conf_vars = toml .load (config_file )
6065 config = Config (conf_vars = conf_vars )
6166
6267 assert config .get ("logging_lvl" ) == "DEBUG"
6368 assert config .get ("var1" ) == "1"
6469 assert config .get ("var2" ) is None
6570 assert config .get ("var2" , default = 0 ) == 0
6671
67- def test_getitem (self , config_file : pathlib . Path ) -> None :
72+ def test_getitem (self , conf_vars : dict [ str , Any ] ) -> None :
6873 """Test getitem function"""
69- conf_vars = toml .load (config_file )
7074 config = Config (conf_vars = conf_vars )
7175
7276 assert config ["logging_lvl" ] == "DEBUG"
7377 assert config ["var1" ] == "1"
7478 with pytest .raises (KeyError , match = "Unknown config name var2" ):
7579 assert config ["var2" ]
7680
77- def test_contains (self , config_file : pathlib . Path ) -> None :
81+ def test_contains (self , conf_vars : dict [ str , Any ] ) -> None :
7882 """Test contains function"""
79- conf_vars = toml .load (config_file )
8083 config = Config (conf_vars = conf_vars )
8184
8285 assert "logging_lvl" in config
@@ -111,11 +114,10 @@ def test_contains(self, config_file: pathlib.Path) -> None:
111114 ],
112115 )
113116 def test_get_as_success (
114- self , key : str , expected_type : Any , value : Any , config_file : pathlib . Path
117+ self , key : str , expected_type : Any , value : Any , conf_vars : dict [ str , Any ]
115118 ) -> None :
116119 """Test get_as function with proper arguments"""
117120
118- conf_vars = toml .load (config_file )
119121 config = Config (conf_vars = conf_vars )
120122 result = config .get_as (key , expected_type )
121123 assert result == value
@@ -132,10 +134,9 @@ def test_get_as_success(
132134 ],
133135 )
134136 def test_get_as_validation_error (
135- self , key : str , expected_type : Any , config_file : pathlib . Path
137+ self , key : str , expected_type : Any , conf_vars : dict [ str , Any ]
136138 ) -> None :
137139 """Test get_as function which raise ValidationError"""
138- conf_vars = toml .load (config_file )
139140 config = Config (conf_vars = conf_vars )
140141
141142 err_msg = (
@@ -158,11 +159,10 @@ def test_get_dict_values_success(
158159 key_prefix : str ,
159160 expected_values_type : Any ,
160161 value : Any ,
161- config_file : pathlib . Path ,
162+ conf_vars : dict [ str , Any ] ,
162163 ) -> None :
163164 """Test get_as function with proper arguments"""
164165
165- conf_vars = toml .load (config_file )
166166 config = Config (conf_vars = conf_vars )
167167 result = config .get_dict (key_prefix , expected_values_type )
168168 assert result == value
@@ -176,11 +176,10 @@ def test_get_dict_values_success(
176176 ],
177177 )
178178 def test_get_dict_success (
179- self , key_prefix : str , expected_values_type : Any , config_file : pathlib . Path
179+ self , key_prefix : str , expected_values_type : Any , conf_vars : dict [ str , Any ]
180180 ) -> None :
181181 """Test get_as function with proper arguments"""
182182
183- conf_vars = toml .load (config_file )
184183 config = Config (conf_vars = conf_vars )
185184
186185 err_msg_re = (
0 commit comments