Skip to content
This repository has been archived by the owner on Apr 23, 2022. It is now read-only.

Add tests #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -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
83 changes: 75 additions & 8 deletions tests/test_temperature.py
Original file line number Diff line number Diff line change
@@ -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'
19 changes: 19 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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