Coverage for tests/test_data_loading.py: 100%

39 statements  

« prev     ^ index     » next       coverage.py v7.15.0, created at 2026-07-05 04:36 +0000

1import pytest 

2 

3from sankey_cashflow import read_csv_as_df, fetch_data, DataRow 

4 

5 

6class TestReadCsvAsDf: 

7 

8 def test_reads_single_csv(self): 

9 df = read_csv_as_df('sample_data/expenses.csv') 

10 assert len(df) == 33 

11 assert list(df.columns) == [ 

12 'Date', 'Category', 'Description', 'Tags', 'Comments', 'Source', 'Target', 

13 'Type', 'Distribution', 'Amount', 'Sales Tax', 'Tips' 

14 ] 

15 

16 def test_concatenates_multiple_csvs(self, tmp_path): 

17 cols = 'Date,Category,Description,Tags,Comments,Source,Target,Type,Distribution,Amount,Sales Tax,Tips\n' 

18 row = '1/1/23,Groceries,Store,,,,,,,10.00,,\n' 

19 file_a = tmp_path / 'a.csv' 

20 file_b = tmp_path / 'b.csv' 

21 file_a.write_text(cols + row) 

22 file_b.write_text(cols + row + row) 

23 df = read_csv_as_df([str(file_a), str(file_b)]) 

24 assert len(df) == 3 

25 

26 def test_missing_file_raises(self): 

27 with pytest.raises(Exception): 

28 read_csv_as_df('does_not_exist.csv') 

29 

30 def test_non_csv_file_raises(self, tmp_path): 

31 bad_file = tmp_path / 'data.txt' 

32 bad_file.write_text('not a csv') 

33 with pytest.raises(Exception): 

34 read_csv_as_df(str(bad_file)) 

35 

36 

37class TestFetchData: 

38 

39 def test_loads_sample_labels_and_transactions(self, default_app_settings): 

40 src_target, df = fetch_data(default_app_settings) 

41 assert isinstance(src_target, list) 

42 assert len(src_target) > 0 

43 assert 'Category Name' in src_target[0] 

44 assert len(df) == 33 

45 

46 def test_amount_column_normalized_to_float(self, default_app_settings): 

47 _, df = fetch_data(default_app_settings) 

48 assert all(isinstance(v, float) for v in df['Amount']) 

49 # First data row in sample_data/expenses.csv is "$4,000.00" 

50 assert df.iloc[0]['Amount'] == 4000.0 

51 

52 def test_header_matches_data_row_schema(self, default_app_settings): 

53 _, df = fetch_data(default_app_settings) 

54 is_valid = DataRow.validate(df.columns.to_list(), True) 

55 assert is_valid[0] is True