|
| 1 | +import os |
1 | 2 | import os.path
|
2 | 3 |
|
| 4 | +import pandas as pd |
| 5 | +import pytest |
| 6 | + |
3 | 7 | from climate.temperature import DEFAULT_TEMPERATURE_DATA_PATH, Temperatures
|
| 8 | +from climate.exceptions import DataLoadingException |
| 9 | + |
| 10 | +TEST_DATA_FILE_PATH = os.path.join( |
| 11 | + DEFAULT_TEMPERATURE_DATA_PATH, |
| 12 | + 'temperature_by_country_light.csv', |
| 13 | +) |
4 | 14 |
|
5 | 15 |
|
6 | 16 | def test_temperature_data_loading():
|
7 | 17 | """Test temperature data loading"""
|
8 | 18 |
|
9 |
| - test_data_file_path = os.path.join( |
10 |
| - DEFAULT_TEMPERATURE_DATA_PATH, |
11 |
| - 'temperature_by_country_light.csv', |
12 |
| - ) |
13 |
| - t = Temperatures( |
14 |
| - data_file_path=test_data_file_path |
15 |
| - ) |
16 |
| - |
| 19 | + t = Temperatures(data_file_path=TEST_DATA_FILE_PATH) |
17 | 20 | assert t.data is not None
|
| 21 | + |
| 22 | + with pytest.raises(FileNotFoundError): |
| 23 | + t = Temperatures(data_file_path='fake/path') |
| 24 | + |
| 25 | + |
| 26 | +def test_set_country(): |
| 27 | + """Test the _set_country() private method""" |
| 28 | + |
| 29 | + # Country exists |
| 30 | + t = Temperatures(data_file_path=TEST_DATA_FILE_PATH) |
| 31 | + t._set_country('France') |
| 32 | + assert t.country == 'France' |
| 33 | + |
| 34 | + # Country does not exists |
| 35 | + with pytest.raises(KeyError): |
| 36 | + t._set_country('Utopia') |
| 37 | + |
| 38 | + |
| 39 | +def test_select(): |
| 40 | + """Test data subset selection""" |
| 41 | + |
| 42 | + t = Temperatures(data_file_path=TEST_DATA_FILE_PATH) |
| 43 | + |
| 44 | + # Select without condition (no country) |
| 45 | + t.select() |
| 46 | + assert t.country is None |
| 47 | + assert t.selection is None |
| 48 | + |
| 49 | + # Select with an existing country |
| 50 | + t.select(country='France') |
| 51 | + assert t.country == 'France' |
| 52 | + assert t.selection is not None |
| 53 | + assert isinstance(t.selection, pd.DataFrame) |
| 54 | + |
| 55 | + |
| 56 | +def test_monthly_plot(tmpdir): |
| 57 | + """Test monthly plot generation""" |
| 58 | + |
| 59 | + t = Temperatures(data_file_path=TEST_DATA_FILE_PATH) |
| 60 | + |
| 61 | + # No selection has been performed, we should raise an error |
| 62 | + with pytest.raises(DataLoadingException): |
| 63 | + t.monthly_plot() |
| 64 | + |
| 65 | + # Select a country |
| 66 | + t.select(country='France') |
| 67 | + t.monthly_plot(save=True, file_name=tmpdir.join('myplot.png')) |
| 68 | + assert len(tmpdir.listdir()) == 1 |
| 69 | + assert tmpdir.listdir()[0].basename == 'myplot.png' |
| 70 | + |
| 71 | + |
| 72 | +def test_monthly_plot_saving_with_defaults(tmpdir): |
| 73 | + """Test monthly plot generation""" |
| 74 | + |
| 75 | + t = Temperatures(data_file_path=TEST_DATA_FILE_PATH) |
| 76 | + |
| 77 | + # Test saving without file name |
| 78 | + t.select(country='France') |
| 79 | + |
| 80 | + # Go to the temporary directory |
| 81 | + os.chdir(tmpdir) |
| 82 | + t.monthly_plot(save=True) |
| 83 | + assert len(tmpdir.listdir()) == 1 |
| 84 | + assert tmpdir.listdir()[0].basename == 'france.png' |
0 commit comments