Skip to content

Commit

Permalink
Merge pull request #13 from khaeru/travis-config
Browse files Browse the repository at this point in the history
Closes #10.
  • Loading branch information
khaeru committed Mar 1, 2016
2 parents de28c42 + a3d8279 commit cfa59af
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 113 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Python temporary files
*.egg-info/
*.pyc
__pycache__

# Test-related files
.coverage
data/*.gdx
data/*.lst
47 changes: 41 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,50 @@ python:
- "3.4"
- "3.5"

before_install: ./travis.sh before_install
env:
global:
- MINICONDA_URL=http://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
matrix:
- GAMS=24.4
GAMS_URL=http://d37drm4t2jghv5.cloudfront.net/distributions/24.4.3/linux/linux_x64_64_sfx.exe
GAMS_API=api

install: python setup.py install --force
before_install:
# Install Miniconda
- wget -N $MINICONDA_URL -P $HOME/.cache/
- bash $HOME/.cache/Miniconda3-latest-Linux-x86_64.sh -b -f -p $HOME/miniconda
- export PATH="$HOME/miniconda/bin:$PATH"
- hash -r
- conda config --set always_yes yes --set changeps1 no
- conda update -q conda
- conda info -a
# Install GAMS
- pushd $HOME
- wget -N $GAMS_URL -P .cache/gams/$GAMS/
- unzip -q .cache/gams/$GAMS/*.exe
- cd gams*
- ./gamsinst -a
- export PATH="`pwd`:$PATH"
- export GAMS_API_PATH=`readlink -f apifiles/Python/$GAMS_API`
- popd

install:
# Prepare the Anaconda environment
- conda env create --file data/test-requirements-$TRAVIS_PYTHON_VERSION.yml
- source activate test_env
# Install the GAMS GDX Python API into the environment
- cd $GAMS_API_PATH
- python gdxsetup.py install
- cd -
# Install PyGDX
- python setup.py install

cache:
directories:
- $HOME/gams
- $HOME/virtualenv/python$TRAVIS_PYTHON_VERSION*/lib/python*/site-packages
- $HOME/.cache
- $HOME/miniconda/pkgs/*.tar.bz2

before_script: export PATH=$HOME/gams:$PATH
script: py.test gdx --cov=gdx

script: ./travis.sh script
after_success:
- coveralls
8 changes: 8 additions & 0 deletions data/test-requirements-3.4.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: test_env
dependencies:
- python=3.4
- pytest
- xarray
- pip:
- coveralls
- pytest-cov
8 changes: 8 additions & 0 deletions data/test-requirements-3.5.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: test_env
dependencies:
- python=3.5
- pytest
- xarray
- pip:
- coveralls
- pytest-cov
154 changes: 87 additions & 67 deletions gdx/test/test_gdx.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,47 @@
from collections import OrderedDict
from unittest import TestCase

import pytest

import numpy as np
import gdx


URI = 'data/tests.gdx'
@pytest.fixture(scope='session')
def rawgdx(request):
""" Return the path to a GDX file for running tests.
Invoking this fixture causes the file data/tests.gms to be processed to
data/tests.gdx. When the fixture is finalized (torn down), the GDX file
is deleted.
"""
import os
import subprocess
os.chdir('data')
args = ['gams', 'tests.gms']
try: # Python 3.5 and later
subprocess.run(args)
except AttributeError: # Python 3.4 and earlier
subprocess.call(args)
os.remove('tests.lst')
os.chdir('..')

def finalize():
os.remove('data/tests.gdx')
request.addfinalizer(finalize)
return 'data/tests.gdx'


@pytest.fixture(scope='class')
def gdxfile(rawgdx):
"""A gdx.File fixture."""
return gdx.File(rawgdx)


@pytest.fixture(scope='class')
def gdxfile_implicit(rawgdx):
"""A gdx.File fixture, instantiated with implicit=False."""
return gdx.File(rawgdx, implicit=False)


actual = OrderedDict([
Expand Down Expand Up @@ -36,99 +72,83 @@ def list_cmp(l1, l2):
return all([i1 == i2 for i1, i2 in zip(l1, l2)])


class TestGDX:
def test_init(self):
gdx.GDX()

def test_gdx():
gdx.GDX()

class TestFile(TestCase):
def setUp(self):
self.f = gdx.File(URI)

def test_init(self):
gdx.File(URI)
with self.assertRaises(FileNotFoundError):
class TestFile:
def test_init(self, rawgdx):
gdx.File(rawgdx)
with pytest.raises(FileNotFoundError):
gdx.File('nonexistent.gdx')

def test_parameters(self):
params = self.f.parameters()
def test_parameters(self, gdxfile):
params = gdxfile.parameters()
assert len(params) == actual_info['N parameters']

def test_sets(self):
sets = self.f.sets()
def test_sets(self, gdxfile):
sets = gdxfile.sets()
assert len(sets) == actual_info['N sets'] + 1

def test_get_symbol(self):
self.f['s']
def test_get_symbol(self, gdxfile):
gdxfile['s']

def test_get_symbol_by_index(self):
def test_get_symbol_by_index(self, gdxfile):
for i, name in enumerate(actual.keys()):
sym = self.f.get_symbol_by_index(i)
sym = gdxfile.get_symbol_by_index(i)
assert sym.name == name
# Giving too high an index results in IndexError
with self.assertRaises(IndexError):
self.f.get_symbol_by_index(i + 1)
with pytest.raises(IndexError):
gdxfile.get_symbol_by_index(i + 1)

def test_getattr(self):
def test_getattr(self, gdxfile):
for name in actual.keys():
getattr(self.f, name)
with self.assertRaises(AttributeError):
self.f.notasymbolname
getattr(gdxfile, name)
with pytest.raises(AttributeError):
gdxfile.notasymbolname

def test_getitem(self):
def test_getitem(self, gdxfile):
for name in actual.keys():
self.f[name]
with self.assertRaises(KeyError):
self.f['notasymbolname']
gdxfile[name]
with pytest.raises(KeyError):
gdxfile['notasymbolname']

def test_extract(self):
def test_extract(self, gdxfile):
for name in ['p1', 'p2', 'p3', 'p4']:
self.f.extract(name)
with self.assertRaises(KeyError):
self.f.extract('notasymbolname')

def test_implicit(self):
assert self.f['p5'].shape == (3, 3)


class TestImplicit(TestCase):
def setUp(self):
self.f = gdx.File(URI, implicit=False)

def test_implicit(self):
N = len(self.f['*'])
assert self.f['p5'].shape == (N, N)
gdxfile.extract(name)
with pytest.raises(KeyError):
gdxfile.extract('notasymbolname')

def test_implicit(self, gdxfile):
assert gdxfile['p5'].shape == (3, 3)

class TestSymbol:
pass

def test_implicit(gdxfile_implicit):
N = len(gdxfile_implicit['*'])
assert gdxfile_implicit['p5'].shape == (N, N)

class TestSet(TestCase):
def setUp(self):
self.file = gdx.File(URI)
self.star = self.file['*']

def test_len(self):
assert len(self.file.s) == len(actual['s'])
assert len(self.file.set('s1')) == len(actual['s1'])
assert len(self.file.set('s2')) == len(actual['s2'])
class TestSet:
def test_len(self, gdxfile):
assert len(gdxfile.s) == len(actual['s'])
assert len(gdxfile.set('s1')) == len(actual['s1'])
assert len(gdxfile.set('s2')) == len(actual['s2'])

def test_getitem(self):
for i in range(len(self.file.s)):
self.file.s[i]
with self.assertRaises(IndexError):
self.file.s[i + 1]
def test_getitem(self, gdxfile):
for i in range(len(gdxfile.s)):
gdxfile.s[i]
with pytest.raises(IndexError):
gdxfile.s[i + 1]

def test_index(self):
assert np.argwhere(self.file.s.values == 'd') == 3
def test_index(self, gdxfile):
assert np.argwhere(gdxfile.s.values == 'd') == 3

def test_iter(self):
for i, elem in enumerate(self.file.s):
def test_iter(self, gdxfile):
for i, elem in enumerate(gdxfile.s):
assert actual['s'][i] == elem

def test_domain(self):
def domain(name): return self.file[name].attrs['_gdx_domain']
def test_domain(self, gdxfile):
def domain(name): return gdxfile[name].attrs['_gdx_domain']
assert list_cmp(domain('s'), ['*'])
assert list_cmp(domain('t'), ['*'])
assert list_cmp(domain('u'), ['*'])
Expand Down
2 changes: 0 additions & 2 deletions setup.cfg

This file was deleted.

6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
'gdxcc >= 7',
'xarray >= 0.4',
],
tests_require=['pytest'],
tests_require=[
'coveralls',
'pytest',
'pytest-cov',
],
url='https://github.com/khaeru/py-gdx',
packages=find_packages(),
)
36 changes: 0 additions & 36 deletions travis.sh

This file was deleted.

0 comments on commit cfa59af

Please sign in to comment.