Skip to content

Commit

Permalink
Move tests to the pytest framework, add badges(#34)
Browse files Browse the repository at this point in the history
fixes #28
fixes #29
fixes #31

* Rename all examples from `text_` to `example_`
* Add `check_travis_tag.py` test
* Make all tests run by pytest
* Compute test coverage and submit to coveralls
* Add Travis and coveralls badges
* Skip binary restart test for the moment, as it is hanging (see the issue #40)
* Reorder a bit the Docker file, make it more clear
* Move supplementary files from `examples/simple_calculations` to `examples`
  • Loading branch information
yakutovicha authored Nov 29, 2019
1 parent 7610c14 commit d5b81bb
Show file tree
Hide file tree
Showing 36 changed files with 408 additions and 289 deletions.
26 changes: 26 additions & 0 deletions .ci/check_travis_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# pylint: disable=invalid-name
###############################################################################
# Copyright (c), The AiiDA-CP2K authors. #
# SPDX-License-Identifier: MIT #
# AiiDA-CP2K is hosted on GitHub at https://github.com/aiidateam/aiida-cp2k #
# For further information on the license, see the LICENSE.txt file. #
###############################################################################
"""Check travis tag"""
from __future__ import print_function
from __future__ import absolute_import

import os
import sys
import json

a = os.getenv("TRAVIS_TAG")
with open("setup.json") as fhandle:
b = "v{version}".format(**json.load(fhandle))

if not a:
print("TRAVIS_TAG not set")

elif a != b:
print("ERROR: TRAVIS_TAG and version are inconsistent: '{}' vs '{}'".format(a, b))
sys.exit(3)
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ services:
- docker

before_install:
- python .ci/check_travis_tag.py
- docker build -t aiida_raspa_test .
- docker run -d aiida_raspa_test
- sleep 30 # wait untill the container is launched

script:
- sleep 30 # wait untill the container is launched
- "export DOCKERID=`docker ps -qf 'ancestor=aiida_raspa_test'`"
- "echo \"Docker ID: $DOCKERID\""
- "docker exec -it --user aiida \"$DOCKERID\" /bin/bash -l -c 'cd code/aiida-raspa/ && ./run_tests.sh'"
- "docker exec -it --user aiida \"$DOCKERID\" /bin/bash -l -c 'cd code/aiida-raspa/ && pre-commit run --all-files || ( git status --short ; git diff ; exit 1 ) '"
- "docker exec -it --user aiida \"$DOCKERID\" /bin/bash -l -c 'cd code/aiida-raspa/ && py.test --cov aiida_raspa --cov-append .'"

after_success:
- "docker exec -e TRAVIS=\"$TRAVIS\" -e TRAVIS_JOB_ID=\"$TRAVIS_JOB_ID\" -e TRAVIS_BRANCH=\"$TRAVIS_BRANCH\" -it --user aiida \"$DOCKERID\" /bin/bash -l -c 'cd code/aiida-raspa/ && coveralls'"

git:
depth: 3
26 changes: 13 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
FROM aiidateam/aiida-docker-stack

# Set HOME variable:
# Set HOME, PATH and RASPA_DIR variables:
ENV HOME="/home/aiida"
ENV RASPA2_DIR=${HOME}/code/RASPA2_installed
ENV PATH="/home/aiida/code/RASPA2_installed/bin/:${HOME}/.local/bin:${PATH}"

# Install necessary codes
# Install necessary codes to build RASPA
RUN apt-get update && apt-get install -y --no-install-recommends \
automake \
libtool


# Copy the current folder and change permissions
COPY . ${HOME}/code/aiida-raspa
RUN chown -R aiida:aiida ${HOME}/code

# Change user to aiida
# Now do everything as the aiida user
USER aiida

# Install aiida-raspa plugin and it's dependencies
WORKDIR ${HOME}/code/aiida-raspa
RUN pip install --user .[pre-commit,test]

# Install AiiDA
ENV PATH="${HOME}/.local/bin:${PATH}"

# Download, compile and install RASPA
# Download, compile and install RASPA into ~/code folder
WORKDIR ${HOME}/code/
RUN git clone https://github.com/iRASPA/RASPA2.git RASPA2
WORKDIR ${HOME}/code/RASPA2
Expand All @@ -38,10 +31,17 @@ RUN ./configure --prefix=${RASPA2_DIR}
RUN make
RUN make install

# Set the plugin folder as the workdir
WORKDIR ${HOME}/code/aiida-raspa

# Install aiida-raspa plugin and coveralls
RUN pip install --user .[pre-commit,test]
RUN pip install --user coveralls

# Populate reentry cache for aiida user https://pypi.python.org/pypi/reentry/
RUN reentry scan

# Install the ddec and cp2k codes
# Install the RASPA code to AiiDA
COPY .docker/opt/add-codes.sh /opt/
COPY .docker/my_init.d/add-codes.sh /etc/my_init.d/40_add-codes.sh

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Build Status](https://travis-ci.org/yakutovicha/aiida-raspa.svg?branch=develop)](https://travis-ci.org/yakutovicha/aiida-raspa)
[![Coverage Status](https://coveralls.io/repos/github/yakutovicha/aiida-raspa/badge.svg?branch=develop)](https://coveralls.io/github/yakutovicha/aiida-raspa?branch=develop)
[![PyPI version](https://badge.fury.io/py/aiida-raspa.svg)](https://badge.fury.io/py/aiida-raspa)

# aiida-raspa
Expand Down
11 changes: 11 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""
For pytest initialise a test database and profile
"""
from __future__ import absolute_import
import pytest
pytest_plugins = ['aiida.manage.tests.pytest_fixtures'] # pylint: disable=invalid-name


@pytest.fixture(scope='function')
def raspa_code(aiida_local_code_factory): # pylint: disable=unused-argument
return aiida_local_code_factory("raspa", "simulate")
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import sys
import click
import pytest

from aiida.common import NotExistent
from aiida.engine import run_get_pk, run
Expand All @@ -16,16 +17,8 @@
CifData = DataFactory('cif') # pylint: disable=invalid-name


@click.command('cli')
@click.argument('codelabel')
@click.option('--submit', is_flag=True, help='Actually submit calculation')
def main(codelabel, submit):
def example_base(raspa_code, submit=True):
"""Prepare and submit simple RASPA calculation."""
try:
code = Code.get_from_string(codelabel)
except NotExistent:
print("The code '{}' does not exist".format(codelabel))
sys.exit(1)

# parameters
parameters = Dict(
Expand Down Expand Up @@ -61,10 +54,10 @@ def main(codelabel, submit):
})

# framework
framework = CifData(file=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'files', 'TCC1RS.cif'))
framework = CifData(file=os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'files', 'TCC1RS.cif'))

# Contructing builder
builder = code.get_builder()
builder = raspa_code.get_builder()
builder.framework = {
"tcc1rs": framework,
}
Expand All @@ -86,6 +79,7 @@ def main(codelabel, submit):
print("calculation pk: ", pk)
print("Total Energy average (tcc1rs):", res['output_parameters'].dict.tcc1rs['general']['total_energy_average'])
print("OK, calculation has completed successfully")
pytest.base_calc_pk = pk
else:
print("Generating test input ...")
builder.metadata.dry_run = True
Expand All @@ -96,7 +90,20 @@ def main(codelabel, submit):
print("-----")


@click.command('cli')
@click.argument('codelabel')
@click.option('--submit', is_flag=True, help='Actually submit calculation')
def cli(codelabel, submit):
"""Click interface"""
try:
code = Code.get_from_string(codelabel)
except NotExistent:
print("The code '{}' does not exist".format(codelabel))
sys.exit(1)
example_base(code, submit)


if __name__ == '__main__':
main() # pylint: disable=no-value-for-parameter
cli() # pylint: disable=no-value-for-parameter

# EOF
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""Restart from simple RASPA calculation."""

Expand All @@ -7,6 +6,7 @@
import os
import sys
import click
import pytest

from aiida.common import NotExistent
from aiida.engine import run_get_pk, run
Expand All @@ -17,17 +17,12 @@
CifData = DataFactory('cif') # pylint: disable=invalid-name


@click.command('cli')
@click.argument('codelabel')
@click.option('--previous_calc', '-p', required=True, type=int, help='PK of test_raspa_base.py calculation')
@click.option('--submit', is_flag=True, help='Actually submit calculation')
def main(codelabel, previous_calc, submit):
def example_base_restart(raspa_code, base_calc_pk=None, submit=True):
"""Prepare and submit restart from simple RASPA calculation."""
try:
code = Code.get_from_string(codelabel)
except NotExistent:
print("The code '{}' does not exist".format(codelabel))
sys.exit(1)

# This line is needed for tests only
if base_calc_pk is None:
base_calc_pk = pytest.base_calc_pk # pylint: disable=no-member

# parameters
parameters = Dict(
Expand Down Expand Up @@ -62,13 +57,13 @@ def main(codelabel, previous_calc, submit):
})

# framework
framework = CifData(file=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'files', 'TCC1RS.cif'))
framework = CifData(file=os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'files', 'TCC1RS.cif'))

# restart file
retrieved_parent_folder = load_node(previous_calc).outputs.retrieved
retrieved_parent_folder = load_node(base_calc_pk).outputs.retrieved

# Contructing builder
builder = code.get_builder()
builder = raspa_code.get_builder()
builder.framework = {
"tcc1rs": framework,
}
Expand Down Expand Up @@ -101,7 +96,21 @@ def main(codelabel, previous_calc, submit):
print("-----")


@click.command('cli')
@click.argument('codelabel')
@click.option('--previous_calc', '-p', required=True, type=int, help='PK of example_base.py calculation')
@click.option('--submit', is_flag=True, help='Actually submit calculation')
def cli(codelabel, previous_calc, submit):
"""Click interface"""
try:
code = Code.get_from_string(codelabel)
except NotExistent:
print("The code '{}' does not exist".format(codelabel))
sys.exit(1)
example_base_restart(code, previous_calc, submit)


if __name__ == '__main__':
main() # pylint: disable=no-value-for-parameter
cli() # pylint: disable=no-value-for-parameter

# EOF
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,8 @@
from aiida.orm import Code, Dict


@click.command('cli')
@click.argument('codelabel')
@click.option('--submit', is_flag=True, help='Actually submit calculation')
def main(codelabel, submit):
def example_binary_misture(raspa_code, submit=True):
"""Prepare and submit RASPA calculation with components mixture."""
try:
code = Code.get_from_string(codelabel)
except NotExistent:
print("The code '{}' does not exist".format(codelabel))
sys.exit(1)

# parameters
parameters = Dict(
Expand Down Expand Up @@ -63,7 +55,7 @@ def main(codelabel, submit):
})

# Contructing builder
builder = code.get_builder()
builder = raspa_code.get_builder()
builder.parameters = parameters
builder.metadata.options = {
"resources": {
Expand Down Expand Up @@ -93,7 +85,20 @@ def main(codelabel, submit):
print("-----")


@click.command('cli')
@click.argument('codelabel')
@click.option('--submit', is_flag=True, help='Actually submit calculation')
def cli(codelabel, submit):
"""Click interface"""
try:
code = Code.get_from_string(codelabel)
except NotExistent:
print("The code '{}' does not exist".format(codelabel))
sys.exit(1)
example_binary_misture(code, submit)


if __name__ == '__main__':
main() # pylint: disable=no-value-for-parameter
cli() # pylint: disable=no-value-for-parameter

# EOF
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,9 @@
CifData = DataFactory('cif') # pylint: disable=invalid-name


@click.command('cli')
@click.argument('codelabel')
@click.option('--submit', is_flag=True, help='Actually submit calculation')
def main(codelabel, submit):
def example_block_pockets(raspa_code, submit=True):
"""Prepare and submit RASPA calculation with blocked pockets."""
try:
code = Code.get_from_string(codelabel)
except NotExistent:
print("The code '{}' does not exist".format(codelabel))
sys.exit(1)

# parameters
parameters = Dict(
dict={
Expand Down Expand Up @@ -88,15 +81,15 @@ def main(codelabel, submit):

# frameworks
pwd = os.path.dirname(os.path.realpath(__file__))
framework_1 = CifData(file=os.path.join(pwd, 'files', 'IRMOF-1.cif'))
framework_10 = CifData(file=os.path.join(pwd, 'files', 'IRMOF-10.cif'))
framework_1 = CifData(file=os.path.join(pwd, '..', 'files', 'IRMOF-1.cif'))
framework_10 = CifData(file=os.path.join(pwd, '..', 'files', 'IRMOF-10.cif'))

# block pocket
block_pocket_1 = SinglefileData(file=os.path.join(pwd, 'files', 'IRMOF-1_test.block')).store()
block_pocket_10 = SinglefileData(file=os.path.join(pwd, 'files', 'IRMOF-10_test.block')).store()
block_pocket_1 = SinglefileData(file=os.path.join(pwd, '..', 'files', 'IRMOF-1_test.block')).store()
block_pocket_10 = SinglefileData(file=os.path.join(pwd, '..', 'files', 'IRMOF-10_test.block')).store()

# Contructing builder
builder = code.get_builder()
builder = raspa_code.get_builder()
builder.framework = {
"irmof_1": framework_1,
"irmof_10": framework_10,
Expand Down Expand Up @@ -136,7 +129,20 @@ def main(codelabel, submit):
print("In order to actually submit, add '--submit'")


@click.command('cli')
@click.argument('codelabel')
@click.option('--submit', is_flag=True, help='Actually submit calculation')
def cli(codelabel, submit):
"""Click interface"""
try:
code = Code.get_from_string(codelabel)
except NotExistent:
print("The code '{}' does not exist".format(codelabel))
sys.exit(1)
example_block_pockets(code, submit)


if __name__ == '__main__':
main() # pylint: disable=no-value-for-parameter
cli() # pylint: disable=no-value-for-parameter

# EOF
Loading

0 comments on commit d5b81bb

Please sign in to comment.