99
1010from postal_regex import analytics
1111
12+
1213def test_record_validation_and_load_stats (monkeypatch ):
1314 """
1415 Test that recording validations correctly creates and updates the stats file.
1516 """
1617 temp_stats_file = Path .home () / ".postalregex_stats_temp.json"
17- monkeypatch .setattr (analytics , ' STATS_FILE' , temp_stats_file )
18+ monkeypatch .setattr (analytics , " STATS_FILE" , temp_stats_file )
1819
1920 # Ensure the file doesn't exist initially
2021 if temp_stats_file .exists ():
2122 temp_stats_file .unlink ()
2223
2324 analytics .record_validation ("US" , is_valid = True )
24-
25+
2526 with open (temp_stats_file , "r" ) as f :
2627 stats = json .load (f )
27-
28+
2829 assert stats ["US" ]["valid" ] == 1
2930 assert stats ["US" ]["invalid" ] == 0
3031
3132 analytics .record_validation ("US" , is_valid = False )
32-
33+
3334 with open (temp_stats_file , "r" ) as f :
3435 stats = json .load (f )
35-
36+
3637 assert stats ["US" ]["valid" ] == 1
3738 assert stats ["US" ]["invalid" ] == 1
3839
3940 analytics .record_validation ("CA" , is_valid = True )
40-
41+
4142 with open (temp_stats_file , "r" ) as f :
4243 stats = json .load (f )
43-
44+
4445 assert stats ["CA" ]["valid" ] == 1
45- assert "invalid" in stats ["CA" ] # an 'invalid' key should be created
46+ assert "invalid" in stats ["CA" ] # an 'invalid' key should be created
4647
4748 temp_stats_file .unlink ()
4849
50+
4951def test_get_stats (monkeypatch ):
5052 """
5153 Test that get_stats correctly reads and returns data.
5254 """
5355 temp_stats_file = Path .home () / ".postalregex_stats_temp.json"
54- monkeypatch .setattr (analytics , ' STATS_FILE' , temp_stats_file )
56+ monkeypatch .setattr (analytics , " STATS_FILE" , temp_stats_file )
5557
5658 # 1. Test when file doesn't exist
5759 if temp_stats_file .exists ():
@@ -61,21 +63,22 @@ def test_get_stats(monkeypatch):
6163 dummy_data = {"US" : {"valid" : 5 , "invalid" : 1 }}
6264 with open (temp_stats_file , "w" ) as f :
6365 json .dump (dummy_data , f )
64-
66+
6567 assert analytics .get_stats () == dummy_data
66-
68+
6769 temp_stats_file .unlink ()
6870
71+
6972def test_reset_stats (monkeypatch ):
7073 """
7174 Test that reset_stats correctly deletes the statistics file.
7275 """
7376 temp_stats_file = Path .home () / ".postalregex_stats_temp.json"
74- monkeypatch .setattr (analytics , ' STATS_FILE' , temp_stats_file )
77+ monkeypatch .setattr (analytics , " STATS_FILE" , temp_stats_file )
7578
7679 with open (temp_stats_file , "w" ) as f :
7780 json .dump ({"US" : {"valid" : 1 , "invalid" : 0 }}, f )
78-
81+
7982 assert temp_stats_file .exists ()
8083
8184 analytics .reset_stats ()
0 commit comments