From 125068f2192b40478e7db1aa0ba5c8b9b8b6ef50 Mon Sep 17 00:00:00 2001 From: Julien Maupetit Date: Tue, 4 Jul 2017 15:02:17 +0200 Subject: [PATCH 1/3] Add first draft gitlab-ci configuration --- .gitlab-ci.yml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 .gitlab-ci.yml 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 From 762103b445f8dc2f9503d2ebe80c9a283870526f Mon Sep 17 00:00:00 2001 From: Julien Maupetit Date: Tue, 4 Jul 2017 17:54:08 +0200 Subject: [PATCH 2/3] Add tests for utils --- tests/test_utils.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 tests/test_utils.py 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 From e392f72d00570688bb8564af6fc8e870e2eb49b5 Mon Sep 17 00:00:00 2001 From: Julien Maupetit Date: Tue, 4 Jul 2017 22:26:33 +0200 Subject: [PATCH 3/3] Add more tests for the temperatures module --- tests/test_temperature.py | 83 +++++++++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 8 deletions(-) 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'