Skip to content

Commit

Permalink
Merge pull request #865 from DropD/plugin-fixtures
Browse files Browse the repository at this point in the history
Plugin fixtures: improve for pytest, add computer fixture to PluginTestCase
  • Loading branch information
ltalirz authored Oct 27, 2017
2 parents 7186bbf + 69d1373 commit f75ed6c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
33 changes: 30 additions & 3 deletions .travis-data/test_plugin_testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"""
import os
import unittest
import tempfile
import shutil

from aiida.utils.fixtures import PluginTestCase
from aiida.backends.profile import BACKEND_DJANGO, BACKEND_SQLA
Expand All @@ -22,20 +24,45 @@ class PluginTestcaseTestCase(PluginTestCase):

def setUp(self):
from aiida.orm import DataFactory
self.data = DataFactory('parameter')(dict={'data': 'test'})
self.data.store()
from aiida.orm import Computer
self.temp_dir = tempfile.mkdtemp()
self.data = self.get_data()
self.data_pk = self.data.pk
self.computer = self.get_computer(temp_dir=self.temp_dir)

def get_data(self):
from aiida.orm import DataFactory
data = DataFactory('parameter')(dict={'data': 'test'})
data.store()
return data

def get_computer(self, temp_dir):
from aiida.orm import Computer
computer = Computer(
name='localhost',
description='my computer',
hostname='localhost',
workdir=temp_dir,
transport_type='local',
scheduler_type='direct',
enabled_state=True)
computer.store()
return computer

def test_data_loaded(self):
from aiida.orm import Computer
from aiida.orm import load_node
self.assertTrue(is_dbenv_loaded())
load_node(self.data_pk)
self.assertEqual(load_node(self.data_pk).uuid, self.data.uuid)
self.assertEqual(Computer.get('localhost').uuid, self.computer.uuid)


def test_tear_down(self):
from aiida.orm import load_node
super(PluginTestcaseTestCase, self).tearDown()
with self.assertRaises(Exception):
load_node(self.data_pk)
shutil.rmtree(self.temp_dir)


if __name__ == '__main__':
Expand Down
13 changes: 10 additions & 3 deletions aiida/utils/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,12 @@ def __clean_db_sqla(self):
# that deleted our user, we need to recreate it
new_user.force_save()

def has_profile_open(self):
return self.__is_running_on_test_profile


_PYTEST_FIXTURE_MANAGER = FixtureManager()


@contextmanager
def fixture_manager():
Expand All @@ -409,11 +415,12 @@ def aiida_profile():
fixture_mgr.create_profile()
yield fixture_mgr
"""
manager = FixtureManager()
try:
yield manager
if not _PYTEST_FIXTURE_MANAGER.has_profile_open():
_PYTEST_FIXTURE_MANAGER.create_profile()
yield _PYTEST_FIXTURE_MANAGER
finally:
manager.destroy_all()
_PYTEST_FIXTURE_MANAGER.destroy_all()


class PluginTestCase(unittest.TestCase):
Expand Down

0 comments on commit f75ed6c

Please sign in to comment.