diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..ddd51eb --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,28 @@ +image: tailordev/pandas + +# This folder is cached between builds +# http://docs.gitlab.com/ce/ci/yaml/README.html#cache +cache: + paths: + - ~/.cache/pip/ + +# Install the project and dependencies +before_script: + # Print out python version for debugging + - python -V + - pip install -r requirements-dev.txt + - python setup.py install + +stages: + - lint + - test + +flake8: + stage: lint + script: + flake8 + +test climate: + stage: test + script: + pytest diff --git a/tests/test_temperature.py b/tests/test_temperature.py index 6cc434a..5594943 100644 --- a/tests/test_temperature.py +++ b/tests/test_temperature.py @@ -1,17 +1,84 @@ +import os import os.path +import pandas as pd +import pytest + from climate.temperature import DEFAULT_TEMPERATURE_DATA_PATH, Temperatures +from climate.exceptions import DataLoadingException + +TEST_DATA_FILE_PATH = os.path.join( + DEFAULT_TEMPERATURE_DATA_PATH, + 'temperature_by_country_light.csv', +) def test_temperature_data_loading(): """Test temperature data loading""" - test_data_file_path = os.path.join( - DEFAULT_TEMPERATURE_DATA_PATH, - 'temperature_by_country_light.csv', - ) - t = Temperatures( - data_file_path=test_data_file_path - ) - + t = Temperatures(data_file_path=TEST_DATA_FILE_PATH) assert t.data is not None + + with pytest.raises(FileNotFoundError): + t = Temperatures(data_file_path='fake/path') + + +def test_set_country(): + """Test the _set_country() private method""" + + # Country exists + t = Temperatures(data_file_path=TEST_DATA_FILE_PATH) + t._set_country('France') + assert t.country == 'France' + + # Country does not exists + with pytest.raises(KeyError): + t._set_country('Utopia') + + +def test_select(): + """Test data subset selection""" + + t = Temperatures(data_file_path=TEST_DATA_FILE_PATH) + + # Select without condition (no country) + t.select() + assert t.country is None + assert t.selection is None + + # Select with an existing country + t.select(country='France') + assert t.country == 'France' + assert t.selection is not None + assert isinstance(t.selection, pd.DataFrame) + + +def test_monthly_plot(tmpdir): + """Test monthly plot generation""" + + t = Temperatures(data_file_path=TEST_DATA_FILE_PATH) + + # No selection has been performed, we should raise an error + with pytest.raises(DataLoadingException): + t.monthly_plot() + + # Select a country + t.select(country='France') + t.monthly_plot(save=True, file_name=tmpdir.join('myplot.png')) + assert len(tmpdir.listdir()) == 1 + assert tmpdir.listdir()[0].basename == 'myplot.png' + + +def test_monthly_plot_saving_with_defaults(tmpdir): + """Test monthly plot generation""" + + t = Temperatures(data_file_path=TEST_DATA_FILE_PATH) + + # Test saving without file name + t.select(country='France') + + # Go to the temporary directory + os.chdir(tmpdir) + t.monthly_plot(save=True) + assert len(tmpdir.listdir()) == 1 + assert tmpdir.listdir()[0].basename == 'france.png' diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..eb63de0 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,19 @@ +import os.path + +from climate.utils import slugify + + +def test_slugify_with_special_chars(): + """Test slugify() with punctuation marks""" + + text = 'France (Europe)' + expected = 'france-europe' + assert slugify(text) == expected + + +def test_slugify_with_unicode_chars(): + """Test slugify() with unicode chars""" + + text = 'Ă…land' + expected = 'aland' + assert slugify(text) == expected